-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmgarch_max.h
161 lines (152 loc) · 4.35 KB
/
mgarch_max.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef _mgarchop
#define _mgarchop
#include <mgarch2.h>
using namespace std;
extern void (*aktfgv)(double *,double *,int *,int *,void *,Matrix&);
void log_likelihood_var (double* x, double *f, int *nparm, int* i, void* parms, Matrix& data)
{
int N; // x consists of 4xN elements
N = data.nCol();
Matrix Y(2*N+2,1);
for (int i = 1; i <= N; i++)
{
Y(i,1) = x[i]; //lambda
Y(N+i,1) = x[N+i]; //beta
}
Y(2*N+1,1) = x[2*N+1];//kappa
Y(2*N+2,1) = x[2*N+2];//zeta
mgarch obj1;
*f = - obj1.log_likelihood_var (Y, data);
//cout << "log-likelihood-var: " << *f << endl;
}
void log_likelihood_cor (double* x, double *f, int *nparm, int* i, void* parm0, Matrix& data)
{
int N;
N = data.nCol();
Matrix coeff(2*N+2,1);
double *parms = (double *) parm0;
for (int i = 1; i <= 2*N; i++)
{
coeff(i,1) = parms[i];//lambda and beta
}
coeff(2*N+1,1) = parms[2*N+1];//kappa
coeff(2*N+2,1) = parms[2*N+2];//zeta
mgarch obj2;
*f = - obj2.log_likelihood_cor (x[1], x[2], data, coeff);
cout << "log-likelihood-cor: " << *f << endl;
}
Matrix log_likelihood_cor_sup (Matrix& coeff, Matrix& data)//maximize the correlation part of the log-likelihood
{
FILE *out;
VEKTOR_n mmin;
VEKTOR_n mmax;
int nparm;
int m;
int nsampl;
int nsel;
int nsig;
TOMB_nx21 x0;
int nc;
int maxnc;
double f0[21];
int fe =1;
int N;
N = data.nCol();
nparm = 2;
double* parms = new double[2*N+3];
for (int i = 1; i <= 2*N+2; i++)
{
parms[i] = coeff(i,1);
}
if((x0 = (TOMB_nx21)malloc(sizeof(double)*(nparm+1)*21))==NULL) exit(3);
if((mmin=(double*)malloc( sizeof(double)*(nparm+1) ))==NULL) exit(3);
if((mmax=(double*)malloc( sizeof(double)*(nparm+1) ))==NULL) exit(3);
for (int i = 1; i <= nparm; i++)
{
mmax[i] = 0.5;
mmin[i] = 0;
cout << mmin[i] << "," << mmax[i] << endl;
}
m = 1;
nsampl = 100;
nsel=20;
nsig=6;
maxnc = 1;
aktfgv = log_likelihood_cor;
if ((out = fopen("dcc.txt", "w+")) == NULL) {
fprintf(out, "Cannot open output file.\n");
exit(1);
};
global(mmin,mmax,&nparm,&m,parms,data,&nsampl,&nsel,out,&nsig,x0,&nc,f0,&fe,maxnc);
Matrix res(2,1);
res(1,1) = (*x0)[1][1];//a
res(2,1) = (*x0)[2][1];//b
fclose(out);
free(mmin);
free(mmax);
free(x0);
return res;
}
Matrix log_likelihood_var_sup (Matrix& data) //maximize the garch part of the log-likelihood
{
FILE *out;
VEKTOR_n mmin;
VEKTOR_n mmax;
int nparm;
int m;
int nsampl;
int nsel;
int nsig;
TOMB_nx21 x0;
int nc;
int maxnc;
double f0[21];
int fe =1, N = 0;
N = data.nCol();
nparm = 2*N+2;
if((x0 = (TOMB_nx21)malloc(sizeof(double)*(nparm+1)*21))==NULL) exit(3);
if((mmin=(double*)malloc( sizeof(double)*(nparm+1) ))==NULL) exit(3);
if((mmax=(double*)malloc( sizeof(double)*(nparm+1) ))==NULL) exit(3);
for (int i = 1; i <= N; i++)
{
mmax[i] = 10;
mmin[i] = -10;
cout << mmin[i] << "," << mmax[i] << endl;
}
for (int i = N+1; i <= 2*N; i++)
{
mmax[i] = 1;
mmin[i] = -1;
cout << mmin[i] << "," << mmax[i] << endl;
}
mmin[2*N+1] = 0;
mmax[2*N+1] = 0.5;
cout << mmin[2*N+1] << "," << mmax[2*N+1] << endl;
mmin[2*N+2] = 0;
mmax[2*N+2] = 0.5;
cout << mmin[2*N+2] << "," << mmax[2*N+2] << endl;
m = 1;
nsampl = 1000;
nsel=20;
nsig=6;
maxnc = 1;
aktfgv = log_likelihood_var;
if ((out = fopen("garch.txt", "w+")) == NULL) {
fprintf(out, "Cannot open output file.\n");
exit(1);
};
double* parms;
global(mmin,mmax,&nparm,&m,parms,data,&nsampl,&nsel,out,&nsig,x0,&nc,f0,&fe,maxnc);
Matrix res(2*N+2,1);
for (int i = 1; i <= 2*N+2; i++)
{
res(i,1) = (*x0)[i][1];
}
fclose(out);
free(mmin);
free(mmax);
free(x0);
delete parms;
return res;
}
#endif