Stylized facts¶
Stylized facts of market data: the statistical signatures every realistic market model must reproduce (Cont 2001; Bouchaud, Bonart, Donier & Gould 2018).
All functions are pure and work on plain sequences, so they apply equally to
simulator output (SimResult.mid_path / SimResult.fills) and to replayed
historical data.
========================= ==================================== ================== Fact Statistic here Typical equities ========================= ==================================== ================== Fat tails excess kurtosis, Hill tail index kurtosis >> 0, alpha ~ 3 Aggregational Gaussianity kurtosis falls as the horizon grows - No return autocorrelation ACF(r) at lag >= 1 ~ 0 (negative at tick scale) Volatility clustering ACF(|r|) at lags 1..100 > 0, slow decay Long memory of order sign ACF(sign) ~ lag^-gamma gamma ~ 0.5 Square-root impact I(Q) ~ sigma * (Q / V)^delta delta ~ 0.5 ========================= ==================================== ==================
stylized_facts
¶
stylized_facts(mid_path: Sequence[tuple[int, float]], fills: Sequence[Fill], *, end: int, start: int = 0, dt_short: int = 10, dt_long: int = 1000, vol_lags: Sequence[int] = (1, 10, 100), sign_lags: Sequence[int] = (1, 10, 100, 1000)) -> StylizedFacts
Compute the standard stylized facts from a mid path and a fill stream.
StylizedFacts
dataclass
¶
StylizedFacts(dt_short: int, dt_long: int, n_returns: int, kurtosis_short: float, kurtosis_long: float, hill_alpha: float, return_acf_1: float, abs_return_acf: dict[int, float], n_trades: int, sign_acf: dict[int, float], sign_gamma: float, sign_gamma_r2: float)
Summary statistics of one run (or one replayed day).
hill_alpha
instance-attribute
¶
hill_alpha: float
Hill estimate of the tail index of dt_short returns.
abs_return_acf
instance-attribute
¶
Autocorrelation of absolute dt_short returns (volatility clustering), by lag in units of dt_short.
sign_acf
instance-attribute
¶
Autocorrelation of trade signs, by lag in trades.
sample_returns
¶
sample_returns(mid_path: Sequence[tuple[int, float]], dt: int, *, start: int = 0, end: int) -> list[float]
Log returns of a step-function mid path sampled every dt time units.
Intervals where the mid is undefined at either end are skipped.
acf
¶
Sample autocorrelation at each lag (biased estimator, like most references).
log_lags
¶
About n distinct integer lags, logarithmically spaced over [1, max_lag].
excess_kurtosis
¶
Fourth standardized moment minus 3 (0 for a Gaussian).
hill_tail_index
¶
Hill estimator of the tail exponent alpha of |x|: P(|X| > x) ~ x^-alpha.
Uses the largest tail fraction of observations. Smaller alpha means
fatter tails (a Gaussian has none: the estimate grows with sample size).
aggressor_signs
¶
One sign per aggressive order (+1 buy, -1 sell), in time order.
Consecutive fills of the same taker order (a sweep through several makers) count as one trade, as in the empirical literature.
power_law_fit
¶
Least-squares fit of y = c * x^-gamma on log-log axes, ignoring y <= 0.
Returns (gamma, r_squared).
metaorder_impacts
¶
metaorder_impacts(metaorders: Iterable[MetaorderRecord], mid_path: Sequence[tuple[int, float]], *, daily_volume: float, sigma: float) -> list[tuple[float, float]]
(Q / daily_volume, impact / sigma) for each metaorder with a defined mid.
The impact of one metaorder is side * log(mid(end) / mid(start)): the
price move in its direction during execution. sigma is the return
volatility over the same reference "day" as daily_volume.
MetaorderRecord
¶
Bases: NamedTuple
A large order executed as a sequence of child orders.
bin_impacts
¶
bin_impacts(points: Sequence[tuple[float, float]], n_bins: int = 8) -> tuple[list[tuple[float, float, int]], float, float]
Group (size, impact) points into log-spaced size bins and fit impact ~ size^delta.
Returns (bins, delta, r_squared) with bins (mean size, mean impact, count).
Bins whose mean impact is not positive cannot enter the log-log fit.
impact_curve
¶
impact_curve(metaorders: Iterable[MetaorderRecord], mid_path: Sequence[tuple[int, float]], *, daily_volume: float, sigma: float, n_bins: int = 8) -> tuple[list[tuple[float, float, int]], float, float]
Average metaorder impact against size, and the fitted impact exponent.
Combines metaorder_impacts() and
bin_impacts(). The square-root law predicts
impact / sigma ~ Y * (Q / V)^0.5 with Y of order one.