Skip to main content

Module bet_sizing

Module bet_sizing 

Source
Expand description

Bet sizing (AFML chapter 10): turning a prediction into a position size.

Three routes, depending on what the model produces:

You haveEntry pointAFML
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 pricebet_size_dynamic§10.6, Snippet 10.4
only the sides and lifespans of the betsbet_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§

BetSizingError
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_div using the curve named by func.
bet_size_budget
Budgeting bet size L_t / max L - S_t / max S from 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|^w for a price divergence x scaled into [-1, 1].
bet_size_probability
Sizes bets from class probabilities: get_signal, then optionally avg_active_signals, then discrete_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 divergence x = forecast - market (AFML Snippet 10.4).
cdf_mixture
CDF at x of a two-Gaussian mixture p1 N(mu1, sigma1) + (1 - p1) N(mu2, sigma2).
confirm_and_cast_to_df
Broadcasts the four bet_size_dynamic inputs 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_size and 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 f and market price m_p, out of a maximum max_pos, using the curve named by func (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 w so that divergence price_div gives size m_bet_size.
get_w_power
Power exponent w = ln(m / sgn(x)) / ln|x| such that divergence price_div gives size m_bet_size; negative results are floored at 0.
get_w_sigmoid
Sigmoid width w = x^2 (1/m^2 - 1) such that divergence price_div gives size m_bet_size (AFML Snippet 10.4).
inv_price
Market price implied by bet size m_bet_size for the curve named by func.
inv_price_power
Inverse of the power curve: the market price at which the power bet size equals m_bet_size given forecast_price.
inv_price_sigmoid
Inverse of the sigmoid curve: the market price at which the sigmoid bet size equals m_bet_size given forecast_price (AFML Snippet 10.4).
limit_price
Breakeven limit price for moving from position pos to target t_pos, using the curve named by func (AFML Snippet 10.4).
limit_price_power
Power-curve breakeven limit price for moving from pos to t_pos with forecast f, exponent w and maximum position max_pos.
limit_price_sigmoid
Sigmoid breakeven limit price for moving from pos to t_pos with forecast f, width w and maximum position max_pos (AFML Snippet 10.4, extended to every direction).
mp_avg_active_signals
Averages the live signals at each timestamp in molecule (the worker behind avg_active_signals).
single_bet_size_mixed
Reserve bet size for net concurrency c under a fitted mixture fit (AFML §10.2).

Type Aliases§

MixtureParams
Fitted two-normal mixture parameters [mu1, mu2, sigma1, sigma2, p1].
ReserveBetSizeRow
Reserve bet-size row: (timestamp, active_long, active_short, c_t, bet_size).