Code Monkey home page Code Monkey logo

Comments (1)

Team2of2 avatar Team2of2 commented on May 17, 2024

The unbiased estimator of a VAR(p) process is YZ'(ZZ')^-1 as described in equation (14.22) on p. 288 where Z is a Kp+1 x 1 matrix defined as Zt = (1,y_t, ..., y_{t-p+1})'.

The code of Zt is given in p. 293. It looks like you are using the variable Zt of the vector autogressive model with exogenous variable VARX(p,q) defined as Zt = (Y_0,...,Y_{T-1}; X_0,..., X_{T-1}; .... x_1,...,x_T)' (see eq. 14.29 on p 297) instead of Zt defined in eq. 14.22 on p.288.

I will create two name spaces: one for VAR and one for VARX such that Zt for VAR is not overrriden by Zt for VARX.

To reproduce the results on p. 291, please use the code of the book.

.quantQ.ts.vec:{[a]
// a -- matrix
i:(count raze a);
j:1;
v:(flip a)[0;];
while[j<i;
v:v,/(flip a)[j;];
j:j+1];
:v;
};

.quantQ.ts.zt:{[y;t;p]
// y -- historical data (list of floating numbers)
// t -- time point (positive integer)
// p -- lags (positive integer) corresponding to the order of the VAR(p) process
:1f,.quantQ.ts.vec[y[;(t-1)-til p]];
};

.quantQ.ts.Z:{[y;p]
// y -- historical data (list of floating numbers)
// p -- lags (positive integer) corresponding to the order of the VAR(p) process
:flip {[y;i;p]
.quantQ.ts.zt[y;i;p]}[y;;p] each p + til (count y[0])-p;
};

.quantQ.ts.Y:{[y;p]
// y -- historical data (list of floating numbers)
// p -- lags (positive integer) corresponding to the order of the VAR(p) process
:y[;p+til (count y[0;])-p];
};

.quantQ.ts.varpest:{[y;p]
// y -- historical data (list of floating numbers) used to estimate the VAR(p) model.
// p -- order of the VAR(p) model
:.quantQ.ts.Y[y;p] mmu (flip .quantQ.ts.Z[y;p]) mmu
inv[.quantQ.ts.Z[y;p] mmu flip .quantQ.ts.Z[y;p]];
};

.quantQ.ts.covarianceResidual:{[y;p]
// y -- historical data (list of floating numbers)
// p -- order of the VAR(p) process
T:(count y[0])-p;
K:count y;
coef:1f%((T-Kp)-1f);
:coef
(.quantQ.ts.Y[y;p] mmu flip .quantQ.ts.Y[y;p]) -
(.quantQ.ts.Y[y;p] mmu flip .quantQ.ts.Z[y;p]) mmu
inv[.quantQ.ts.Z[y;p] mmu flip .quantQ.ts.Z[y;p]] mmu
(.quantQ.ts.Z[y;p] mmu flip .quantQ.ts.Y[y;p]);
};

.quantQ.ts.eye:{[k]
// k -- rank of matrix
:`float$(til k)=/:til k;
};

.quantQ.ts.beta:{[y;p]
// y -- historical data (list of floating numbers)
// p -- order of the VAR(p) process
:(1f%(count y[0])-p) * (.quantQ.ts.Y[y;p] mmu
(.quantQ.ts.eye[(count y[0])-p] - ((flip .quantQ.ts.Z[y;p]) mmu
inv[.quantQ.ts.Z[y;p] mmu flip .quantQ.ts.Z[y;p]] mmu
.quantQ.ts.Z[y;p]))) mmu flip .quantQ.ts.Y[y;p];
};

.quantQ.ts.varOrder:{[y;m]
// y -- historical data (list of floating numbers
// m -- maximum order of the VAR process
T:count y[0];
K:count y;
i:m;
k:v1:v2:();
while[i>0;
lr:T*(log .quantQ.mat.det[.quantQ.ts.beta[y;i-1]])-(log .quantQ.mat.det[.quantQ.ts.beta[y;i]]);
k:k,(m-i);
v1:v1,lr;
v2:v2,.quantQ.ts.pValueChi2[lr;K*K];
i:i-1];
:([p:k]lr:v1;pvalue:v2);
};

.quantQ.ts.pValueChi2:{[chi;nu]
// chi -- instance of distribution (floating)
// nu -- degrees of freedom of the Chi2 distribution
u:chi%2f;
v:nu%2f;
p:(u-v)+1f;
pi:3.141592654f;
term1:exp[v-u]%(psqrt[2pi]);
term2:(u % v) xexp v;
term3:1f-((v-1f)%((pp)+2fu));
term4:(12f*(v xexp 1.5f))%((12fv)+1);
:term1
term2term3term4;
};

from quantq.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.