//+------------------------------------------------------------------+
//|                                                     TestRLS.mq5  |
//+------------------------------------------------------------------+

#include <RLSRegression\RLSRegression.mqh>

#define ASSERT(cond,msg) { if(cond) Print("PASS: ",msg); else Print("FAIL: ",msg); }

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(void)
  {
   CRLSRegression rls;
   rls.Init(0.98,1000.0,20);

   for(int i=0; i<5; i++)
     {
      double y=2.0+3.0*(double)i;
      rls.Update(y,(double)i);
     }

   ASSERT(rls.IsValid()==false,
          "filter is not valid after only 5 observations (min_obs=20)");

   for(int i=5; i<50; i++)
     {
      double y=2.0+3.0*(double)i;
      rls.Update(y,(double)i);
     }

   double slope=rls.GetSlope();
   double intercept=rls.GetIntercept();

   ASSERT(MathAbs(slope-3.0)<0.01,
          StringFormat("slope converges to 3.0 after 50 observations (got %.6f)",slope));
   ASSERT(MathAbs(intercept-2.0)<0.5,
          StringFormat("intercept converges near 2.0 after 50 observations (got %.6f)",intercept));

   double forecast=rls.Forecast(51.0);
   ASSERT(MathAbs(forecast-155.0)<0.5,
          StringFormat("Forecast(51) is approximately 155.0 (got %.6f)",forecast));

   PrintFormat("Final state after 50 observations: theta0=%.6f, theta1=%.6f, obs_count=%d",
               intercept,slope,rls.GetObsCount());
  }
//+------------------------------------------------------------------+