-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfigure1.m
125 lines (109 loc) · 3.14 KB
/
figure1.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
% //////////////////////////////////////////////////////////////////////
% Abbring and Salimans (2021), Figure 1 (fka laplace/invtest.m)
% - Approximation Error of the Log Likelihood for Various M
%
% Dependencies: strkdur.asc migaussmle.m lhmigauss.m numinvlap.m
% numinvlap2.m pointpoint.m
% Output: fig1.csv fig1times.tex
% //////////////////////////////////////////////////////////////////////
%% clear screen and workspace and set seed
clear
clc
format long
est=false; % false=as in paper; true=use MIG ML estimates
rng(230670) % seed for random starting values
%% settings
dispplot = false; % set to true to have script plot results
nrunobs=4;
nrshocks=0;
nrsim = 100;
if est
nrsim=1;
end
%% read strike data
rawdata=load('strkdur.asc');
x=rawdata(:,2);
y=rawdata(:,1);
cens=false(length(y),1);
timea = [];
timen = [];
for j=1:nrsim
fprintf('.');
%% parameters analytical, estimated or start
if est
[parMLE,cov,llh,opt]=migaussmle(y,cens,x,nrunobs);
else
% set starting values
mm=mean(y);
mu=1/mm;
var=mean(1./y-mu);
v=exp(randn(nrunobs-1,1));
p=ones(nrunobs-1,1)/nrunobs;
if isempty(x);
beta=[];
else
beta=zeros(size(x,2),1);
end
parMLE=[mu; var; v; p; beta];
end
mu=parMLE(1);
var=parMLE(2);
v=[1; parMLE(3:1+nrunobs)];
p=[1-sum(parMLE(2+nrunobs:2*nrunobs)); parMLE(2+nrunobs:2*nrunobs)];
beta=parMLE(end);
%% parameters numerical
% rescale
var=var/mu^2;
v=v/mu;
p=log(p);
p=p-p(1)+1;
p=p(2:end);
% convert to parameter vector of startvalues
par2=[log(var); p; log(v); beta];
%% test
tic
a_probs=lhmigauss(parMLE,y,cens,x,nrunobs);
timea=[timea;toc];
tic
n_probs=numinvlap(@pointpoint,par2,y,cens,x,nrunobs,nrshocks)./y;
timen=[timen;toc];
errs=[a_probs abs(a_probs-n_probs) abs(a_probs-n_probs)./n_probs];
maxerrs=max(errs);
meanerrs=mean(abs(errs));
logerr=abs(sum(log(n_probs))-sum(log(a_probs)));
%% graph maker
le{j}=zeros(30,1);
for i=1:30
le{j}(i)=log(abs(sum(log(numinvlap2(@pointpoint,par2,y,i,cens,x,...
nrunobs,nrshocks)./y))-sum(log(a_probs))));
end
end
fprintf('\n');
lerr=zeros(30,1);
for j=1:nrsim
lerr=lerr+le{j}/nrsim;
end
if dispplot
fig1 = figure('PaperSize',[20.98 29.68],'Color',[1 1 1]);
axes1 = axes('Parent',fig1,'YScale','log','YMinorTick','on','LineWidth',1,'FontSize',14);
box(axes1,'on');
hold(axes1,'all');
plot(exp(lerr),'LineWidth',2,'Color','blue','Marker','o');
title('Figure 1. Approximation Error of the Log Likelihood for Various M');
xlabel({'M'},'LineWidth',2,'FontSize',14);
ylabel({'Average absolute error in log likelihood'},'LineWidth',2,...
'FontSize',14);
end
f1=fopen('fig1times.tex','w');
fprintf(f1,'Note: Mean calculation times for Figure 1 are $%0.5e$ seconds ',...
mean(timea));
fprintf(f1,'(analytical) and $%0.5e$ seconds (numerical inversion), so that ',...
mean(timen));
fprintf(f1,'mean time numerical $=%6.2f\\times$ mean time analytical.\n',...
mean(timen)/mean(timea));
fclose(f1);
%% Export data to csv file for TikZ
f1=fopen('fig1.csv','w'); % mhtellherr.csv
fprintf(f1,'M, llherr, dummy\n');
fprintf(f1,'%6.0f, %6.6f, 1.0\n',[(1:30)' lerr/log(10)]');
fclose(f1);