Class KylesLambda

java.lang.Object
com.quantfinlib.microstructure.KylesLambda

public final class KylesLambda extends Object
Kyle's lambda — market impact LEARNED from the tape instead of assumed from a formula. Kyle (1985): price change is linear in signed order flow, Δp = λ·q + noise, and λ (price per unit of signed volume) is the market's depth read off its own behavior. Where MarketImpactModel parameterizes impact (square-root law with calibrated constants), this class runs the streaming through-origin regression λ = E[q·Δp] / E[q²] on time-decayed moments — the regression-through-origin form is standard here because both the signed flow and the mid change are zero-mean at these horizons.

Feed one onSample(double, double) per aggregation window (a trade, a bar, a decision interval): the mid change over the window and the signed volume that traded in it (buy-aggressor positive — TradeClassifier supplies the sign when the feed doesn't). impactBps(double, double) then prices a contemplated child order in the units execution.BenchmarkExecutor.MarketState.impactBps expects — closing the last MarketState input that lacked a live producer. A noisy negative λ estimate is clamped to zero impact there: "the market pays you to trade" is an estimation artifact, and feeding it to the executor would ACCELERATE the schedule on garbage. lambda() stays raw for diagnostics.

Gap discipline: non-finite inputs are skipped whole; the moments seed from the first valid sample. Zero allocation per event, single writer, one instance per symbol. Persistable via persist.Checkpoint — depth is a slowly-moving property worth carrying across the overnight.

  • Constructor Summary

    Constructors
    Constructor
    Description
    2% sample weight.
    KylesLambda(double alpha)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    impactBps(double quantity, double mid)
    The estimated impact of trading quantity now, in basis points of mid — the live producer for MarketState.impactBps.
    double
    The learned λ: price change per unit of signed volume.
    void
    onSample(double midChange, double signedVolume)
    One aggregation window: the mid change over the window and the signed volume traded in it (+ = buyer-initiated).
    void
    Restores the moments.
    long
     
    void
    Persists the learned depth moments — see persist.Checkpoint.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • KylesLambda

      public KylesLambda(double alpha)
      Parameters:
      alpha - EWMA weight per sample, e.g. 0.02 (≈ 50-sample memory)
    • KylesLambda

      public KylesLambda()
      2% sample weight.
  • Method Details

    • onSample

      public void onSample(double midChange, double signedVolume)
      One aggregation window: the mid change over the window and the signed volume traded in it (+ = buyer-initiated). Non-finite or volume-free windows are skipped — no flow, no impact information.
    • lambda

      public double lambda()
      The learned λ: price change per unit of signed volume. Raw — can be negative while the estimate is noise. 0 until any flow is observed.
    • impactBps

      public double impactBps(double quantity, double mid)
      The estimated impact of trading quantity now, in basis points of mid — the live producer for MarketState.impactBps. The SIGN of the quantity is ignored (impact is a cost in both directions — a signed sell size must not read as free), a negative λ estimate is clamped to 0 (noise, never a subsidy), and non-finite inputs are neutral.
    • samples

      public long samples()
    • writeState

      public void writeState(DataOutput out) throws IOException
      Persists the learned depth moments — see persist.Checkpoint.
      Throws:
      IOException
    • readState

      public void readState(DataInput in) throws IOException
      Restores the moments. Throws on a version mismatch.
      Throws:
      IOException