Kernel Density Estimation code - Help needed please

 

Hi,

I've found a nice article about KDE here https://www.mql5.com/en/articles/396 which is used for clustering large data. I've tested on MT5 didn't worked as I expected. Since I'm not too much familiar with MT5 environment and I'd like to use it on MT4, I simply used the files on MQL4. Again the same unexpected result.

In the code to be simpler, I've disabled the readings x[] data from symbol and manually add 20 decimal inputs to array X[].  I also disable graphical part to simplify the code thus I'm writing the results to a file to plot it on excel.

The results are as below tables and graph.

Kernel Density Esmimation - Output


As clearly seen from red graph, the density should not be like this. I've tested with several different parameters but couldn't make it work.

I also would expect that the density output would be larger than 20 lines, may be 100 lines or more?

Could someone kindly help me to figure out why the code does not work properly? Or may be recommend me a different KDE code for MQL4.

Thanks in advance.



//+------------------------------------------------------------------+
//|                                                  KDE_Example.mq5 |
//|                                                    2012, victorg |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2012, victorg"
#property link      "http://www.mql5.com"
#property strict

#include <CKDensity.mqh>
//#include <ChartTools\CLinDraw.mqh>

int d;   // File handle

double X[];
double Sc[];
double Z[];
  
void OnInit()
  {
   int i;
   int ndata=20;                       // Input data length
  
   d=FileOpen("density_test",FILE_CSV|FILE_WRITE);

  //-------------------------- Preparation of the input sequence
  ArrayResize(X,ndata+1);
  /* i=CopyOpen(_Symbol,PERIOD_CURRENT,0,ndata+1,X);
   if(i!=ndata+1)
     {
      Print("Not enough historical data.");
      return;
     }
   for(i=0;i<ndata;i++)X[i]=MathLog(X[i+1]/X[i]); // Logarithmic returns
   
   */
   
   X[0]=0.30;X[1]=0.30;X[2]=0.31;X[3]=0.32;X[4]=0.30;X[5]=0.29;X[6]=0.40;X[7]=0.50;
   X[8]=0.70;X[9]=0.71;X[10]=0.70;X[11]=0.70;X[12]=0.68;X[13]=0.71;X[14]=0.72;X[15]=0.90;
   X[16]=0.91;X[17]=0.90;X[18]=1.10;X[19]=1.30;
   
   ArrayResize(X,ndata);

//-------------------------- Kernel density estimation
   CKDensity *kd=new CKDensity;
   kd.PluginMode(1);                     // Enable Plug-in mode
   kd.Density(X);                        // Density estimation 

//-------------------------- Graph of a Laplace distribution
/*
   n=kd.Np;                              // Number of test point
   ArrayResize(Z,n);
   for(i=0;i<n;i++)
     {
      a=MathAbs(kd.T[i]*M_SQRT2);
      Z[i]=0.5*MathExp(-a)*M_SQRT2;
     }
//-------------------------- Scatter plot
   n=kd.N;                               // Data length
   if(n<400)
     {
      ArrayResize(Sc,n);
      for(i=0;i<n;i++)Sc[i]=kd.X[i];
     }
   else                                  // Decimation of data
     {
      ArrayResize(Sc,400);
      for(i=0;i<400;i++)
        {
         a=i*(n-1.0)/399.0+0.5;
         Sc[i]=kd.X[(int)a];
        }
     }
*/
//-------------------------- Visualization
 /*  CLinDraw *ld=new CLinDraw;
   ld.Title("Kernel Density Estimation");
   ld.SubTitle(StringFormat("Data lenght=%i, h=%.3f",kd.N,kd.H));
   ld.YTitle("density");
   ld.XTitle("normalized X (mean=0, variance=1)");

   ld.AddGraph(kd.T,Z,"line","Laplace",1,"200,120,70,1");
   ld.AddGraph(kd.T,kd.Y,"line","Estimation");
   ld.AddGraph(Sc,-0.02,"scatter","Data",0,"0,4,16,0.4");

   ld.LDraw();                           // With WEB-browser autostart 
                                        //   ld.LDraw(0);                        // Without autostart 
   //  delete(ld);
*/
   int tt=ArraySize(kd.Y);
   
   for(i=0;i<tt;i++) FileWrite(d,DoubleToStr(X[i],5)+"&"+DoubleToStr(kd.T[i],5)+"&"+DoubleToStr(kd.Y[i],5));
 



   delete(kd);
  }
//--------------------------------------------------------------------


void OnDeinit(const int xreason)
{
   FileFlush(d);
   FileClose(d);
}
Kernel Density Estimation of the Unknown Probability Density Function
Kernel Density Estimation of the Unknown Probability Density Function
  • www.mql5.com
Improvement of MQL5 performance and steady growth of PC productivity allow MetaTrader 5 platform users to apply fairly sophisticated and advanced mathematical methods for the market analysis. These methods may belong to various areas of economics, econometrics or statistics but in any case we will have to deal with the concept of probability...
Files:
CSJPlugin.mqh  20 kb
CKDensity.mqh  11 kb
Density4.mq4  4 kb
 

Hi

there are some mistakes inside the code


in the file  CSJPlugin

in the routine 

//+------------------------------------------------------------------+

//| Choose the parameters for the algorithm                          |

//+------------------------------------------------------------------+

void CSJPlugin::choose_parameters(void)

................................................................

put => while((error>comp_eps)&&(p<=P_UL))   

instead of while((error>comp_eps)&(p<=P_UL))


/////////////////////////

also, the KDE_example is a script, so you should replicate all the additionnal files "include" inside the MQL5\Scripts directory