ADMB Documentation  11.1.1927
 All Classes Files Functions Variables Typedefs Friends Defines
vnegbin.cpp
Go to the documentation of this file.
00001 /*
00002   $Id: vnegbin.cpp 1677 2014-02-25 20:58:46Z johnoel $
00003 
00004   Author: David Fournier
00005   Copyright (c) 2008-2012 Regents of the University of California
00006  */
00012 #include "fvar.hpp"
00013 
00014 
00025   dvariable log_negbinomial_density(double x,const prevariable& mu,
00026     const prevariable& tau)
00027   {
00028     if (value(tau)-1.0<0.0)
00029     {
00030       cerr << "tau <=1 in log_negbinomial_density " << endl;
00031       ad_exit(1);
00032     }
00033     RETURN_ARRAYS_INCREMENT();
00034     dvariable r=mu/(1.e-120+(tau-1));
00035     dvariable tmp;
00036     tmp=gammln(x+r)-gammln(r) -gammln(x+1)
00037       +r*log(r)+x*log(mu)-(r+x)*log(r+mu);
00038     RETURN_ARRAYS_DECREMENT();
00039     return tmp;
00040   }
00041 
00051   dvariable negbinomial_density(double x,const prevariable& mu,
00052     const prevariable& tau)
00053   {
00054     if (value(tau)-1.0<=0.0)
00055     {
00056       cerr << "tau <=1 in log_negbinomial_density " << endl;
00057       ad_exit(1);
00058     }
00059     RETURN_ARRAYS_INCREMENT();
00060     dvariable r=mu/(1.e-120+(tau-1));
00061     dvariable tmp;
00062     //tmp=exp(gammln(x+r)-gammln(r) -gammln(x+1)
00063      // +r*log(r)+x*log(mu)-(r+x)*log(r+mu));
00064     tmp=gammln(x+r);
00065     tmp-=gammln(r);
00066     tmp-=gammln(x+1);
00067     tmp+=r*log(r);
00068     tmp+=x*log(mu);
00069     tmp-=(r+x)*log(r+mu);
00070     tmp=exp(tmp);
00071 
00072 
00073     RETURN_ARRAYS_DECREMENT();
00074     return tmp;
00075   }
00076 
00083   dvariable log_density_poisson(double x,const prevariable& mu)
00084   {
00085     return -mu+x*log(mu)-gammln(x+1);
00086   }