permutation_test¶
Defined in fynance.research
- permutation_test(strategy, data, *, metric='sharpe', n_permutations=200, seed=0)[source]
Permutation test for a spurious edge.
Runs the strategy on the real series, then on
n_permutationsshuffles of the asset’s returns (which destroys any temporal structure). If the strategy scores as well on shuffled data as on the real data, its edge is not real.The price series must be strictly positive: the null is built on log-returns (
np.diff(np.log(prices))), so a non-positive price would yieldnan/-infreturns.Each run is seeded with a distinct seed derived from
seed(the observed run and every permutation), so a stochastic strategy does not see the same model RNG draws on every path — which would bias the null variance. The whole test stays reproducible for a fixedseed.- Parameters:
- strategyfynance.strategy.Strategy
The strategy to evaluate.
- dataPriceSeries or array-like
Strictly positive price series (
np.logis taken).- metricstr
Metric key from the run summary (default
"sharpe").- n_permutationsint
Number of shuffles forming the null distribution.
- seedint
Master seed for the shuffles and the runs (per-run seeds are derived from it, so the test is fully reproducible).
- Returns:
- dict
observed,p_value,null_mean,null_std. The p-value is the (smoothed) fraction of shuffles scoring at least the observed metric.