Hi Sandu,here is my first code that uses Memorization
garch[s_] := Module[{k = s, R, \[Sigma]sqr, ll, sll, u},
k = Flatten@k;
R[t_] := Flatten[k][[t]];
\[Sigma]sqr[1, w_, a_, b_] := Variance[Flatten[k]];
\[Sigma]sqr[t_, w_?NumberQ, a_?NumberQ,
b_?NumberQ] := \[Sigma]sqr[t, w, a, b] =
w + a R[t - 1]^2 + b \[Sigma]sqr[t - 1, w, a, b];
ll[t_, w_?NumberQ, a_?NumberQ, b_?NumberQ] :=
ll[t, w, a, b] =
If[\[Sigma]sqr[t, w, a, b] >
0, -.5 (Log[2 \[Pi]] + Log[\[Sigma]sqr[t, w, a, b]] +
R[t]^2/\[Sigma]sqr[t, w, a, b]), -1000];
sll = Sum[ll[t, w, a, b], {t, 2, k // Length}];
u = NMaximize[{sll, a > 0, b > 0, w > 0, a + b < 1}, {w, a, b},
WorkingPrecision -> 12, MaxIterations -> 100];
Print[u];
Remove[k];
Remove[R];
Remove[\[Sigma]sqr];
Remove[ll];
Remove[sll];
ClearAll[k, R, \[Sigma]sqr, ll, sll, u];]
I used If
function to avoid numerical obstacles of imaginary numbers (log(negative value) may occur) as in Christoffersen book.
This is Garch with normal innovation but you can always change log likelihood. Unfortunately this is still kinda slow so after some time i found a new way, I wont post the code here, instead I'll attach a notebook with code.It is pretty fast and If you want your function to be entirely in Mathematica it will do.The problem for me was (as will be for you) the mathematica optimizer, it didn't always converge and it needs tuning( WorkingPrecision
and MaxIterations
) in order to give satisfying result (I use default algorithm Neldear Mead). And finally If you give me your mail I'll send you my package with installation instructions, it has functions for univariate and multivariate garch estimation and it took me a lot of time to develop it.Please contact me on this post if you have any questions.
Attachments: