Skip to content

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).

dt_short instance-attribute

dt_short: int

Short return horizon, in time steps.

dt_long instance-attribute

dt_long: int

Long return horizon, in time steps.

n_returns instance-attribute

n_returns: int

Number of dt_short returns.

kurtosis_short instance-attribute

kurtosis_short: float

Excess kurtosis of dt_short returns.

kurtosis_long instance-attribute

kurtosis_long: float

Excess kurtosis of dt_long returns.

hill_alpha instance-attribute

hill_alpha: float

Hill estimate of the tail index of dt_short returns.

return_acf_1 instance-attribute

return_acf_1: float

Lag-1 autocorrelation of dt_short returns.

abs_return_acf instance-attribute

abs_return_acf: dict[int, float]

Autocorrelation of absolute dt_short returns (volatility clustering), by lag in units of dt_short.

n_trades instance-attribute

n_trades: int

Number of trades (aggressive orders) with a known side.

sign_acf instance-attribute

sign_acf: dict[int, float]

Autocorrelation of trade signs, by lag in trades.

sign_gamma instance-attribute

sign_gamma: float

Fitted exponent γ in ACF(sign) ∝ lag^−γ.

sign_gamma_r2 instance-attribute

sign_gamma_r2: float

R² of that fit, on log-log axes.

rows

rows() -> dict[str, float]

Flat {statistic: value} dict, convenient for tables.

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

acf(xs: Sequence[float], lags: Iterable[int]) -> list[float]

Sample autocorrelation at each lag (biased estimator, like most references).

log_lags

log_lags(max_lag: int, n: int = 20) -> list[int]

About n distinct integer lags, logarithmically spaced over [1, max_lag].

excess_kurtosis

excess_kurtosis(xs: Sequence[float]) -> float

Fourth standardized moment minus 3 (0 for a Gaussian).

hill_tail_index

hill_tail_index(xs: Sequence[float], tail: float = 0.05) -> float

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

aggressor_signs(fills: Iterable[Fill]) -> list[int]

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

power_law_fit(xs: Sequence[float], ys: Sequence[float]) -> tuple[float, float]

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.

side instance-attribute

side: int

+1 for a buy metaorder, -1 for a sell.

qty instance-attribute

qty: int

Total size, in shares.

start instance-attribute

start: int

Time the metaorder started.

end instance-attribute

end: int

Time of its last child order.

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.