ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mod_pmin.cpp
Go to the documentation of this file.
1 
5 #include <admodel.h>
6 
7 void get_confidence_interval(const dvector& left_bd, const dvector& right_bd,
8  dmatrix& ms, const dvector& xs, const dvector& siglevel,
9  const int& level_index, dvector& xdist,int index);
10 void get_onesided_intervals(const dvector& left_bd, const dvector& right_bd,
11  dmatrix& ms, const dvector& xs, const dvector& siglevel,
12  const int& level_index, dvector& xdist,int index);
13 void report_confidence_limits(const ofstream& ofs3,int numsig_levels,
14  dvector& siglevel, const dvector& left_bd, const dvector& right_bd);
15 void report_onesided_confidence_limits(const ofstream& ofs3,int numsig_levels,
16  dvector& siglevel, const dvector& left_bd, const dvector& right_bd,int ip);
17 
18 
20 {
21  int mmin=x.indexmin();
22  int mmax=x.indexmax();
23  dmatrix tmp(mmin,mmax,1,1);
24  for (int i=mmin;i<=mmax;i++)
25  {
26  tmp(i,1)=x(i);
27  }
28  return tmp;
29 }
30  double mult_factor(int j)
31  {
32  switch(j)
33  {
34  case 0:
35  return .0;
36  case 1:
37  return .5;
38  case 2:
39  return 1.0;
40  case 3:
41  return 1.5;
42  default:
43  return 2.0;
44  }
45  }
46 
47  double trimax(double x,double y,double z);
48 
49 #if defined(USE_ADPVM)
51  {
52  do
53  {
54  int prof_switch=get_int_from_master();
55  if (!prof_switch) break;
56  if (prof_switch !=3)
57  {
58  cerr << "Error in prof_switch " << prof_switch << endl;
59  ad_exit(1);
60  }
61  int underflow_flag=get_int_from_master();
62  pvm_slave_prof_minimize(underflow_flag);
63  }
64  while(1);
65  }
66 #endif
67  void function_minimizer::likeprof_routine(double global_min)
68  {
69  int on1 = 0;
70  int nopt = 0;
72  if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-iprint",nopt))>-1)
73  {
74  if (!nopt)
75  {
76  cerr << "Usage -iprint option needs integer -- ignored" << endl;
77  }
78  else
79  {
80  int jj=atoi(ad_comm::argv[on1+1]);
81  iprint = jj;
82  }
83  }
84 
85  dvector siglevel("{.90,.95,.975}");
87  {
88  for (int ip=1;ip<likeprof_params::num_likeprof_params;ip++)
89  {
91  if (sno)
92  {
93  if (sno>num_pp) num_pp=sno;
94  }
95  }
96  }
98  // DF NOV 28 11
100  {
102  }
103  int nvar=initial_params::nvarcalc();
104  dvector xsave(1,nvar);
105  {
106  int ii=1;
108  }
109  double old_value; // this is where we were
110  double new_value; // this is where we want to go
111  double fprof = 0.0;
112  double current_value;
113  dmatrix lprof(0,likeprof_params::num_likeprof_params-1,-num_pp,num_pp);
114  dmatrix ldet(0,likeprof_params::num_likeprof_params-1,-num_pp,num_pp);
115  dmatrix ln_det_proj_jac(0,likeprof_params::num_likeprof_params-1,
116  -num_pp,num_pp);
117  dmatrix pdet(0,likeprof_params::num_likeprof_params-1,-num_pp,num_pp);
119  -num_pp,num_pp);
120  dvector all_values(-num_pp,num_pp);
121  dvector all_num_sigs(-num_pp,num_pp);
122  //dvector xxxtmp(-num_pp,num_pp);
123  //d3_array hesses(-num_pp,num_pp,1,nvar,1,nvar);
124  dmatrix lg_jacob(0,likeprof_params::num_likeprof_params-1,-num_pp,num_pp);
125  dmatrix lg_prjacob(0,likeprof_params::num_likeprof_params-1,-num_pp,num_pp);
126  //dmatrix xxtmp(-num_pp,num_pp,1,nvar);
127  dvector xvector(1,nvar);
128  dmatrix xmax(-num_pp,num_pp,1,nvar); // this holds the conditional max
129  dmatrix gprof(-num_pp,num_pp,1,nvar); // this holds the conditional max
130  dmatrix fgrads(-num_pp,num_pp,1,nvar); // this holds the conditional max
131  dmatrix xdist(0,likeprof_params::num_likeprof_params-1,-num_pp,num_pp);
132  int sign=0;
133  double sigma;
134  ofstream ofs2((char*) (ad_comm::adprogram_name + adstring(".prf")) );
135  double udet =unrestricted_hess_determinant();
136  const int offset=0;
137  dvector xscale(1,nvar); // need to get scale from somewhere
139  dmatrix penalties(0,likeprof_params::num_likeprof_params-1,-num_pp,num_pp);
140  penalties.initialize();
141 #ifdef CURVE_CORRECT
143  d3_array eigenvals(0,nlp-1,-num_pp,num_pp,1,nvar-1);
144  d3_array curvcor(0,nlp-1,-num_pp,num_pp,1,nvar-1);
145 #endif
146 
147  int ip;
148  for (ip=0;ip<likeprof_params::num_likeprof_params;ip++)
149  {
150  likeprof_save(ip)=likeprof_params::likeprofptr[ip]->get_value();
151  }
152  double final_weight;
153  for (ip=0;ip<likeprof_params::num_likeprof_params;ip++)
154  {
156  double snz=likeprof_params::likeprofptr[ip]->get_stepsize();
157  if (sno)
158  {
159  num_pp=sno;
160  }
161  //snz (step size) must be greater than zero.
162  //Check "void likeprof_params::set_stepsize(double x)"
163  const double relsig = snz > 0 ? snz : 0.5;
164 
165  if (ip>0)
166  {
167  int ii=1;
169  }
170  sigma=likeprof_params::likeprofptr[ip]->get_sigma(); // this is the
171  if (sigma==0.0)
172  cerr << "error standard dev of likeporf parameter is 0" << endl;
173  // estimated sd
174  old_value=likeprof_save(ip);
175  old_value=old_value+offset*relsig*sigma; // this is where we
176  int bigint_flag=0;
177  int bigint_flag1=0;
178  double ldiff=0.0;
179  double num_sigs;
180  for (int i=1;i<=2;i++) // go in positive and negative directions
181  {
182  num_sigs=0.0;
183  bigint_flag=0;
184  bigint_flag1=0;
185  int underflow_flag=0;
186  if (i>1) // get the parameter values at the global minimum
187  {
188  int ii=1;
190  }
191  if (i==1)
192  {
193  sign=1;
194  }
195  else
196  {
197  sign=-1;
198  }
199  current_value=old_value; // initialize at the minimum
200  for (int j=0;j<=num_pp;j++) // go in positive and negative directions
201  {
202  if (j!=0 || sign > 0)
203  {
204  if (bigint_flag==0)
205  {
206  num_sigs+=mult_factor(j)*relsig*sign;
207  current_value+=mult_factor(j)*relsig*sign*sigma;
208  new_value=current_value;
209  // new_value=current_value+j*relsig*sign*sigma;
210  }
211  else
212  {
213  if (bigint_flag1==0)
214  {
215  num_sigs+=1.5*relsig*sign;
216  current_value+=1.5*relsig*sign*sigma;
217  new_value=current_value;
218  }
219  else
220  {
221  num_sigs+=2.5*relsig*sign;
222  current_value+=2.5*relsig*sign*sigma;
223  new_value=current_value;
224  }
225  }
226 #if defined(USE_ADPVM)
228  {
229  if (ad_comm::pvm_manager->mode==1) // master
230  {
231  send_int_to_slaves(3);
232  pvm_master_prof_minimize(ip,sigma,new_value,fprof,underflow_flag,
233  global_min,penalties(ip,j*sign),final_weight); // get the
234  }
235  }
236  else
237 #endif
238  {
239  if (random_effects_flag==0)
240  {
241  prof_minimize(ip,sigma,new_value,fprof,underflow_flag,
242  global_min,penalties(ip,j*sign),final_weight); // get the
243  // conditional max
244  }
245  else
246  {
247  prof_minimize_re(ip,sigma,new_value,fprof,underflow_flag,
248  global_min,penalties(ip,j*sign),final_weight); // get the
249  // conditional max
250  }
251  }
252  all_num_sigs(j*sign)=num_sigs;
253  initial_params::xinit(xvector); // save the
254  int ic=1;
255  // save the conditional maximum
256  initial_params::copy_all_values(xmax(sign*j),ic);
257  /*int check=*/initial_params::stddev_scale(xscale,xvector);
258  //#if defined(DO_PROFILE)
259  //dvector curvscale(1,nvar); // need to get scale from somewhere
260  //#endif
261 
262  // #if defined(DO_PROFILE)
263  // check=initial_params::stddev_curvscale(curvscale,xvector);
264  // #endif
265  //cout << "xscale = " << endl << xscale << endl;
266  //{
267  // ofstream ofs("xscale");
268  // ofs << xscale << endl;
269  // }
270 #if defined(USE_DDOUBLE)
271  lg_jacob(ip,sign*j)=sum(log(fabs(xscale)+double(1.e-60)));
272 #else
273  lg_jacob(ip,sign*j)=sum(log(fabs(xscale)+1.e-60));
274 #endif
275  if (!underflow_flag)
276  {
277  lprof(ip,sign*j)=fprof;
278  }
279  else
280  {
281  lprof(ip,sign*j)=lprof(ip,sign*(j-1))+2;
282  }
283  double xx=likeprof_params::likeprofptr[ip]->get_value();
284  if (!underflow_flag)
285  {
286  actual_value(ip,sign*j)=xx;
287  }
288  else
289  {
290  actual_value(ip,sign*j)=new_value; // this is where we
291  }
292 
293  ldiff=fprof-lprof(ip,0);
294  if ( ldiff > 40.0)
295  {
296  underflow_flag=1;
297  }
298 
299  //if ( (fprof-lprof(ip,0)) > 3.0 && bigint_flag ==0)
300  if (abs(j) > 4)
301  {
302  bigint_flag=1;
303  }
304  if (abs(j) > 5)
305  {
306  bigint_flag1=1;
307  }
308  // get the gradient for the profile likelihood variable at
309  // the conditional maximum
312  dvector g(1,nvar);
313  dvector fg(1,nvar);
314  if (!underflow_flag)
315  {
316  // g is the grad. wrt the prof lik var
317  // fg is the grad. wrt the log-likelihood
318  get_particular_grad(ip,nvar,fg,g);
319  gprof(sign*j)=g;
320 
321  fgrads(sign*j)=fg;
322  xdist(ip,sign*j)=norm(elem_div(gprof(sign*j),xscale));
323  }
324  else
325  {
326  gprof(sign*j)=gprof(sign*(j-1));
327  xdist(ip,sign*j)=xdist(ip,sign*(j-1));
328  }
329 
330  //#if defined(DO_PROFILE)
331  /*
332  if (!underflow_flag)
333  {
334  hess_routine_and_constraint(ip,g,fg); // calculate the hessian
335  // at the conditional max
336  }
337 
338  if (!underflow_flag)
339  {
340  ldet(ip,sign*j)=projected_hess_determinant(g,underflow_flag,
341  xscale,ln_det_proj_jac(ip,sign*j));
342  }
343  else
344  {
345  ldet(ip,sign*j)=ldet(ip,sign*(j-1));
346  ln_det_proj_jac(ip,sign*j)=ln_det_proj_jac(ip,sign*(j-1));
347  }
348  */
349  //#endif
350  } // end of the j loop
351  }
352  }
353  //for (int ix=-num_pp;ix<=num_pp;ix++)
354  //{
355  // xdist(ip,ix)=norm(elem_div(gprof(ix),xscale));
356  // }
357  if ( (option_match(ad_comm::argc,ad_comm::argv,"-prsave"))>-1)
358  {
359  adstring profrep_name=(likeprof_params::likeprofptr[ip]->label());
360  ofstream ofs3((char*) (profrep_name+adstring(".pvl")));
361  for (int ix=-num_pp;ix<=num_pp;ix++)
362  {
363  ofs3 << "#Step " << ix << endl;
364  ofs3 << "#num sigmas " << all_num_sigs(ix) << endl;
365  ofs3 << xmax(ix) << endl;
366  }
367  }
368  }
369  #if defined(DO_PROFILE)
370  for (ip=0;ip<likeprof_params::num_likeprof_params;ip++)
371  {
372  ldet(ip)=ldet(ip)-ln_det_proj_jac(ip);
373  }
374  {
375  ofstream ofs("det.tmp");
376  for (ip=0;ip<likeprof_params::num_likeprof_params;ip++)
377  {
378  ofs << "the log dets" << endl;
379  ofs << "ldet" << endl;
380  ofs << ldet(ip) << endl << endl;
381  ofs << "lndet_proj_jac" << endl;
382  ofs << ln_det_proj_jac(ip) << endl << endl;
383  ofs << "ldet-lndet_proj_jac" << endl;
384  ofs << ldet(ip)-ln_det_proj_jac(ip) +ln_det_proj_jac(ip,0)
385  << endl << endl;
386  }
387  }
388  //for (ip=0;ip<likeprof_params::num_likeprof_params;ip++)
389  //{
390  // ldet(ip)=ldet(ip)-(ln_det_proj_jac(ip)-ln_det_proj_jac(ip));
391  //}
392  #endif
393  sigma=likeprof_params::likeprofptr[0]->get_sigma(); // this is the
394  if (sigma==0.0)
395  cerr << "Error standard dev of likeprof parameter is 0" << endl;
396 #ifndef CURVE_CORRECT
397  normalize_posterior_distribution(udet,siglevel,ofs2,num_pp,
398  all_values,actual_value,global_min,offset,lprof,ldet,xdist,
399  penalties);
400 #else
401  normalize_posterior_distribution(udet,siglevel,ofs2,num_pp,
402  all_values,actual_value,global_min,offset,lprof,ldet,xdist,
403  eigenvals,curvcor);
404 #endif
405  }
406 
407 /*
408 ofstream of5("eigv.rpt");
409 void get_ee(const dmatrix& hh, const ofstream& _of5)
410 {
411  ofstream& of5= (ofstream&) _of5;
412  int mmin=hh.rowmin();
413  int mmax=hh.rowmax();
414  dvector l(mmin,mmax);
415  dmatrix tmp(mmin,mmax,1,2);
416  dvector ll(mmin,mmax);
417  dmatrix e=eigenvectors(hh,l);
418  for (int i=mmin;i<=mmax;i++)
419  {
420  ll(i)=e(i)*(hh*e(i));
421  of5 << l(i) << " " << ll(i) << endl;
422  }
423 }
424 */
static likeprof_params * likeprofptr[500]
Definition: admodel.h:2257
void get_onesided_intervals(const dvector &left_bd, const dvector &right_bd, dmatrix &ms, const dvector &xs, const dvector &siglevel, const int &level_index, dvector &xdist, int index)
static void restore_all_values(const dvector &x, const int &ii)
Definition: model3.cpp:105
static adpvm_manager * pvm_manager
Definition: fvar.hpp:8849
void pvm_master_prof_minimize(int iprof, double sigma, double new_value, const double &_fprof, const int underflow_flag, double global_min, const double &_penalties, const double &_final_weight)
void report_onesided_confidence_limits(const ofstream &ofs3, int numsig_levels, dvector &siglevel, const dvector &left_bd, const dvector &right_bd, int ip)
dmatrix trans(const dmatrix &m1)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat2.cpp:13
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
void prof_minimize(int iprof, double sigma, double new_value, const double &fprof, const int underflow_flag, double global_min, const double &penalties, const double &final_weight)
Definition: newmodm2.cpp:221
static char ** argv
Definition: fvar.hpp:8866
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
d3_array elem_div(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements division of a(i, j, k) / b(i, j, k).
Definition: d3arr2a.cpp:112
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
virtual double & get_sigma(void)=0
const int iprint
Definition: fvar.hpp:9504
static adstring adprogram_name
Definition: fvar.hpp:8860
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
void get_confidence_interval(const dvector &left_bd, const dvector &right_bd, dmatrix &ms, const dvector &xs, const dvector &siglevel, const int &level_index, dvector &xdist, int index)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
void likeprof_routine(double global_min)
Definition: mod_pmin.cpp:67
int atoi(adstring &s)
Returns a integer converted from input s.
Definition: atoi.cpp:20
static int nvarcalc()
Definition: model.cpp:152
double mult_factor(int j)
Definition: mod_pmin.cpp:30
prnstream & endl(prnstream &)
static int current_phase
Definition: admodel.h:842
#define abs(x)
Definition: cbivnorm.cpp:186
static int max_number_phases
Definition: admodel.h:841
static int argc
Definition: fvar.hpp:8863
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
void normalize_posterior_distribution(double udet, const dvector &siglevel, const ofstream &ofs2, int num_pp, const dvector &all_values, const dmatrix &actual_value, double global_min, int offset, const dmatrix &lprof, const dmatrix &ldet, const dmatrix &xdist, const dmatrix &penalties)
Definition: newmodmn.cpp:63
Description not yet available.
static void xinit(const dvector &x)
Definition: model.cpp:226
Description not yet available.
Definition: fvar.hpp:2819
int get_stepnumber(void)
Definition: model14.cpp:41
virtual double get_value(void)=0
void prof_minimize_re(int iprof, double sigma, double new_value, const double &fprof, const int underflow_flag, double global_min, const double &penalties, const double &final_weight)
Definition: newmodm5.cpp:17
double unrestricted_hess_determinant(void)
Definition: newmodm4.cpp:10
static void copy_all_values(const dvector &x, const int &ii)
Definition: model3.cpp:9
int option_match(int argc, char *argv[], const char *string)
Checks if the program has been invoked with a particular command line argument (&quot;string&quot;).
Definition: optmatch.cpp:25
void get_particular_grad(int iprof, int nvar, const dvector &fg, const dvector &g)
Definition: newmodm2.cpp:175
void pvm_slave_likeprof_routine(void)
Definition: mod_pmin.cpp:50
virtual const char * label()=0
void report_confidence_limits(const ofstream &ofs3, int numsig_levels, dvector &siglevel, const dvector &left_bd, const dvector &right_bd)
double sign(const double x)
The sign of a number.
Definition: gaussher.cpp:25
static int stddev_scale(const dvector &d, const dvector &x)
Definition: model.cpp:202
Description not yet available.
Definition: fvar.hpp:3727
double trimax(double x, double y, double z)
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat7.cpp:12
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
void pvm_slave_prof_minimize(int underflow_flag)
static void set_inactive_only_random_effects(void)
Definition: model.cpp:259
static int random_effects_flag
Definition: admodel.h:1857
double get_stepsize(void)
Definition: model14.cpp:36
static int num_likeprof_params
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: admodel.h:2259