Go to the documentation of this file.00001
00002 #define WANT_STREAM
00003 #define WANT_MATH
00004 #define WANT_FSTREAM
00005
00006 #include "newmatap.h"
00007 #include "newmatio.h"
00008 #include "newmatnl.h"
00009
00010 #ifdef use_namespace
00011 using namespace RBD_LIBRARIES;
00012 #endif
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 inline Real square(Real x) { return x*x; }
00028
00029
00030
00031 class GARCH11_LL : public LL_D_FI
00032 {
00033 ColumnVector Y;
00034 ColumnVector X;
00035 ColumnVector D;
00036 SymmetricMatrix D2;
00037 int n;
00038 Real beta, alpha0, alpha1, beta1;
00039
00040
00041 public:
00042
00043 GARCH11_LL(const ColumnVector& y, const ColumnVector& x)
00044 : Y(y), X(x), n(y.Nrows()) {}
00045
00046
00047 void Set(const ColumnVector& p)
00048 {
00049 para = p;
00050 beta = para(1); alpha0 = para(2);
00051 alpha1 = para(3); beta1 = para(4);
00052 }
00053
00054 bool IsValid();
00055 Real LogLikelihood();
00056 ReturnMatrix Derivatives();
00057 ReturnMatrix FI();
00058 };
00059
00060 bool GARCH11_LL::IsValid()
00061 { return alpha0>0 && alpha1>0 && beta1>0 && (alpha1+beta1)<1.0; }
00062
00063 Real GARCH11_LL::LogLikelihood()
00064 {
00065
00066
00067
00068
00069
00070
00071 ColumnVector H(n);
00072 ColumnVector U = Y - X * beta;
00073 ColumnVector LH(n);
00074
00075 LH(1)=0;
00076 Matrix Hderiv(n,4);
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 Real denom = (1-alpha1-beta1);
00087 H(1) = alpha0/denom;
00088 Hderiv(1,1) = 0;
00089 Hderiv(1,2) = 1.0 / denom;
00090 Hderiv(1,3) = alpha0 / square(denom);
00091 Hderiv(1,4) = Hderiv(1,3);
00092 Real LL = 0.0;
00093 Real sum1 = 0;
00094 Real sum2 = 0;
00095 for (int i=2; i<=n; i++)
00096 {
00097 Real u1 = U(i-1); Real h1 = H(i-1);
00098 Real h = alpha0 + alpha1*square(u1) + beta1*h1;
00099 H(i) = h; Real u = U(i);
00100 LL += log(h) + square(u) / h;
00101
00102
00103 Hderiv(i,1) = -2*u1*alpha1*X(i-1) + beta1*Hderiv(i-1,1);
00104 Hderiv(i,2) = 1 + beta1*Hderiv(i-1,2);
00105 Hderiv(i,3) = square(u1) + beta1*Hderiv(i-1,3);
00106 Hderiv(i,4) = h1 + beta1*Hderiv(i-1,4);
00107 LH(i) = -0.5 * (1/h - square(u/h));
00108 sum1 += u * X(i)/ h;
00109 sum2 += square(X(i)) / h;
00110 }
00111 D = Hderiv.t()*LH;
00112 D(1) += sum1;
00113
00114
00115
00116 if (wg)
00117 {
00118 Hderiv.Row(1) = 0.0;
00119 Hderiv = H.AsDiagonal().i() * Hderiv;
00120 D2 << Hderiv.t() * Hderiv; D2 = D2 / 2.0;
00121 D2(1,1) += sum2;
00122
00123
00124
00125
00126 }
00127 return -0.5 * LL;
00128 }
00129
00130 ReturnMatrix GARCH11_LL::Derivatives()
00131 { return D; }
00132
00133 ReturnMatrix GARCH11_LL::FI()
00134 {
00135 if (!wg) cout << endl << "unexpected call of FI" << endl;
00136 return D2;
00137 }
00138
00139
00140
00141 int main()
00142 {
00143
00144 ifstream fin("garch.dat");
00145 if (!fin) { cout << "cannot find garch.dat\n"; exit(1); }
00146 int n; fin >> n;
00147
00148 ColumnVector Y(n), X(n);
00149 int i;
00150 for (i=1; i<=n; i++) fin >> Y(i) >> X(i);
00151 cout << "Read " << n << " data points - begin fit\n\n";
00152
00153 ColumnVector H(n);
00154 GARCH11_LL garch11(Y,X);
00155 MLE_D_FI mle_d_fi(garch11,100,0.0001);
00156 ColumnVector Para(4);
00157 Para << 0.0 << 0.1 << 0.1 << 0.1;
00158
00159 mle_d_fi.Fit(Para);
00160 ColumnVector SE;
00161 mle_d_fi.GetStandardErrors(SE);
00162 cout << "\n\n";
00163 cout << "estimates and standard errors\n";
00164 cout << setw(15) << setprecision(5) << (Para | SE) << endl << endl;
00165 SymmetricMatrix Corr;
00166 mle_d_fi.GetCorrelations(Corr);
00167 cout << "correlation matrix\n";
00168 cout << setw(10) << setprecision(2) << Corr << endl << endl;
00169 cout << "inverse of correlation matrix\n";
00170 cout << setw(10) << setprecision(2) << Corr.i() << endl << endl;
00171 return 0;
00172 }
00173
00174
00175