-
Notifications
You must be signed in to change notification settings - Fork 3
/
osrdiag.m
132 lines (122 loc) · 3.92 KB
/
osrdiag.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
132
function [thhat,thini,tseiter,scl,L,gam,hes,optis,momx,covX]=osrdiag(fname,ddir,np)
% [thhat,thini,tseiter,scl,L,gam,hes,optis,momx,covX]=OSRDIAG(fname,ddir,np)
%
% Reads in a single file with diagnostics from MLEOS, MLEROS0, MLEROS.
%
% INPUT:
%
% ddir A directory name, e.g. OLHEDE?/Simulations-lemaitre
% fname A file name, e.g. 'diagn_21-Jun-2018'
% np The number of parameters that are being solved for [default: 3]
%
% OUTPUT:
%
% See MLEOS, MLEROS0
%
% thhat The maximum-likelihood estimate of the vector with elements:
% [ s2 nu rho], in "nothing", see SIMULOSL
% [D f2 r s2 nu rho], in Nm, and "nothing", see SIMULROS
% [D f2 s2 nu rho], in Nm, and "nothing", see SIMULOS, SIMULROS0
% thini The starting guess used in the optimization
% tseiter Time taken, error code, number of iterations
% scl The scaling applied as part of the optimization procedure
% L The likelihood of this solution, by FMINUNC/FMINCON
% gam The score, or first derivative of the likelihood, by FMINUNC/FMINCON
% hes The Hessian, second derivative of the likelihood, by FMINUNC/FMINCON
% optis The first-order optimality condition, by FMINUNC/FMINCON
% momx The various moments of the quadratic piece of the likelihood
% covX The covariance matrix estimate of the estimate
%
% SEE ALSO:
%
% OSOPEN, OSLOAD, OSWDIAG
%
% Last modified by fjsimons-at-alum.mit.edu, 10/14/2023
defval('ddir','/u/fjsimons/PROGRAMS/MFILES/olhede4')
defval('fname','mleosl_diagn_21-Jun-2018')
% The number of parameters to solve for
defval('np',3)
% The number of unique entries in an np*np symmetric matrix
npp=np*(np+1)/2;
% The number of sample variances that will be read - reset below
defval('nvar',2)
% Get a rough estimate of the number of estimates from the size
% You want this number to err on the side of being too large!
if np==3
nsize=472; nvar=1;
elseif np==5
nsize=560;
elseif np==6
nsize=721;
end
ndim=ceil(fsize(fullfile(ddir,fname))/nsize);
% Initialize
tseiter=nan(ndim,3);
L=nan(ndim,1);
optis=nan(ndim,1);
momx=nan(ndim,3);
% Make room for the data sample variance(s)
thhat=deal(nan(ndim,np+nvar));
[thini,gam]=deal(nan(ndim,np));
hes=nan(ndim,npp);
covX=nan(ndim,npp);
% Rarely, in SPMD mode does the file get written too quickly and does a
% confusion between labs happen - look into the file and fix easily
% Read the contents
fid=fopen(fullfile(ddir,fname),'r');
for index=1:ndim
try
% The initial guess
thini(index,:)=fscanf(fid,'%e',np);
catch
% Quit if you're done
index=index-1;
break
end
% The estimates, and the scaled spatial sample variance(s)
thhat(index,:)=fscanf(fid,'%e',np+nvar);
% Three diagnostics (time taken, exitflag, number of iterations)
tseiter(index,:)=fscanf(fid,'%i',3);
try
% The likelihood
L(index)=fscanf(fid,'%e',1);
% disp(sprintf('L= %7.4f',L(index)))
catch
% Tell if you're wrong
L(index-1)
sprintf('%12.8g',thhat(index-1,3))
break
end
% The first-order optimality criterion
optis(index)=fscanf(fid,'%e',1);
% The two moments of Xk, and the magic parameter
momx(index,:)=fscanf(fid,'%e',3);
% The scalings
scl(index,:)=fscanf(fid,'%e',np);
% The scores
gam(index,:)=fscanf(fid,'%e',np);
% The scaled Hessian elements at the solution
hes(index,:)=fscanf(fid,'%f',npp);
% An unscaled covariance estimate
covX(index,:)=fscanf(fid,'%f',npp);
end
fclose(fid);
% Finetune the preallocation over time
if size(thhat,1)~=index
disp(sprintf('\n%s allocated length %i, received length %i',...
upper(mfilename),size(thhat,1),index))
end
% Trim the possibly wrongly preallocated arrays
thhat=thhat(1:index,:);
thini=thini(1:index,:);
tseiter=tseiter(1:index,:);
scl=scl(1:index,:);
L=L(1:index,:);
gam=gam(1:index,:);
hes=hes(1:index,:);
covX=covX(1:index,:);
optis=optis(1:index,:);
momx=momx(1:index,:);
% Put out
varns={thhat,thini,tseiter,scl,L,gam,hes,optis,momx,covX};
varargout=varns(1:nargout);