ADMB Documentation  11.1.1932
 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 // #include <plogis.cpp>
00107 template <typename T1, typename T2>
00108 T1 plogis(T1 *x, T2 *location, T2 *scale)
00109 {
00110   T1 y = 1./(1.+mfexp((*location - *x) / *scale));
00111   return (y);
00112 }  
00113 
00114 template <typename T1, typename T2>
00115 T1 plogis(T1 x, T2 location, T2 scale)
00116 {
00117   T1 y = 1./(1.+mfexp((location - x) / scale));
00118   return (y);
00119 } 
00120 
00121 template <typename T1, typename T2, typename T3>
00122 T1 plogis(T2 x, T3 location, T3 scale)
00123 {
00124   T1 y = 1./(1.+mfexp((location - x) / scale));
00125   //cout<<typeid(y)<<endl;
00126   return (y);
00127 } 
00128 
00129 // template class plogis<dvariable>;
00130 // template class plogis<dvector>;
00131 // template class plogis<dvar_vector>;
00132 // template<typename T>
00133 // class AccumulationTraits;
00134 // template<>
00135 // class AccumulationTraits<dvector> {
00136 //   public:
00137 //   typedef dvar_vector AccT;
00138 // };
00139 // template<>
00140 // class AccumulationTraits<dvar_vector> {
00141 //   public:
00142 //   typedef dvector AccT;
00143 // };
00144 
00145 // template <typename T1, typename T2>
00146 // typename AccumulationTraits<T1>::AccT plogis(  T1& x,  T2& location,  T2& scale )
00147 // {
00148 //   if( scale<=0 ) 
00149 //   {
00150 //     cerr<<"Standard deviation is less than or equal to zero in "
00151 //     "plogis( const dvar_vector& x, const dvariable& location, "
00152 //             "const dvariable& scale )\n";
00153 //     return 0;
00154 //   }
00155 //   typedef typename AccumulationTraits<T1>::AccT AccT;
00156 //   AccT logistic = 1./(1.+mfexp((location-x)/scale));
00157 //   return (logistic);
00158 // }  
00159 
00160 // dvariable   plogis( const prevariable& x, const double& location, const double& scale );
00161 // dvariable   plogis( const prevariable& x, const prevariable& location, const prevariable& scale );
00162 // dvector     plogis( const dvector& x, const double& location, const double& scale );
00163 // dvar_vector plogis( const dvector& x, const prevariable& location, const prevariable& scale );
00164 // dvar_vector plogis( const dvar_vector& x, const prevariable& location, const prevariable& scale );
00165 
00166 // Exponential logistic distribution
00167 dvar_vector eplogis(const dvar_vector& x, const dvariable& alpha, const dvariable& beta, const dvariable& gamma);
00168 dvector eplogis(const dvector& x, const double& alpha, const double& beta, const double& gamma);
00169 
00170 // Beta distribution
00171 dvariable dbeta( const dvariable& x, const double& shape1, const double& shape2 );
00172 
00173 // Lognormal distribution
00174 dvariable dlnorm( const prevariable& x, const double& mu, const double& std );
00175 dvariable dlnorm( const prevariable& x, const prevariable& mu, const double& std );
00176 dvariable dlnorm( const prevariable& x, const prevariable& mu, const prevariable& std );
00177 dvariable dlnorm( const dvar_vector& x, const double& mu, const double& std );
00178 dvariable dlnorm( const dvar_vector& x, const prevariable& mu, const double& std );
00179 dvariable dlnorm( const dvar_vector& x, const prevariable& mu, const prevariable& std );
00180 
00181 // Poisson distribution
00182 dvariable dpois(const double& k, const prevariable& lambda);
00183 dvariable dpois(const prevariable& k, const prevariable& lambda);
00184 
00185 // Multinomial distribution
00186 dvariable dmultinom(const dvector& x, const dvar_vector& p);
00187 // dvariable dmultinom(const dmatrix o, const dvar_matrix& p,dvar_matrix& nu,double& tau2,const double minp, double &neff);
00188 dvariable dmultinom(const dmatrix o, const dvar_matrix& p,dvar_matrix& nu,double& tau2,const double minp);
00189 double neff(const dvector& obs, const dvar_vector& pred);
00190 
00191 // Multivariate logistic negative log likelihood
00192 dvariable dmvlogistic(const dmatrix o, const dvar_matrix& p,dvar_matrix& nu, double& tau2,const double minp);
00193 dvariable dmvlogistic(const dmatrix o, const dvar_matrix& p, double& tau2); // *deprecated*
00194 
00195 // Random multivariate logistic negative log likelihood
00196 dvector rmvlogistic(const dvector& p, const double& tau2, const int& seed);
00197 
00198 // Robust normal approximation to the multinomial distribution
00199 dvariable multifan(const dmatrix oprop,const dvar_matrix& pprop, const int& Nsamp);
00200 dvariable multifan(const int& n, const dmatrix obs, const dvar_matrix pred,double& nef);
00201 dvariable multifan(const double& s,const dvector obsQ,const dvar_vector& preQ, double& nmle);
00202 
00203 
00204 // Spline class and functions
00205 typedef vcubic_spline_function * pvcubic_spline_function;
00206 
00213 class vcubic_spline_function_array
00214 {
00215 public:
00216   // GET methods
00217   int indexmin(void) {return index_min;}
00218   int indexmax(void) {return index_max;}
00219 
00220   // Constructors
00221   vcubic_spline_function_array(int,int,const dvector & x, const dvar_matrix& _y);
00222   vcubic_spline_function_array(const dvector & x);
00223   vcubic_spline_function_array(const vcubic_spline_function_array&);
00224 
00225   // Destructor
00226   ~vcubic_spline_function_array();
00227 
00228   // Operator overloads
00229   vcubic_spline_function & operator () (int i);
00230   dvar_matrix operator( ) (const dvector & v);
00231 
00232 private:
00233   vcubic_spline_function ** ptr;
00234   int index_min;
00235   int index_max;
00236 };
00237 
00238 void bicubic_spline(const dvector& x, const dvector& y, dvar_matrix& knots, dvar_matrix& S);
00239 
00240 dvar_vector cubic_spline(const dvar_vector& spline_nodes, const dvar_vector& ip);
00241 
00242 dvariable splin2(const dvector& _x1a,const dvector& _x2a, const dvar_matrix _ya,dvar_matrix& _y2a, const double& x1,const double& x2);
00243 
00244 dvar_matrix splie2(const dvector& _x1a,const dvector& _x2a,const dvar_matrix& _ya);
00245 
00246 dvar_vector spline(const dvector &_x,const dvar_vector&_y,double yp1,double ypn);
00247 dvariable splint(const dvector& _xa,const dvar_vector& _ya, const dvar_vector& _y2a,double x);
00248 
00265 #endif
00266 
00267 
00268 
00269 
00270 
00271 
00272 
00273 
00274 
00275 
00276 
00277 
00278 
00279 
00280 
00281 
00282