Class OrnsteinUhlenbeck

java.lang.Object
com.quantfinlib.microstructure.OrnsteinUhlenbeck

public final class OrnsteinUhlenbeck extends Object
Ornstein-Uhlenbeck estimation — the mean-reversion engine under every pairs trade and basis position: dx = κ(θ − x)dt + σ dW. Fit from a sampled series by exact AR(1) mapping (x_{t+1} = a + b·x_t + ε with b = e^{−κΔt}), giving the three numbers a spread trader actually uses:
  • half-life ln2/κ — how long the spread takes to close half its gap: the holding-period estimate, and the first filter (a 200-day half-life is not a trade);
  • z-score (x − θ)/σ_stat with the STATIONARY stdev σ/√(2κ) — entry/exit in units the strategy can threshold;
  • the refusal: a fitted b ≥ 1 means the series shows NO mean reversion in-sample — the fit throws rather than reporting an infinite half-life as a tradable number, because fitting OU to a random walk is how pairs desks die.

Small-sample honesty: the OLS AR(1) slope is DOWNWARD-biased in finite samples (Kendall: E[b_hat - b] ~ -(1+3b)/n), so near the minimum n=30 the fitted κ runs high and the half-life SHORT — a 20-day true half-life can fit as ~12. Treat short-sample half-lives as optimistic lower bounds and prefer n in the hundreds before sizing a holding period off them. Stated, not corrected: bias corrections trade variance for bias and are themselves sample-size-sensitive.

Static, deterministic, research lane. Pair with hedging.CointegrationTest (is the spread stationary at all?) and hand the trade to execution.SpreadExecutionAlgo.

  • Method Details

    • fit

      public static OrnsteinUhlenbeck.Params fit(double[] series, double dt)
      Fits OU to a series sampled every dt time units (e.g. dt = 1.0/252 for daily samples in years). Throws when the series shows no mean reversion — see class doc.
      Parameters:
      series - ≥ 30 finite observations
      dt - sampling interval, > 0
    • lastZScore

      public static double lastZScore(double[] series, double dt)
      Convenience: the fitted z-score of the LAST observation.