ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
xmonte2.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 
14 void bounded_multivariate_normal_mcmc(int nvar, const dvector& a1,
15  const dvector& b1, dmatrix& ch, const double& _wght, const dvector& y,
16  const random_number_generator& rng)
17 {
18  double & wght=(double &) _wght;
19  //cout << y << endl;
20  const double sqrt_tpi = sqrt(2*PI);
21  dvector a(1,nvar);
22  dvector b(1,nvar);
23  dvector alpha(1,nvar);
24  dvector beta(1,nvar);
25  a=a1;
26  b=b1;
27  wght=0;
28  int expflag;
29  int in=0;
30  int ie=0;
31  double* pyi = y.get_v() + 1;
32  double* pai = a.get_v() + 1;
33  double* pbi = b.get_v() + 1;
34  dvector* pchi = &ch(1);
35  for (int i=1;i<=nvar;++i)
36  {
37  double chii = *(pchi->get_v() + i);
38  double ah = *pai / chii;
39  double bl = *pbi / chii;
40  double upper=cumd_norm(bl);
41  double lower=cumd_norm(ah);
42  double diff=upper-lower;
43  if (diff>1.e-5)
44  {
45  wght-=log(diff);
46  expflag=0;
47  }
48  else
49  {
50  upper=cumd_cauchy(bl);
51  lower=cumd_cauchy(ah);
52  diff=upper-lower;
53  wght-=log(diff);
54  expflag=1;
55  }
56 
57  if (!expflag)
58  {
59  wght -= 0.5 * *pyi * *pyi;
60  ++in;
61  }
62  else
63  {
64  ++ie;
65  wght += log_density_cauchy(*pyi);
66  }
67 
68  double* paj = a.get_v() + i;
69  double* pbj = b.get_v() + i;
70  dvector* pchj = &ch(i);
71  for (int j=i;j<=nvar;++j)
72  {
73  double tmp = *pyi * *(pchj->get_v() + i);
74  *paj -= tmp;
75  *pbj -= tmp;
76 
77  ++pchj;
78  }
79  ++pyi;
80  ++pai;
81  ++pbi;
82  ++pchi;
83  }
84  wght += in*log(1./sqrt_tpi);
85 }
86 
88  const dvector& b1, dmatrix& ch, const double& _wght, const dvector& y,
89  double pprobe, const random_number_generator& rng)
90 {
91  double & wght=(double &) _wght;
92  //cout << y << endl;
93  const double sqrt_tpi =sqrt(2*PI);
94  dvector a(1,nvar);
95  dvector b(1,nvar);
96  dvector alpha(1,nvar);
97  dvector beta(1,nvar);
98  a=a1;
99  b=b1;
100  wght=0;
101  double ah;
102  double bl;
103  double upper;
104  double lower;
105  double diff;
106  double diff1;
107  //int in=0;
108  //int ie=0;
109  for (int i=1;i<=nvar;i++)
110  {
111  ah=a(i)/ch(i,i);
112  bl=b(i)/ch(i,i);
113  upper=cumd_norm(bl);
114  lower=cumd_norm(ah);
115  diff=upper-lower;
116  upper=cumd_cauchy(bl);
117  lower=cumd_cauchy(ah);
118  diff1=upper-lower;
119  if (diff>1.e-5)
120  {
121  wght+=log((1.0-pprobe)*exp(-.5*y(i)*y(i))/(sqrt_tpi*diff)
122  +pprobe*density_cauchy(y(i))/diff1);
123  }
124  else
125  {
126  wght += log_density_cauchy(y(i))-log(diff1);
127  }
128 
129  for (int j=i;j<=nvar;j++)
130  {
131  double tmp=y(i)*ch(j,i);
132  a(j)-=tmp;
133  b(j)-=tmp;
134  }
135  }
136 }
137 
139  const dvector& b1, dmatrix& ch, const double& _wght, const dvector& y,
140  const random_number_generator& rng)
141 {
142  double& wght=(double&) _wght;
143  dvector a(1,nvar);
144  dvector b(1,nvar);
145  a=a1;
146  b=b1;
147  wght=0;
148  double ah;
149  double bl;
150  double upper;
151  double lower;
152  double diff;
153  for (int i=1;i<=nvar;i++)
154  {
155  ah=a(i)/ch(i,i);
156  bl=b(i)/ch(i,i);
157  lower=ffmax(-1.0,ah);
158  upper=ffmin(1.0,bl);
159  diff=upper-lower;
160  wght-=log(diff);
161  for (int j=i;j<=nvar;j++)
162  {
163  double tmp=y(i)*ch(j,i);
164  a(j)-=tmp;
165  b(j)-=tmp;
166  }
167  }
168 }
void probing_bounded_multivariate_normal_mcmc(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &wght, const dvector &v, double pprobe, const random_number_generator &rng)
Definition: xmonte2.cpp:87
#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 ffmax(double a, double b)
Definition: monte.cpp:14
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
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 bounded_multivariate_uniform_mcmc(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &wght, const dvector &y, const random_number_generator &rng)
Definition: xmonte2.cpp:138
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
void bounded_multivariate_normal_mcmc(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &wght, const dvector &y, const random_number_generator &rng)
Definition: xmonte2.cpp:14
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13