ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
statsLib.h
Go to the documentation of this file.
00001 #ifndef _STATSLIB_H_
00002 #define _STATSLIB_H_
00003 
00004 #include <fvar.hpp>
00005 #include <admodel.h>
00006 #include <df1b2fun.h>
00007 #include <adrndeff.h> 
00008 
00009 
00010 
00011 dvar_matrix ageLengthKey( const dvar_vector& mu, const dvar_vector& sig, const dvector& x );
00012 dmatrix ageLengthKey( const dvector& mu, const dvector& sig, const dvector& x );
00013 dvar_matrix ALK(dvar_vector mu, dvar_vector sig, dvector x);
00014 
00015 void fill( const dmatrix& _d,const dvector& _v );
00016 void fill( const dvar_matrix& _d,const dvar_vector& _v );
00017 
00018 double get_ft(const double& ct, const double& m, const dvector& va, const dvector& ba );
00019 dvector get_ft( dvector& ct,const double& m, const dmatrix& V,const dvector& ba );
00020 dvector get_ft( dvector& ct,const double& m, const dmatrix& V,const dvector& na, const dvector& wa );
00021 
00022 dvector pearson_residuals(long m, dvector obs_p, dvector pred_p);
00023 
00024 // DISTRIBUTION FUNCTIONS
00025 
00026 // Uniform distribution
00027 dvariable dunif( const dvariable& x, const double min, const double max );
00028 
00029 // Student-t Distribution
00030 dvariable dstudent_t( const dvar_vector& residual, const dvar_vector& df);
00031 
00032 // Inverse gamma distribution
00033 dvariable dinvgamma( const dvariable& x, const double a, const double b );
00034 
00035 // Binomial distribution
00036 dvariable dbinom( const prevariable& x,const double& n,const double& p );
00037 dvariable dbinom( const prevariable& x,const prevariable& n,const double& p );
00038 dvariable dbinom( const prevariable& x,const prevariable& n,const prevariable& p );
00039 dvariable dbinom(const dvar_vector& x,const dvar_vector& n,const dvar_vector& p);
00040 dvariable dbinom(const prevariable& x,const dvar_vector& n,const dvar_vector& p);
00041 dvariable dbinom(const dvar_vector& x,const prevariable& n,const dvar_vector& p);
00042 dvariable dbinom(const dvar_vector& x,const dvar_vector& n,const prevariable& p);
00043 df1b2variable dbinom( const df1b2variable& x,const double& n,const double& p );
00044 df1b2variable dbinom( const df1b2variable& x,const df1b2variable& n,const double& p );
00045 df1b2variable dbinom( const df1b2variable& x,const df1b2variable& n,const df1b2variable& p );
00046 df1b2variable dbinom(const df1b2vector& x,const df1b2vector& n,const df1b2vector& p);
00047 df1b2variable dbinom(const df1b2variable& x,const df1b2vector& n,const df1b2vector& p);
00048 df1b2variable dbinom(const df1b2vector& x,const df1b2variable& n,const df1b2vector& p);
00049 df1b2variable dbinom(const df1b2vector& x,const df1b2vector& n,const df1b2variable& p);
00050 
00051 // Negative binomial distribution
00052 dvariable dnbinom(const double& x,const prevariable& mu, const prevariable& k);
00053 df1b2variable dnbinom(const double& x, const df1b2variable& mu, const df1b2variable& k);
00054 df1b2variable dnbinom(const dvector& x, const df1b2vector& mu, const df1b2variable& k);
00055 df1b2variable dnbinom(const dvector& x, const df1b2vector& mu, const df1b2vector& k);
00056 dvariable dnbinom(const dvector& x, const dvar_vector& mu, const prevariable& k);
00057 dvariable dnbinom(const dvector& x, const dvar_vector& mu, const dvar_vector& k);
00058 
00059 // Negative binomial distribution (other parametarization)
00060 dvariable dnbinom_tau(const double& x,const prevariable& mu, const prevariable& tau);
00061 df1b2variable dnbinom_tau(const double& x, const df1b2variable& mu, const df1b2variable& tau);
00062 df1b2variable dnbinom_tau(const dvector& x, const df1b2vector& mu, const df1b2variable& tau);
00063 df1b2variable dnbinom_tau(const dvector& x, const df1b2vector& mu, const df1b2vector& tau);
00064 dvariable dnbinom_tau(const dvector& x, const dvar_vector& mu, const prevariable& tau);
00065 dvariable dnbinom_tau(const dvector& x, const dvar_vector& mu, const dvar_vector& tau);
00066 
00067 // Zero Inflated Negative binomial distribution
00068 df1b2variable dzinbinom(const double& x, const df1b2variable& mu, const df1b2variable& k, const df1b2variable& p);
00069 dvariable dzinbinom(const double& x, const prevariable& mu, const prevariable& k, const prevariable& p);
00070 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2variable& k, const df1b2variable& p);
00071 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2vector& k, const df1b2variable& p);
00072 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const prevariable& k, const prevariable& p);
00073 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const dvar_vector& k, const prevariable& p);
00074 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2variable& k, const df1b2vector& p);
00075 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2vector& k, const df1b2vector& p);
00076 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const prevariable& k, const dvar_vector& p);
00077 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const dvar_vector& k, const dvar_vector& p);
00078 
00079 // Gamma distribution
00080 dvariable dgamma( const dvariable &x, const double& a, const double& b );
00081 dvariable dgamma( const dvariable &x, const dvariable& a, const dvariable &b );
00082 dvariable dgamma( const dvector &x, const prevariable& a, const prevariable& b );
00083 dvariable dgamma( const dvector &x, const prevariable& a, const dvar_vector& b );
00084 dvariable dgamma( const dvector &x, const dvar_vector& a, const dvar_vector& b );
00085 dvariable dgamma( const dvector &x, const dvar_vector& a, const prevariable& b );
00086 
00087 // Normal distribution
00088 dvariable dnorm( const prevariable& x, const double& mu, const double& std );
00089 dvariable dnorm( const dvar_vector& x, const double& mu, const double& std );
00090 dvariable dnorm( const prevariable& x, const double& mu, const double& std, bool bLog );
00091 dvariable dnorm( const dvector& x, const prevariable& mu, const prevariable& std );
00092 dvariable dnorm( const dvector& x, const prevariable& mu, const prevariable& std, bool dLog );
00093 dvariable dnorm( const dvar_vector& residual, const prevariable& std );
00094 dvariable dnorm( const dvar_vector& residual, const double& std );
00095 dvariable dnorm( const dvar_vector& residual, const dvector& std );
00096 dvariable dnorm( const dvar_vector& residual, const dvar_vector std );
00097 dvariable dnorm( const dvar_vector& residual );
00098 dvariable dnorm( const dmatrix& obs, const dvar_matrix& pred );
00099 df1b2variable dnorm( const df1b2variable& x, const double& mu, const double& std );
00100 df1b2variable dnorm( const df1b2variable& x, const double& mu, const double& std, bool bLog );
00101 df1b2variable dnorm( const df1b2variable& x, const df1b2variable& mu, const df1b2variable& std, bool bLog );
00102 df1b2variable dnorm( const df1b2vector& x, const df1b2variable& mu, const df1b2variable& std, bool dLog );
00103 
00104 
00105 // Plogis distribution
00106 #ifndef LOGISTIC_HPP
00107 #define LOGISTIC_HPP
00108 
00109 template <typename T>
00110 class plogisTraits;
00111 
00112 template<>
00113 class plogisTraits<double> {
00114 public:
00115   typedef dvector plogisT;
00116 };
00117 
00118 template<>
00119 class plogisTraits<named_dvariable> {
00120 public:
00121   typedef dvar_vector plogisT;
00122 };
00123 
00124 template<>
00125 class plogisTraits<dvariable> {
00126 public:
00127   typedef dvar_vector plogisT;
00128 };
00129 
00130 
00131 template<typename T, typename T1>
00132 inline
00133 typename plogisTraits<T>::plogisT plogis(const T1 &x, const T &location, const T &scale)
00134 {
00135   typedef typename plogisTraits<T>::plogisT plogisT;
00136   plogisT y = 1./(1.+mfexp((location - x) / scale));
00137   return (y);
00138 }
00139 
00140 
00141 // template <typename T1, typename T2>
00142 // T1 plogis(T1 *x, T2 *location, T2 *scale)
00143 // {
00144 //   T1 y = 1./(1.+mfexp((*location - *x) / *scale));
00145 //   return (y);
00146 // }  
00147 
00148 // template <typename T1, typename T2>
00149 // T1 plogis(T1 x, T2 location, T2 scale)
00150 // {
00151 //   T1 y = 1./(1.+mfexp((location - x) / scale));
00152 //   return (y);
00153 // } 
00154 
00155 
00156 
00157 #endif
00158 
00159 
00160 // #include "plogis.hpp"
00161 // template <typename T1, typename T2>
00162 // T1 plogis(T1 *x, T2 *location, T2 *scale)
00163 // {
00164 //   T1 y = 1./(1.+mfexp((*location - *x) / *scale));
00165 //   return (y);
00166 // }  
00167 
00168 // template <typename T1, typename T2>
00169 // T1 plogis(T1 x, T2 location, T2 scale)
00170 // {
00171 //   T1 y = 1./(1.+mfexp((location - x) / scale));
00172 //   return (y);
00173 // } 
00174 
00175 // template <typename T1, typename T2, typename T3>
00176 // T1 plogis(T2 x, T3 location, T3 scale)
00177 // {
00178 //   T1 y = 1./(1.+mfexp((location - x) / scale));
00179 //   //cout<<typeid(y)<<endl;
00180 //   return (y);
00181 // } 
00182 
00183 // template class plogis<dvariable>;
00184 // template class plogis<dvector>;
00185 // template class plogis<dvar_vector>;
00186 // template<typename T>
00187 // class AccumulationTraits;
00188 // template<>
00189 // class AccumulationTraits<dvector> {
00190 //   public:
00191 //   typedef dvar_vector AccT;
00192 // };
00193 // template<>
00194 // class AccumulationTraits<dvar_vector> {
00195 //   public:
00196 //   typedef dvector AccT;
00197 // };
00198 
00199 // template <typename T1, typename T2>
00200 // typename AccumulationTraits<T1>::AccT plogis(  T1& x,  T2& location,  T2& scale )
00201 // {
00202 //   if( scale<=0 ) 
00203 //   {
00204 //     cerr<<"Standard deviation is less than or equal to zero in "
00205 //     "plogis( const dvar_vector& x, const dvariable& location, "
00206 //             "const dvariable& scale )\n";
00207 //     return 0;
00208 //   }
00209 //   typedef typename AccumulationTraits<T1>::AccT AccT;
00210 //   AccT logistic = 1./(1.+mfexp((location-x)/scale));
00211 //   return (logistic);
00212 // }  
00213 
00214 // dvariable   plogis( const prevariable& x, const double& location, const double& scale );
00215 // dvariable   plogis( const prevariable& x, const prevariable& location, const prevariable& scale );
00216 // dvector     plogis( const dvector& x, const double& location, const double& scale );
00217 // dvar_vector plogis( const dvector& x, const prevariable& location, const prevariable& scale );
00218 // dvar_vector plogis( const dvar_vector& x, const prevariable& location, const prevariable& scale );
00219 
00220 // Exponential logistic distribution
00221 dvar_vector eplogis(const dvar_vector& x, const dvariable& alpha, const dvariable& beta, const dvariable& gamma);
00222 dvector eplogis(const dvector& x, const double& alpha, const double& beta, const double& gamma);
00223 
00224 // Beta distribution
00225 dvariable dbeta( const dvariable& x, const double& shape1, const double& shape2 );
00226 dvariable dbeta( const dvariable& x, const dvariable& shape1, const dvariable& shape2 );
00227 dvariable dbeta( const dvariable& x, const prevariable& shape1, const prevariable& shape2 );
00228 
00229 // Lognormal distribution
00230 dvariable dlnorm( const prevariable& x, const double& mu, const double& std );
00231 dvariable dlnorm( const prevariable& x, const prevariable& mu, const double& std );
00232 dvariable dlnorm( const prevariable& x, const prevariable& mu, const prevariable& std );
00233 dvariable dlnorm( const dvar_vector& x, const double& mu, const double& std );
00234 dvariable dlnorm( const dvar_vector& x, const prevariable& mu, const double& std );
00235 dvariable dlnorm( const dvar_vector& x, const prevariable& mu, const prevariable& std );
00236 
00237 // Poisson distribution
00238 dvariable dpois(const double& k, const prevariable& lambda);
00239 dvariable dpois(const prevariable& k, const prevariable& lambda);
00240 
00241 // Multinomial distribution
00242 dvariable dmultinom(const dvector& x, const dvar_vector& p);
00243 // dvariable dmultinom(const dmatrix o, const dvar_matrix& p,dvar_matrix& nu,double& tau2,const double minp, double &neff);
00244 dvariable dmultinom(const dmatrix o, const dvar_matrix& p,dvar_matrix& nu,double& tau2,const double minp);
00245 double neff(const dvector& obs, const dvar_vector& pred);
00246 
00247 // Multivariate logistic negative log likelihood
00248 dvariable dmvlogistic(const dmatrix o, const dvar_matrix& p,dvar_matrix& nu, double& tau2,const double minp);
00249 dvariable dmvlogistic(const dmatrix o, const dvar_matrix& p, double& tau2); // *deprecated*
00250 
00251 // Random multivariate logistic negative log likelihood
00252 dvector rmvlogistic(const dvector& p, const double& tau2, const int& seed);
00253 
00254 // Robust normal approximation to the multinomial distribution
00255 dvariable multifan(const dmatrix oprop,const dvar_matrix& pprop, const int& Nsamp);
00256 dvariable multifan(const int& n, const dmatrix obs, const dvar_matrix pred,double& nef);
00257 dvariable multifan(const double& s,const dvector obsQ,const dvar_vector& preQ, double& nmle);
00258 
00259 
00260 // Spline class and functions
00261 typedef vcubic_spline_function * pvcubic_spline_function;
00262 
00269 class vcubic_spline_function_array
00270 {
00271 public:
00272   // GET methods
00273   int indexmin(void) {return index_min;}
00274   int indexmax(void) {return index_max;}
00275 
00276   // Constructors
00277   vcubic_spline_function_array(int,int,const dvector & x, const dvar_matrix& _y);
00278   vcubic_spline_function_array(const dvector & x);
00279   vcubic_spline_function_array(const vcubic_spline_function_array&);
00280 
00281   // Destructor
00282   ~vcubic_spline_function_array();
00283 
00284   // Operator overloads
00285   vcubic_spline_function & operator () (int i);
00286   dvar_matrix operator( ) (const dvector & v);
00287 
00288 private:
00289   vcubic_spline_function ** ptr;
00290   int index_min;
00291   int index_max;
00292 };
00293 
00294 void bicubic_spline(const dvector& x, const dvector& y, dvar_matrix& knots, dvar_matrix& S);
00295 
00296 dvar_vector cubic_spline(const dvar_vector& spline_nodes, const dvar_vector& ip);
00297 
00298 dvariable splin2(const dvector& _x1a,const dvector& _x2a, const dvar_matrix _ya,dvar_matrix& _y2a, const double& x1,const double& x2);
00299 
00300 dvar_matrix splie2(const dvector& _x1a,const dvector& _x2a,const dvar_matrix& _ya);
00301 
00302 dvar_vector spline(const dvector &_x,const dvar_vector&_y,double yp1,double ypn);
00303 dvariable splint(const dvector& _xa,const dvar_vector& _ya, const dvar_vector& _y2a,double x);
00304 
00321 #endif
00322 
00323 
00324 
00325 
00326 
00327 
00328 
00329 
00330 
00331 
00332 
00333 
00334 
00335 
00336 
00337 
00338