ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2f15.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  */
11 #include <df1b2fun.h>
12 
17 void df1b2_init_vector::allocate(int mmin,int mmax,const adstring&s)
18 {
19  allocate(mmin,mmax,1,s);
20 }
21 
26 void df1b2_init_vector::allocate(int mmin,int mmax,int ps,const adstring&s)
27 {
28  set_phase_start(ps);
29  df1b2vector::allocate(mmin,mmax);
30 }
31 
37 {
38  if (num_initial_df1b2params>=1000)
39  {
40  cerr << " This version of ADMB only supports 1000 initial df1b2parameter"
41  " objects" << endl;
42  ad_exit(1);
43  }
45 }
46 
52  const df1b2variable& _pen)
53 {
55  //df1b2variable::nvar=global_nvar;
56  pen=0.0;
57  df1b2variable pen1=0.0;
58  int ii=1;
59  for (int i=0;i<num_initial_df1b2params;i++)
60  {
62  {
63  (varsptr[i])->set_value(x,ii,pen1);
64  }
65  }
66  pen+=pen1;
67 }
68 
69 void set_value(const df1b2vector& _x,const init_df1b2vector& _v, const int& _ii,
70  double fmin,double fmax,const df1b2variable& fpen,double s);
76  {
77  return scalefactor;
78  }
79 
86 {
87  scalefactor=sf;
88 }
89 
91 {
92  int ii=1;
93  for (int i=0;i<num_initial_df1b2params;i++)
94  {
96  {
97  (varsptr[i])->set_index(1,ii);
98  }
99  else
100  {
101  (varsptr[i])->set_index(0,ii);
102  }
103  }
104  return ii-1;
105 }
106 
112  const int& _ii,const df1b2variable& pen)
113 {
115  ADUNCONST(int,ii)
116 
117  int mmin=indexmin();
118  int mmax=indexmax();
119  if (scalefactor==0.0)
120  {
121  for (int i=mmin;i<=mmax;i++)
122  {
123  (*this)(i) = (x(ii++));
124  }
125  }
126  else
127  {
128  for (int i=mmin;i<=mmax;i++)
129  {
130  (*this)(i) = (x(ii++)/scalefactor);
131  }
132  }
133 }
134 
139 void df1b2_init_vector::set_index(int aflag,const int& _ii)
140 {
141  ADUNCONST(int,ii)
142 
143  int mmin=indexmin();
144  int mmax=indexmax();
145  if (aflag)
146  {
147  for (int i=mmin;i<=mmax;i++)
148  {
149  (*this)(i).get_ind_index() = ii++;
150  }
151  }
152  else
153  {
154  for (int i=mmin;i<=mmax;i++)
155  {
156  (*this)(i).get_ind_index() = -1;
157  }
158  }
159 }
160 
165 void df1b2_init_number::set_index(int aflag,const int& _ii)
166 {
167  ADUNCONST(int,ii)
168  if (aflag)
169  {
170  get_ind_index()=ii++;
171  }
172  else
173  {
174  get_ind_index()=-1;
175  }
176 }
177 
182 void df1b2_init_matrix::set_index(int aflag,const int& _ii)
183 {
184  ADUNCONST(int,ii)
185 
186  int rmin=indexmin();
187  int rmax=indexmax();
188  if (aflag)
189  {
190  for (int i=rmin;i<=rmax;i++)
191  {
192  int cmin=(*this)(i).indexmin();
193  int cmax=(*this)(i).indexmax();
194  {
195  for (int j=cmin;j<=cmax;j++)
196  {
197  (*this)(i,j).get_ind_index() = ii++;
198  }
199  }
200  }
201  }
202  else
203  {
204  for (int i=rmin;i<=rmax;i++)
205  {
206  int cmin=(*this)(i).indexmin();
207  int cmax=(*this)(i).indexmax();
208  {
209  for (int j=cmin;j<=cmax;j++)
210  {
211  (*this)(i,j).get_ind_index() = -1;
212  }
213  }
214  }
215  }
216 }
217 
223  const int& _ii,const df1b2variable& pen)
224 {
226  ADUNCONST(int,ii)
227 
228  int rmin=indexmin();
229  int rmax=indexmax();
230  for (int i=rmin;i<=rmax;i++)
231  {
232  int cmin=(*this)(i).indexmin();
233  int cmax=(*this)(i).indexmax();
234  {
235  for (int j=cmin;j<=cmax;j++)
236  {
237  (*this)(i,j) = (x(ii++));
238  }
239  }
240  }
241 }
242 
247 void df1b2_init_number::set_value(const init_df1b2vector& _x,const int& _ii,
248  const df1b2variable& pen)
249 {
251  ADUNCONST(int,ii)
252  if (scalefactor==0.0)
253  operator = (x(ii++));
254  else
255  operator = (x(ii++)/scalefactor);
256 }
257 
263 {
265  {
267  }
268  set_phase_start(1);
269 }
270 
276 {
278  set_phase_start(_n);
279 }
280 
285 void df1b2variable::allocate(const char *s)
286 {
288 }
289 
295 {
296  //cout << "Here" << endl;
297 }
298 
304 {
306 }
307 
312 void df1b2_init_bounded_number::allocate(double _minb,double _maxb,
313  int _n,const char * s)
314 {
315  minb=_minb;
316  maxb=_maxb;
318 
319  set_phase_start(_n);
320 }
321 
327  double _maxb,const char * s)
328 {
329  minb=_minb;
330  maxb=_maxb;
332  set_phase_start(1);
333 }
334 
340  const int& _ii,const df1b2variable& pen)
341 {
343  ADUNCONST(int,ii)
344  if (scalefactor==0.0)
345  ::set_value(*this,x,ii,minb,maxb,pen);
346  else
347  ::set_value(*this,x,ii,minb,maxb,pen,scalefactor);
348 }
349 
354 void set_value(const df1b2variable& _u,const init_df1b2vector& _x,
355  const int& _ii,double fmin,double fmax,const df1b2variable& _fpen)
356 {
358  ADUNCONST(int,ii)
362  {
363  u=boundp(x(ii++),fmin,fmax,fpen);
364  }
365  else
366  {
367  u=x(ii);
368  value(u)=boundp(value(x(ii++)),fmin,fmax);
369  double diff=fmax-fmin;
370  //t=fmin + diff*ss;
371  df1b2variable ss=(u-fmin)/diff;
372 # ifdef USE_BARD_PEN
373  const double l4=log(4.0);
374  double pen=.000001/diff;
375  fpen-=pen*(log(ss+1.e-40)+log((1.0-ss)+1.e-40)+l4);
376 # else
377  XXXX
378 # endif
379  }
380 }
381 
382 void set_value(const df1b2variable& _u,const init_df1b2vector& _x,
383  const int& _ii,double fmin,double fmax,const df1b2variable& _fpen,
384  double s)
385 {
387  ADUNCONST(int,ii)
391  {
392  u=boundp(x(ii++),fmin,fmax,fpen,s);
393  }
394  else
395  {
396  u=x(ii);
397  value(u)=boundp(value(x(ii++)),fmin,fmax,s);
398  double diff=fmax-fmin;
399  //t=fmin + diff*ss;
400  df1b2variable ss=(u-fmin)/diff;
401 # ifdef USE_BARD_PEN
402  const double l4=log(4.0);
403  double pen=.000001/diff;
404  fpen-=pen*(log(ss+1.e-40)+log((1.0-ss)+1.e-40)+l4);
405 # else
406  XXXX
407 # endif
408  }
409 }
410 
418 df1b2variable boundp(const df1b2variable& x, double fmin, double fmax,
419  const df1b2variable& _fpen)
420 {
422  df1b2variable t;
423  //df1b2variable y;
424  //y=x;
425  double diff=fmax-fmin;
426  const double l4=log(4.0);
427  df1b2variable ss=0.49999999999999999*sin(x*1.57079632679489661)+0.50;
428  t=fmin + diff*ss;
429 
430 #ifdef USE_BARD_PEN
431  double pen=.000001/diff;
432  fpen-=pen*(log(ss+1.e-40)+log((1.0-ss)+1.e-40)+l4);
433 #else
434  if (x < -.9999)
435  {
436  fpen+=cube(-0.9999-x);
437  if (x < -1.)
438  {
439  fpen+=1.e+6*cube(-1.0-x);
440  if (x < -1.02)
441  {
442  fpen+=1.e+10*cube(-1.02-x);
443  }
444  }
445  }
446  if (x > 0.9999)
447  {
448  fpen+=cube(x-0.9999);
449  if (x > 1.)
450  {
451  fpen+=1.e+6*cube(x-1.);
452  if (x > 1.02)
453  {
454  fpen+=1.e+10*cube(x-1.02);
455  }
456  }
457  }
458 #endif
459  return(t);
460 }
461 
469 df1b2variable boundp(const df1b2variable& _x, double fmin, double fmax,
470  const df1b2variable& _fpen,double s)
471 {
473  df1b2variable t;
474  df1b2variable x=_x/s;
475  //df1b2variable y;
476  //y=x;
477  double diff=fmax-fmin;
478  const double l4=log(4.0);
479 
480  // ss is underlying varialbe on [0,1] and t lives in [fmin,fmax]
481  df1b2variable ss=0.49999999999999999*sin(x*1.57079632679489661)+0.50;
482  t=fmin + diff*ss;
483 
484  // Add penalty
485 #ifdef USE_BARD_PEN
486  double pen=.000001/diff;
487  fpen-=pen*(log(ss+1.e-40)+log((1.0-ss)+1.e-40)+l4);
488 #else
489  if (x < -.9999)
490  {
491  fpen+=cube(-0.9999-x);
492  if (x < -1.)
493  {
494  fpen+=1.e+6*cube(-1.0-x);
495  if (x < -1.02)
496  {
497  fpen+=1.e+10*cube(-1.02-x);
498  }
499  }
500  }
501  if (x > 0.9999)
502  {
503  fpen+=cube(x-0.9999);
504  if (x > 1.)
505  {
506  fpen+=1.e+6*cube(x-1.);
507  if (x > 1.02)
508  {
509  fpen+=1.e+10*cube(x-1.02);
510  }
511  }
512  }
513 #endif
514  return(t);
515 }
516 
521 df1b2variable boundp(const df1b2variable& x, double fmin, double fmax)
522 {
523  df1b2variable t;
524  //df1b2variable y;
525  //y=x;
526  double diff=fmax-fmin;
527  df1b2variable ss=0.49999999999999999*sin(x*1.57079632679489661)+0.50;
528  t=fmin + diff*ss;
529 
530  return(t);
531 }
532 
537 void set_value_mc(const df1b2vector& _x,const init_df1b2vector& _v,
538  const int& _ii, double fmin,double fmax)
539 {
540  ADUNCONST(int,ii)
543  int min=x.indexmin();
544  int max=x.indexmax();
545  for (int i=min;i<=max;i++)
546  {
547  //x(i)=boundp(v(ii++),fmin,fmax,fpen);
548  const double pinv=1./PI;
549  df1b2variable y=atan(v(ii++))*pinv+0.5;
550  x(i)=fmin+(fmax-fmin)*y;
551  }
552 }
553 
558 void set_value(const df1b2vector& _x,const init_df1b2vector& _v,
559  const int& _ii, double fmin,double fmax,const df1b2variable& fpen)
560 {
561  ADUNCONST(int,ii)
564  int min=x.indexmin();
565  int max=x.indexmax();
566  for (int i=min;i<=max;i++)
567  {
568  x(i)=boundp(v(ii++),fmin,fmax,fpen);
569  //cout << setprecision(15) << fpen << " " << fmin << " " << fmax
570  // << " " << v(ii-1) << " " << x(i) << endl;
571  }
572 }
573 void set_value(const df1b2vector& _x,const init_df1b2vector& _v,
574  const int& _ii, double fmin,double fmax,const df1b2variable& fpen,double s)
575 {
576  ADUNCONST(int,ii)
579  int min=x.indexmin();
580  int max=x.indexmax();
581  for (int i=min;i<=max;i++)
582  {
583  x(i)=boundp(v(ii++),fmin,fmax,fpen,s);
584  //cout << setprecision(15) << fpen << " " << fmin << " " << fmax
585  // << " " << v(ii-1) << " " << x(i) << endl;
586  }
587 }
588 
594  const int& ii,const df1b2variable& pen)
595 {
597 
598  if (scalefactor==0.0)
599  {
601  {
602  ::set_value_mc(*this,x,ii,minb,maxb);
603  }
604  else
605  {
606  ::set_value(*this,x,ii,minb,maxb,pen);
607  }
608  }
609  else
610  {
612  {
613  // should this have scalefactor ??
614  //::set_value_mc(*this,x,ii,minb,maxb,scalefactor);
615  ::set_value_mc(*this,x,ii,minb,maxb);
616  }
617  else
618  {
619  ::set_value(*this,x,ii,minb,maxb,pen,scalefactor);
620  //::set_value(*this,x,ii,minb,maxb,pen);
621  }
622  }
623 }
624 
630 {
632 }
633 
639  const int& _ii)
640 {
642 }
643 
649  const int& _ii,const df1b2variable& _pen)
650 {
651  ADUNCONST(int,ii)
654  int mmin=indexmin();
655  int mmax=indexmax();
656  for (int i=mmin;i<=mmax;i++)
657  {
660  {
661  y = (boundp(x(ii++),getminb(),getmaxb(),pen));
662  }
663  else
664  {
665  y = (x(ii++));
666  *y.get_u()=boundp(*y.get_u(),getminb(),getmaxb());
667  double diff=getmaxb()-getminb();
668  df1b2variable ss=(y-getminb())/diff;
669 # ifdef USE_BARD_PEN
670  const double l4=log(4.0);
671  double wght=.000001/diff;
672  pen-=wght*(log(ss+1.e-40)+log((1.0-ss)+1.e-40)+l4);
673 # else
674  XXXX
675 # endif
676  }
677  }
678 }
679 
685  const int& ii,const df1b2variable& _pen)
686 {
689  df1b2variable m=mean(*this);
690  pen+=1000.0*square(m);
691 }
692 
697 void df1b2_init_bounded_vector::allocate(int mmin,int mmax,
698  double _minb,double _maxb)
699 {
700  minb=_minb;
701  maxb=_maxb;
702  df1b2_init_vector::allocate(mmin,mmax);
703 }
704 
709 void df1b2_init_bounded_vector::allocate(int mmin,int mmax,
710  double _minb,double _maxb,const char * s)
711 {
712  allocate(mmin,mmax,_minb,_maxb);
713 }
714 
719 void df1b2_init_bounded_vector::allocate(int mmin,int mmax,
720  double _minb,double _maxb,int n,const char * s)
721 {
722  allocate(mmin,mmax,_minb,_maxb);
723  set_phase_start(n);
724 }
725 
731 {
732  if (x.current_phase >= x.phase_start)
733  return 1;
734  else
735  return 0;
736 }
737 
743 {
744  if (x.current_phase >= x.phase_start)
745  return 1;
746  else
747  return 0;
748 }
749 
755 {
756  if (x.current_phase >= x.phase_start)
757  return 1;
758  else
759  return 0;
760 }
761 
767  (const init_df1b2vector& _x,const int& _ii,const df1b2variable& _pen)
768 {
769  ADUNCONST(int,ii)
772  df1b2variable& y=pv->df1b2vector::operator()(i);
774  {
775  // df1b2variable& tmp = boundp(x(ii++),b.getminb(),b.getmaxb(),pen);
776  // df1b2variable::operator = (tmp);
777  //df1b2variable::operator =
778  y = (boundp(x(ii++),pv->getminb(),pv->getmaxb(),pen));
779  }
780  else
781  {
782  y = (x(ii++));
783  *y.get_u()=boundp(*y.get_u(),pv->getminb(),pv->getmaxb());
784  double diff=pv->getmaxb()-pv->getminb();
785  df1b2variable ss=(y-pv->getminb())/diff;
786 # ifdef USE_BARD_PEN
787  const double l4=log(4.0);
788  double wght=.000001/diff;
789  pen-=wght*(log(ss+1.e-40)+log((1.0-ss)+1.e-40)+l4);
790 # else
791  XXXX
792 # endif
793  }
794 }
static int straight_through_flag
Definition: admodel.h:839
static void reset(const init_df1b2vector &, const df1b2variable &)
Description not yet available.
Definition: df1b2f15.cpp:51
void allocate(void)
Description not yet available.
Definition: df1b2fn5.cpp:126
virtual void add_to_list(void)
Description not yet available.
Definition: df1b2f15.cpp:36
int withinbound(int lb, int n, int ub)
Definition: model.cpp:45
df1b2variable & operator=(const df3_one_variable &v)
Definition: df3fun.cpp:779
double * get_u() const
Definition: df1b2fun.h:228
#define x
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin(void) const
Definition: df1b2fun.h:1054
Description not yet available.
Definition: df1b2fun.h:1401
void fmin(double f, const independent_variables &x, const dvector &g, const int &n, const dvector &w, const dvector &h, const fmm_control &fmc)
Description not yet available.
Definition: df1b2fun.h:1420
static int noallocate
Definition: df1b2fun.h:294
Description not yet available.
Definition: df1b2fun.h:953
df1_one_variable atan(const df1_one_variable &x)
Definition: df11fun.cpp:312
d3_array sin(const d3_array &arr3)
Returns d3_array results with computed sin from elements in arr3.
Definition: d3arr2a.cpp:43
void operator=(const df1b2variable &_x)
Description not yet available.
Definition: df1b2f15.cpp:303
df1b2_init_number()
Description not yet available.
Definition: df1b2f15.cpp:294
exitptr ad_exit
Definition: gradstrc.cpp:53
int indexmin(void) const
Definition: df1b2fun.h:969
d3_array cube(const d3_array &m)
Description not yet available.
Definition: d3arr5.cpp:17
static initial_df1b2params ** varsptr
Definition: df1b2fun.h:1367
df1b2variable & operator()(int i) const
Definition: df1b2fun.h:994
double mean(const dvector &vec)
Returns computed mean of vec.
Definition: cranfill.cpp:43
Description not yet available.
Definition: df1b2fun.h:266
static int current_phase
Definition: df1b2fun.h:1350
static int num_initial_df1b2params
Definition: df1b2fun.h:1369
virtual void set_value(const dvector &x, const int &_ii)
Description not yet available.
Definition: df1b2f18.cpp:125
prnstream & endl(prnstream &)
void set_scalefactor(const double)
Set scale factor for parameter in RE model.
Definition: df1b2f15.cpp:85
#define min(a, b)
Definition: cbivnorm.cpp:188
virtual void set_value(const init_df1b2vector &x, const int &_ii, const df1b2variable &pen)
Description not yet available.
Definition: df1b2f15.cpp:648
double get_scalefactor()
Description not yet available.
Definition: df1b2f15.cpp:75
void set_value(const dvar_matrix &x, const dvar_vector &v, const int &_ii, double s)
Description not yet available.
Definition: set.cpp:235
void set_phase_start(int n)
Definition: df1b2fun.h:1475
void set_value_mc(const dvar_vector &x, const dvar_vector &v, const int &ii, const double fmin, const double fmax)
Definition: mod_mc3.cpp:152
Description not yet available.
Definition: df1b2fun.h:373
re_df1b2_init_bounded_vector(void)
Description not yet available.
Definition: df1b2f15.cpp:629
virtual void set_value(const init_df1b2vector &x, const int &_ii, const df1b2variable &pen)
Description not yet available.
Definition: df1b2f15.cpp:767
static int have_bounded_random_effects
Definition: df1b2fun.h:1353
int active(const initial_params &ip)
Definition: model.cpp:437
void set_phase_start(int n)
Definition: df1b2fun.h:1407
int indexmax(void) const
Definition: df1b2fun.h:970
virtual int & get_ind_index(void)
Definition: df1b2fun.h:1474
Description not yet available.
Definition: df1b2fun.h:1469
int indexmax(void) const
Definition: df1b2fun.h:1055
dvariable boundp(const prevariable &x, double fmin, double fmax, const prevariable &_fpen, double s)
Compute penalty for exceeding bounds on parameter; variable ojbects.
Definition: boundfun.cpp:89
static int set_index(void)
Definition: df1b2f15.cpp:90
Description not yet available.
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
#define PI
Definition: fvar.hpp:95
#define max(a, b)
Definition: cbivnorm.cpp:189
static int mc_phase
Definition: admodel.h:846
virtual void set_value(const dvector &x, const int &_ii)
Description not yet available.
Definition: df1b2f18.cpp:46
virtual void set_value(const dvector &x, const int &_ii)
Description not yet available.
Definition: df1b2f18.cpp:136
virtual void set_value(const dvector &x, const int &_ii)
Description not yet available.
Definition: df1b2f18.cpp:30
void allocate(void)
Initialize df1b2vector to empty.
Definition: f1b2vc1.cpp:627
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
virtual void set_value(const dvector &x, const int &_ii)
Description not yet available.
Definition: df1b2f18.cpp:84
virtual void set_value(const dvector &x, const int &_ii)
Description not yet available.
Definition: df1b2f18.cpp:148
virtual void set_value(const dvector &, const int &ii)=0