openquant.strategy_risk
Compiled from Rust (crates/pyopenquant). Signatures and types are from the shipped .pyi stubs; descriptions are the functions’ own docstrings, as help() shows them. All Python modules.
Functions
estimate_strategy_failure_probability
estimate_strategy_failure_probability(
bet_outcomes: Sequence[float],
years_elapsed: float,
target_sharpe: float,
investor_horizon_years: float,
bootstrap_iterations: int = 1000,
seed: int = 42,
kde_bandwidth: float | None = None,
) -> AnyProbability that a strategy misses its target Sharpe ratio, by bootstrapping its precision.
AFML §15.4, Snippets 15.4-15.5. From a record of per-bet outcomes (> 0 is a win; zero counts as a loss), estimates the payouts as the mean win and mean loss, the frequency as len(bet_outcomes) / years_elapsed, and the required precision p* with implied_precision_asymmetric. It then bootstraps the precision bootstrap_iterations times from samples of floor(frequency * investor_horizon_years) bets (at least 1), drawn with replacement by an RNG seeded with seed, and reports the share of samples at or below p* and a Gaussian-KDE estimate of the same probability. Deterministic for a given seed. Bets are assumed i.i.d.; the bootstrap does not price edge decay.
Parameters
bet_outcomes: list[float]Per-bet returns, all finite, with at least one win and one loss.
years_elapsed: floatLength of the record in years; finite and > 0.
target_sharpe: floatAnnualised Sharpe ratio the strategy must reach; finite and > 0.
investor_horizon_years: floatHorizon over which the investor judges the strategy, in years (sets the bootstrap sample size); finite and > 0.
bootstrap_iterations: int, default 1000Number of bootstrap resamples; must be > 0.
seed: int, default 42Seed of the bootstrap RNG.
kde_bandwidth: float | None, default NoneKDE bandwidth; finite and > 0.
Noneuses Silverman's rule.
Returns
- dict[str, Any]
Keys
pi_plusandpi_minus(mean win and mean loss),annual_bet_frequency,implied_precision_threshold(p*),bootstrap_precision_mean,bootstrap_precision_std(sample standard deviation),empirical_failure_probability(share of samples<= p*),kde_failure_probability(all floats) andbootstrap_precision_samples(list[float], every bootstrapped precision).
Raises
- ValueError
If
bet_outcomesis empty, has a non-finite value, or lacks either a win or a loss;years_elapsed,target_sharpe,investor_horizon_yearsorkde_bandwidthis not finite and positive;bootstrap_iterationsis 0; or no precision reaches the target.
implied_frequency_asymmetric
implied_frequency_asymmetric(
precision: float,
target_sharpe: float,
pi_plus: float,
pi_minus: float,
) -> floatBets per year needed for an asymmetric-payout strategy to reach a target Sharpe ratio.
AFML §15.3: theta^2 d^2 p (1 - p) / (d p + pi_minus)^2 with d = pi_plus - pi_minus. A strategy whose mean payoff d p + pi_minus is negative gets a positive frequency from this formula even though its Sharpe ratio is negative; check the sign of the mean payoff first.
Parameters
precision: floatProbability that a bet wins, in
[0, 1].target_sharpe: floatAnnualised Sharpe ratio to reach; finite and > 0.
pi_plus: floatReturn of a winning bet.
pi_minus: floatReturn of a losing bet (typically negative); must be below
pi_plus.
Returns
- float
The required number of bets per year.
Raises
- ValueError
If
precisionis outside[0, 1]or exactly 0 or 1 (zero implied frequency),target_sharpeis not finite and positive, the payouts are not finite withpi_plus > pi_minus, or the mean payoff is within1e-12of zero.
implied_frequency_symmetric
implied_frequency_symmetric(precision: float, target_sharpe: float) -> floatBets per year needed for a symmetric-payout strategy to reach a target Sharpe ratio.
AFML §15.2: 4 theta^2 p (1 - p) / (2p - 1)^2 for precision p and target Sharpe theta; the inverse of sharpe_symmetric. A precision below 0.5 gives a positive frequency even though its Sharpe ratio is negative; check p > 0.5 first.
Parameters
precision: floatProbability that a bet wins, in
[0, 1].target_sharpe: floatAnnualised Sharpe ratio to reach; finite and > 0.
Returns
- float
The required number of bets per year.
Raises
- ValueError
If
precisionis outside[0, 1], within1e-12of 0.5, or exactly 0 or 1 (zero implied frequency), ortarget_sharpeis not finite and positive.
implied_precision_asymmetric
implied_precision_asymmetric(
target_sharpe: float,
annual_bet_frequency: float,
pi_plus: float,
pi_minus: float,
) -> floatSmallest precision at which an asymmetric-payout strategy reaches a target Sharpe ratio.
AFML §15.3, Snippet 15.3: solves the quadratic in p obtained by inverting sharpe_asymmetric and returns the smallest root in [0, 1] whose Sharpe ratio reaches target_sharpe.
Parameters
target_sharpe: floatAnnualised Sharpe ratio to reach; finite and > 0.
annual_bet_frequency: floatBets per year; finite and > 0.
pi_plus: floatReturn of a winning bet.
pi_minus: floatReturn of a losing bet (typically negative); must be below
pi_plus.
Returns
- float
The required precision, in
[0, 1].
Raises
- ValueError
If
annual_bet_frequencyortarget_sharpeis not finite and positive, the payouts are not finite withpi_plus > pi_minus, or no real root in[0, 1]reaches the target.
implied_precision_symmetric
implied_precision_symmetric(target_sharpe: float, annual_bet_frequency: float) -> floatPrecision needed for a symmetric-payout strategy to reach a target Sharpe ratio.
AFML §15.2: (1 + theta / sqrt(theta^2 + n)) / 2 for target Sharpe theta and n bets a year; the inverse of sharpe_symmetric.
Parameters
target_sharpe: floatAnnualised Sharpe ratio to reach; finite and > 0.
annual_bet_frequency: floatBets per year; finite and > 0.
Returns
- float
The required precision, in
(0.5, 1).
Raises
- ValueError
If
annual_bet_frequencyortarget_sharpeis not finite and positive.
sharpe_asymmetric
sharpe_asymmetric(
precision: float,
annual_bet_frequency: float,
pi_plus: float,
pi_minus: float,
) -> floatAnnualised Sharpe ratio of a binary strategy with asymmetric payouts.
AFML §15.3, Snippet 15.2: (d p + pi_minus) / (|d| sqrt(p (1 - p))) * sqrt(n) with d = pi_plus - pi_minus, for n independent bets a year that return pi_plus with probability p and pi_minus otherwise.
Parameters
precision: floatProbability
pthat a bet wins, strictly between 0 and 1.annual_bet_frequency: floatBets per year
n; finite and > 0.pi_plus: floatReturn of a winning bet.
pi_minus: floatReturn of a losing bet (typically negative); must be below
pi_plus.
Returns
- float
The annualised Sharpe ratio.
Raises
- ValueError
If
precisionis not strictly inside(0, 1),annual_bet_frequencyis not finite and positive, or the payouts are not finite withpi_plus > pi_minus.
sharpe_symmetric
sharpe_symmetric(precision: float, annual_bet_frequency: float) -> floatAnnualised Sharpe ratio of a binary strategy with symmetric payouts.
AFML §15.2, Snippet 15.1: (2p - 1) / (2 sqrt(p (1 - p))) * sqrt(n) for n independent bets a year that each win +pi with probability p and lose -pi otherwise. The payout size cancels.
Parameters
precision: floatProbability
pthat a bet wins, strictly between 0 and 1.annual_bet_frequency: floatBets per year
n; finite and > 0.
Returns
- float
The annualised Sharpe ratio.
Raises
- ValueError
If
precisionis not strictly inside(0, 1)orannual_bet_frequencyis not finite and positive.