Reconcile/Scripts/Reconcile/Recon_Evidence.mq5
ayantrader ayantrader 71fdcd7c2c Revision after review: reconciled covariance, ridge reporting, guarantee check
- ReconciledCovariance now takes the base error covariance and returns
  P Sigma P^T; the old S A^-1 S^T form was only valid when W equals it.
- RecSpdInverse reports the ridge the normal-equation matrix needed.
- Diebold-Mariano p-value from Student t with n-1 degrees of freedom.
- WeightedDistance added: the metric each projection is orthogonal in.
- Recon_Evidence reports the data span, a per-origin guarantee table and
  how far the realised outcome sits from coherent.
- Last-value forecast is no longer described as a random walk.
- README brought in line with the revised article.
2026-10-07 21:07:45 +05:00
1104 lines
39 KiB
MQL5
//+------------------------------------------------------------------+
//| Recon_Evidence.mq5 |
//| MMQ — Muhammad Minhas Qamar |
//| www.mql5.com/en/articles/24318 |
//+------------------------------------------------------------------+
#property copyright "MMQ — Muhammad Minhas Qamar"
#property link "https://www.mql5.com/en/articles/24318"
#property version "1.00"
#property strict
#property script_show_inputs
#property description "Walk-forward comparison of base forecasts against"
#property description "five reconciliation weightings, on both hierarchies."
#include <Reconcile\ReconcileTypes.mqh>
#include <Reconcile\Hierarchy.mqh>
#include <Reconcile\BaseForecast.mqh>
#include <Reconcile\Shrinkage.mqh>
#include <Reconcile\Reconcile.mqh>
input group "What to run"
input ENUM_REC_HIERARCHY InpHierarchy = REC_HIER_TEMPORAL; // hierarchy to evaluate
input bool InpRunBoth = false; // evaluate both in one pass
input group "Temporal hierarchy"
input string InpSymbol = "EURUSD"; // symbol
input ENUM_TIMEFRAMES InpBaseTF = PERIOD_H1; // base timeframe
input int InpTopPeriod = 24; // base bars per top period
input string InpFactors = "1,2,3,4,6,8,12,24"; // aggregation factors
input group "Currency hierarchy"
input string InpPairs = "EURUSD,GBPUSD,USDJPY,AUDUSD,USDCHF,USDCAD,NZDUSD,EURJPY,EURGBP,EURCHF,EURAUD,GBPJPY,AUDJPY,CADJPY,CHFJPY,AUDNZD,AUDCAD,GBPCAD,GBPCHF,EURCAD,NZDJPY";
input string InpNumeraire = "USD"; // numeraire currency
input ENUM_TIMEFRAMES InpPairTF = PERIOD_H1; // timeframe for the pairs
input group "Base forecasts"
input ENUM_REC_BASE_MODEL InpModel = REC_BASE_AR1; // base model per node
input int InpFitWindow = 120; // observations per fit
input int InpBars = 12000; // history to load
input group "Reconciliation"
input int InpErrWindow = 250; // residuals kept for W
input int InpMinObs = 80; // residuals before scoring
input int InpRefitEvery = 1; // origins between refits
input int InpDmLag = 1; // Bartlett lags for the DM test
input group "Incoherence diagnostic"
input ENUM_REC_WEIGHT InpBucketScheme = REC_W_OLS; // scheme the diagnostic compares
input int InpBuckets = 5; // incoherence buckets
//--- base forecasts plus the five weightings under test.
#define REC_NCOL 6
ENUM_REC_WEIGHT g_scheme[5]=
{
REC_W_OLS,REC_W_WLS_STRUCT,REC_W_WLS_VAR,REC_W_MINT_SAMPLE,REC_W_MINT_SHRINK
};
//+------------------------------------------------------------------+
//| CEvidence - the scoreboard. |
//| |
//| Squared errors are accumulated per level so the report can show |
//| where a gain lands rather than only that there was one, and the |
//| per-origin mean squared error of every column is kept so that |
//| the difference between two columns can be tested rather than |
//| eyeballed. Both hierarchies feed the same object, which is what |
//| keeps the two halves of the report comparable. |
//+------------------------------------------------------------------+
class CEvidence
{
private:
int m_nlev;
int m_origins;
int m_capacity;
double m_sse[]; // REC_NCOL x (nlev+1), last slot is ALL
long m_cnt[]; // nlev+1
double m_omse[]; // capacity x REC_NCOL
double m_lambda_sum;
int m_refits;
int m_rise_w[REC_NCOL]; // origins farther in the scheme's own metric
int m_ridge_w[REC_NCOL]; // refits whose W needed a ridge
int m_ridge_a[REC_NCOL]; // refits whose normal matrix needed one
double m_truth_sq; // sum of squared realised incoherence
public:
CEvidence(void);
bool Init(const int nlev,const int capacity);
//--- score one origin. 'pred' holds the base forecasts in row 0 and
//--- the reconciled sets in the rows after it.
bool Origin(const vector &y,const matrix &pred,
const int &level[],const int n);
void NoteFit(CReconciler &rec[]);
void NoteGuarantee(CReconciler &rec[],const vector &y,
const matrix &pred,const int n);
int Origins(void) const { return m_origins; }
int Refits(void) const { return m_refits; }
double Rmse(const int col,const int level) const;
double MsePct(const int col,const int level) const;
bool Dm(const int col,const int lag,double &stat,double &pval) const;
double MeanLambda(void) const;
void ReportGuarantee(void) const;
};
//+------------------------------------------------------------------+
//| Construct empty. |
//+------------------------------------------------------------------+
CEvidence::CEvidence(void)
{
m_nlev =0;
m_origins =0;
m_capacity =0;
m_lambda_sum=0.0;
m_refits =0;
m_truth_sq =0.0;
ArrayInitialize(m_rise_w,0);
ArrayInitialize(m_ridge_w,0);
ArrayInitialize(m_ridge_a,0);
}
//+------------------------------------------------------------------+
//| Size the accumulators for a hierarchy with 'nlev' levels. |
//+------------------------------------------------------------------+
bool CEvidence::Init(const int nlev,const int capacity)
{
if(nlev<1 || capacity<8)
return false;
m_nlev =nlev;
m_capacity=capacity;
if(ArrayResize(m_sse,REC_NCOL*(nlev+1))!=REC_NCOL*(nlev+1))
return false;
if(ArrayResize(m_cnt,nlev+1)!=nlev+1)
return false;
if(ArrayResize(m_omse,capacity*REC_NCOL)!=capacity*REC_NCOL)
return false;
ArrayInitialize(m_sse,0.0);
ArrayInitialize(m_cnt,0);
ArrayInitialize(m_omse,0.0);
m_origins =0;
m_lambda_sum=0.0;
m_refits =0;
m_truth_sq =0.0;
ArrayInitialize(m_rise_w,0);
ArrayInitialize(m_ridge_w,0);
ArrayInitialize(m_ridge_a,0);
return true;
}
//+------------------------------------------------------------------+
//| Record what one refit of the weights cost in regularisation. |
//+------------------------------------------------------------------+
void CEvidence::NoteFit(CReconciler &rec[])
{
for(int s=0; s<REC_NCOL-1; s++)
{
if(rec[s].Scheme()==REC_W_MINT_SHRINK)
m_lambda_sum+=rec[s].Lambda();
if(rec[s].Ridge()>0.0)
m_ridge_w[s+1]++;
if(rec[s].NormalRidge()>0.0)
m_ridge_a[s+1]++;
}
m_refits++;
}
//+------------------------------------------------------------------+
//| Check the guarantee at one origin. Each scheme may not move |
//| farther from a coherent truth in its own metric; the OLS |
//| residue of the truth says how coherent the truth really was. |
//+------------------------------------------------------------------+
void CEvidence::NoteGuarantee(CReconciler &rec[],const vector &y,
const matrix &pred,const int n)
{
vector eb=vector::Zeros((ulong)n), er=vector::Zeros((ulong)n);
for(int s=0; s<REC_NCOL-1; s++)
{
for(int i=0; i<n; i++)
{
eb[(ulong)i]=y[(ulong)i]-pred[0][(ulong)i];
er[(ulong)i]=y[(ulong)i]-pred[(ulong)(s+1)][(ulong)i];
}
double db=0.0, dr=0.0;
if(rec[s].WeightedDistance(eb,db) && rec[s].WeightedDistance(er,dr)
&& dr>db*(1.0+1.0e-9))
m_rise_w[s+1]++;
if(rec[s].Scheme()==REC_W_OLS)
{
const double r=rec[s].Incoherence(y);
m_truth_sq+=r*r;
}
}
}
//+------------------------------------------------------------------+
//| Score one forecast origin across every column. |
//+------------------------------------------------------------------+
bool CEvidence::Origin(const vector &y,const matrix &pred,
const int &level[],const int n)
{
if(m_origins>=m_capacity || (int)pred.Rows()!=REC_NCOL)
return false;
//--- squared error goes into its own level and into the ALL slot,
//--- and the per-origin mean is kept separately because that is
//--- what the Diebold-Mariano test consumes.
const int stride=m_nlev+1;
for(int c=0; c<REC_NCOL; c++)
{
double tot=0.0;
for(int i=0; i<n; i++)
{
const double d=y[(ulong)i]-pred[(ulong)c][(ulong)i];
const double se=d*d;
m_sse[c*stride+level[i]]+=se;
m_sse[c*stride+m_nlev] +=se;
tot+=se;
}
m_omse[m_origins*REC_NCOL+c]=tot/(double)n;
}
//--- node counts are per origin, not per column, or every mean
//--- would be divided by six times too many observations.
for(int i=0; i<n; i++)
{
m_cnt[level[i]]++;
m_cnt[m_nlev]++;
}
m_origins++;
return true;
}
//+------------------------------------------------------------------+
//| Root mean squared error of one column at one level. |
//+------------------------------------------------------------------+
double CEvidence::Rmse(const int col,const int level) const
{
const int stride=m_nlev+1;
if(m_cnt[level]<1)
return 0.0;
return MathSqrt(m_sse[col*stride+level]/(double)m_cnt[level]);
}
//+------------------------------------------------------------------+
//| Change in mean squared error against the base column, in |
//| percent. Negative is an improvement. |
//+------------------------------------------------------------------+
double CEvidence::MsePct(const int col,const int level) const
{
const int stride=m_nlev+1;
const double b=m_sse[level];
if(b<=0.0)
return 0.0;
return 100.0*(m_sse[col*stride+level]-b)/b;
}
//+------------------------------------------------------------------+
//| Diebold-Mariano of one column against the base column. |
//+------------------------------------------------------------------+
bool CEvidence::Dm(const int col,const int lag,double &stat,double &pval) const
{
double d[];
if(ArrayResize(d,m_origins)!=m_origins)
return false;
for(int o=0; o<m_origins; o++)
d[o]=m_omse[o*REC_NCOL]-m_omse[o*REC_NCOL+col];
return RecDieboldMariano(d,m_origins,lag,stat,pval);
}
//+------------------------------------------------------------------+
//| Average shrinkage intensity across refits. |
//+------------------------------------------------------------------+
double CEvidence::MeanLambda(void) const
{
return (m_refits>0 ? m_lambda_sum/(double)m_refits : 0.0);
}
//+------------------------------------------------------------------+
//| How often each scheme moved farther from the outcome, in squared |
//| error and in its own metric, and how often it needed a ridge. |
//+------------------------------------------------------------------+
void CEvidence::ReportGuarantee(void) const
{
Print("origins where reconciliation moved farther from the outcome");
PrintFormat("%-14s %17s %17s %9s %9s","scheme","squared error",
"own metric","ridge W","ridge A");
for(int c=1; c<REC_NCOL; c++)
{
int rise=0;
for(int o=0; o<m_origins; o++)
if(m_omse[o*REC_NCOL+c]>m_omse[o*REC_NCOL]*(1.0+1.0e-9))
rise++;
PrintFormat("%-14s %17s %17s %9d %9d",RecWeightName(g_scheme[c-1]),
StringFormat("%d of %d",rise,m_origins),
StringFormat("%d of %d",m_rise_w[c],m_origins),
m_ridge_w[c],m_ridge_a[c]);
}
const double base=(m_cnt[m_nlev]>0 ? m_sse[m_nlev]/(double)m_cnt[m_nlev] : 0.0);
const double truth=(m_origins>0 ? m_truth_sq/(double)m_origins : 0.0);
PrintFormat("realised targets off the coherent subspace: rms %.4f bp, %.4f%% of base MSE",
MathSqrt(truth)*1.0e4,(base>0.0 ? 100.0*truth/base : 0.0));
}
//+------------------------------------------------------------------+
//| CIncoherenceStudy - what the reconciliation residue is worth. |
//| |
//| Reconciliation reports one number per origin that the method |
//| itself has no use for: how far the base forecasts sat from |
//| coherence before they were projected. The literature discards |
//| it, since once the reconciled values exist the disagreement has |
//| served its purpose. |
//| |
//| This class asks whether it predicts anything. Origins are |
//| sorted by that residue and split into equal buckets, and inside |
//| each bucket the base and reconciled forecasts are scored |
//| against each other. If the residue is inert, the two columns |
//| keep the same relationship in every bucket. If it is not, the |
//| relationship moves with it, and the direction of the movement |
//| says what the residue is measuring. |
//| |
//| Both squared error and directional hit rate are reported, |
//| because they answer different questions and can disagree. A |
//| spread in squared error alone would be consistent with nothing |
//| more interesting than large forecasts sitting alongside large |
//| moves; a spread in hit rate cannot be explained that way. |
//| |
//| The buckets are whole-sample quantiles, which no trading rule |
//| could have known in advance. That is deliberate and it is why |
//| this is a diagnostic rather than a signal: the question here is |
//| what the residue is, not what could have been done with it. |
//+------------------------------------------------------------------+
class CIncoherenceStudy
{
private:
int m_cap;
int m_count;
int m_node; // the single node reported separately
double m_incoh[]; // residue, root mean square across nodes
double m_sse_b[]; // pooled squared error, base
double m_sse_r[]; // pooled squared error, reconciled
double m_hit_b[]; // pooled sign agreements, base
double m_hit_r[]; // pooled sign agreements, reconciled
double m_signed[]; // nodes with a defined sign this origin
double m_nse_b[]; // single node squared error, base
double m_nse_r[];
double m_nhit_b[]; // single node sign agreement, base
double m_nhit_r[];
public:
CIncoherenceStudy(void);
bool Init(const int capacity,const int node);
bool Add(const vector &y,const vector &base,const vector &rec,
const int n);
int Count(void) const { return m_count; }
void Report(CHierarchy &h,const int buckets);
};
//+------------------------------------------------------------------+
//| Construct empty. |
//+------------------------------------------------------------------+
CIncoherenceStudy::CIncoherenceStudy(void)
{
m_cap =0;
m_count=0;
m_node =0;
}
//+------------------------------------------------------------------+
//| Size the per-origin stores. |
//+------------------------------------------------------------------+
bool CIncoherenceStudy::Init(const int capacity,const int node)
{
if(capacity<8 || node<0)
return false;
m_cap =capacity;
m_count=0;
m_node =node;
if(ArrayResize(m_incoh,capacity)!=capacity)
return false;
ArrayResize(m_sse_b,capacity);
ArrayResize(m_sse_r,capacity);
ArrayResize(m_hit_b,capacity);
ArrayResize(m_hit_r,capacity);
ArrayResize(m_signed,capacity);
ArrayResize(m_nse_b,capacity);
ArrayResize(m_nse_r,capacity);
ArrayResize(m_nhit_b,capacity);
ArrayResize(m_nhit_r,capacity);
return true;
}
//+------------------------------------------------------------------+
//| Record one origin: the residue, and how both columns scored. |
//+------------------------------------------------------------------+
bool CIncoherenceStudy::Add(const vector &y,const vector &base,const vector &rec,
const int n)
{
if(m_count>=m_cap || n<1)
return false;
double gap=0.0, sb=0.0, sr=0.0, hb=0.0, hr=0.0, sg=0.0;
for(int i=0; i<n; i++)
{
const double yi=y[(ulong)i];
const double bi=base[(ulong)i];
const double ri=rec[(ulong)i];
const double d =bi-ri;
gap+=d*d;
sb +=(yi-bi)*(yi-bi);
sr +=(yi-ri)*(yi-ri);
//--- a realised move of exactly zero has no sign to agree with.
if(yi==0.0)
continue;
sg+=1.0;
if((yi>0.0)==(bi>0.0))
hb+=1.0;
if((yi>0.0)==(ri>0.0))
hr+=1.0;
}
m_incoh[m_count] =MathSqrt(gap/(double)n);
m_sse_b[m_count] =sb;
m_sse_r[m_count] =sr;
m_hit_b[m_count] =hb;
m_hit_r[m_count] =hr;
m_signed[m_count]=sg;
const ulong k=(ulong)m_node;
const double yk=y[k], bk=base[k], rk=rec[k];
m_nse_b[m_count] =(yk-bk)*(yk-bk);
m_nse_r[m_count] =(yk-rk)*(yk-rk);
m_nhit_b[m_count]=(yk!=0.0 && ((yk>0.0)==(bk>0.0))) ? 1.0 : 0.0;
m_nhit_r[m_count]=(yk!=0.0 && ((yk>0.0)==(rk>0.0))) ? 1.0 : 0.0;
m_count++;
return true;
}
//+------------------------------------------------------------------+
//| Split the origins into equal buckets of residue and report. |
//| |
//| Bucket edges come from a sorted copy of the residues rather |
//| than from a sort of the origins themselves, which keeps the |
//| per-origin records where they are and needs no index sort. |
//+------------------------------------------------------------------+
void CIncoherenceStudy::Report(CHierarchy &h,const int buckets)
{
const int nb=MathMax(2,MathMin(10,buckets));
if(m_count<nb*8)
{
PrintFormat("incoherence diagnostic: only %d origins, need %d",
m_count,nb*8);
return;
}
double sorted[];
ArrayResize(sorted,m_count);
ArrayCopy(sorted,m_incoh,0,0,m_count);
ArraySort(sorted);
double edge[];
ArrayResize(edge,nb-1);
for(int k=0; k<nb-1; k++)
edge[k]=sorted[(int)((double)(k+1)*m_count/nb)-1];
double cnt[], inc[], sb[], sr[], hb[], hr[], sg[];
double nsb[], nsr[], nhb[], nhr[];
ArrayResize(cnt,nb);
ArrayResize(inc,nb);
ArrayResize(sb,nb);
ArrayResize(sr,nb);
ArrayResize(hb,nb);
ArrayResize(hr,nb);
ArrayResize(sg,nb);
ArrayResize(nsb,nb);
ArrayResize(nsr,nb);
ArrayResize(nhb,nb);
ArrayResize(nhr,nb);
ArrayInitialize(cnt,0.0);
ArrayInitialize(inc,0.0);
ArrayInitialize(sb,0.0);
ArrayInitialize(sr,0.0);
ArrayInitialize(hb,0.0);
ArrayInitialize(hr,0.0);
ArrayInitialize(sg,0.0);
ArrayInitialize(nsb,0.0);
ArrayInitialize(nsr,0.0);
ArrayInitialize(nhb,0.0);
ArrayInitialize(nhr,0.0);
for(int i=0; i<m_count; i++)
{
int k=nb-1;
for(int e=0; e<nb-1; e++)
{
if(m_incoh[i]<=edge[e])
{
k=e;
break;
}
}
cnt[k]+=1.0;
inc[k]+=m_incoh[i];
sb[k] +=m_sse_b[i];
sr[k] +=m_sse_r[i];
hb[k] +=m_hit_b[i];
hr[k] +=m_hit_r[i];
sg[k] +=m_signed[i];
nsb[k]+=m_nse_b[i];
nsr[k]+=m_nse_r[i];
nhb[k]+=m_nhit_b[i];
nhr[k]+=m_nhit_r[i];
}
PrintFormat("incoherence diagnostic, %d buckets, scheme %s, %d origins",
nb,RecWeightName(InpBucketScheme),m_count);
Print("all nodes pooled");
PrintFormat("%-8s %8s %10s %10s %10s %9s %9s %9s","bucket","origins",
"incoh bp","base RMSE","rec RMSE","MSE chg","base hit","rec hit");
for(int k=0; k<nb; k++)
{
if(cnt[k]<1.0)
continue;
const double nodes=sg[k];
PrintFormat("%-8d %8d %10.3f %10.3f %10.3f %8.2f%% %8.2f%% %8.2f%%",
k+1,(int)cnt[k],inc[k]/cnt[k]*1.0e4,
MathSqrt(sb[k]/nodes)*1.0e4,MathSqrt(sr[k]/nodes)*1.0e4,
(sb[k]>0.0 ? 100.0*(sr[k]-sb[k])/sb[k] : 0.0),
(nodes>0.0 ? 100.0*hb[k]/nodes : 0.0),
(nodes>0.0 ? 100.0*hr[k]/nodes : 0.0));
}
PrintFormat("single node: %s",h.Label(m_node));
PrintFormat("%-8s %8s %10s %10s %10s %9s %9s %9s","bucket","origins",
"incoh bp","base RMSE","rec RMSE","MSE chg","base hit","rec hit");
for(int k=0; k<nb; k++)
{
if(cnt[k]<1.0)
continue;
PrintFormat("%-8d %8d %10.3f %10.3f %10.3f %8.2f%% %8.2f%% %8.2f%%",
k+1,(int)cnt[k],inc[k]/cnt[k]*1.0e4,
MathSqrt(nsb[k]/cnt[k])*1.0e4,MathSqrt(nsr[k]/cnt[k])*1.0e4,
(nsb[k]>0.0 ? 100.0*(nsr[k]-nsb[k])/nsb[k] : 0.0),
100.0*nhb[k]/cnt[k],100.0*nhr[k]/cnt[k]);
}
}
//+------------------------------------------------------------------+
//| Column of 'pred' holding the scheme the diagnostic compares. |
//+------------------------------------------------------------------+
int BucketColumn(void)
{
for(int s=0; s<5; s++)
{
if(g_scheme[s]==InpBucketScheme)
return s+1;
}
return 1;
}
//+------------------------------------------------------------------+
//| Split a comma separated list into a string array. |
//+------------------------------------------------------------------+
int SplitList(const string csv,string &out[])
{
string parts[];
const int n=StringSplit(csv,',',parts);
int w=0;
ArrayResize(out,n);
for(int i=0; i<n; i++)
{
string t=parts[i];
StringTrimLeft(t);
StringTrimRight(t);
if(StringLen(t)>0)
out[w++]=t;
}
ArrayResize(out,w);
return w;
}
//+------------------------------------------------------------------+
//| Same, parsed as integers. |
//+------------------------------------------------------------------+
int SplitInts(const string csv,int &out[])
{
string parts[];
const int n=SplitList(csv,parts);
ArrayResize(out,n);
for(int i=0; i<n; i++)
out[i]=(int)StringToInteger(parts[i]);
return n;
}
//+------------------------------------------------------------------+
//| Wait for history. A script may block like this; the library |
//| never does, because the same code has to serve an indicator. |
//+------------------------------------------------------------------+
bool WaitForHistory(const string &symbols[],const ENUM_TIMEFRAMES tf,const int bars)
{
for(int s=0; s<ArraySize(symbols); s++)
{
if(!SymbolSelect(symbols[s],true))
{
PrintFormat("cannot select %s",symbols[s]);
return false;
}
int waited=0;
while(Bars(symbols[s],tf)<bars && waited<15000)
{
datetime probe[];
CopyTime(symbols[s],tf,0,bars,probe);
Sleep(200);
waited+=200;
}
if(Bars(symbols[s],tf)<bars/2)
{
PrintFormat("%s has only %d bars on %s",symbols[s],
Bars(symbols[s],tf),EnumToString(tf));
return false;
}
}
return true;
}
//+------------------------------------------------------------------+
//| Where the bars came from: the server and the span they cover. |
//+------------------------------------------------------------------+
void PrintData(const datetime &times[])
{
const int k=ArraySize(times);
if(k<1)
return;
PrintFormat("%s, bar closes from %s to %s",AccountInfoString(ACCOUNT_SERVER),
TimeToString(times[0]),TimeToString(times[k-1]));
}
//+------------------------------------------------------------------+
//| Log returns of one column of a close matrix. |
//+------------------------------------------------------------------+
bool LogReturns(const matrix &closes,const int col,double &r[])
{
const int rows=(int)closes.Rows();
if(rows<3)
return false;
ArrayResize(r,rows-1);
for(int i=1; i<rows; i++)
{
const double a=closes[(ulong)(i-1)][(ulong)col];
const double b=closes[(ulong)i][(ulong)col];
if(a<=0.0 || b<=0.0)
return false;
r[i-1]=MathLog(b/a);
}
return true;
}
//+------------------------------------------------------------------+
//| Print the finished scoreboard for one hierarchy. |
//+------------------------------------------------------------------+
void Report(CEvidence &ev,CHierarchy &h)
{
const int nlev=h.LevelCount();
string head=StringFormat("%-8s %10s","level","base");
for(int s=0; s<5; s++)
head+=StringFormat(" %12s",RecWeightName(g_scheme[s]));
//--- basis points rather than points, so that a temporal hierarchy
//--- on one symbol and a currency hierarchy over twenty-one of them
//--- can be read off the same scale.
Print("RMSE by level, basis points of log return");
Print(head);
for(int l=0; l<=nlev; l++)
{
const string name=(l==nlev ? "ALL" : h.LevelName(l));
string row=StringFormat("%-8s %10.3f",name,ev.Rmse(0,l)*1.0e4);
for(int c=1; c<REC_NCOL; c++)
row+=StringFormat(" %12.3f",ev.Rmse(c,l)*1.0e4);
Print(row);
}
//--- the same numbers as ratios, which is the form the guarantee is
//--- stated in and the form a per-level regression shows up in.
Print("change in mean squared error against base, percent (negative is better)");
Print(head);
for(int l=0; l<=nlev; l++)
{
const string name=(l==nlev ? "ALL" : h.LevelName(l));
string row=StringFormat("%-8s %10s",name,"-");
for(int c=1; c<REC_NCOL; c++)
row+=StringFormat(" %11.2f%%",ev.MsePct(c,l));
Print(row);
}
Print("Diebold-Mariano against base, on mean squared error per origin");
PrintFormat("%-14s %10s %10s","scheme","stat","p");
for(int c=1; c<REC_NCOL; c++)
{
double stat=0.0, pval=1.0;
if(ev.Dm(c,InpDmLag,stat,pval))
PrintFormat("%-14s %10.3f %10.4f",RecWeightName(g_scheme[c-1]),stat,pval);
else
PrintFormat("%-14s %10s %10s",RecWeightName(g_scheme[c-1]),"n/a","n/a");
}
PrintFormat("origins scored=%d refits=%d mean shrinkage lambda=%.4f",
ev.Origins(),ev.Refits(),ev.MeanLambda());
ev.ReportGuarantee();
}
//+------------------------------------------------------------------+
//| Walk forward over a temporal hierarchy. |
//| |
//| One origin per top period. At each of them every aggregation |
//| level is refitted on its own past and forecast forward far |
//| enough to cover the coming period, the realised aggregates are |
//| formed once that period has happened, and the resulting |
//| residual vector is pushed into the error store only after it |
//| has been scored. That ordering is the whole no-lookahead |
//| argument: the weights used at an origin are built from |
//| residuals that were all observable strictly before it. |
//+------------------------------------------------------------------+
void RunTemporal(void)
{
int factors[];
if(SplitInts(InpFactors,factors)<2)
return;
CHierarchy h;
if(!h.BuildTemporal(factors,InpTopPeriod))
return;
string symbols[1];
symbols[0]=InpSymbol;
if(!WaitForHistory(symbols,InpBaseTF,InpBars))
return;
matrix closes;
datetime times[];
if(!CAlignedBars::Load(symbols,InpBaseTF,InpBars,closes,times))
return;
double r[];
if(!LogReturns(closes,0,r))
return;
const int nret=ArraySize(r);
const int top=InpTopPeriod;
const int blocks=nret/top;
const int base0=nret-blocks*top;
const int n=h.N();
const int nlev=h.LevelCount();
if(blocks<InpMinObs+24)
{
PrintFormat("only %d top periods; need at least %d",blocks,InpMinObs+24);
return;
}
//--- aggregate every level once over the whole history, then read
//--- slices of it at each origin rather than rebuilding per origin.
//--- The blocks are anchored at the end of the sample, so a partial
//--- leading period is dropped rather than half-filled.
int offs[], per[];
ArrayResize(offs,nlev);
ArrayResize(per,nlev);
int total=0;
for(int f=0; f<nlev; f++)
{
per[f]=top/factors[f];
offs[f]=total;
total+=blocks*per[f];
}
double agg[];
ArrayResize(agg,total);
for(int f=0; f<nlev; f++)
{
const int k=factors[f];
const int cnt=blocks*per[f];
for(int a=0; a<cnt; a++)
{
double s=0.0;
for(int c=0; c<k; c++)
s+=r[base0+a*k+c];
agg[offs[f]+a]=s;
}
}
//--- row bookkeeping: which level a row belongs to, and where its
//--- level block starts.
int level[], rowbase[];
ArrayResize(level,n);
ArrayResize(rowbase,nlev);
for(int i=0; i<n; i++)
{
level[i]=h.Level(i);
if(h.Step(i)==1)
rowbase[h.Level(i)]=i;
}
matrix S;
h.GetSummingMatrix(S);
CBaseForecaster fc;
if(!fc.Init(InpModel,InpFitWindow))
return;
CCovarianceEstimator est;
if(!est.Init(n,InpErrWindow))
return;
CReconciler rec[5];
for(int s=0; s<5; s++)
if(!rec[s].SetSummingMatrix(S))
return;
CEvidence ev;
if(!ev.Init(nlev,blocks+1))
return;
//--- the single node the diagnostic reports on its own is the
//--- widest aggregation, which is the last row by construction.
CIncoherenceStudy st;
if(!st.Init(blocks+1,n-1))
return;
const int bcol=BucketColumn();
PrintFormat("=== reconciliation evidence: temporal, %s %s, top period %d ===",
InpSymbol,EnumToString(InpBaseTF),top);
PrintFormat("n=%d m=%d levels=%d | base=%s(%d) | residual window=%d min obs=%d refit every %d",
n,h.M(),nlev,RecBaseModelName(InpModel),InpFitWindow,
InpErrWindow,InpMinObs,InpRefitEvery);
PrintFormat("aligned bars=%d, returns=%d, complete top periods=%d",
(int)closes.Rows(),nret,blocks);
PrintData(times);
//--- twelve periods is the smallest history that leaves the coarsest
//--- level anything to fit; everything narrower has more by then.
const int warm=12;
int since_refit=InpRefitEvery;
double tail[], path[];
for(int b=warm; b<blocks; b++)
{
vector yhat=vector::Zeros((ulong)n);
vector y =vector::Zeros((ulong)n);
bool ok=true;
//--- one fit per level, forecast forward across the coming period,
//--- and read the realised aggregates for the same period.
for(int f=0; f<nlev && ok; f++)
{
const int cnt=b*per[f];
const int take=MathMin(fc.Window(),cnt);
if(take<4)
{
ok=false;
break;
}
ArrayResize(tail,take);
ArrayCopy(tail,agg,0,offs[f]+cnt-take,take);
if(!fc.ForecastPath(tail,take,per[f],path))
{
ok=false;
break;
}
for(int j=0; j<per[f]; j++)
{
yhat[(ulong)(rowbase[f]+j)]=path[j];
y[(ulong)(rowbase[f]+j)] =agg[offs[f]+b*per[f]+j];
}
}
if(!ok)
continue;
if(est.Ready(InpMinObs))
{
//--- refitting the weights is the expensive half, and the error
//--- regime moves far more slowly than the forecasts do, so the
//--- schedule is a caller's choice rather than every origin.
if(since_refit>=InpRefitEvery)
{
bool fitted=true;
for(int s=0; s<5 && fitted; s++)
fitted=rec[s].Prepare(g_scheme[s],est);
if(fitted)
{
ev.NoteFit(rec);
since_refit=0;
}
}
since_refit++;
//--- every column is scored on the same origin or none of them is,
//--- otherwise the loss differentials would not be paired.
if(rec[4].IsReady())
{
matrix pred=matrix::Zeros(REC_NCOL,(ulong)n);
for(int i=0; i<n; i++)
pred[0][(ulong)i]=yhat[(ulong)i];
bool good=true;
for(int s=0; s<5 && good; s++)
{
vector yt;
good=rec[s].Reconcile(yhat,yt);
if(good)
for(int i=0; i<n; i++)
pred[(ulong)(s+1)][(ulong)i]=yt[(ulong)i];
}
if(good)
{
ev.Origin(y,pred,level,n);
ev.NoteGuarantee(rec,y,pred,n);
vector bs=vector::Zeros((ulong)n), rc=vector::Zeros((ulong)n);
for(int i=0; i<n; i++)
{
bs[(ulong)i]=pred[0][(ulong)i];
rc[(ulong)i]=pred[(ulong)bcol][(ulong)i];
}
st.Add(y,bs,rc,n);
}
}
}
//--- the residual enters the store only now that it has been
//--- scored, which is what keeps the weights strictly historical.
vector e=vector::Zeros((ulong)n);
for(int i=0; i<n; i++)
e[(ulong)i]=y[(ulong)i]-yhat[(ulong)i];
est.Push(e);
}
if(ev.Origins()<8)
{
Print("too few scored origins to report");
return;
}
Report(ev,h);
st.Report(h,InpBuckets);
}
//+------------------------------------------------------------------+
//| Walk forward over a currency hierarchy. |
//| |
//| One origin per bar, one step ahead, each pair fitted only on |
//| its own returns. The constraint being imposed is arithmetic |
//| rather than statistical, so nothing here has to be true about |
//| the market for the projection to be legitimate; what is being |
//| measured is only whether imposing it helps. |
//+------------------------------------------------------------------+
void RunCurrency(void)
{
string pairs[];
const int ns=SplitList(InpPairs,pairs);
if(ns<3)
return;
CHierarchy h;
if(!h.BuildCurrency(pairs,InpNumeraire))
return;
if(!h.IsConnected(pairs,InpNumeraire))
return;
if(!WaitForHistory(pairs,InpPairTF,InpBars))
return;
matrix closes;
datetime times[];
if(!CAlignedBars::Load(pairs,InpPairTF,InpBars,closes,times))
return;
const int rows=(int)closes.Rows();
const int nret=rows-1;
const int n=h.N();
const int nlev=h.LevelCount();
//--- returns for every pair, held as one matrix so that a slice per
//--- origin is a copy of the tail rather than a recomputation.
matrix ret=matrix::Zeros((ulong)nret,(ulong)n);
double col[];
for(int s=0; s<n; s++)
{
if(!LogReturns(closes,s,col))
return;
for(int i=0; i<nret; i++)
ret[(ulong)i][(ulong)s]=col[i];
}
int level[];
ArrayResize(level,n);
for(int i=0; i<n; i++)
level[i]=h.Level(i);
matrix S;
h.GetSummingMatrix(S);
CBaseForecaster fc;
if(!fc.Init(InpModel,InpFitWindow))
return;
CCovarianceEstimator est;
if(!est.Init(n,InpErrWindow))
return;
CReconciler rec[5];
for(int s=0; s<5; s++)
if(!rec[s].SetSummingMatrix(S))
return;
CEvidence ev;
if(!ev.Init(nlev,nret+1))
return;
//--- with no aggregation to speak of, the single node reported on
//--- its own is simply the last pair in the list.
CIncoherenceStudy st;
if(!st.Init(nret+1,n-1))
return;
const int bcol=BucketColumn();
PrintFormat("=== reconciliation evidence: currency, %d pairs on %s, numeraire %s ===",
n,EnumToString(InpPairTF),InpNumeraire);
PrintFormat("n=%d m=%d levels=%d | base=%s(%d) | residual window=%d min obs=%d refit every %d",
n,h.M(),nlev,RecBaseModelName(InpModel),InpFitWindow,
InpErrWindow,InpMinObs,InpRefitEvery);
PrintFormat("aligned bars=%d, returns=%d",rows,nret);
PrintData(times);
//--- start once a full estimation window exists, so that no pair is
//--- fitted on a shorter history than any other.
const int warm=MathMax(16,InpFitWindow);
int since_refit=InpRefitEvery;
double tail[];
ArrayResize(tail,InpFitWindow);
for(int i=warm; i<nret; i++)
{
vector yhat=vector::Zeros((ulong)n);
vector y =vector::Zeros((ulong)n);
const int take=MathMin(InpFitWindow,i);
ArrayResize(tail,take);
//--- each pair sees only its own returns, which is what leaves the
//--- resulting set of forecasts free to contradict itself.
for(int s=0; s<n; s++)
{
for(int t=0; t<take; t++)
tail[t]=ret[(ulong)(i-take+t)][(ulong)s];
yhat[(ulong)s]=fc.Forecast(tail,take,1);
y[(ulong)s] =ret[(ulong)i][(ulong)s];
}
if(est.Ready(InpMinObs))
{
//--- as above: weights are refitted on a schedule, not per bar.
if(since_refit>=InpRefitEvery)
{
bool fitted=true;
for(int s=0; s<5 && fitted; s++)
fitted=rec[s].Prepare(g_scheme[s],est);
if(fitted)
{
ev.NoteFit(rec);
since_refit=0;
}
}
since_refit++;
if(rec[4].IsReady())
{
matrix pred=matrix::Zeros(REC_NCOL,(ulong)n);
for(int s=0; s<n; s++)
pred[0][(ulong)s]=yhat[(ulong)s];
bool good=true;
for(int s=0; s<5 && good; s++)
{
vector yt;
good=rec[s].Reconcile(yhat,yt);
if(good)
for(int q=0; q<n; q++)
pred[(ulong)(s+1)][(ulong)q]=yt[(ulong)q];
}
if(good)
{
ev.Origin(y,pred,level,n);
ev.NoteGuarantee(rec,y,pred,n);
vector bs=vector::Zeros((ulong)n), rc=vector::Zeros((ulong)n);
for(int q=0; q<n; q++)
{
bs[(ulong)q]=pred[0][(ulong)q];
rc[(ulong)q]=pred[(ulong)bcol][(ulong)q];
}
st.Add(y,bs,rc,n);
}
}
}
//--- scored first, stored second, for the same reason as above.
vector e=vector::Zeros((ulong)n);
for(int s=0; s<n; s++)
e[(ulong)s]=y[(ulong)s]-yhat[(ulong)s];
est.Push(e);
}
if(ev.Origins()<8)
{
Print("too few scored origins to report");
return;
}
Report(ev,h);
st.Report(h,InpBuckets);
}
//+------------------------------------------------------------------+
//| Entry point. |
//+------------------------------------------------------------------+
void OnStart(void)
{
const uint t0=GetTickCount();
if(InpRunBoth || InpHierarchy==REC_HIER_TEMPORAL)
RunTemporal();
if(InpRunBoth || InpHierarchy==REC_HIER_CURRENCY)
RunCurrency();
PrintFormat("=== evidence complete in %.1f s ===",(GetTickCount()-t0)/1000.0);
}
//+------------------------------------------------------------------+