-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheckgradient.m
131 lines (116 loc) · 3.24 KB
/
checkgradient.m
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
% //////////////////////////////////////////////////////////////////////
% Abbring and Salimans (2021), extra calculations (fka laplace/test3.m)
% - Check analytical gradients against numerical ones
%
% Dependencies: strkdur.asc weibullmph.mat numgrad.m mhtobj.m nllhmph.m
% $(specs)
% Output: chckgrad.tex
% //////////////////////////////////////////////////////////////////////
%% clear screen and workspace
clear
clc
format short
rng(230670);
%% read strike data
rawdata=load('strkdur.asc');
x=rawdata(:,2);
y=rawdata(:,1)/7;
%% test
cens=false;
unobs_form='point';
shock_form='point';
nrunobs=5;
nrshocks=1;
% check input
if size(y,1)<size(y,2);
y=y';
end
if size(x,1)<size(x,2);
x=x';
end
if size(cens,1)<size(cens,2);
cens=cens';
end
n=length(y);
k=size(x,2);
if ~all(size(y)==[n 1])
error('input y has the wrong size')
end
if ~all(size(x)==[n k])
error('input x has the wrong size')
end
cens=logical(cens);
if length(cens)==1
cens=(ones(n,1)*cens>0);
end
if ~all(size(cens)==[n 1])
error('input cens has the wrong size')
end
% initialize
llh=[];
iter=0;
maxllh=-Inf;
% scale of data
mm=mean(y);
% set starting values
var=log(mm^2*mean(1./y-1/mm))+randn/10;
if isequal(unobs_form,'point')
v=log(ones(nrunobs,1)*mm)+randn(nrunobs,1)/2;
p=ones(nrunobs-1,1);
elseif isequal(unobs_form,'gamma')
if nrunobs>1
error('nrunobs should be 1 for gamma specification')
end
v=randn/2;
p=randn/2-log(mm);
else
error('wrong distribution for unobservables')
end
k=size(x,2);
if k==0
beta=[];
else
beta=zeros(size(x,2),1);
end
if isequal(shock_form,'point')
nu=log(mm)-log(4:1:3+nrshocks)'+randn(nrshocks,1)/2;
lambda=-log(mm)*ones(nrshocks,1);
elseif isequal(shock_form,'gamma')
if nrshocks>1
error('nrshocks should be 1 for gamma specification')
end
lambda=-log(mm);
nu=[randn/2; randn/2];
else
error('wrong distribution for shocks')
end
% convert to parameter vector of startvalues
startvalues=[var; p; v; lambda; nu; beta];
[obj, pargrad] = mhtobj(eval(['@' unobs_form shock_form]),startvalues,y,...
cens,x,nrunobs,nrshocks);
numgradient = numgrad(@(par)mhtobj(eval(['@' unobs_form shock_form]),...
par,y,cens,x,nrunobs,nrshocks),startvalues);
f1=fopen('chckgrad.tex','w');
fprintf(f1,'\\begin{table}[ht]\n');
fprintf(f1,'\\caption{Analytical and Numerical Gradients MHT}\n');
fprintf(f1,'\\begin{center}\n');
fprintf(f1,'\\begin{tabular}{rr}\n');
fprintf(f1,'$%6.6f$&$%6.6f$\\\\\n',[pargrad numgradient]');
fprintf(f1,'\\end{tabular}\n');
fprintf(f1,'\\end{center}\n');
fprintf(f1,'\\end{table}\n');
mph=load('weibullmph');
[obj,pargrad1] = nllhmph(mph.par,y,false,x,4);
numgradient1 = numgrad(@(par)nllhmph(par',y,false,x,4),mph.par');
altpar=mph.par.*(1+randn(size(mph.par))/10);
[obj,pargrad2] = nllhmph(altpar,y,false,x,4);
numgradient2 = numgrad(@(par)nllhmph(par',y,false,x,4),altpar');
fprintf(f1,'\\begin{table}[ht]\n');
fprintf(f1,'\\caption{Analytical and Numerical Gradients MPH}\n');
fprintf(f1,'\\begin{center}\n');
fprintf(f1,'\\begin{tabular}{rr|rr}\n');
fprintf(f1,'$%6.6f$&$%6.6f$&$%6.6f$&$%6.6f$\\\\\n',[pargrad1' numgradient1 pargrad2' numgradient2]');
fprintf(f1,'\\end{tabular}\n');
fprintf(f1,'\\end{center}\n');
fprintf(f1,'\\end{table}\n');
fclose(f1);