//+------------------------------------------------------------------+ //| pacf.mqh | //| Copyright 2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, MetaQuotes Ltd." #property link "https://www.mql5.com" #include"..\..\np_graphics.mqh" #include"..\..\Regression\utils.mqh" //+------------------------------------------------------------------+ //| autocorrelation function result struct | //+------------------------------------------------------------------+ struct PACFResult { vector pacf; matrix conf_intervals; PACFResult(void) { pacf = vector::Zeros(0); conf_intervals = matrix::Zeros(0,0); } PACFResult(vector& _pacf, matrix& _confintervals) { pacf = _pacf; conf_intervals = _confintervals; } PACFResult(PACFResult& other) { pacf = other.pacf; conf_intervals = other.conf_intervals; } void operator=(PACFResult& other) { pacf = other.pacf; conf_intervals = other.conf_intervals; } }; //+------------------------------------------------------------------+ //| yule walker wrapper | //+------------------------------------------------------------------+ vector pacf_yw(vector& x, ulong lags=0, ENUM_YW_METHOD method=YW_ADJUSTED) { ulong nobs = x.Size(); if(!lags) lags = (ulong)MathMax(MathMin(ulong(10*log10(nobs)),nobs-1),1); vector pacf = vector::Zeros(lags+1); pacf[0] = 1.; vector inputx = x; for(ulong k = 1; k(x_data.Size()/2)) { Print(__FUNCTION__, " Can only compute partial correlations for lags up to 50% of the sample size."); return PACFResult(); } vector ret = pacf_yw(x_data,nlags,YW_ADJUSTED); double varacf = 1.0/double(x_data.Size()); double interval = CNormalDistr::InvNormalCDF(1.0 - alpha/2.0)*sqrt(varacf); matrix confint = matrix::Zeros(2,ret.Size()); if(!confint.Row(ret-interval,0) || !confint.Row(ret+interval,1)) { Print(__FUNCTION__," error ", GetLastError()); return PACFResult(); } confint[0,0] = confint[1,0] = ret[0]; return PACFResult(ret,confint); } //+------------------------------------------------------------------+ //| plot the PACF | //+------------------------------------------------------------------+ void plot_pacf(PACFResult& pacf,long chart_id = 0,int x_coordinate = 0, int y_coordinate = 0, int subwindow = 0,int width = 600, int height = 400,string plot_name = NULL, int fontsize = 15, long viewtimeseconds = 30, bool showchart=true ) { if(!pacf.pacf.Size()) { Print(__FUNCTION__, " Results object is empty "); return; } if(StringLen(plot_name)<1) plot_name = "Partial Autocorrelation Function"; CGraphic* graph = new CGraphic(); //--- if(!chart_id) chart_id = ChartID(); //--- ChartRedraw(chart_id); //--- ChartSetInteger(chart_id, CHART_SHOW, showchart); //--- if(!graph.Create(chart_id,plot_name,subwindow,x_coordinate,y_coordinate,width,height)) { delete graph; Print(__FUNCTION__," Failed to Create graphical object on the Main chart Err ", GetLastError()); return;; } //--- double x[], y[]; ArrayResize(x,(int)pacf.pacf.Size()); ArrayResize(y,(int)pacf.pacf.Size()); //--- int ncurves = 4; //--- ENUM_CURVE_TYPE ctype = WRONG_VALUE; //--- for(int i=0; i