ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
newmodmn.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 #include <admodel.h>
8 
9 double polint(double * xa,double * ya,double x);
10 
11 void get_confidence_interval(const dvector& left_bd, const dvector& right_bd,
12  dmatrix& ms, const dvector& xs, const dvector& siglevel,
13  const int& level_index, int index);
14 void get_onesided_intervals(const dvector& left_bd, const dvector& right_bd,
15  dmatrix& ms, const dvector& xs, const dvector& siglevel,
16  const int& level_index, int index);
17 void report_confidence_limits(const ofstream& ofs3,int numsig_levels,
18  const dvector& siglevel, const dvector& left_bd, const dvector& right_bd);
19 void report_onesided_confidence_limits(const ofstream& ofs3,int numsig_levels,
20  const dvector& siglevel, const dvector& left_bd, const dvector& right_bd,
21  int ip);
22 
23 void report_confidence_limits(const ofstream& _ofs3,int numsig_levels,
24  const dvector& siglevel, const dvector& left_bd, const dvector& right_bd)
25 {
26  ofstream& ofs3=(ofstream&) _ofs3;
27  ofs3 << "Minimum width confidence limits:" << endl
28  << " significance level lower bound"
29  << " upper bound" << endl;
30  for (int i=1;i<=numsig_levels;i++)
31  {
32  ofs3 << " " << setw(12) << siglevel(i)
33  << " " << left_bd(i) << " " << right_bd(i) << endl;
34  }
35  ofs3 << endl;
36 }
37 
38 void report_onesided_confidence_limits(const ofstream& _ofs3,int numsig_levels,
39  const dvector& siglevel, const dvector& left_bd, const dvector& right_bd,
40  int ip)
41 {
42  ofstream& ofs3=(ofstream&) _ofs3;
43  int i;
44  for (i=1;i<=numsig_levels;i++)
45  {
46  ofs3 << "The probability is " << setw(7) << siglevel(i) << " that "
48  << " is greater than " << left_bd(i) << endl;
49  }
50  ofs3 << endl;
51  for (i=1;i<=numsig_levels;i++)
52  {
53  ofs3 << "The probability is " << setw(7) << siglevel(i) << " that "
55  << " is less than " << right_bd(i) << endl;
56  }
57  ofs3 << endl;
58 }
59 
60  dvector smooth(const dvector& v);
61 
62 #ifndef CURVE_CORRECT
64  const dvector& siglevel, const ofstream& _ofs2,int num_pp,
65  const dvector& _all_values, const dmatrix& actual_value,double global_min,
66  int offset, const dmatrix& lprof, const dmatrix& ldet, const dmatrix& xdist,
67  const dmatrix& penalties)
68  /*
69  void function_minimizer::normalize_posterior_distribution(double udet,
70  dvector& siglevel, const ofstream& ofs2,int num_pp, const dvector& all_values,
71  dmatrix& actual_value,double global_min,int offset, const dmatrix& lprof,
72  dmatrix& ldet, const dmatrix& xdist, const dmatrix& penalties)
73  */
74  // dmatrix& ldet, const dmatrix& xdist, const dmatrix& penalties,
75  // const dmatrix& lg_jacob)
76 #else
77 
79  const dvector& siglevel, const ofstream& ofs2,int num_pp,
80  const dvector& all_values, const dmatrix& actual_value,
81  double global_min,
82  int offset, const dmatrix& lprof, const dmatrix& ldet,
83  const dmatrix& xdist,
84  const d3_array& eigenvals,d3_array& curvcor)
85 /*
86  void function_minimizer::normalize_posterior_distribution(double udet,
87  const dvector& siglevel, const ofstream& ofs2,int num_pp,
88  const dvector& all_values,
89  const dmatrix& actual_value,double global_min,int offset,
90  const dmatrix& lprof,
91  const dmatrix& ldet, const dmatrix& xdist,
92  const d3_array& eigenvals,d3_array& curvcor)
93 */
94 #endif
95  {
96  dvector& all_values=(dvector&) _all_values;
97  ofstream& ofs2=(ofstream&) _ofs2;
98  ofstream savef("diags");
99  //ofstream ofs3((char*) (ad_comm::adprogram_name + ".plt") );
100  //dvector siglevel("{.90,.95,.975}");
101 #ifndef CURVE_CORRECT
102  dmatrix left_bd(1,3,1,3);
103  dmatrix right_bd(1,3,1,3);
104  dmatrix lower_bd(1,3,1,3);
105  dmatrix upper_bd(1,3,1,3);
106 #else
107  dmatrix left_bd(0,3,1,3);
108  dmatrix right_bd(0,3,1,3);
109  dmatrix lower_bd(0,3,1,3);
110  dmatrix upper_bd(0,3,1,3);
111 #endif
112  double sigma;
113  /*int nvar=*/initial_params::nvarcalc();
114  int numsig_levels=siglevel.indexmax();
115  for (int ip=0;ip<likeprof_params::num_likeprof_params;ip++)
116  {
117  {
118  adstring profrep_name=(likeprof_params::likeprofptr[ip]->label());
119  size_t llen = length(profrep_name);
120  if (llen>8) profrep_name=profrep_name(1,8);
121  ofstream ofs3((char*) (profrep_name+adstring(".plt")));
123  //double diff;
124  ofs2 << likeprof_params::likeprofptr[ip]->label() << ":" << endl;
125  ofs3 << likeprof_params::likeprofptr[ip]->label() << ":" << endl;
126  ofs2 << sigma << endl << global_min << " " << udet << endl;
127  dvector tempint0(-num_pp,num_pp);
128  dvector tempint1(-num_pp,num_pp);
129  dvector tempint2(-num_pp,num_pp);
130  {
131  ofstream ofs("dgs2");
132  ofs << "lprof" << endl << lprof << endl;
133  }
134  for (int j=-num_pp;j<=num_pp;j++) //go in positive and negative directions
135  {
136  all_values(j)=actual_value(ip,j-offset);
137  double lp=lprof(ip,j);
138  #if defined(DO_PROFILE)
139  tempint0(j)= exp(global_min-lp+.5*(-ldet(ip,j)+ldet(ip,0)));
140  #endif
141  tempint1(j)= exp(global_min-lp);
142 
143  tempint2(j)=
144  square((actual_value(ip,j)-actual_value(ip,-offset))/
145  (1.414*sigma));
146  tempint2(j)=exp(-tempint2(j));
147  }
148  dmatrix m(1,3,-num_pp,num_pp);
149  for (int j=-num_pp;j<=num_pp;j++)
150  {
151  #if defined(DO_PROFILE)
152  m(1,j)=tempint0(j)/xdist(ip,j);
153  #endif
154  if (xdist(ip,j)<1.e-50)
155  {
156  cerr << " xdist(" << ip << "," << j << ") too small" << endl;
157  char ch;
158  cin >> ch;
159  m(2,j)=1.e+20;
160  }
161  else
162  {
163  // profile likelihood adjusted for gradient of dep var
164  m(2,j)=tempint1(j)/xdist(ip,j);
165  }
166  //m(2,j)=tempint1(j);
167  }
168  //m(2,num_pp)=tempint1(num_pp)/xdist(ip,num_pp);
169  m(3)=tempint2; //+ 1.e-4*max(tempint2);
170 
171  /*
172  savef << "penalties" << endl << setw(9) << setprecision(4)
173  << penalties(ip) << endl;
174  savef << "normalized exp(lg_jacob)" << endl << setw(9) << setprecision(4)
175  << exp(2.*(lg_jacob(ip)-lg_jacob(ip,0))) << endl;
176  */
177  savef << "tempint1 " << endl << setw(9) << setprecision(3)
178  << tempint1 << endl;
179  #if defined(DO_PROFILE)
180  savef << "m(1) " << endl << setw(9) << setprecision(3) << m(1) << endl;
181  #endif
182  savef << "m(2) " << endl << setw(9) << setprecision(3) << m(2) << endl;
183  savef << "m(3) " << endl << setw(9) << setprecision(3) << m(3) << endl;
184  savef << "xdistance" << endl;
185  savef << xdist(ip) << endl << endl;
186 
187  int min_ind = -num_pp;
188  int max_ind = num_pp;
189 
190  dvector xs(7*min_ind,7*max_ind);
191  dmatrix ms(1,3,7*min_ind,7*max_ind);
192 
193  int kmult=7;
194  for (int k=min_ind;k<=max_ind;k++)
195  {
196  double val=all_values(k);
197  xs(kmult*k)=val;
198  if (k<max_ind)
199  {
200  double diff=all_values(k+1)-all_values(k);
201  for (int i=1;i<kmult;i++)
202  {
203  xs(kmult*k+i)=val+i/double(kmult)*diff;
204  }
205  }
206  }
207  {
208  int mmin=m.rowmin();
209  int mmax=m.rowmax();
210  for (int i=mmin;i<=mmax;i++)
211  {
212  int cmin=m(i).indexmin();
213  int cmax=m(i).indexmax();
214  for (int j=cmin;j<=cmax;j++)
215  {
216  if (m(i,j)<=0.0) m(i,j)=1.e-50;
217  }
218  }
219  }
220  //dmatrix lm=log(m);
221  dmatrix lm=m;
222  int lowlimit=2;
223  #if defined(DO_PROFILE)
224  lowlimit=1;
225  #else
226  lowlimit=2;
227  #endif
228 
229  for (int ii=lowlimit;ii<=3;ii++)
230  {
231  int k;
232  for (k=min_ind;k<=0;k++)
233  {
234  //double * xa=(&all_values(k))-1;
235  //double * ya=(&lm(ii,k))-1;
236  //ms(ii,kmult*k)=exp(lm(ii,k));
237  ms(ii,kmult*k)=lm(ii,k);
238  if (k<max_ind)
239  {
240  double l=lm(ii,k);
241  double u=lm(ii,k+1);
242  for (int i=1;i<kmult;i++)
243  {
244  ms(ii,kmult*k+i)=l+double(i)/kmult*(u-l);
245  //ms(ii,kmult*k+i)=polint(xa,ya,xs(k*kmult+i));
246  }
247  }
248  }
249 
250  for (k=1;k<=max_ind;k++)
251  {
252  //double * xa=(&all_values(k))-2;
253  //double * ya=(&lm(ii,k))-2;
254  //ms(ii,kmult*k)=exp(lm(ii,k));
255  ms(ii,kmult*k)=lm(ii,k);
256  if (k<max_ind)
257  {
258  double l=lm(ii,k);
259  double u=lm(ii,k+1);
260  for (int i=1;i<kmult;i++)
261  {
262  ms(ii,kmult*k+i)=l+double(i)/kmult*(u-l);
263  }
264  }
265  }
266  }
267 
268  /*
269  savef << "ms(2) " << endl << setw(9) << setprecision(3) << ms(2) << endl;
270  savef << "ms(3) " << endl << setw(9) << setprecision(3) << ms(3) << endl;
271  */
272 
273  dvector ssum(1,3);
274  ssum.initialize();
275  for (int j=lowlimit;j<=3;j++)
276  {
277  for (int i=7*min_ind;i<7*max_ind;i++)
278  {
279  if (ms(j,i)<0.0)
280  {
281  ms(j,i)=0.0;
282  }
283  else
284  {
285  ssum(j)+=ms(j,i)*(xs(i+1)-xs(i));
286  }
287  }
288  }
289  for (int j=lowlimit;j<=3;j++)
290  {
291  if (ssum(j) !=0)
292  {
293  /*
294  cout << ms(j) << endl << ssum(j) << endl << endl;
295  char ch;
296  cin >> ch;
297  */
298  ms(j)/=ssum(j);
299  }
300  }
301  // now do the integrals
302  for (int j=lowlimit;j<=3;j++)
303  {
304  int level_index=1;
305  do
306  {
307  get_confidence_interval(left_bd(j),right_bd(j),ms,xs,
308  siglevel,level_index,j);
309  get_onesided_intervals(lower_bd(j),upper_bd(j),ms,xs,
310  siglevel,level_index,j);
311  level_index++;
312  }
313  while (level_index <= numsig_levels);
314  }
315 
316  double min_cutoff = 1.e-3/sigma;
317  int i;
318  for (i=7*min_ind;i<=0;i++)
319  {
320  if (max(ms(2,i),ms(3,i)) > min_cutoff) break;
321  }
322  int new_min_ind = int(max(i,7*min_ind));
323  for (i=0;i<=7*max_ind;i++)
324  {
325  if (max(ms(2,i),ms(3,i)) < min_cutoff) break;
326  }
327  int new_max_ind = min(i,7*max_ind);
328  dmatrix output(1,2,new_min_ind,new_max_ind);
329 
330  output(1)=xs(new_min_ind,new_max_ind);
331 
332  #if defined(DO_PROFILE)
333  output(2)=ms(1)(new_min_ind,new_max_ind);
334  {
335  ofs3 << "Adjusted Profile likelihood" << endl;
336  ofs3 << trans(output) << endl;
337  report_confidence_limits(ofs3,numsig_levels,siglevel,left_bd(1),
338  right_bd(1));
339  ofs3 << "One sided confidence limits for the "
340  "adjusted profile likelihood:" << endl << endl;
341  report_onesided_confidence_limits(ofs3,numsig_levels,siglevel,
342  lower_bd(1),upper_bd(1),ip);
343  }
344  #endif
345 
346  output(2)=ms(2)(new_min_ind,new_max_ind);
347  {
348  ofs3 << "Profile likelihood" << endl;
349  ofs3 << trans(output) << endl;
350  report_confidence_limits(ofs3,numsig_levels,siglevel,left_bd(2),
351  right_bd(2));
352  ofs3 << "One sided confidence limits for the "
353  "profile likelihood:" << endl << endl;
354  report_onesided_confidence_limits(ofs3,numsig_levels,siglevel,
355  lower_bd(2),upper_bd(2),ip);
356  }
357 
358  output(2)=ms(3)(new_min_ind,new_max_ind);
359  {
360  ofs3 << "Normal approximation" << endl;
361  ofs3 << trans(output) << endl;
362  report_confidence_limits(ofs3,numsig_levels,siglevel,left_bd(3),
363  right_bd(3));
364  ofs3 << "One sided confidence limits for the "
365  "Normal approximation:" << endl << endl;
366  report_onesided_confidence_limits(ofs3,numsig_levels,siglevel,
367  lower_bd(3),upper_bd(3),ip);
368  }
369  }
370  }
371  }
372 
373 
375  {
376  int mmin=v.indexmin();
377  int mmax=v.indexmax();
378  int diff=mmax-mmin+1;
379  dvector tmp(mmin,mmax);
380  tmp(mmin)=.8*v(mmin)+.2*v(mmin+1);
381  tmp(mmin+1)=.2*v(mmin)+.6*v(mmin+1)+.2*v(mmin+2);
382  for (int i=mmin+2;i<=mmin+diff/4;i++)
383  {
384  tmp(i)=.1*v(i-2)+.2*v(i-1)+.4*v(i)+.2*v(i+1)+.1*v(i+2);
385  }
386  for (int i=mmin+diff/4+1;i<mmax-diff/4;i++)
387  {
388  tmp(i)=v(i);
389  }
390  for (int i=mmax-diff/4;i<=mmax-2;i++)
391  {
392  tmp(i)=.1*v(i-2)+.2*v(i-1)+.4*v(i)+.2*v(i+1)+.1*v(i+2);
393  }
394  tmp(mmax)=.8*v(mmax)+.2*v(mmax-1);
395  tmp(mmax-1)=.2*v(mmax)+.6*v(mmax-1)+.2*v(mmax-2);
396  return tmp;
397  }
398 
399 double polint(double * xa,double * ya,double x)
400 {
401  double prod1=(xa[1]-xa[2])*(xa[1]-xa[3]);
402  double prod2=(xa[2]-xa[1])*(xa[2]-xa[3]);
403  double prod3=(xa[3]-xa[1])*(xa[3]-xa[2]);
404  if (prod1==0)
405  {
406  double y=ya[1];
407  return y;
408  }
409  if (prod2==0)
410  {
411  double y=ya[2];
412  return y;
413  }
414  if (prod3==0)
415  {
416  double y=ya[2];
417  return y;
418  }
419  double y= (x-xa[2])*(x-xa[3])/prod1*ya[1]
420  +(x-xa[1])*(x-xa[3])/prod2*ya[2]
421  +(x-xa[1])*(x-xa[2])/prod3*ya[3];
422  return y;
423 }
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)
dvector smooth(const dvector &v)
Definition: newmodmn.cpp:374
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
int indexmin() const
Definition: fvar.hpp:2917
void polint(const dvector &xa, const dvar_vector &ya, int n, double x, const dvariable &y, const dvariable &dy)
Definition: dfqromb.cpp:267
void val(const adstring &s, int &v, int &code)
Definition: val.cpp:11
virtual double & get_sigma(void)=0
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.
static int nvarcalc()
Definition: model.cpp:152
prnstream & endl(prnstream &)
int rowmax() const
Definition: fvar.hpp:2929
#define min(a, b)
Definition: cbivnorm.cpp:188
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.
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
Description not yet available.
Definition: fvar.hpp:2819
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
int indexmax() const
Definition: fvar.hpp:2921
const int output
Definition: fvar.hpp:9505
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)
#define max(a, b)
Definition: cbivnorm.cpp:189
Description not yet available.
Definition: fvar.hpp:3727
size_t length(const adstring &t)
Returns the size of adstr.
Definition: string1.cpp:228
int rowmin() const
Definition: fvar.hpp:2925
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16
static int num_likeprof_params
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: admodel.h:2259