ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mod_mc.cpp
Go to the documentation of this file.
1 
8 #include <admodel.h>
9 
10 double inv_cumd_norm(const double& x);
11 double inv_cumd_cauchy(const double& x);
12 double inv_cumd_norms(const double& x);
13 double cumd_norm(const double& x);
14 double cumd_cauchy(const double& x);
15 double density_cauchy(const double& x);
16 double myran1(long int&);
17 double better_rand(long int&);
18 
19 double log_likelihood_mixture(const double& x);
20 
21 void multivariate_mixture(const dvector& _mix,int nvar,long int& iseed,
22  const double& _log_density_normal, const double& _log_density_cauchy,
23  const double& _log_density_small_normal,int is);
24 
25 dvector bounded_multivariate_cauchy(int nvar, const dvector& a1,
26  dvector& b1, const dmatrix& ch,long int& iseed, const double& lprob,
27  double& log_tprob, const int& outflag);
28 
30  dvector& b1, const dmatrix& ch, const dmatrix& ch3, const dmatrix& chinv,
31  const dmatrix& ch3inv, double contaminant,long int& iseed,
32  const double& lprob, const double& lprob3, double& log_tprob,
33  const int& outflag);
34 
36 {
38  if (stddev_params::num_stddev_params==0) return;
39  {
40  int nvar=initial_params::nvarcalc(); // get the number of active parameters
41  dvector x(1,nvar);
42  dvector jac(1,nvar);
45  dvector bmn(1,nvar);
46  bmn.initialize();
47 // ***************************************************************
48  dvector scale(1,nvar); // need to get scale from somewhere
49  dvector diag(1,nvar); // need to get scale from somewhere
50  dvector v(1,nvar); // need to read in v from model.rep
51  dmatrix S(1,nvar,1,nvar);
52  {
53  adstring tmpstring="admodel.cov";
54  if (ad_comm::wd_flag)
55  tmpstring = ad_comm::adprogram_name + ".cov";
56  uistream cif((char*)tmpstring);
57  if (!cif)
58  {
59  cerr << "Error trying to open file " << tmpstring
60  << " for reading" << endl;
61  }
62  int tmp_nvar = 0;
63  cif >> tmp_nvar;
64  if (nvar !=tmp_nvar)
65  {
66  cerr << "Incorrect number of independent variables in file"
67  << tmpstring << endl;
68  ad_exit(1);
69  }
70  cif >> S;
71  if (!cif)
72  {
73  cerr << "error reading covariance matrix from "
74  << tmpstring << endl;
75  ad_exit(1);
76  }
77 
79  /*int check=*/initial_params::stddev_scale(scale,x);
81  {
82  dmatrix tmp(1,nvar,1,nvar);
83 
84  int i;
85  for (i=1;i<=nvar;i++)
86  {
87  tmp(i,i)=S(i,i)*scale(i)*scale(i);
88  for (int j=1;j<i;j++)
89  {
90  tmp(i,j)=S(i,j)*scale(i)*scale(j);
91  tmp(j,i)=tmp(i,j);
92  }
93  diag(i)=sqrt(tmp(i,i));
94  }
95  S=tmp;
96  //cout << endl << S << endl << endl;
97  //cout << endl << choleski_decomp(S) << endl << endl;
98 
99  for (i=1;i<=nvar;i++)
100  {
101  S(i,i)=S(i,i)/(diag(i)*diag(i));
102  for (int j=1;j<i;j++)
103  {
104  S(i,j)=S(i,j)/(diag(i)*diag(j));
105  S(j,i)=S(i,j);
106  }
107  }
108  }
109  }
110  dmatrix CHD= choleski_decomp(S);
111  int sgn;
112  /*double lnd=*/ln_det(S,sgn);
113  //cout << endl << S << endl << endl;
114  //cout << endl << CHD << endl << endl;
115 
116  dmatrix chdec(1,nvar,1,nvar);
117  chdec=choleski_decomp(S);
118  {
119  ofstream ofs("chol2");
120  ofs << chdec << endl;
121  }
122  //cout << endl << chdec << endl << endl;
123 
124  //int check=initial_params::stddev_scale(jac,x);
126  {
127  ofstream ofs("admodel.tmp");
128  dmatrix covar(1,nvar,1,nvar);
129  }
130  double ll=0.0;
131  initial_params::add_random_vector(jac,bmn,ll,diag);
132 /*
133 #if defined(USE_DDOUBLE)
134  double ljac0=sum(log(jac+double(1.e-50)));
135 #else
136  double ljac0=sum(log(jac+1.e-50));
137 #endif
138 */
139 
140  dmatrix ch3=3.*chdec;
141  dmatrix chinv=inv(chdec);
142  dmatrix ch3inv=inv(ch3);
143  //double xxin;
144 
145  {
146  long int iseed=0;
147  int number_sims= 500;
148  //cin >> iseed;
149  iseed=-33517;
150  cout << "Enter value for seed" << endl;
151  cin >> iseed;
152  cout << "Enter number of simulations" << endl;
153  cin >> number_sims;
154  if (iseed>0)
155  {
156  iseed=-iseed;
157  }
158  better_rand(iseed);
159  //double lprob=0.0;
160  //double lprob3=0.0;
161  // get lower and upper bounds
162 
163  //independent_variables x(1,nvar);
164  independent_variables parsave(1,nvar);
165 
166  int ii=1;
169  //ofstream ooff((char*) (ad_comm::adprogram_name + adstring(".mte")) );
170  //ofstream ooff1((char*) (ad_comm::adprogram_name + adstring(".mt2")) );
171  //double cont=0.00;
172  double log_tprob_normal=0.0;
173  double log_tprob_small_normal=0.0;
174  double log_tprob_cauchy=0.0;
175  //double log_tprob=0.0;
176  int ndvar=stddev_params::num_stddev_calc();
177  dvector param_values(1,ndvar);
178  //int outflag;
179  //ooff1 << "Number of simulations = " << number_sims << endl;
180  ii=1;
182 
183  //double fbest;
184 
185 #ifdef USE_ADPVM
187  {
188  switch (ad_comm::pvm_manager->mode)
189  {
190  case 1: // master
191  /*fbest=*/pvm_master_get_monte_carlo_value(nvar,x);
192  break;
193  case 2: // slave
195  break;
196  default:
197  cerr << "error illegal value for pvm_manager->mode" << endl;
198  ad_exit(1);
199  }
200  }
201  else
202 #endif
203  {
204  /*fbest=*/get_monte_carlo_value(nvar,x);
205  }
206 
207  multivariate_mixture(bmn,nvar,iseed,log_tprob_normal,
208  log_tprob_cauchy,log_tprob_small_normal,-1);
209  bmn=elem_div(bmn,scale);
210 /*
211  if (log_tprob_normal >= log_tprob_cauchy)
212  {
213  log_tprob=log_tprob_normal
214  +log(0.95+0.05*exp(log_tprob_cauchy-log_tprob_normal));
215  }
216  else
217  {
218  log_tprob=log_tprob_cauchy
219  +log(0.95*exp(log_tprob_normal-log_tprob_cauchy)+.05);
220  }
221 */
222  //double cdiff=-fbest-log_tprob;
223  //double cfb=-fbest;
224  //double clt=log_tprob;
225  //ooff1 << " * weight likelihood simprob ln det "
226  //" ljac0 ljac parameter value " << endl;
227  //ooff1 << setw(10) << exp(-fbest-log_tprob) << " "
228  //<< setw(10) << exp(-fbest) << " "
229  // << setw(10) << exp(log_tprob) << " "
230  // << setw(10) << lnd << " "
231  // << setw(10) << param_values << endl;
232 
233  dvector y(1,nvar);
234 
235  // what is this supposed to do?
236  //initial_params::get_jacobian_value(y,jac);
237  //for (int i=1;i<=nvar;i++)
238  //{
239  // for (int j=1;j<=nvar;j++)
240  //{
241  // chdec(i,j)/=jac(i);
242  //}
243  //}
244 
245  //ofstream ogs("sims");
246  //ogs << nvar << " " << number_sims << endl;
247  for (int i=1;i<=number_sims;i++)
248  {
249  log_tprob_normal=0.0;
250  log_tprob_small_normal=0.0;
251  log_tprob_cauchy=0.0;
252  //log_tprob=0.0;
253  ii=1;
255 
256  double mixprob=better_rand(iseed);
257  int mixswitch;
258  //if (mixprob<.0)
259  if (mixprob<.05)
260  {
261  mixswitch=1;
262  }
263  else if (mixprob<.50)
264  {
265  mixswitch=2;
266  }
267  else
268  {
269  mixswitch=0;
270  }
271  multivariate_mixture(bmn,nvar,iseed,log_tprob_normal,
272  log_tprob_cauchy,log_tprob_small_normal,mixswitch);
273  //bmn=elem_div(bmn,scale);
274 
275 /*
276  if (log_tprob_normal >= log_tprob_cauchy)
277  {
278  log_tprob=log_tprob_normal
279  +log( 0.50+.45*exp(log_tprob_small_normal-log_tprob_normal)
280  + .05*exp(log_tprob_cauchy-log_tprob_normal));
281  }
282  else
283  {
284  log_tprob=log_tprob_cauchy
285  +log( 0.50*exp(log_tprob_normal-log_tprob_cauchy)
286  + 0.45*exp(log_tprob_small_normal-log_tprob_cauchy)+.05 );
287  }
288 */
289  dvector bmn1=CHD*bmn;
290  //bmn1=elem_div(bmn1,scale);
291 
292  ll=0.0;
293  initial_params::add_random_vector(jac,bmn1,ll,diag);
295 
296  //initial_params::stddev_scale(jac,y);
297 /*
298 #if defined(USE_DDOUBLE)
299  double ljac=sum(log(jac+double(1.e-50)));
300 #else
301  double ljac=sum(log(jac+1.e-50));
302 #endif
303 */
304 
305 
306  // ogs << log_tprob << " " << ll << " " << x << endl;
307 #ifdef USE_ADPVM
309  {
310  switch (ad_comm::pvm_manager->mode)
311  {
312  case 1: // master
314  break;
315  case 2: // slave
317  break;
318  default:
319  cerr << "error illega value for pvm_manager->mode" << endl;
320  ad_exit(1);
321  }
322  }
323  else
324 #endif
325  {
326  get_monte_carlo_value(nvar,x);
327  }
328 
329  //ooff << setw(12) << -f-log_tprob << " "
330  // << setw(12) << -f-log_tprob-ll << " "
331  // << setw(12) << fbest-f << " "
332  // << setw(12) << log_tprob << " "
333  // << setw(5) << outflag << " " << setw(12) << -f;
334  //ooff << endl;
335 
336  ii=1;
337  stddev_params::copy_all_values(param_values,ii);
338  //ooff << " " << setw(6) << " " << param_values << endl;
339  /*
340  if (mixswitch==1)
341  {
342  ooff1 << " * weight likelihood simprob ln det "
343  " ljac0 ljac parameter value " << endl;
344  }
345  else if (mixswitch==2)
346  {
347  ooff1 << " weight likelihood simprob ln det "
348  " ljac0 ljac parameter value " << endl;
349  }
350  else
351  {
352  ooff1 << " + weight likelihood simprob ln det "
353  " ljac0 ljac parameter value " << endl;
354  }
355  ooff1 << setw(10) << exp(-f-log_tprob-cdiff+(ljac0-ljac) ) << " "
356  << setw(10) << -f << " "
357  << setw(10) << log_tprob << " "
358  << setw(10) << lnd << " "
359  << setw(10) << ljac0 << " "
360  << setw(10) << ljac << " "
361  << setw(10) << param_values << endl;
362  */
363  }
365  }
366  }
367 }
static void restore_all_values(const dvector &x, const int &ii)
Definition: model3.cpp:105
static adpvm_manager * pvm_manager
Definition: fvar.hpp:8849
dvector bounded_multivariate_cauchy(int nvar, const dvector &a1, dvector &b1, const dmatrix &ch, long int &iseed, const double &lprob, double &log_tprob, const int &outflag)
Definition: nmonte.cpp:97
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Definition: gradstrc.cpp:641
#define x
Vector of double precision numbers.
Definition: dvector.h:50
double inv_cumd_norm(const double &x)
Description not yet available.
Definition: cumdist.cpp:78
static void add_random_vector(const dvector &x)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: mod_mc1.cpp:7
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
exitptr ad_exit
Definition: gradstrc.cpp:53
double density_cauchy(const double &x)
Description not yet available.
Definition: cumd_cau.cpp:29
double inv_cumd_norms(const double &x)
static adstring adprogram_name
Definition: fvar.hpp:8860
static int num_stddev_calc(void)
Definition: model2.cpp:51
void monte_carlo_routine(void)
Definition: mod_mc.cpp:35
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
Definition: df11fun.cpp:606
static int nvarcalc()
Definition: model.cpp:152
ivector sgn(const dvector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dvect24.cpp:11
double myran1(long int &)
prnstream & endl(prnstream &)
Description not yet available.
Definition: fvar.hpp:1937
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
dvector bounded_robust_multivariate_normal(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const dmatrix &ch3, double contaminant, const double &_wght, random_number_generator &rng)
Definition: monte.cpp:256
Description not yet available.
static void xinit(const dvector &x)
Definition: model.cpp:226
double log_likelihood_mixture(const double &x)
Definition: nmonte.cpp:540
Description not yet available.
Definition: fvar.hpp:2819
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
double pvm_master_get_monte_carlo_value(int nvar, const dvector &x)
Definition: mod_mc2.cpp:36
static void copy_all_values(const dvector &x, const int &ii)
Definition: model3.cpp:9
double ln_det(const dmatrix &m1, int &sgn)
Compute log determinant of a constant matrix.
Definition: dmat3.cpp:536
void pvm_slave_get_monte_carlo_value(int nvar)
Definition: mod_mc2.cpp:59
double inv_cumd_cauchy(const double &x)
Description not yet available.
Definition: cumd_cau.cpp:49
double cumd_cauchy(const double &x)
Description not yet available.
Definition: cumd_cau.cpp:17
double better_rand(long int &idum)
Description not yet available.
Definition: bet_rand.cpp:18
Description not yet available.
Definition: fvar.hpp:3516
static void copy_all_values(const dvector &x, const int &ii)
Definition: model4.cpp:9
void multivariate_mixture(const dvector &_mix, int nvar, long int &iseed, const double &_log_density_normal, const double &_log_density_cauchy, const double &_log_density_small_normal, int is)
Definition: mod_mc3.cpp:33
double get_monte_carlo_value(int nvar, const independent_variables &x)
Definition: mod_mc2.cpp:11
static int stddev_scale(const dvector &d, const dvector &x)
Definition: model.cpp:202
static int mc_phase
Definition: admodel.h:846
double cumd_norm(const double &x)
Culative normal distribution; constant objects.
Definition: cumdist.cpp:90
static unsigned int wd_flag
Definition: fvar.hpp:8864
df1_one_variable inv(const df1_one_variable &x)
Definition: df11fun.cpp:384
static int montecarlo_scale(const dvector &d, const dvector &x)
Description not yet available.
Definition: mc_scale.cpp:15
static int num_stddev_params
Definition: admodel.h:2219