ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
statsLib.h
Go to the documentation of this file.
1 #ifndef _STATSLIB_H_
2 #define _STATSLIB_H_
3 
4 #include <fvar.hpp>
5 #include <admodel.h>
6 #include <df1b2fun.h>
7 #include <adrndeff.h>
8 
9 
10 
11 dvar_matrix ageLengthKey( const dvar_vector& mu, const dvar_vector& sig, const dvector& x );
12 dmatrix ageLengthKey( const dvector& mu, const dvector& sig, const dvector& x );
14 
15 void fill( const dmatrix& _d,const dvector& _v );
16 void fill( const dvar_matrix& _d,const dvar_vector& _v );
17 
18 double get_ft(const double& ct, const double& m, const dvector& va, const dvector& ba );
19 dvector get_ft( dvector& ct,const double& m, const dmatrix& V,const dvector& ba );
20 dvector get_ft( dvector& ct,const double& m, const dmatrix& V,const dvector& na, const dvector& wa );
21 
22 dvector pearson_residuals(long m, dvector obs_p, dvector pred_p);
23 
24 // DISTRIBUTION FUNCTIONS
25 
26 // Uniform distribution
27 dvariable dunif( const dvariable& x, const double min, const double max );
28 
29 // Student-t Distribution
30 dvariable dstudent_t( const dvar_vector& residual, const dvar_vector& df);
31 
32 // Inverse gamma distribution
33 dvariable dinvgamma( const dvariable& x, const double a, const double b );
34 
35 // Binomial distribution
36 dvariable dbinom( const prevariable& x,const double& n,const double& p );
37 dvariable dbinom( const prevariable& x,const prevariable& n,const double& p );
38 dvariable dbinom( const prevariable& x,const prevariable& n,const prevariable& p );
39 dvariable dbinom(const dvar_vector& x,const dvar_vector& n,const dvar_vector& p);
40 dvariable dbinom(const prevariable& x,const dvar_vector& n,const dvar_vector& p);
41 dvariable dbinom(const dvar_vector& x,const prevariable& n,const dvar_vector& p);
42 dvariable dbinom(const dvar_vector& x,const dvar_vector& n,const prevariable& p);
43 df1b2variable dbinom( const df1b2variable& x,const double& n,const double& p );
44 df1b2variable dbinom( const df1b2variable& x,const df1b2variable& n,const double& p );
46 df1b2variable dbinom(const df1b2vector& x,const df1b2vector& n,const df1b2vector& p);
47 df1b2variable dbinom(const df1b2variable& x,const df1b2vector& n,const df1b2vector& p);
48 df1b2variable dbinom(const df1b2vector& x,const df1b2variable& n,const df1b2vector& p);
49 df1b2variable dbinom(const df1b2vector& x,const df1b2vector& n,const df1b2variable& p);
50 
51 // Negative binomial distribution
52 dvariable dnbinom(const double& x,const prevariable& mu, const prevariable& k);
53 df1b2variable dnbinom(const double& x, const df1b2variable& mu, const df1b2variable& k);
54 df1b2variable dnbinom(const dvector& x, const df1b2vector& mu, const df1b2variable& k);
55 df1b2variable dnbinom(const dvector& x, const df1b2vector& mu, const df1b2vector& k);
56 dvariable dnbinom(const dvector& x, const dvar_vector& mu, const prevariable& k);
57 dvariable dnbinom(const dvector& x, const dvar_vector& mu, const dvar_vector& k);
58 
59 // Negative binomial distribution (other parametarization)
60 dvariable dnbinom_tau(const double& x,const prevariable& mu, const prevariable& tau);
61 df1b2variable dnbinom_tau(const double& x, const df1b2variable& mu, const df1b2variable& tau);
62 df1b2variable dnbinom_tau(const dvector& x, const df1b2vector& mu, const df1b2variable& tau);
63 df1b2variable dnbinom_tau(const dvector& x, const df1b2vector& mu, const df1b2vector& tau);
64 dvariable dnbinom_tau(const dvector& x, const dvar_vector& mu, const prevariable& tau);
65 dvariable dnbinom_tau(const dvector& x, const dvar_vector& mu, const dvar_vector& tau);
66 
67 // Zero Inflated Negative binomial distribution
68 df1b2variable dzinbinom(const double& x, const df1b2variable& mu, const df1b2variable& k, const df1b2variable& p);
69 dvariable dzinbinom(const double& x, const prevariable& mu, const prevariable& k, const prevariable& p);
70 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2variable& k, const df1b2variable& p);
71 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2vector& k, const df1b2variable& p);
72 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const prevariable& k, const prevariable& p);
73 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const dvar_vector& k, const prevariable& p);
74 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2variable& k, const df1b2vector& p);
75 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2vector& k, const df1b2vector& p);
76 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const prevariable& k, const dvar_vector& p);
77 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const dvar_vector& k, const dvar_vector& p);
78 
79 // Gamma distribution
80 dvariable dgamma( const dvariable &x, const double& a, const double& b );
81 dvariable dgamma( const dvariable &x, const dvariable& a, const dvariable &b );
82 dvariable dgamma( const dvector &x, const prevariable& a, const prevariable& b );
83 dvariable dgamma( const dvector &x, const prevariable& a, const dvar_vector& b );
84 dvariable dgamma( const dvector &x, const dvar_vector& a, const dvar_vector& b );
85 dvariable dgamma( const dvector &x, const dvar_vector& a, const prevariable& b );
86 
87 // Normal distribution
88 dvariable dnorm( const prevariable& x, const double& mu, const double& std );
89 dvariable dnorm( const dvar_vector& x, const double& mu, const double& std );
90 dvariable dnorm( const prevariable& x, const double& mu, const double& std, bool bLog );
91 dvariable dnorm( const dvector& x, const prevariable& mu, const prevariable& std );
92 dvariable dnorm( const dvector& x, const prevariable& mu, const prevariable& std, bool dLog );
93 dvariable dnorm( const dvar_vector& residual, const prevariable& std );
94 dvariable dnorm( const dvar_vector& residual, const double& std );
95 dvariable dnorm( const dvar_vector& residual, const dvector& std );
96 dvariable dnorm( const dvar_vector& residual, const dvar_vector std );
97 dvariable dnorm( const dvar_vector& residual );
98 dvariable dnorm( const dmatrix& obs, const dvar_matrix& pred );
99 df1b2variable dnorm( const df1b2variable& x, const double& mu, const double& std );
100 df1b2variable dnorm( const df1b2variable& x, const double& mu, const double& std, bool bLog );
101 df1b2variable dnorm( const df1b2variable& x, const df1b2variable& mu, const df1b2variable& std, bool bLog );
102 df1b2variable dnorm( const df1b2vector& x, const df1b2variable& mu, const df1b2variable& std, bool dLog );
103 
104 
105 // Plogis distribution
106 #ifndef LOGISTIC_HPP
107 #define LOGISTIC_HPP
108 
109 template <typename T>
111 
112 template<>
113 class plogisTraits<double> {
114 public:
115  typedef dvector plogisT;
116 };
117 
118 template<>
120 public:
122 };
123 
124 template<>
126 public:
128 };
129 
130 
131 template<typename T, typename T1>
132 inline
133 typename plogisTraits<T>::plogisT plogis(const T1 &x, const T &location, const T &scale)
134 {
135  typedef typename plogisTraits<T>::plogisT plogisT;
136  plogisT y = 1./(1.+mfexp((location - x) / scale));
137  return (y);
138 }
139 
140 
141 // template <typename T1, typename T2>
142 // T1 plogis(T1 *x, T2 *location, T2 *scale)
143 // {
144 // T1 y = 1./(1.+mfexp((*location - *x) / *scale));
145 // return (y);
146 // }
147 
148 // template <typename T1, typename T2>
149 // T1 plogis(T1 x, T2 location, T2 scale)
150 // {
151 // T1 y = 1./(1.+mfexp((location - x) / scale));
152 // return (y);
153 // }
154 
155 
156 
157 #endif
158 
159 
160 // #include "plogis.hpp"
161 // template <typename T1, typename T2>
162 // T1 plogis(T1 *x, T2 *location, T2 *scale)
163 // {
164 // T1 y = 1./(1.+mfexp((*location - *x) / *scale));
165 // return (y);
166 // }
167 
168 // template <typename T1, typename T2>
169 // T1 plogis(T1 x, T2 location, T2 scale)
170 // {
171 // T1 y = 1./(1.+mfexp((location - x) / scale));
172 // return (y);
173 // }
174 
175 // template <typename T1, typename T2, typename T3>
176 // T1 plogis(T2 x, T3 location, T3 scale)
177 // {
178 // T1 y = 1./(1.+mfexp((location - x) / scale));
179 // //cout<<typeid(y)<<endl;
180 // return (y);
181 // }
182 
183 // template class plogis<dvariable>;
184 // template class plogis<dvector>;
185 // template class plogis<dvar_vector>;
186 // template<typename T>
187 // class AccumulationTraits;
188 // template<>
189 // class AccumulationTraits<dvector> {
190 // public:
191 // typedef dvar_vector AccT;
192 // };
193 // template<>
194 // class AccumulationTraits<dvar_vector> {
195 // public:
196 // typedef dvector AccT;
197 // };
198 
199 // template <typename T1, typename T2>
200 // typename AccumulationTraits<T1>::AccT plogis( T1& x, T2& location, T2& scale )
201 // {
202 // if( scale<=0 )
203 // {
204 // cerr<<"Standard deviation is less than or equal to zero in "
205 // "plogis( const dvar_vector& x, const dvariable& location, "
206 // "const dvariable& scale )\n";
207 // return 0;
208 // }
209 // typedef typename AccumulationTraits<T1>::AccT AccT;
210 // AccT logistic = 1./(1.+mfexp((location-x)/scale));
211 // return (logistic);
212 // }
213 
214 // dvariable plogis( const prevariable& x, const double& location, const double& scale );
215 // dvariable plogis( const prevariable& x, const prevariable& location, const prevariable& scale );
216 // dvector plogis( const dvector& x, const double& location, const double& scale );
217 // dvar_vector plogis( const dvector& x, const prevariable& location, const prevariable& scale );
218 // dvar_vector plogis( const dvar_vector& x, const prevariable& location, const prevariable& scale );
219 
220 // Exponential logistic distribution
221 dvar_vector eplogis(const dvar_vector& x, const dvariable& alpha, const dvariable& beta, const dvariable& gamma);
222 dvector eplogis(const dvector& x, const double& alpha, const double& beta, const double& gamma);
223 
224 // Beta distribution
225 dvariable dbeta( const dvariable& x, const double& shape1, const double& shape2 );
226 dvariable dbeta( const dvariable& x, const dvariable& shape1, const dvariable& shape2 );
227 dvariable dbeta( const dvariable& x, const prevariable& shape1, const prevariable& shape2 );
228 
229 // Lognormal distribution
230 dvariable dlnorm( const prevariable& x, const double& mu, const double& std );
231 dvariable dlnorm( const prevariable& x, const prevariable& mu, const double& std );
232 dvariable dlnorm( const prevariable& x, const prevariable& mu, const prevariable& std );
233 dvariable dlnorm( const dvar_vector& x, const double& mu, const double& std );
234 dvariable dlnorm( const dvar_vector& x, const prevariable& mu, const double& std );
235 dvariable dlnorm( const dvar_vector& x, const prevariable& mu, const prevariable& std );
236 
237 // Poisson distribution
238 dvariable dpois(const double& k, const prevariable& lambda);
239 dvariable dpois(const prevariable& k, const prevariable& lambda);
240 
241 // Multinomial distribution
242 dvariable dmultinom(const dvector& x, const dvar_vector& p);
243 // dvariable dmultinom(const dmatrix o, const dvar_matrix& p,dvar_matrix& nu,double& tau2,const double minp, double &neff);
244 dvariable dmultinom(const dmatrix o, const dvar_matrix& p,dvar_matrix& nu,double& tau2,const double minp);
245 double neff(const dvector& obs, const dvar_vector& pred);
246 
247 // Multivariate logistic negative log likelihood
248 dvariable dmvlogistic(const dmatrix o, const dvar_matrix& p,dvar_matrix& nu, double& tau2,const double minp);
249 dvariable dmvlogistic(const dmatrix o, const dvar_matrix& p, double& tau2); // *deprecated*
250 
251 // Random multivariate logistic negative log likelihood
252 dvector rmvlogistic(const dvector& p, const double& tau2, const int& seed);
253 
254 // Robust normal approximation to the multinomial distribution
255 dvariable multifan(const dmatrix oprop,const dvar_matrix& pprop, const int& Nsamp);
256 dvariable multifan(const int& n, const dmatrix obs, const dvar_matrix pred,double& nef);
257 dvariable multifan(const double& s,const dvector obsQ,const dvar_vector& preQ, double& nmle);
258 
259 
260 // Spline class and functions
262 
270 {
271 public:
272  // GET methods
273  int indexmin(void) {return index_min;}
274  int indexmax(void) {return index_max;}
275 
276  // Constructors
277  vcubic_spline_function_array(int,int,const dvector & x, const dvar_matrix& _y);
280 
281  // Destructor
283 
284  // Operator overloads
286  dvar_matrix operator( ) (const dvector & v);
287 
288 private:
292 };
293 
294 void bicubic_spline(const dvector& x, const dvector& y, dvar_matrix& knots, dvar_matrix& S);
295 
296 dvar_vector cubic_spline(const dvar_vector& spline_nodes, const dvector& ip);
297 
298 dvariable splin2(const dvector& _x1a,const dvector& _x2a, const dvar_matrix _ya,dvar_matrix& _y2a, const double& x1,const double& x2);
299 
300 dvar_matrix splie2(const dvector& _x1a,const dvector& _x2a,const dvar_matrix& _ya);
301 
302 dvar_vector spline(const dvector &_x,const dvar_vector&_y,double yp1,double ypn);
303 dvariable splint(const dvector& _xa,const dvar_vector& _ya, const dvar_vector& _y2a,double x);
304 
321 #endif
322 
323 
324 
325 
326 
327 
328 
329 
330 
331 
332 
333 
334 
335 
336 
337 
338 
Base class for dvariable.
Definition: fvar.hpp:1315
dvariable dinvgamma(const dvariable &x, const double a, const double b)
Definition: dinvgamma.cpp:16
Description not yet available.
dvariable dlnorm(const prevariable &x, const double &mu, const double &std)
Definition: dlnorm.cpp:36
Description not yet available.
Definition: admodel.h:242
#define x
Vector of double precision numbers.
Definition: dvector.h:50
dvector rmvlogistic(const dvector &p, const double &tau2, const int &seed)
Definition: rmvlogistic.cpp:15
dvariable dnbinom_tau(const double &x, const prevariable &mu, const prevariable &tau)
negative log likelihood of negative binomial with mean and tau
Definition: dnbinom_tau.cpp:16
Description not yet available.
Definition: df1b2fun.h:953
double splint(const dvector &xa, const dvector &ya, const dvector &y2a, double x)
Definition: cspline.cpp:92
dvar_vector eplogis(const dvar_vector &x, const dvariable &alpha, const dvariable &beta, const dvariable &gamma)
Definition: eplogis.cpp:14
dvariable multifan(const dmatrix oprop, const dvar_matrix &pprop, const int &Nsamp)
Definition: multifan.cpp:14
ADMB variable vector.
Definition: fvar.hpp:2172
dvar_matrix splie2(const dvector &_x1a, const dvector &_x2a, const dvar_matrix &_ya)
dvar_matrix ageLengthKey(const dvar_vector &mu, const dvar_vector &sig, const dvector &x)
Age Length Key.
Definition: alk.cpp:41
Description not yet available.
Definition: df1b2fun.h:266
vcubic_spline_function_array(int, int, const dvector &x, const dvar_matrix &_y)
dvar_vector cubic_spline(const dvar_vector &spline_nodes, const dvector &ip)
A Wrapper for the vcubic_spline_function.
d3_array mfexp(const d3_array &m)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr7.cpp:10
dvariable dpois(const double &k, const prevariable &lambda)
Definition: dpois.cpp:35
vcubic_spline_function & operator()(int i)
double get_ft(const double &ct, const double &m, const dvector &va, const dvector &ba)
Definition: baranov.cpp:51
dvariable dmultinom(const dvector &x, const dvar_vector &p)
Definition: dmultinom.cpp:15
double neff(const dvector &obs, const dvar_vector &pred)
Definition: dmultinom.cpp:28
vcubic_spline_function ** ptr
Definition: statsLib.h:289
vcubic_spline_function * pvcubic_spline_function
Definition: statsLib.h:261
#define min(a, b)
Definition: cbivnorm.cpp:188
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Description not yet available.
dvariable beta(const prevariable &a, const prevariable &b)
Beta density function.
Definition: vbeta.cpp:18
dvector spline(const dvector &x, const dvector &y, double yp1, double ypn)
Definition: cspline.cpp:37
dvariable dnbinom(const double &x, const prevariable &mu, const prevariable &k)
negative log likelihood of negative binomial with mean=mu and variance = mu + mu^2 /k ...
Definition: dnbinom.cpp:12
Description not yet available.
Definition: fvar.hpp:2819
dvariable dunif(const dvariable &x, const double min, const double max)
Definition: dunif.cpp:16
dvariable dgamma(const dvariable &x, const double &a, const double &b)
Definition: dgamma.cpp:37
dvariable dmvlogistic(const dmatrix o, const dvar_matrix &p, dvar_matrix &nu, double &tau2, const double minp)
Definition: dmvlogistic.cpp:15
A class for implementin cubic splines.
Definition: statsLib.h:269
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
dvector pearson_residuals(long m, dvector obs_p, dvector pred_p)
dvariable dnorm(const prevariable &x, const double &mu, const double &std)
Definition: dnorm.cpp:46
dvariable dstudent_t(const dvar_vector &residual, const dvar_vector &df)
Definition: studentT.cpp:38
Description not yet available.
void bicubic_spline(const dvector &x, const dvector &y, dvar_matrix &knots, dvar_matrix &S)
df1b2variable dzinbinom(const double &x, const df1b2variable &mu, const df1b2variable &k, const df1b2variable &p)
ecologically parametarized negative binomial with zero inflation
Definition: dzinbinom.cpp:20
dvariable dbinom(const prevariable &x, const double &n, const double &p)
Definition: dbinom.cpp:39
dvariable dbeta(const dvariable &x, const double &shape1, const double &shape2)
Definition: dbeta.cpp:37
#define max(a, b)
Definition: cbivnorm.cpp:189
void fill(const dmatrix &_d, const dvector &_v)
Definition: fill.cpp:40
Description not yet available.
Definition: fvar.hpp:5039
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
dvar_matrix ALK(dvar_vector mu, dvar_vector sig, dvector x)
Definition: alk.cpp:105
dvariable splin2(const dvector &_x1a, const dvector &_x2a, const dvar_matrix _ya, dvar_matrix &_y2a, const double &x1, const double &x2)
dvariable plogis(const prevariable &x, const double &location, const double &scale)