Backtesting¶
Performance statistics, overfitting detection, strategy risk analysis, bet sizing, and synthetic data generation (AFML Ch. 10, 14–15).
backtesting ¶
avg_active_signals ¶
Average active signals at each bar.
Computes the mean signal strength from all active positions at each point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signals
|
list[tuple[int, int, float]]
|
List of (start_idx, end_idx, signal_value) triples. |
required |
num_bars
|
int
|
Total number of bars. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Average signal at each bar. |
avg_holding_period ¶
Average holding period from entry-exit pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entry_exit_pairs
|
list[tuple[int, int]]
|
List of (entry_idx, exit_idx) pairs. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Mean holding period in bars. |
bonferroni_correction ¶
Bonferroni correction for multiple hypothesis testing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p_values
|
ndarray
|
Uncorrected p-values. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Corrected p-values (clamped to [0, 1]). |
compute_drawdowns ¶
Compute drawdown statistics from a return series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
ndarray
|
Periodic return series. |
required |
Returns:
| Type | Description |
|---|---|
DrawdownResult
|
Contains max drawdown, max duration, drawdown series, and time-under-water series. |
cscv ¶
Combinatorially Symmetric Cross-Validation (CSCV).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns_matrix
|
ndarray
|
Matrix of shape (n_periods, n_strategies). |
required |
num_groups
|
int
|
Number of groups for combinatorial splitting. |
required |
Returns:
| Type | Description |
|---|---|
CscvResult
|
PBO and rank-logit vector. |
deflated_sharpe_ratio ¶
Deflated Sharpe Ratio — adjusts for multiple testing (AFML Ch. 14).
Corrects the observed SR for the number of strategies tested, accounting for higher moments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed_sr
|
float
|
Best observed Sharpe ratio. |
required |
sr_std
|
float
|
Standard deviation of Sharpe ratios across all trials. |
required |
n_observations
|
int
|
Number of return observations. |
required |
n_trials
|
int
|
Number of strategies/configurations tested. |
required |
skewness
|
float
|
Skewness of returns. |
required |
kurtosis
|
float
|
Excess kurtosis of returns. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Deflated Sharpe ratio (probability-adjusted). |
discrete_signal ¶
Discretize a continuous signal to the nearest step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
float
|
Continuous signal value. |
required |
step_size
|
float
|
Discretization step (e.g. 0.1 for 10% increments). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Rounded signal. |
estimate_ou_params ¶
Estimate O-U process parameters from a time series via OLS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
ndarray
|
Observed time series. |
required |
dt
|
float
|
Time step between observations. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float, float]
|
(theta, mu, sigma) — mean-reversion speed, long-term mean, volatility. |
hhi ¶
Herfindahl-Hirschman Index measuring concentration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
ndarray
|
Weight or share vector (should sum to 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
HHI value. Ranges from 1/n (perfectly diversified) to 1 (fully concentrated). |
hhi_concentration ¶
Concentration of positive and negative returns using HHI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
ndarray
|
Return series. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
(positive_hhi, negative_hhi) — concentration in gains and losses. |
hit_ratio ¶
Fraction of positive returns (hit rate / win rate).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
ndarray
|
Return series. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Proportion of returns > 0. |
holm_correction ¶
Holm-Bonferroni step-down correction for multiple testing.
Less conservative than Bonferroni while still controlling the family-wise error rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p_values
|
ndarray
|
Uncorrected p-values. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Corrected p-values. |
implied_frequency ¶
Implied trading frequency needed to achieve a target Sharpe ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_sr
|
float
|
Desired annualized Sharpe ratio. |
required |
precision
|
float
|
Hit rate. |
required |
avg_win_loss_ratio
|
float
|
Average win/loss ratio. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Required trades per year. |
implied_precision ¶
Implied precision needed to achieve a target Sharpe ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_sr
|
float
|
Desired annualized Sharpe ratio. |
required |
freq
|
float
|
Average trades per year. |
required |
avg_win_loss_ratio
|
float
|
Average win/loss ratio. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Required hit rate (precision). |
otr_mesh ¶
Compute an optimal trading rule (OTR) mesh over profit-take and stop-loss ranges.
Simulates O-U processes and evaluates average returns for each (profit_take, stop_loss) combination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
float
|
Mean-reversion speed. |
required |
mu
|
float
|
Long-term mean. |
required |
sigma
|
float
|
Volatility. |
required |
pt_range
|
ndarray
|
Profit-take thresholds to evaluate. |
required |
sl_range
|
ndarray
|
Stop-loss thresholds to evaluate. |
required |
n_simulations
|
int
|
Monte Carlo simulations per grid point. |
required |
seed
|
int
|
Random seed. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
2-D array of shape (len(pt_range), len(sl_range)) with average returns. |
power_bet_size ¶
Power-law bet sizing from prediction probability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prob
|
float
|
Predicted probability. |
required |
num_classes
|
int
|
Number of classes. |
required |
exponent
|
float
|
Power exponent (higher = more aggressive sizing). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Bet size in [-1, 1]. |
probabilistic_sharpe_ratio ¶
Probabilistic Sharpe Ratio (PSR) — probability that the true SR exceeds a benchmark.
Accounts for skewness and kurtosis of the return distribution (AFML Ch. 14).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed_sr
|
float
|
Observed (sample) Sharpe ratio. |
required |
benchmark_sr
|
float
|
Benchmark Sharpe ratio to compare against. |
required |
n_observations
|
int
|
Number of return observations. |
required |
skewness
|
float
|
Skewness of returns. |
required |
kurtosis
|
float
|
Excess kurtosis of returns. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability (0 to 1) that the true SR exceeds the benchmark. |
probability_of_backtest_overfitting ¶
Probability of Backtest Overfitting (PBO) via CSCV (AFML Ch. 14).
Estimates the probability that the best in-sample strategy will underperform out-of-sample using combinatorial data splitting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns_matrix
|
ndarray
|
Matrix of shape (n_periods, n_strategies) with period returns. |
required |
num_partitions
|
int
|
Number of partitions for CSCV (must be even). |
required |
seed
|
int
|
Random seed for partition shuffling. |
required |
Returns:
| Type | Description |
|---|---|
float
|
PBO estimate in [0, 1]. Higher means more overfitting risk. |
sharpe_mesh ¶
Compute Sharpe ratios over a grid of return series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns_grid
|
ndarray
|
2-D array where each column is a return series. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Sharpe ratio for each column, reshaped to match the input grid. |
sharpe_ratio ¶
Annualized Sharpe ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
ndarray
|
Periodic return series. |
required |
risk_free_rate
|
float
|
Risk-free rate per period. |
0.0
|
periods_per_year
|
float
|
Annualization factor (252 for daily, 12 for monthly). |
252.0
|
Returns:
| Type | Description |
|---|---|
float
|
Annualized Sharpe ratio. |
sigmoid_bet_size ¶
Sigmoid bet sizing from prediction probability (AFML Ch. 10).
Maps a probability to a position size using a sigmoid function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prob
|
float
|
Predicted probability of the positive class. |
required |
num_classes
|
int
|
Number of discrete classes (usually 2). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Bet size in [-1, 1]. |
simulate_ou ¶
Simulate an Ornstein-Uhlenbeck mean-reverting process.
dX = theta * (mu - X) * dt + sigma * dW
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
float
|
Mean-reversion speed. |
required |
mu
|
float
|
Long-term mean. |
required |
sigma
|
float
|
Volatility. |
required |
x0
|
float
|
Initial value. |
required |
dt
|
float
|
Time step. |
required |
n_steps
|
int
|
Number of simulation steps. |
required |
seed
|
int
|
Random seed. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Simulated path of length n_steps + 1 (including x0). |
sr_from_precision ¶
Compute Sharpe ratio from precision, frequency, and win/loss ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
precision
|
float
|
Hit rate (probability of a winning trade). |
required |
freq
|
float
|
Average number of trades per year. |
required |
avg_win_loss_ratio
|
float
|
Average win size divided by average loss size. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Expected annualized Sharpe ratio. |
strategy_failure_probability ¶
Probability that a strategy will fail (precision below break-even).
Uses a binomial test to estimate the probability that the true precision is below the break-even threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimated_precision
|
float
|
Observed hit rate. |
required |
n_observations
|
int
|
Number of trades observed. |
required |
break_even_precision
|
float
|
Minimum precision needed to break even. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Failure probability. |
turnover ¶
Portfolio turnover from a position series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
ndarray
|
Position sizes over time. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Average absolute change in position per period. |