ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mod_mc3.cpp
Go to the documentation of this file.
1 
5 #include <admodel.h>
6 
7 double better_rand(long int&);
8 
10  const double& ll, const dvector& diag)
11 {
12  int ii=1;
13  for (int i=0;i<num_initial_params;i++)
14  {
16  {
17  (varsptr[i])->add_value(y,x,ii,ll,diag);
18  }
19  }
20 }
22 {
23  int ii=1;
24  for (int i=0;i<num_initial_params;i++)
25  {
27  {
28  (varsptr[i])->get_jacobian(y,jac,ii);
29  }
30  }
31 }
32 
33 void multivariate_mixture(const dvector& _mix, int nvar, long int& iseed,
34  const double& _log_density_normal, const double& _log_density_cauchy,
35  const double& _log_density_small_normal, int is)
36 {
37  dvector& mix=(dvector&) _mix;
38  double& log_density_cauchy=(double&) _log_density_cauchy;
39  double& log_density_normal=(double&) _log_density_normal;
40  double& log_density_small_normal=(double&) _log_density_small_normal;
41  const double r2=sqrt(2.0);
42  const double l2p=0.5*log(2*PI);
43  const double l3p=0.5*log(2*PI)-log(3.0);
44  const double pr2=log(PI*r2);
45  log_density_normal=0.0;
46  log_density_cauchy=0.0;
47 
48  if (is==0)
49  {
50  for (int i=1;i<=nvar;i++)
51  {
52  double u;
53  do
54  {
55  u = better_rand(iseed);
56  }
57  while (u<.0001 || u>.9999);
58  mix(i) = inv_cumd_norm(u);
59  log_density_normal-= l2p +.5*mix(i)*mix(i);
60  log_density_small_normal-= l3p +4.5*mix(i)*mix(i);
61  log_density_cauchy+= -pr2 + 1./(1.+mix(i)*mix(i));
62  }
63  }
64  else if (is==2)
65  {
66  for (int i=1;i<=nvar;i++)
67  {
68  double u;
69  do
70  {
71  u = better_rand(iseed);
72  }
73  while (u<.0001 || u>.9999);
74  mix(i) = inv_cumd_norm(u);
75  mix(i)/=3;
76  log_density_normal-= l2p +.5*mix(i)*mix(i);
77  log_density_cauchy+= -pr2 + 1./(1.+mix(i)*mix(i));
78  log_density_small_normal-= l3p +4.5*mix(i)*mix(i);
79  }
80  }
81  else if (is==1)
82  {
83  for (int i=1;i<=nvar;i++)
84  {
85  double u;
86  do
87  {
88  u = better_rand(iseed);
89  }
90  while (u<.0001 || u>.9999);
91  mix(i) = inv_cumd_cauchy(u);
92  log_density_normal-= l2p +.5*mix(i)*mix(i);
93  log_density_small_normal-= l3p +4.5*mix(i)*mix(i);
94  log_density_cauchy+= -pr2 + 1./(1.+mix(i)*mix(i));
95  }
96  }
97  else
98  {
99  for (int i=1;i<=nvar;i++)
100  {
101  double u=0.5;
102  while (u<.0001 || u>.9999);
103  mix(i) = inv_cumd_cauchy(u);
104  log_density_normal-= l2p +.5*mix(i)*mix(i);
105  log_density_small_normal-= l3p +4.5*mix(i)*mix(i);
106  log_density_cauchy+= -pr2 + 1./(1.+mix(i)*mix(i));
107  }
108  }
109 }
110 
111 double set_value_inv_mc(const prevariable& z,double min, double max)
112 {
113  return tan(PI*( (value(z)-min)/(max-min)-0.5 ));
114 }
115 
116 double set_value_inv_mc(double z,double min, double max)
117 {
118  return tan(PI*( (z-min)/(max-min)-0.5 ));
119 }
120 
121 dvariable set_value_mc(const prevariable& z,double min,double max)
122 {
123  const double pinv=1./PI;
124  dvariable y=atan(z)*pinv+0.5;
125  return min+(max-min)*y;
126 }
127 
128 double set_value_mc(double z,double min,double max)
129 {
130  const double pinv=1./PI;
131  double y=atan(z)*pinv+0.5;
132  return min+(max-min)*y;
133 }
134 
135 void set_value_inv_mc(const dvar_vector& x, const dvector& _v, const int& _ii,
136  const double fmin, const double fmax)
137 {
138  dvector& v=(dvector&) _v;
139  int& ii=(int&) _ii;
140  int min=x.indexmin();
141  int max=x.indexmax();
142  double* pvii = v.get_v() + ii;
143  for (int i=min;i<=max;i++)
144  {
145  *pvii = set_value_inv_mc(x(i), fmin, fmax);
146 
147  ++pvii;
148  ++ii;
149  }
150 }
151 
152 void set_value_mc(const dvar_vector& _x,const dvar_vector& v, const int& _ii,
153  const double fmin, const double fmax)
154 {
156  int& ii=(int&) _ii;
157  int min=x.indexmin();
158  int max=x.indexmax();
159  for (int i=min;i<=max;i++)
160  {
161  x(i) = set_value_mc(v(ii), fmin, fmax);
162 
163  ++ii;
164  }
165 }
Base class for dvariable.
Definition: fvar.hpp:1315
virtual void add_value(const dvector &, const int &)=0
d3_array tan(const d3_array &arr3)
Returns d3_array results with computed tan from elements in arr3.
Definition: d3arr2a.cpp:73
static void get_jacobian_value(const dvector &y, const dvector &jac)
Definition: mod_mc3.cpp:21
int withinbound(int lb, int n, int ub)
Definition: model.cpp:45
#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
void fmin(double f, const independent_variables &x, const dvector &g, const int &n, const dvector &w, const dvector &h, const fmm_control &fmc)
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
df1_one_variable atan(const df1_one_variable &x)
Definition: df11fun.cpp:312
ADMB variable vector.
Definition: fvar.hpp:2172
double log_density_cauchy(const double &x)
Description not yet available.
Definition: cumd_cau.cpp:39
static int current_phase
Definition: admodel.h:842
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
#define min(a, b)
Definition: cbivnorm.cpp:188
Description not yet available.
double set_value_inv_mc(double v, double fmin, double fmax)
Definition: mod_mc3.cpp:116
virtual void get_jacobian(const dvector &, const dvector &, const int &)=0
static int num_initial_params
Definition: admodel.h:836
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
int indexmin() const
Definition: fvar.hpp:2287
double inv_cumd_cauchy(const double &x)
Description not yet available.
Definition: cumd_cau.cpp:49
int phase_start
Definition: admodel.h:848
double better_rand(long int &idum)
Description not yet available.
Definition: bet_rand.cpp:18
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
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
double *& get_v(void)
Definition: dvector.h:148
int indexmax() const
Definition: fvar.hpp:2292
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
static adlist_ptr varsptr
Definition: admodel.h:838