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 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 negative λ estimate is clamped to 0 (noise, never a subsidy), and non-finite inputs are neutral.

      Units and aggregation. λ is estimated from onSample(double, double) windows as price change per unit of net signed volume over one aggregation window (Kyle 1985, Econometrica 53(6); Hasbrouck 2007, ch. 5). Pricing a single child order with it assumes the child is the window's entire net flow: a child that is 10% of the window's flow is charged as if it moved the whole window. Pass the net quantity you expect to trade over one window of the same length as the one used for estimation (for example the parent order's slice per window), not an individual fill, and keep quantity in the same share units as the signed volume fed to onSample(double, double).

    • 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