-
Notifications
You must be signed in to change notification settings - Fork 1
/
getbigs.cpp
274 lines (261 loc) · 6.31 KB
/
getbigs.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* $Id: getbigs.cpp 494 2012-06-13 20:41:16Z johnoel $
*
* Author: David Fournier
* Copyright (c) 2008-2012 Regents of the University of California
*/
/**
* \file
* Description not yet available.
*/
#if defined(USE_LAPLACE)
# include <df1b2fun.h>
#endif
#include <admodel.h>
/**
* Description not yet available.
* \param
*/
void function_minimizer::get_bigS(int ndvar,int nvar1,int nvar,
dmatrix& S,dmatrix& BS,dvector& scale)
{
dmatrix tv(1,ndvar,1,nvar1);
adstring tmpstring="admodel.dep";
if (ad_comm::wd_flag)
tmpstring = ad_comm::adprogram_name + ".dep";
cifstream cif((char*)tmpstring);
int tmp_nvar,tmp_ndvar;
cif >> tmp_nvar >> tmp_ndvar;
if (tmp_nvar!=nvar1)
{
cerr << " tmp_nvar != nvar1 in file " << tmpstring
<< endl;
ad_exit(1);
}
#if defined(USE_LAPLACE)
int us=nvar1-nvar;
int xsize,usize;
dmatrix minv(1,us,1,us);
dmatrix Dux(1,us,1,nvar);
dmatrix uhat_prime(1,us,1,nvar);
uhat_prime.initialize();
int Bnvar=nvar+us;
// get l_uu and l_xu for covariance calculations
if (lapprox->hesstype !=2)
{
tmpstring = ad_comm::adprogram_name + ".luu";
uistream ifs1((char*)(tmpstring));
ifs1 >> usize >> xsize;
if (!ifs1)
{
cerr << "Error reading from file " << tmpstring << endl;
ad_exit(1);
}
// check xsize and usize
if (xsize !=nvar ||usize !=us)
{
cerr << "size error in file " << tmpstring << endl;
ad_exit(1);
}
ifs1 >> minv;
ifs1 >> Dux;
if (!ifs1)
{
cerr << "Error reading from file " << tmpstring << endl;
ad_exit(1);
}
uhat_prime=-minv*Dux;
}
else
{
if (nvar !=lapprox->xsize)
{
cerr << "error in sizes in mod_sd" << endl;
ad_exit(1);
}
if (us !=lapprox->usize)
{
cerr << "error in sizes in mod_sd" << endl;
ad_exit(1);
}
xsize=lapprox->xsize;
usize=lapprox->usize;
// calculate uhat_prime from the block diagnal matrix
d3_array & H=*(lapprox->block_diagonal_hessian);
d3_array & Dux=*(lapprox->block_diagonal_Dux);
int mmin=H.indexmin();
int mmax=H.indexmax();
for (int i=mmin;i<=mmax;i++)
{
if (allocated(H(i)))
{
dmatrix tmp=inv(H(i))*Dux(i);
int rmin=H(i).indexmin();
int rmax=H(i).indexmax();
int tmpmin=Dux(i).indexmin();
int cmin=Dux(i,tmpmin).indexmin();
int cmax=Dux(i,tmpmin).indexmax();
for (int j=rmin;j<=rmax;j++)
{
for (int k=cmin;k<=cmax;k++)
{
int i1=(*lapprox->block_diagonal_re_list)(i,j)-nvar;
uhat_prime(i1,(*lapprox->block_diagonal_fe_list)(i,k))=
tmp(j,k);
}
}
}
}
// rescale uhat_prime to be der wrt x
{
int rmin=uhat_prime.indexmin();
int rmax=uhat_prime.indexmax();
int cmin=uhat_prime(rmin).indexmin();
int cmax=uhat_prime(rmin).indexmax();
for (int i=rmin;i<=rmax;i++)
{
for (int j=cmin;j<=cmax;j++)
{
uhat_prime(i,j)*=scale(j);
}
}
}
}
dmatrix Sux=uhat_prime*S;
dmatrix Suu=Sux*trans(uhat_prime);
if (lapprox->hesstype !=2)
{
if (lapprox->saddlepointflag==2)
{
Suu-=minv;
}
else
{
Suu+=minv;
}
}
else
{
d3_array & H=*(lapprox->block_diagonal_hessian);
int mmin=H.indexmin();
int mmax=H.indexmax();
for (int i=mmin;i<=mmax;i++)
{
if (allocated(H(i)))
{
dmatrix tmp=inv(H(i));
int rmin=H(i).indexmin();
int rmax=H(i).indexmax();
for (int j=rmin;j<=rmax;j++)
{
for (int k=rmin;k<=rmax;k++)
{
int j1=(*lapprox->block_diagonal_re_list)(i,j)-nvar;
int k1=(*lapprox->block_diagonal_re_list)(i,k)-nvar;
if (lapprox->saddlepointflag==2)
{
Suu(j1,k1)-=tmp(j,k);
}
else
{
Suu(j1,k1)+=tmp(j,k);
}
}
}
}
}
}
minv.deallocate();
BS.initialize();
// random effects are never bounded?
scale(xsize+1,Bnvar)=1.0;
int i;
for (i=1;i<=xsize;i++)
{
for (int j=1;j<=xsize;j++)
{
BS(i,j)=S(i,j);
}
}
for (i=xsize+1;i<=Bnvar;i++)
{
for (int j=1;j<=xsize;j++)
{
BS(i,j)=Sux(i-xsize,j);
BS(j,i)=BS(i,j);
}
}
for (i=xsize+1;i<=Bnvar;i++)
{
for (int j=xsize+1;j<=Bnvar;j++)
{
BS(i,j)=Suu(i-xsize,j-xsize);
}
}
# endif
//
// int us=nvar1-nvar;
// int xsize,usize;
// // get l_uu and l_xu for covariance calculations
// tmpstring = ad_comm::adprogram_name + ".luu";
// uistream ifs1((char*)(tmpstring));
// ifs1 >> usize >> xsize;
// if (!ifs1)
// {
// cerr << "Error reading from file " << tmpstring << endl;
// ad_exit(1);
// }
// // check xsize and usize
// if (xsize !=nvar ||usize !=us)
// {
// cerr << "size error in file " << tmpstring << endl;
// ad_exit(1);
// }
// dmatrix minv(1,usize,1,usize);
// dmatrix Dux(1,usize,1,xsize);
// int Bnvar=xsize+usize;
// ifs1 >> minv;
// ifs1 >> Dux;
// if (!ifs1)
// {
// cerr << "Error reading from file " << tmpstring << endl;
// ad_exit(1);
// }
// dmatrix S(1,nvar,1,nvar);
// read_covariance_matrix(S,nvar);
// dmatrix uhat_prime=minv*Dux;
// dmatrix Sux=uhat_prime*S;
// dmatrix Suu=Sux*trans(uhat_prime);
// Suu+=minv;
// //Suu=minv;
// minv.deallocate();
// BS.initialize();
// // random effects are never bounded?
//
// int i;
//
// for (i=1;i<=xsize;i++)
// {
// for (int j=1;j<=xsize;j++)
// {
// BS(i,j)=S(i,j);
// }
// }
//
// for (i=xsize+1;i<=Bnvar;i++)
// {
// for (int j=1;j<=xsize;j++)
// {
// BS(i,j)=Sux(i-xsize,j);
// BS(j,i)=BS(i,j);
// }
// }
//
// for (i=xsize+1;i<=Bnvar;i++)
// {
// for (int j=xsize+1;j<=Bnvar;j++)
// {
// BS(i,j)=Suu(i-xsize,j-xsize);
// }
// }
}