/***********************************************************************
* https://www.ingber.com/markets96_lag_cmi.c
* Lester Ingber <ingber@ingber.com>
* Copyright (c) 1996-2017 Lester Ingber.  All Rights Reserved.
* This LICENSE file must be included with markets96_lag_cmi.c code.
***********************************************************************/
  /*
     Redistribution and use in source and binary forms, with or without
     modification, are permitted provided that the following conditions are
     met:

     CONDITIONS

     1. Redistributions of markets96_lag_cmi.c source code must retain
     the above copyright notice, this list of conditions, and the
     following disclaimer.

     2. Redistributions in binary form must contain the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

     3. All advertising and published materials mentioning features or use
     of this software must display the following acknowledgment:  "This
     product includes software developed by Lester Ingber and other
     contributors."

     4. The name of Lester Ingber may not be used to endorse or promote
     products derived from this software without specific prior written
     permission.

     DISCLAIMER

     This software is provided by Lester Ingber and contributors "as is" and
     any expressed or implied warranties, including, but not limited to, the
     implied warranties of merchantability and fitness for a particular
     purpose are disclaimed.  In no event shall Lester Ingber or
     contributors be liable for any direct, indirect, incidental, special,
     exemplary, or consequential damages (including, but not limited to,
     procurement of substitute goods or services; loss of use, data, or
     profits; or business interruption) however caused and on any theory of
     liability, whether in contract, strict liability, or tort (including
     negligence or otherwise) arising in any way out of the use of this
     software, even if advised of the possibility of such damage.
   */

   /* lag_cmi () returns the value of the cost function to be fit.
      Canonical momenta indicators (CMI) also are returned, which may
      be used as input for an outer-shell cost function, e.g., a set
      of trading rules.

      This C code evaluates the effective short-time (dt) action
      (Lagrangian dt) and momenta of the mathematically equivalent
      set of coupled stochastic differential equations:
      d_cash/dt = f_cc cash + f_cf futures + g_1c eta_c^1 + g_2c eta_c_c2
      d_futures/dt = f_fc cash + f_ff futures + g_1f eta_f^1 + g_2f eta_f^2
      where the 4 eta's are independent sources of Gaussian-Markovian noise,
      and the f_'s and g_'s are simple constants.

      Note it may be useful to use the "detrended" variables, e.g.,
      cash(t) = price_cash(t) / price_cash(t-1)
      futures(t) = price_futures(t) / price_futures(t-1)

      This yields a short-time conditional probability distribution p,
      p[x(t + dt) | x(t)] = (2 pi dt determ)^-1 exp(- action)
      for the evolution of the two coupled cash-futures variables
      x = { cash, futures}.

      This is an extremely simple two-variable example with linear drifts
      and constant diffusions.  For more complex examples see papers in
      https://www.ingber.com/
    */

#include <math.h>		/* add includes required by your system */

#define PI1 3.141592653589	/* 79323846 */

#define N_PAR 7			/* number of parameters */
double param[N_PAR];		/* input values of params to be fit */

#define N_DATA 1000		/* number of points in time mesh */
double time[N_DATA];		/* input time mesh */
double cash[N_DATA];		/* input cash variables */
double futures[N_DATA];		/* input futures variables */
double m_cash[N_DATA];		/* output values of cash CMI */
double m_futures[N_DATA];	/* output values of futures CMI */

double lag_cmi (double *time,
		double *cash,
		double *futures,
		double *param,
		double *m_cash,
		double *m_futures);

double
lag_cmi (double *time,
	 double *cash,
	 double *futures,
	 double *param,
	 double *m_cash,
	 double *m_futures)
{
  int s;
  double dt, action, determ, lgdeterm;
  double drift_c, drift_f, c_factor, f_factor;
  double Gcc, Gff, Gcf, Gfc;
  double metric_cc, metric_ff, metric_cf, metric_fc;	/* inverse covariance */
  double f_cc, f_cf, f_fc, f_ff;

  f_cc = param[0];
  f_cf = param[1];
  f_fc = param[2];
  f_ff = param[3];

  Gcc = param[4];
  Gff = param[5];
  Gcf = Gfc = param[6];

  /* In terms of the coefficients of noise in the above differential eqs:
     Gcc = g_1c * g_1c + g_2c * g_2c
     Gff = g_1f * g_1f + g_2f * g_2f
     Gcf = Gfc = g_1c * g_1f + g_2c * g_2f
   */

  action = 0;
  for (s = 0; s < N_DATA - 1; ++s)
    {
      dt = time[s + 1] - time[s];
      drift_c = f_cc * cash[s] + f_cf * futures[s];
      drift_f = f_fc * cash[s] + f_ff * futures[s];

      c_factor = (cash[s + 1] - cash[s]) / dt - drift_c;
      f_factor = (futures[s + 1] - futures[s]) / dt - drift_f;

      determ = Gcc * Gff - Gcf * Gfc;
      lgdeterm = log (2.0 * PI1 * dt) + 0.5 * log (determ);

      metric_cc = Gff / determ;
      metric_ff = Gcc / determ;
      metric_cf = -Gfc / determ;
      metric_fc = -Gcf / determ;

      m_cash[s + 1] = metric_cc * c_factor + metric_cf * f_factor;
      m_futures[s + 1] = metric_fc * c_factor + metric_ff * f_factor;

      action +=
	(
	  (c_factor * c_factor) / (2.0 * Gcc) +
	  (f_factor * f_factor) / (2.0 * Gff) +
	  (c_factor * f_factor) / Gcf
	) * dt;

      action += lgdeterm;	/* add prefactor to "effective" action */
    }

  return (action);
}

/* $Id: markets96_lag_cmi.c,v 10.15 2017/06/09 00:47:38 ingber Exp ingber $ */
