ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
monte.cpp
Go to the documentation of this file.
1 
8 #include <admodel.h>
9 
10 double inv_cumd_norm(const double& x);
11 double cumd_norm(const double& x);
12 double myran1(long int&);
13 //double better_rand(long int&);
14 double ffmax(double a,double b)
15 {
16  if (a>=b) return a;
17  return b;
18 }
19 double ffmin(double a,double b)
20 {
21  if (a<=b) return a;
22  return b;
23 }
24 
25 double inv_cumd_exp(double x)
26 {
27  if (x>.5)
28  {
29  return -log(2.*(x-1.));
30  }
31  else
32  {
33  return -log(2.*x);
34  }
35 }
36 
37 double cumd_exp(double x)
38 {
39  if (x<=0.0)
40  {
41  return 0.5*exp(-x);
42  }
43  else
44  {
45  return 1.0-0.5*exp(-x);
46  }
47 }
48 
50  const dvector& b1, dmatrix& ch, const double& _wght,
52 {
53  double& wght= (double&) _wght;
54  const double sqrt_tpi =sqrt(2*PI);
55  dvector w(1,nvar);
56  //dvector ty(1,nvar);
57  dvector a(1,nvar);
58  dvector b(1,nvar);
59  dvector alpha(1,nvar);
60  dvector beta(1,nvar);
61  a=a1;
62  b=b1;
63  wght=0;
64  w.initialize();
65  int expflag;
66  double y;
67  int in=0;
68  int ie=0;
69 
70  double* pai = a.get_v() + 1;
71  double* pbi = b.get_v() + 1;
72  dvector* pchi = &ch(1);
73  for (int i=1;i<=nvar;++i)
74  {
75  double chii = *(pchi->get_v() + i);
76  double ah = *pai / chii;
77  double bl = *pbi / chii;
78  double u = rng.better_rand();
79  double upper=cumd_norm(bl);
80  double lower=cumd_norm(ah);
81  double diff=upper-lower;
82  if (diff>1.e-5)
83  {
84  wght-=log(diff);
85  expflag=0;
86  }
87  else
88  {
89  upper=cumd_cauchy(bl);
90  lower=cumd_cauchy(ah);
91  diff=upper-lower;
92  wght-=log(diff);
93  expflag=1;
94  }
95 
96  u=u*.9998+.0001;
97  if (!expflag)
98  {
99  y = inv_cumd_norm(u*(upper-lower)+lower);
100  wght -= .5*y*y;
101  ++in;
102  }
103  else
104  {
105  y = inv_cumd_cauchy(u*(upper-lower)+lower);
106  ++ie;
107  wght += log_density_cauchy(y);
108  }
109  //ty(i)=y;
110  double* pwj = w.get_v() + i;
111  double* paj = a.get_v() + i;
112  double* pbj = b.get_v() + i;
113  dvector* pchj = &ch(i);
114  for (int j=i;j<=nvar;++j)
115  {
116  double tmp = y * *(pchj->get_v() + i);
117 
118  *pwj += tmp;
119  *paj -= tmp;
120  *pbj -= tmp;
121 
122  ++pwj;
123  ++paj;
124  ++pbj;
125  ++pchj;
126  }
127 
128  ++pai;
129  ++pbi;
130  ++pchi;
131  }
132  //cout << ty << endl;
133  wght += in*log(1./sqrt_tpi);
134  return w;
135 }
136 
138  const dvector& b1, dmatrix& ch, const double& _wght, double pprobe,
140 {
141  double& wght= (double&) _wght;
142  const double sqrt_tpi =sqrt(2*PI);
143  dvector w(1,nvar);
144  dvector a(1,nvar);
145  dvector b(1,nvar);
146  dvector alpha(1,nvar);
147  dvector beta(1,nvar);
148  a=a1;
149  b=b1;
150  wght=0;
151  w.initialize();
152  double ah;
153  double bl;
154  double upper;
155  double lower;
156  double upper1;
157  double lower1;
158  double diff;
159  double diff1;
160  int expflag;
161  double y;
162  for (int i=1;i<=nvar;i++)
163  {
164  ah=a(i)/ch(i,i);
165  bl=b(i)/ch(i,i);
166  double u = rng.better_rand();
167  double pp = rng.better_rand();
168  upper=cumd_norm(bl);
169  lower=cumd_norm(ah);
170  diff=upper-lower;
171  if (diff>1.e-5)
172  {
173  expflag=0;
174  }
175  else
176  {
177  expflag=1;
178  }
179  upper1=cumd_cauchy(bl);
180  lower1=cumd_cauchy(ah);
181  diff1=upper1-lower1;
182 
183  u=u*.9998+.0001;
184  if (!expflag)
185  {
186  if (pp>pprobe)
187  y = inv_cumd_norm(u*(upper-lower)+lower);
188  else
189  y = inv_cumd_cauchy(u*(upper1-lower1)+lower1);
190  }
191  else
192  {
193  y = inv_cumd_cauchy(u*(upper1-lower1)+lower1);
194  }
195  if (diff>1.e-5)
196  {
197  wght+=log((1.0-pprobe)*exp(-.5*y*y)/(sqrt_tpi*diff)
198  +pprobe*density_cauchy(y)/diff1);
199  }
200  else
201  {
202  wght += log_density_cauchy(y)-log(diff1);
203  }
204 
205  for (int j=i;j<=nvar;j++)
206  {
207  double tmp=y*ch(j,i);
208  w(j)+=tmp;
209  a(j)-=tmp;
210  b(j)-=tmp;
211  }
212  }
213  return w;
214 }
215 
217  const dvector& b1, dmatrix& ch, const double& _wght,
219 {
220  double& wght= (double&) _wght;
221  dvector a(1,nvar);
222  dvector b(1,nvar);
223  dvector w(1,nvar);
224  w.initialize();
225  a=a1;
226  b=b1;
227  wght=0;
228  double ah;
229  double bl;
230  double upper;
231  double lower;
232  double diff;
233  double y;
234  for (int i=1;i<=nvar;i++)
235  {
236  ah=a(i)/ch(i,i);
237  bl=b(i)/ch(i,i);
238  double u = rng.better_rand();
239  lower=ffmax(-1.0,ah);
240  upper=ffmin(1.0,bl);
241  diff=upper-lower;
242  wght-=log(diff);
243 
244  y=lower+u*diff;
245  for (int j=i;j<=nvar;j++)
246  {
247  double tmp=y*ch(j,i);
248  w(j)+=tmp;
249  a(j)-=tmp;
250  b(j)-=tmp;
251  }
252  }
253  return w;
254 }
255 
257  const dvector& b1, dmatrix& ch, const dmatrix& ch3, double contaminant,
258  const double& _wght, random_number_generator & rng)
259 {
260  double& wght= (double&) _wght;
261  dvector w(1,nvar);
262  dvector a(1,nvar);
263  dvector b(1,nvar);
264  dvector alpha(1,nvar);
265  dvector beta(1,nvar);
266  a=a1;
267  b=b1;
268  wght=1;
269  w.initialize();
270  double ah;
271  double bl;
272  double r = rng.better_rand();
273  if (r>contaminant)
274  {
275  for (int i=1;i<=nvar;i++)
276  {
277  ah=a(i)/ch(i,i);
278  bl=b(i)/ch(i,i);
279  double u = rng.better_rand();
280  double upper=cumd_norm(bl);
281  double lower=cumd_norm(ah);
282  wght *= upper-lower;
283  u=u*.9998+.0001;
284  double y = inv_cumd_norm(u*(upper-lower)+lower);
285  for (int j=i;j<=nvar;j++)
286  {
287  double tmp=y*ch(j,i);
288  w(j)+=tmp;
289  a(j)-=tmp;
290  b(j)-=tmp;
291  }
292  }
293  }
294  else
295  {
296  // use the bigger variance normal
297  for (int i=1;i<=nvar;i++)
298  {
299  ah=a(i)/ch3(i,i);
300  bl=b(i)/ch3(i,i);
301  double u = rng.better_rand();
302  double upper=cumd_norm(bl);
303  double lower=cumd_norm(ah);
304  wght *= upper-lower;
305  u=u*.9998+.0001;
306  double y = inv_cumd_norm(u*(upper-lower)+lower);
307  for (int j=i;j<=nvar;j++)
308  {
309  double tmp=y*ch3(j,i);
310  w(j)+=tmp;
311  a(j)-=tmp;
312  b(j)-=tmp;
313  }
314  }
315  }
316  return w;
317 }
dvector bounded_multivariate_uniform(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &lprob, random_number_generator &rng)
Definition: monte.cpp:216
#define x
double cumd_exp(double x)
Definition: monte.cpp:37
Vector of double precision numbers.
Definition: dvector.h:50
double inv_cumd_norm(const double &x)
Description not yet available.
Definition: cumdist.cpp:78
double density_cauchy(const double &x)
Description not yet available.
Definition: cumd_cau.cpp:29
dvector probing_bounded_multivariate_normal(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &lprob, double pprobe, const random_number_generator &rng)
dvector bounded_multivariate_normal(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &_wght, const random_number_generator &rng)
double better_rand()
Random number generator.
Definition: rngen.cpp:134
double ffmax(double a, double b)
Definition: monte.cpp:14
double inv_cumd_exp(double x)
Definition: monte.cpp:25
double myran1(long int &)
double ffmin(double a, double b)
Definition: monte.cpp:19
double log_density_cauchy(const double &x)
Description not yet available.
Definition: cumd_cau.cpp:39
Description not yet available.
Definition: fvar.hpp:7951
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.
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
dvariable beta(const prevariable &a, const prevariable &b)
Beta density function.
Definition: vbeta.cpp:18
Description not yet available.
Definition: fvar.hpp:2819
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
#define w
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
#define PI
Definition: fvar.hpp:95
double cumd_norm(const double &x)
Culative normal distribution; constant objects.
Definition: cumdist.cpp:90
double *& get_v(void)
Definition: dvector.h:148
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13