Class CommonlyUsedMoments

java.lang.Object
com.polytechnik.freemoney.CommonlyUsedMoments
All Implemented Interfaces:
CommonBasisOperations
Direct Known Subclasses:
CommonlyUsedMomentsLaguerre, CommonlyUsedMomentsLegendreShifted, CommonlyUsedMomentsMonomials

public abstract class CommonlyUsedMoments extends Object implements CommonBasisOperations
A class to calculate commonly used moments. It has an internal state WIntegrator and methods for updating the internal state with new t,P,dv coming.
  • Field Details

    • wi

      private final WIntegrator wi
      Recurrent integrator.
    • p_offset

      private double p_offset
      Price offset to increase numerical stability.
    • P_lastsaved

      private double P_lastsaved
      For dp/dt calculation.
    • IH_lastsaved

      private double IH_lastsaved
      For d lambda^{IH}/dt calculation using secondary sampling.
    • P_for_IH_lastsaved

      private double P_for_IH_lastsaved
      For d lambda^{IH}/dt calculation using secondary sampling.
    • t_lastsaved

      private long t_lastsaved
      Time of last trade added, may be different from last basis time reference WIntegrator.currenttime_previous.
    • counter_addObservationNoBasisShift

      private long counter_addObservationNoBasisShift
    • counter_addIHObservationSecondarySampling

      private long counter_addIHObservationSecondarySampling
  • Constructor Details

    • CommonlyUsedMoments

      public CommonlyUsedMoments(int ntm, double atau)
      Initial state.
  • Method Details

    • updateWithSingleObservation

      public void updateWithSingleObservation(long currenttime, long t_last, double P_last, double dv_last)
      A trivial update with a single triple (t,P,dv) coming. The method shifts the basis to correspond to currenttime; new value of getPOffset() can be also set to increase numerical stability. Internally the method adjusts the basis using updateBasisToNewTimeReference(long, double), then calls addObservationNoBasisShift(long, long, double, double) to add an observation.
    • updateBasisToNewTimeReference

      public void updateBasisToNewTimeReference(long currenttime, double new_p_offset)
    • addObservationNoBasisShift

      public void addObservationNoBasisShift(long currenttime, long t, double P, double dv)
    • addIHObservationSecondarySampling

      public void addIHObservationSecondarySampling(double IH)
      An experiment to calculate \( \frac{d}{dt}I^H \), \( \frac{d}{dt} P I^H \), and \( P\frac{d}{dt}I^H \) moments by using secondary sampling; the \( I^H \) is calculated from directly sampled moments and then used as it were a regular observable. The call of addIHObservationSecondarySampling(double) should immediately follow updateWithSingleObservation(long, long, double, double). We use incremental calculation of moments.
      See Also:
      • CommonlyUsedMoments_TestFromSample.fromSeqRecurrentlyTestSecondarySampling(com.polytechnik.trading.CommonlyUsedMoments_TestFromSample.InitStorage<T>, int, long, long, com.polytechnik.realtimebook.Trade[], double)
    • getPOffset

      public double getPOffset()
      Price Offset. Selected close to last price to simplify calculations. Average price would probably provide a slightly better numerical stability. All price moments are calculated relatively this offset, this greatly increases numerical stability. To obtain "actual" price from the values stored in moments data -- add the value returned by this method.
      See Also:
    • getPLast_withoutPOffset

      public double getPLast_withoutPOffset()
      Last price as per addObservationNoBasisShift(long, long, double, double) call. No price offset used. To use it with saved price moments -- use the value getPLast_withoutPOffset()-getPOffset().
    • getdVMoments

      public double[] getdVMoments()
      Volume moments <I*Qk>. Corresponds to DataInterfaceDirectTradesHistorical.M_I and price power=0.
      See Also:
    • getTotalVolume

      public double getTotalVolume()
    • getTotalA

      public double getTotalA()
    • getPdVMoments

      public double[] getPdVMoments()
      <p*I*Qk>, corresponds to DataInterfaceDirectTradesHistorical.M_I and price power=1.
    • getP2dVMoments

      public double[] getP2dVMoments()
      <p^2*I*Qk>, corresponds to DataInterfaceDirectTradesHistorical.M_I and price power=2.
    • getP3dVMoments

      public double[] getP3dVMoments()
      <p^3*I*Qk>, corresponds to DataInterfaceDirectTradesHistorical.M_I and price power=3.
    • getTdVMoments

      public double[] getTdVMoments()
      <T*I*Qk>, corresponds to DataInterfaceDirectTradesHistorical.M_TI and price power=0.
    • getTPdVMoments

      public double[] getTPdVMoments()
      <p*T*I*Qk>, corresponds to DataInterfaceDirectTradesHistorical.M_TI and price power=1.
    • getTP2dVMoments

      public double[] getTP2dVMoments()
      <p^2*T*I*Qk>, corresponds to DataInterfaceDirectTradesHistorical.M_TI and price power=2.
    • getTP3dVMoments

      public double[] getTP3dVMoments()
      <p^3*T*I*Qk>, corresponds to DataInterfaceDirectTradesHistorical.M_TI and price power=3.
    • getdpMoments

      public double[] getdpMoments()
      Price change moments <dp/dt*Qk>. Corresponds to DataInterfaceDirectTradesHistorical.M_DP and price power=1.
    • getTdpMoments

      public double[] getTdpMoments()
      <T*dp/dt*Qk>, corresponds to DataInterfaceDirectTradesHistorical.M_TDP and price power=1.
    • getdAMoments

      public double[] getdAMoments()
      A poor man volume, <abs(dp)/dt*Qk>. Instead of a number of shares traded the absolute value of every tick price change is used as integration measure. Can be used instead of volume with mixed results, see Fig. 6. Corresponds to DataInterfaceDirectTradesHistorical.M_A and price power=0.
      See Also:
    • getPdAMoments

      public double[] getPdAMoments()
      The moments <p*abs(dp)/dt*Qk>. Instead of a number of shares traded the absolute value of every tick price change is used as integration measure. Corresponds to DataInterfaceDirectTradesHistorical.M_A and price power=1.
      See Also:
    • getAdPMoments

      public double[] getAdPMoments()
      <A*dP/dt *Qk>.
    • getTdAMoments

      public double[] getTdAMoments()
      <T*abs(dp)/dt*Qk>.
      See Also:
    • getTdtMoments

      public double[] getTdtMoments()
      Obtained using integration by parts from getdtMoments().
    • getVdtMoments

      public double[] getVdtMoments()
      Obtained using integration by parts from getdVMoments().
    • getdtMoments

      public double[] getdtMoments()
      The moments of a constant <Qk>, analytically obtained exact value. Corresponds to DataInterfaceDirect.setMT(double[], int).
    • getVdPMoments

      public double[] getVdPMoments()
      <V*dP/dt *Qk>. Corresponds to DataInterfaceDirectTradesHistorical.M_VdPdt and price power=1.
      See Also:
      • CommonlyUsedMoments_TestFromSample.fromSeqRecurrentlyTestSecondarySampling(com.polytechnik.trading.CommonlyUsedMoments_TestFromSample.InitStorage<T>, int, long, long, com.polytechnik.realtimebook.Trade[], double)
    • getdIHdtMoments

      public double[] getdIHdtMoments()
      An experimental method to obtain \( \frac{d}{dt}I^H \) moments using secondary sampling. Requires addIHObservationSecondarySampling(double) to be properly called on every tick.
      See Also:
    • getPdIHdtMoments

      public double[] getPdIHdtMoments()
      An experimental method to obtain \( P\frac{d}{dt}I^H \) moments using secondary sampling. Requires addIHObservationSecondarySampling(double) to be properly called on every tick.
      See Also:
    • getdPIHdtMoments

      public double[] getdPIHdtMoments()
      An experimental method to obtain \( \frac{d}{dt}PI^H \) moments using secondary sampling. Requires addIHObservationSecondarySampling(double) to be properly called on every tick. For secondary sampling where the \( I^H \) can be of various meaning it is better to use \( \frac{d}{dt}PI^H \) rather than \( I^H\frac{d}{dt}P \) as in getVdPMoments() for the reason of unit test simplification. The \( I^H\frac{d}{dt}P \) can be obtained as getdPIHdtMoments()-getPdIHdtMoments().
      See Also:
    • setInitIHForSecondarySampling

      public void setInitIHForSecondarySampling(double IH_lastsaved)
      For unit tests to imitate initial input to match an observable's derivative. In application the initial value is zero and this method is not required.