Expand description
Bet sizing (AFML chapter 10): turning a prediction into a position size.
Three routes, depending on what the model produces:
| You have | Entry point | AFML |
|---|---|---|
| a class probability (typically from a meta-label model) | bet_size_probability | §10.3–10.5, Snippets 10.1–10.3 |
| a price forecast and the current market price | bet_size_dynamic | §10.6, Snippet 10.4 |
| only the sides and lifespans of the bets | bet_size_budget, bet_size_reserve | §10.2 |
Sizes are fractions of the maximum position in [-1, 1], signed by side. The func
string arguments select the sizing curve: "sigmoid" (m(x) = x / sqrt(w + x^2)) or
"power" (m(x) = sgn(x) |x|^w, which needs the price divergence x scaled into
[-1, 1]). Any other name is BetSizingError::InvalidFunction.
A bet (t0, t1) is live on the half-open interval [t0, t1).
use openquant::bet_sizing::{get_signal, get_target_pos, get_w, limit_price};
// Probability to size for a two-class model: no edge at 1/K, saturating towards 1.
let sizes = get_signal(&[0.5, 0.7, 0.9], 2, None);
assert!(sizes[0].abs() < 1e-12);
assert!((sizes[1] - 0.3374).abs() < 1e-4 && (sizes[2] - 0.8176).abs() < 1e-4);
// Calibrate the sigmoid so a price gap of 2.0 is a 0.9 bet, then size a 50-lot book.
let w = get_w(2.0, 0.9, "sigmoid")?;
let target = get_target_pos(w, 101.5, 100.0, 50.0, "sigmoid")?;
let limit = limit_price(target, 0.0, 101.5, w, 50.0, "sigmoid")?;
assert_eq!(target, 42.0);
assert!(limit > 100.0 && limit < 101.5);Enums§
- BetSizing
Error - Errors returned by the bet-sizing functions.
Functions§
- avg_
active_ signals - Averages the signals that are live at each change point (AFML Snippet 10.2).
- bet_
size - Bet size for price divergence
price_divusing the curve named byfunc. - bet_
size_ budget - Budgeting bet size
L_t / max L - S_t / max Sfrom concurrent bet counts (AFML §10.2). - bet_
size_ dynamic - Dynamic bet sizing from a price forecast (AFML §10.6, Snippet 10.4).
- bet_
size_ power - Power bet size
sgn(x) |x|^wfor a price divergencexscaled into[-1, 1]. - bet_
size_ probability - Sizes bets from class probabilities:
get_signal, then optionallyavg_active_signals, thendiscrete_signal(AFML §10.3–10.5). - bet_
size_ reserve - Reserve bet sizes under a given mixture fit (AFML §10.2).
- bet_
size_ reserve_ full - Reserve bet sizing with the mixture fitted to the data (AFML §10.2).
- bet_
size_ reserve_ with_ fit - Reserve bet sizes under a given mixture fit, with the net concurrency kept in each row.
- bet_
size_ sigmoid - Sigmoid bet size
x / sqrt(w + x^2)for price divergencex = forecast - market(AFML Snippet 10.4). - cdf_
mixture - CDF at
xof a two-Gaussian mixturep1 N(mu1, sigma1) + (1 - p1) N(mu2, sigma2). - confirm_
and_ cast_ to_ df - Broadcasts the four
bet_size_dynamicinputs to a common length and zips them into(pos, max_pos, market_price, forecast)rows. - discrete_
signal - Rounds sizes to the nearest multiple of
step_sizeand clamps them to[-1, 1](AFML Snippet 10.3). - get_
concurrent_ sides - Counts the long and short bets live at each bet’s start (AFML §10.2).
- get_
signal - Converts predicted-class probabilities into bet sizes (AFML Snippet 10.1).
- get_
target_ pos - Target position for forecast
fand market pricem_p, out of a maximummax_pos, using the curve named byfunc(AFML Snippet 10.4). - get_
target_ pos_ power - Power-curve target position
trunc(m(forecast - market) * max_pos). - get_
target_ pos_ sigmoid - Sigmoid target position
trunc(m(forecast - market) * max_pos)(AFML Snippet 10.4). - get_w
- Calibrates the curve width
wso that divergenceprice_divgives sizem_bet_size. - get_
w_ power - Power exponent
w = ln(m / sgn(x)) / ln|x|such that divergenceprice_divgives sizem_bet_size; negative results are floored at 0. - get_
w_ sigmoid - Sigmoid width
w = x^2 (1/m^2 - 1)such that divergenceprice_divgives sizem_bet_size(AFML Snippet 10.4). - inv_
price - Market price implied by bet size
m_bet_sizefor the curve named byfunc. - inv_
price_ power - Inverse of the power curve: the market price at which the power bet size equals
m_bet_sizegivenforecast_price. - inv_
price_ sigmoid - Inverse of the sigmoid curve: the market price at which the sigmoid bet size equals
m_bet_sizegivenforecast_price(AFML Snippet 10.4). - limit_
price - Breakeven limit price for moving from position
posto targett_pos, using the curve named byfunc(AFML Snippet 10.4). - limit_
price_ power - Power-curve breakeven limit price for moving from
postot_poswith forecastf, exponentwand maximum positionmax_pos. - limit_
price_ sigmoid - Sigmoid breakeven limit price for moving from
postot_poswith forecastf, widthwand maximum positionmax_pos(AFML Snippet 10.4, extended to every direction). - mp_
avg_ active_ signals - Averages the live signals at each timestamp in
molecule(the worker behindavg_active_signals). - single_
bet_ size_ mixed - Reserve bet size for net concurrency
cunder a fitted mixturefit(AFML §10.2).
Type Aliases§
- Mixture
Params - Fitted two-normal mixture parameters
[mu1, mu2, sigma1, sigma2, p1]. - Reserve
BetSize Row - Reserve bet-size row:
(timestamp, active_long, active_short, c_t, bet_size).