#include #include #include #include #include #include #include using std::vector; using std::max; static inline double PROB(vector const &par) { return *par[0]->value(); } static inline double SIZE(vector const &par) { return *par[1]->value(); } DNegBin::DNegBin() : DistDiscrete("dnegbin", 2, DIST_POSITIVE, true) {} DNegBin::~DNegBin() {} bool DNegBin::checkParameterDim (vector const &par) const { return (par[0]->length() == 1) && (par[1]->length() == 1) && (par[1]->isDiscreteValued()); } bool DNegBin::checkParameterValue(vector const &par) const { double p = PROB(par); return (SIZE(par) > 0 && p > 0 && p < 1); } double DNegBin::d(double x, vector const &par, bool give_log) const { return dnbinom(x, SIZE(par), PROB(par), give_log); } double DNegBin::p(double q, vector const &par, bool lower, bool give_log) const { return pnbinom(q, SIZE(par), PROB(par), lower, give_log); } double DNegBin::q(double p, vector const &par, bool lower, bool log_p) const { return qnbinom(p, SIZE(par), PROB(par), lower, log_p); } double DNegBin::r(vector const &par) const { return rnbinom(SIZE(par), PROB(par)); } double DNegBin::mean(std::vector const &par) const { double p = PROB(par); return SIZE(par) * (1 - p)/p; } double DNegBin::var(std::vector const &par) const { double p = PROB(par); return SIZE(par) * (1 - p)/(p*p); }