ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mod_sd.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
7 #ifndef _MSC_VER
8  #include <unistd.h>
9 #endif
10 #if !defined(DOS386)
11  #define DOS386
12 #endif
13 
14 #include <chrono>
15 #include <df1b2fun.h>
16 #include <admodel.h>
17 
19 
21 {
22  GAUSS_varcovariance_matrix = &((dmatrix&)(m) );
23 }
24 
26 {
27  GAUSS_varcovariance_matrix = &((dmatrix&)(m) );
28 }
29 
31 {
32  GAUSS_varcovariance_matrix = &((dmatrix&)(m) );
33 }
34 
36 {
37  GAUSS_varcovariance_matrix = &((dmatrix&)(m) );
38 }
39 
40 #include <string>
41 std::string get_elapsed_time(
42  const std::chrono::time_point<std::chrono::system_clock>& from,
43  const std::chrono::time_point<std::chrono::system_clock>& to)
44 {
45  std::stringstream ss;
46 
47  /*
48  using namespace std::chrono_literals;
49  auto elapsed = 86400000ms * 2 + 3600000ms * 11 + 60000ms * 34 + 1500ms;
50  //(2d 11h 34m 1.5s)
51  */
52  auto elapsed = to - from;
53  auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed);
54  auto count = ms.count();
55 
56  if (count >= 86400000)
57  {
58  ss << (count / 86400000) << " d ";
59  count %= 86400000;
60  }
61  if (count >= 3600000)
62  {
63  ss << (count / 3600000) << " h ";
64  count %= 3600000;
65  }
66  if (count >= 60000)
67  {
68  ss << (count / 60000) << " m ";
69  count %= 60000;
70  }
71  ss << setprecision(2) << (static_cast<double>(count) * 0.001) << " s";
72 
73 /*
74  double runtime = ( std::clock()-start)/(double) CLOCKS_PER_SEC;
75  // Depending on how long it ran convert to sec/min/hour/days so
76  // the outputs are interpretable
77  std::string u; // units
78  if(runtime<=60){
79  u=" s";
80  } else if(runtime > 60 && runtime <=60*60){
81  runtime/=60; u=" mins";
82  } else if(runtime > (60*60) && runtime <= (360*24)){
83  runtime/=(60*60); u=" hours";
84  } else {
85  runtime/=(24*60*60); u=" days";
86  }
87  runtime=std::round(runtime * 10.0) / 10.0;
88  cout << " done! (" << runtime << u << ")" << endl;
89 */
90 
91  return std::move(ss.str());
92 }
93 
95  const std::chrono::time_point<std::chrono::system_clock>& from,
96  const std::chrono::time_point<std::chrono::system_clock>& to)
97 {
98  cout << " done (" << get_elapsed_time(from, to) << ") " << endl;
99 }
100 
102 {
103  std::chrono::time_point<std::chrono::system_clock> from_start;
105  {
106  from_start = std::chrono::system_clock::now();
107  cout << "Starting standard error calculations... " ;
108  cout.flush();
109  }
110 
111  int nvar=initial_params::nvarcalc(); // get the number of active parameters
112  dvector x(1,nvar);
113  initial_params::xinit(x); // get the number of active parameters
114 
116  int nvar1=initial_params::nvarcalc(); // get the number of active parameters
117  int num_sdrep_types=stddev_params::num_stddev_params +
119 
120  param_labels.allocate(1,num_sdrep_types);
121  param_size.allocate(1,num_sdrep_types);
122 
123  int ii=1;
124  size_t max_name_length = 0;
125  for (int i=0;i<initial_params::num_initial_params;i++)
126  {
127  //if ((initial_params::varsptr[i])->phase_start
128  // <= initial_params::current_phase)
129  if (withinbound(0,(initial_params::varsptr[i])->phase_start,
131  {
132  param_labels[ii]=
133  (initial_params::varsptr[i])->label();
134  param_size[ii]=
136  if (max_name_length<param_labels[ii].size())
137  {
138  max_name_length=param_labels[ii].size();
139  }
140  ii++;
141  }
142  }
143 
144  int start_stdlabels=ii;
145  for (int i=0;i< stddev_params::num_stddev_params;i++)
146  {
147  param_labels[ii]=
149  param_size[ii]=
151  if (max_name_length<param_labels[ii].size())
152  {
153  max_name_length=param_labels[ii].size();
154  }
155  ii++;
156  }
157  int end_stdlabels=ii-1;
158 
159  int ndvar=stddev_params::num_stddev_calc();
160  dvector scale(1,nvar1); // need to get scale from somewhere
161  dvector v(1,nvar); // need to read in v from model.rep
162  dmatrix S(1,nvar,1,nvar);
163  {
164  uistream cif("admodel.cov");
165  int tmp_nvar = 0;
166  cif >> tmp_nvar;
167  if (nvar !=tmp_nvar)
168  {
169  cerr << "Incorrect number of independent variables in file"
170  " model.cov" << endl;
171  ad_exit(1);
172  }
173  cif >> S;
174  if (!cif)
175  {
176  cerr << "error reading covariance matrix from model.cov" << endl;
177  ad_exit(1);
178  }
179  }
180  int sgn;
182  _ln_det_value = -ln_det(S,sgn)-2.0*sum(log(scale));
184  //int nvar1=initial_params::nvarcalc();
185  dvector diag(1,nvar1+ndvar);
186  dvector tmp(1,nvar1+ndvar);
187 
188  {
189  ofstream ofs("admodel.tmp");
190 
191  #if defined(__GNU__) || defined(DOS386) || defined(__GNUDOS__)
192  // *******************************************************
193  // *******************************************************
194  {
195  if (nvar==nvar1) // no random effects
196  {
197  for (int i=1;i<=nvar;i++)
198  {
199  for (int j=1;j<=i;j++)
200  {
201  tmp(j)=S(i,j)*scale(i)*scale(j);
202  ofs << tmp(j) << " ";
203  }
204  ofs << endl;
205  diag(i)=tmp(i);
206  }
207  dmatrix tv(1,ndvar,1,nvar1);
208  adstring tmpstring="admodel.dep";
209  if (ad_comm::wd_flag)
210  tmpstring = ad_comm::adprogram_name + ".dep";
211  cifstream cif((char*)tmpstring);
212 
213  int tmp_nvar = 0, tmp_ndvar = 0;
214  cif >> tmp_nvar >> tmp_ndvar;
215  if (tmp_nvar!=nvar1)
216  {
217  cerr << " tmp_nvar != nvar1 in file " << tmpstring
218  << endl;
219  ad_exit(1);
220  }
221  if (ndvar>0)
222  {
223  cif >> tv;
224  dvector tmpsub(1,nvar);
225  bool bad_vars=false;
226  for (int i=1;i<=ndvar;i++)
227  {
228  for (int j=1;j<=nvar;j++)
229  {
230  tmpsub(j)=(tv(i)*S(j))*scale(j);
231  }
232  ofs << tmpsub << " ";
233  tmpsub=tv(i)*S;
234  for (int j=1;j<=i;j++)
235  {
236  tmp(nvar+j)=tmpsub*tv(j);
237  ofs << tmp(nvar+j) << " ";
238  }
239  diag(i+nvar)=tmp(i+nvar);
240 
241  if (diag(i + nvar) <= 0.0)
242  {
243  std::ostream& output_stream = get_output_stream();
244  output_stream << "Estimated covariance matrix may not be positive definite.\n"
245  << std::scientific << setprecision(10) << sort(eigenvalues(S)) << endl;
246 
248  {
249  // If first variable print message, otherwise tack it on
250  if(!bad_vars)
251  cout << "\n Warning: Non-positive variance of sdreport variables: ";
252  else
253  cout << ", ";
254 
255  cout << i + nvar;
256  bad_vars=true;
257  }
258  }
259  ofs << endl;
260  }
261  if (bad_vars) cout << endl;
262  }
263  }
264  else // have random effects
265  {
266  dmatrix tv(1,ndvar,1,nvar1);
267  adstring tmpstring="admodel.dep";
268  if (ad_comm::wd_flag)
269  tmpstring = ad_comm::adprogram_name + ".dep";
270  cifstream cif((char*)tmpstring);
271 
272  int tmp_nvar = 0, tmp_ndvar = 0;
273  cif >> tmp_nvar >> tmp_ndvar;
274  if (tmp_nvar!=nvar1)
275  {
276  cerr << " tmp_nvar != nvar1 in file " << tmpstring
277  << endl;
278  ad_exit(1);
279  }
280 
281  dmatrix BS(1,nvar1,1,nvar1);
282  BS.initialize();
283  get_bigS(ndvar,nvar1,nvar,S,BS,scale);
284 
285  {
286  tmpstring = ad_comm::adprogram_name + ".bgs";
287  uostream uos((char*)(tmpstring));
288  if (!uos)
289  {
290  cerr << "error opening file " << tmpstring << endl;
291  ad_exit(1);
292  }
293  uos << nvar1;
294  uos << BS;
295  if (!uos)
296  {
297  cerr << "error writing to file " << tmpstring << endl;
298  ad_exit(1);
299  }
300  }
301 
302  dvector* pBSi = &BS(1);
303  double* pscalei = scale.get_v() + 1;
304  double* ptmpi = tmp.get_v() + 1;
305  double* pdiagi = diag.get_v() + 1;
306  for (int i=1;i<=nvar1;i++)
307  {
308  double* pBSij = pBSi->get_v() + 1;
309  double* ptmpj = tmp.get_v() + 1;
310  double* pscalej = scale.get_v() + 1;
311  for (int j=1;j<=i;j++)
312  {
313  *ptmpj = *pBSij * *pscalei * *pscalej;
314  ofs << *ptmpj << " ";
315 
316  ++ptmpj;
317  ++pBSij;
318  ++pscalej;
319  }
320  ofs << endl;
321  *pdiagi = *ptmpi;
322 
323  ++pBSi;
324  ++ptmpi;
325  ++pscalei;
326  ++pdiagi;
327  }
328 
329  if (ndvar>0)
330  {
331  cif >> tv;
332  dvector tmpsub(1,nvar1);
333  for (int i=1;i<=ndvar;i++)
334  {
335  for (int j=1;j<=nvar1;j++)
336  {
337  tmpsub(j)=(tv(i)*BS(j))*scale(j);
338  }
339  ofs << tmpsub << " ";
340  tmpsub=tv(i)*BS;
341  for (int j=1;j<=i;j++)
342  {
343  tmp(nvar1+j)=tmpsub*tv(j);
344  ofs << tmp(nvar1+j) << " ";
345  }
346  diag(i+nvar1)=tmp(i+nvar1);
347 
348  if (diag(i+nvar1)<=0.0)
349  {
350  if (norm(tv(i))>1.e-100)
351  {
352  cerr << "Estimated covariance matrix may not"
353  " be positive definite" << endl;
354  cerr << sort(eigenvalues(BS)) << endl;
355  }
356  }
357  ofs << endl;
358  }
359  }
360  }
361  }
362  // *******************************************************
363  #else
364  // *******************************************************
365  {
366  for (int i=1;i<=nvar;i++)
367  {
368  for (int j=1;j<=i;j++)
369  {
370  tmp(j)=S(i,j)*scale(i)*scale(j);
371  ofs << tmp(j) << " ";
372  }
373  ofs << endl;
374  diag(i)=tmp(i);
375  }
376  dvector tv(1,nvar);
377  adstring tmpstring="admodel.dep";
378  if (ad_comm::wd_flag)
379  tmpstring = ad_comm::adprogram_name + ".dep";
380  cifstream cif((char*)tmpstring);
381  int tmp_nvar,tmp_ndvar;
382  cif >> tmp_nvar >> tmp_ndvar;
383  dvector tmpsub(1,nvar);
384  for (int i=1;i<=ndvar;i++)
385  {
386  cif >> tv; // v(i)
387  for (int j=1;j<=nvar;j++)
388  {
389  tmpsub(j)=(tv*S(j))*scale(j);
390  }
391  ofs << tmpsub << " ";
392  tmpsub=tv*S;
393  cif.seekg(0,ios::beg);
394  cif >> tmp_nvar >> tmp_ndvar;
395  for (int j=1;j<=i;j++)
396  {
397  cif >> v;
398  tmp(nvar+j)=tmpsub*v;
399  ofs << tmp(nvar+j) << " ";
400  }
401  diag(i+nvar)=tmp(i+nvar);
402 
403  if (diag(i+nvar)<=0.0)
404  {
405  cerr << "Estimated covariance matrix may not be positive definite"
406  << endl;
407  }
408  ofs << endl;
409  }
410  }
411  // *******************************************************
412  // *******************************************************
413  #endif
414  }
415 
416 
417  {
418  cifstream cif("admodel.tmp");
419  //ofstream ofs("admodel.cor");
420  ofstream ofs((char*)(ad_comm::adprogram_name + adstring(".cor")));
421  ofstream ofsd((char*)(ad_comm::adprogram_name + adstring(".std")));
422 
423  int offset=1;
424  dvector param_values(1,nvar1+ndvar);
425  initial_params::copy_all_values(param_values,offset);
426  stddev_params::copy_all_values(param_values,offset);
427 
428  for (int i=1;i<=nvar1;i++)
429  {
430  if (diag(i)<=0.0)
431  {
432  cerr << "Estimated covariance matrix may not be positive definite"
433  << endl;
434  ad_exit(1);
435  }
436  else
437  {
438  diag(i)=sqrt(diag(i));
439  }
440  }
441  for (int i=nvar1+1;i<=nvar1+ndvar;i++)
442  {
443  if (diag(i)<0.0)
444  {
445  cerr << "Estimated covariance matrix may not be positive definite"
446  << endl;
447  ad_exit(1);
448  }
449  else if (diag(i)==0.0)
450  {
451  diag(i)=0.0;
452  }
453  else
454  {
455  diag(i)=sqrt(diag(i));
456  }
457  }
458 
459  {
460  dvector dd=diag(nvar1+1,nvar1+ndvar);
461  dd.shift(1);
462  ii=0;
464  }
465 
466  int lc=1;
467  int ic=1;
468  ofs << " The logarithm of the determinant of the hessian = "
469  << _ln_det_value << endl;
470  ofs << " index ";
471  ofsd << " index ";
472  ofs << " name ";
473  ofsd << " name ";
474  size_t inmax = max_name_length > 5 ? max_name_length - 5 : 0;
475  for (size_t in = 1;in <= inmax; in++)
476  {
477  ofs << " ";
478  ofsd << " ";
479  }
480  ofs << " value std.dev ";
481  ofsd << " value std.dev";
482  for (int i=1;i<=nvar+ndvar;i++)
483  {
484  ofs << " " << setw(4) << i << " ";
485  }
486  ofs << endl;
487  ofsd << endl;
488 
489  if (GAUSS_varcovariance_matrix) (*GAUSS_varcovariance_matrix).initialize();
490 
491  double* pdiagi = diag.get_v() + 1;
492  for (int i=1;i<=nvar1+ndvar;i++)
493  {
494  double* ptmpj = tmp.get_v() + 1;
495  for (int j=1;j<=i;j++)
496  {
497  cif >> *ptmpj;
498 
499  ++ptmpj;
500  }
501  ptmpj = tmp.get_v() + 1;
502  double* pdiagj = diag.get_v() + 1;
503  for (int j=1;j<=i;j++)
504  {
505  if (*pdiagi == 0.0 || *pdiagj == 0.0)
506  {
507  *ptmpj = 0.0;
508  }
509  else
510  {
511  if (i!=j)
512  {
513  *ptmpj /= (*pdiagi * *pdiagj);
514  }
515  else
516  {
517  *ptmpj = 1;
518  }
519  }
520  ++ptmpj;
521  ++pdiagj;
522  }
523  ofs << " " << setw(4) << i << " ";
524  ofsd << " " << setw(4) << i << " ";
525  ofs << param_labels[lc];
526  ofsd << param_labels[lc];
527 // get the std dev of profiles likelihood variables into the right slots
528  if (start_stdlabels <= lc && end_stdlabels >= lc)
529  {
530  for (int ix=0;ix<likeprof_params::num_likeprof_params;ix++)
531  {
532  if (param_labels[lc]==likeprof_params::likeprofptr[ix]->label())
533  {
534  likeprof_params::likeprofptr[ix]->get_sigma()= *pdiagi;
535  }
536  }
537  }
538 
539  inmax = max_name_length + 1 > param_labels[lc].size()
540  ? max_name_length + 1 - param_labels[lc].size() : 0;
541  for (size_t in = 1; in <= inmax; in++)
542  {
543  ofs << " ";
544  ofsd << " ";
545  }
546  if (++ic> param_size[lc])
547  {
548  lc++;
549  ic=1;
550  }
551  ofs << setscientific() << setw(11) << setprecision(4) << param_values(i)
552  << " ";
553  ofs << setscientific() << setw(10) << setprecision(4) << *pdiagi << " ";
554 
555  if (GAUSS_varcovariance_matrix)
556  {
557  if (GAUSS_varcovariance_matrix->indexmax()>=i)
558  (*GAUSS_varcovariance_matrix) (i,1)= *pdiagi;
559  }
560 
561  ofsd << setscientific() << setw(11) << setprecision(4) << param_values(i)
562  << " ";
563  ofsd << setscientific() << setw(10) << setprecision(4) << *pdiagi;
564  ptmpj = tmp.get_v() + 1;
565  for (int j=1;j<=i;j++)
566  {
567  ofs << " " << setfixed() << setprecision(4) << setw(7) << *ptmpj;
568  if (GAUSS_varcovariance_matrix)
569  {
570  if (GAUSS_varcovariance_matrix->indexmax()>=i &&
571  (*GAUSS_varcovariance_matrix)(i).indexmax()>j)
572  {
573  (*GAUSS_varcovariance_matrix) (i,j+1) = *ptmpj;
574  }
575  }
576  ++ptmpj;
577  }
578  ofs << endl;
579  ofsd << endl;
580 
581  ++pdiagi;
582  }
583  }
584 #if defined(_MSC_VER)
585  if (system("del admodel.tmp") == -1)
586 #else
587  if (unlink("admodel.tmp") == -1)
588 #endif
589  {
590  char msg[40] = {"Error trying to delete temporary file "};
591  cerr << msg << "admodel.tmp" << endl;
592  }
593 
595  {
596  print_elapsed_time(from_start, std::chrono::system_clock::now());
597  }
598 }
static likeprof_params * likeprofptr[500]
Definition: admodel.h:2257
static void set_active_random_effects(void)
Definition: model.cpp:267
int withinbound(int lb, int n, int ub)
Definition: model.cpp:45
dmatrix * GAUSS_varcovariance_matrix
Definition: mod_sd.cpp:18
ivector param_size
Definition: admodel.h:1912
static int num_active_calc(void)
Definition: model.cpp:177
dvector eigenvalues(const banded_symmetric_dmatrix &_SS)
Description not yet available.
Definition: dmat28.cpp:411
#define x
Vector of double precision numbers.
Definition: dvector.h:50
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
void print_elapsed_time(const std::chrono::time_point< std::chrono::system_clock > &from, const std::chrono::time_point< std::chrono::system_clock > &to)
Definition: mod_sd.cpp:94
exitptr ad_exit
Definition: gradstrc.cpp:53
void set_gauss_covariance_matrix(const dll_data_matrix &m)
Definition: mod_sd.cpp:20
virtual double & get_sigma(void)=0
void set_covariance_matrix(const dll_data_matrix &m)
Definition: mod_sd.cpp:30
static void restore_start_phase(void)
Definition: model.cpp:275
static adstring adprogram_name
Definition: fvar.hpp:8860
static int num_stddev_calc(void)
Definition: model2.cpp:51
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
static int nvarcalc()
Definition: model.cpp:152
std::string get_elapsed_time(const std::chrono::time_point< std::chrono::system_clock > &from, const std::chrono::time_point< std::chrono::system_clock > &to)
Definition: mod_sd.cpp:41
ivector sgn(const dvector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dvect24.cpp:11
virtual unsigned int size_count() const =0
prescientific setscientific(void)
Description not yet available.
Definition: admanip.cpp:80
void allocate(int min, int max)
Allocate vector of adstring with range [min, max].
Definition: string5.cpp:80
virtual const char * label()=0
dmatrix sort(const dmatrix &m, int column, int NSTACK)
Description not yet available.
Definition: dmsort.cpp:17
prnstream & endl(prnstream &)
static int current_phase
Definition: admodel.h:842
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
Description not yet available.
Definition: fvar.hpp:3398
Description not yet available.
static void xinit(const dvector &x)
Definition: model.cpp:226
adstring_array param_labels
Definition: admodel.h:1911
void get_bigS(int ndvar, int nvar1, int nvar, dmatrix &S, dmatrix &BS, dvector &scale)
Description not yet available.
Definition: getbigs.cpp:15
Description not yet available.
Definition: fvar.hpp:2819
dvector & shift(int min)
Shift valid range of subscripts.
Definition: dvector.cpp:52
Description not yet available.
Definition: admodel.h:1610
static void copy_all_values(const dvector &x, const int &ii)
Definition: model3.cpp:9
static int num_initial_params
Definition: admodel.h:836
double ln_det(const dmatrix &m1, int &sgn)
Compute log determinant of a constant matrix.
Definition: dmat3.cpp:536
int indexmax() const
Definition: fvar.hpp:2921
std::ostream & get_output_stream()
Definition: adglobl.cpp:45
void sd_routine(void)
Definition: mod_sd.cpp:101
int size() const
Definition: string5.cpp:65
static stddev_params * stddevptr[150]
Definition: admodel.h:2215
unsigned int size_count(const dvector &x)
Returns total size of elements in vector x.
Definition: dsize.cpp:17
Description not yet available.
Description not yet available.
Definition: fvar.hpp:3516
static int output_flag
Definition: admodel.h:1972
static void get_all_sd_values(const dvector &x, const int &ii)
Definition: model41.cpp:9
static void copy_all_values(const dvector &x, const int &ii)
Definition: model4.cpp:9
double _ln_det_value
Definition: admodel.h:2197
static int stddev_scale(const dvector &d, const dvector &x)
Definition: model.cpp:202
prefixed setfixed(void)
Description not yet available.
Definition: admanip.cpp:59
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat7.cpp:12
double *& get_v(void)
Definition: dvector.h:148
static unsigned int wd_flag
Definition: fvar.hpp:8864
void allocate(const ad_integer &ncl, const index_type &ncu)
Allocate vector of integers with dimension [_ncl to _nch].
Definition: indextyp.cpp:488
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
static adlist_ptr varsptr
Definition: admodel.h:838
static int num_stddev_params
Definition: admodel.h:2219
static int num_likeprof_params
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: admodel.h:2259