ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
prmonte.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 inv_cumd_norm(const double& x);
10 double cumd_norm(const double& x);
11 double myran1(long int&);
12 //double better_rand(long int&);
13 
15  const dvector& b1, dmatrix& ch, const double& _wght,double pprobe,
17 {
18  double& wght=(double&) _wght;
19  const double rob1=0.95;
20  const double sqrt_tpi =sqrt(2*PI);
21  dvector w(1,nvar);
22  dvector a(1,nvar);
23  dvector b(1,nvar);
24  dvector alpha(1,nvar);
25  dvector beta(1,nvar);
26  a=a1;
27  b=b1;
28  wght=0;
29  double rwght=0;
30  double nwght=0;
31  w.initialize();
32  double ah;
33  double bl;
34  double upper;
35  double lower;
36  double upper1;
37  double lower1;
38  double diff;
39  double diff1;
40  int expflag;
41  double y;
42  //int in=0;
43  //int ie=0;
44  double _u = rng.better_rand();
45  int rflag;
46  if (_u>rob1)
47  {
48  rflag=1;
49  }
50  else
51  {
52  rflag=0;
53  }
54  if (!rflag)
55  {
56  for (int i=1;i<=nvar;i++)
57  {
58  ah=a(i)/ch(i,i);
59  bl=b(i)/ch(i,i);
60  double u = rng.better_rand();
61  upper=cumd_norm(bl);
62  lower=cumd_norm(ah);
63  diff=upper-lower;
64  if (diff>1.e-5)
65  {
66  expflag=0;
67  }
68  else
69  {
70  expflag=1;
71  }
72  upper1=cumd_cauchy(bl);
73  lower1=cumd_cauchy(ah);
74  diff1=upper1-lower1;
75 
76  u=u*.9998+.0001;
77  if (!expflag)
78  {
79  y = inv_cumd_norm(u*(upper-lower)+lower);
80  }
81  else
82  {
83  y = inv_cumd_cauchy(u*(upper1-lower1)+lower1);
84  }
85  if (diff>1.e-5)
86  {
87  nwght+=-.5*y*y -log(sqrt_tpi*diff);
88  }
89  else
90  {
91  nwght += log_density_cauchy(y)-log(diff1);
92  }
93  for (int j=i;j<=nvar;j++)
94  {
95  double tmp=y*ch(j,i);
96  w(j)+=tmp;
97  a(j)-=tmp;
98  b(j)-=tmp;
99  }
100  }
101  wght = nwght;
102  }
103  else
104  {
105  a=a1;
106  b=b1;
107  for (int i=1;i<=nvar;i++)
108  {
109  ah=a(i)/ch(i,i);
110  bl=b(i)/ch(i,i);
111  double u = rng.better_rand();
112  double pp = rng.better_rand();
113  upper=cumd_norm(bl);
114  lower=cumd_norm(ah);
115  diff=upper-lower;
116  if (diff>1.e-5)
117  {
118  expflag=0;
119  }
120  else
121  {
122  expflag=1;
123  }
124  upper1=cumd_cauchy(bl);
125  lower1=cumd_cauchy(ah);
126  diff1=upper1-lower1;
127 
128  u=u*.9998+.0001;
129  if (!expflag)
130  {
131  if (pp>pprobe)
132  y = inv_cumd_norm(u*(upper-lower)+lower);
133  else
134  y = inv_cumd_cauchy(u*(upper1-lower1)+lower1);
135  }
136  else
137  {
138  y = inv_cumd_cauchy(u*(upper1-lower1)+lower1);
139  }
140  if (diff>1.e-5)
141  {
142  rwght+=log((1.0-pprobe)*exp(-.5*y*y)/(sqrt_tpi*diff)
143  +pprobe*density_cauchy(y)/diff1);
144  }
145  else
146  {
147  rwght += log_density_cauchy(y)-log(diff1);
148  }
149  for (int j=i;j<=nvar;j++)
150  {
151  double tmp=y*ch(j,i);
152  w(j)+=tmp;
153  a(j)-=tmp;
154  b(j)-=tmp;
155  }
156  }
157  double dd=rob1*(exp(nwght-rwght))+1.0-rob1;
158  if (dd<=0)
159  {
160  cerr << "dd <=0" << endl;
161  }
162  wght = log(dd)+rwght;
163  }
164  return w;
165 }
166 
168  const dvector& b1, dmatrix& ch, const double& _wght, const dvector& _y,
169  double pprobe, random_number_generator& rng)
170 {
171  double& wght=(double&) _wght;
172  const double rob1=0.95;
173  const double sqrt_tpi =sqrt(2*PI);
174  dvector w(1,nvar);
175  dvector a(1,nvar);
176  dvector b(1,nvar);
177  dvector alpha(1,nvar);
178  dvector beta(1,nvar);
179  wght=0;
180  double rwght=0;
181  double nwght=0;
182  w.initialize();
183  double ah;
184  double bl;
185  double upper;
186  double lower;
187  double upper1;
188  double lower1;
189  double diff;
190  double diff1;
191  int expflag;
192  double y = 0;
193  //int in=0;
194  //int ie=0;
195 
196  a=a1;
197  b=b1;
198  int i;
199  for (i=1;i<=nvar;i++)
200  {
201  y=_y(i);
202  ah=a(i)/ch(i,i);
203  bl=b(i)/ch(i,i);
204  /*double u = */rng.better_rand();
205  upper=cumd_norm(bl);
206  lower=cumd_norm(ah);
207  diff=upper-lower;
208 /*
209  if (diff>1.e-5)
210  {
211  expflag=0;
212  }
213  else
214  {
215  expflag=1;
216  }
217 */
218  upper1=cumd_cauchy(bl);
219  lower1=cumd_cauchy(ah);
220  diff1=upper1-lower1;
221 
222  if (diff>1.e-5)
223  {
224  nwght+=-.5*y*y -log(sqrt_tpi*diff);
225  }
226  else
227  {
228  nwght+= log_density_cauchy(y)-log(diff1);
229  }
230  for (int j=i;j<=nvar;j++)
231  {
232  double tmp=y*ch(j,i);
233  w(j)+=tmp;
234  a(j)-=tmp;
235  b(j)-=tmp;
236  }
237  }
238  a=a1;
239  b=b1;
240  w.initialize();
241  for (i=1;i<=nvar;i++)
242  {
243  //y=_y(i);
244  ah=a(i)/ch(i,i);
245  bl=b(i)/ch(i,i);
246  double u = rng.better_rand();
247  double pp = rng.better_rand();
248  upper=cumd_norm(bl);
249  lower=cumd_norm(ah);
250  diff=upper-lower;
251  if (diff>1.e-5)
252  {
253  expflag=0;
254  }
255  else
256  {
257  expflag=1;
258  }
259  upper1=cumd_cauchy(bl);
260  lower1=cumd_cauchy(ah);
261  diff1=upper1-lower1;
262 
263  u=u*.9998+.0001;
264  if (!expflag)
265  {
266  if (pp>pprobe)
267  y = inv_cumd_norm(u*(upper-lower)+lower);
268  else
269  y = inv_cumd_cauchy(u*(upper1-lower1)+lower1);
270  }
271  else
272  {
273  y = inv_cumd_cauchy(u*(upper1-lower1)+lower1);
274  }
275  if (diff>1.e-5)
276  {
277  rwght+=log((1.0-pprobe)*exp(-.5*y*y)/(sqrt_tpi*diff)
278  +pprobe*density_cauchy(y)/diff1);
279  }
280  else
281  {
282  rwght += log_density_cauchy(y)-log(diff1);
283  }
284  }
285  wght = log(rob1*(exp(nwght-rwght))+(1.0-rob1))+rwght;
286  for (int j=i;j<=nvar;j++)
287  {
288  double tmp=y*ch(j,i);
289  w(j)+=tmp;
290  a(j)-=tmp;
291  b(j)-=tmp;
292  }
293 }
dvector new_probing_bounded_multivariate_normal(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &wght, double pprobe, random_number_generator &rng)
Definition: prmonte.cpp:14
#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
double density_cauchy(const double &x)
Description not yet available.
Definition: cumd_cau.cpp:29
double better_rand()
Random number generator.
Definition: rngen.cpp:134
double myran1(long int &)
double log_density_cauchy(const double &x)
Description not yet available.
Definition: cumd_cau.cpp:39
Description not yet available.
Definition: fvar.hpp:7951
prnstream & endl(prnstream &)
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
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
void new_probing_bounded_multivariate_normal_mcmc(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &wght, const dvector &_y, double pprobe, random_number_generator &rng)
Definition: prmonte.cpp:167
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
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13