1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__POISSON_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__POISSON_HPP
4 #include <boost/random/poisson_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
8 #include <boost/math/special_functions/fpclassify.hpp>
24 template <
bool propto,
25 typename T_n,
typename T_rate>
26 typename return_type<T_rate>::type
29 static const char*
function =
"stan::prob::poisson_log(%1%)";
49 "Rate parameter", &logp);
51 "Rate parameter", &logp);
54 "Random variable",
"Rate parameter",
66 for (
size_t i = 0; i <
size; i++)
69 for (
size_t i = 0; i <
size; i++)
70 if (lambda_vec[i] == 0 && n_vec[i] != 0)
77 for (
size_t i = 0; i <
size; i++) {
78 if (!(lambda_vec[i] == 0 && n_vec[i] == 0)) {
80 logp -=
lgamma(n_vec[i] + 1.0);
88 operands_and_partials.
d_x1[i]
89 += n_vec[i] /
value_of(lambda_vec[i]) - 1.0;
94 return operands_and_partials.
to_var(logp);
97 template <
typename T_n,
102 return poisson_log<false>(n,lambda);
106 template <
bool propto,
107 typename T_n,
typename T_log_rate>
111 static const char*
function =
"stan::prob::poisson_log_log(%1%)";
132 "Log rate parameter", &logp);
135 "Random variable",
"Log rate parameter",
148 for (
size_t i = 0; i <
size; i++)
149 if (std::numeric_limits<double>::infinity() == alpha_vec[i])
151 for (
size_t i = 0; i <
size; i++)
152 if (-std::numeric_limits<double>::infinity() == alpha_vec[i]
163 for (
size_t i = 0; i <
length(alpha); i++)
167 for (
size_t i = 0; i <
size; i++) {
168 if (!(alpha_vec[i] == -std::numeric_limits<double>::infinity()
171 logp -=
lgamma(n_vec[i] + 1.0);
173 logp += n_vec[i] *
value_of(alpha_vec[i]) - exp_alpha[i];
178 operands_and_partials.
d_x1[i] += n_vec[i] - exp_alpha[i];
180 return operands_and_partials.
to_var(logp);
183 template <
typename T_n,
188 return poisson_log_log<false>(n,alpha);
192 template <
typename T_n,
typename T_rate>
195 static const char*
function =
"stan::prob::poisson_cdf(%1%)";
212 "Random variable",
"Rate parameter",
222 using boost::math::gamma_p_derivative;
231 return operands_and_partials.
to_var(0.0);
234 for (
size_t i = 0; i <
size; i++) {
237 if (
value_of(n_vec[i]) == std::numeric_limits<double>::infinity())
240 const double n_dbl =
value_of(n_vec[i]);
241 const double lambda_dbl =
value_of(lambda_vec[i]);
242 const double Pi =
gamma_q(n_dbl+1, lambda_dbl);
247 operands_and_partials.
d_x1[i]
248 -= gamma_p_derivative(n_dbl + 1, lambda_dbl) / Pi;
253 operands_and_partials.
d_x1[i] *= P;
255 return operands_and_partials.
to_var(P);
258 template <
typename T_n,
typename T_rate>
261 static const char*
function =
"stan::prob::poisson_cdf_log(%1%)";
278 "Random variable",
"Rate parameter",
288 using boost::math::gamma_p_derivative;
300 for (
size_t i = 0; i <
size; i++) {
303 if (
value_of(n_vec[i]) == std::numeric_limits<double>::infinity())
306 const double n_dbl =
value_of(n_vec[i]);
307 const double lambda_dbl =
value_of(lambda_vec[i]);
308 const double Pi =
gamma_q(n_dbl+1, lambda_dbl);
313 operands_and_partials.
d_x1[i]
314 -= gamma_p_derivative(n_dbl + 1, lambda_dbl) / Pi;
317 return operands_and_partials.
to_var(P);
320 template <
typename T_n,
typename T_rate>
323 static const char*
function =
"stan::prob::poisson_ccdf_log(%1%)";
340 "Random variable",
"Rate parameter",
350 using boost::math::gamma_p_derivative;
359 return operands_and_partials.
to_var(0.0);
362 for (
size_t i = 0; i <
size; i++) {
365 if (
value_of(n_vec[i]) == std::numeric_limits<double>::infinity())
368 const double n_dbl =
value_of(n_vec[i]);
369 const double lambda_dbl =
value_of(lambda_vec[i]);
370 const double Pi = 1.0 -
gamma_q(n_dbl+1, lambda_dbl);
375 operands_and_partials.
d_x1[i]
376 += gamma_p_derivative(n_dbl + 1, lambda_dbl) / Pi;
379 return operands_and_partials.
to_var(P);
386 using boost::variate_generator;
387 using boost::random::poisson_distribution;
389 static const char*
function =
"stan::prob::poisson_rng(%1%)";
395 "Rate parameter", (
double*)0);
397 "Rate parameter", (
double*)0);
399 variate_generator<RNG&, poisson_distribution<> >
T_return_type to_var(double logp)
boost::math::tools::promote_args< T_a, T_b >::type multiply_log(const T_a a, const T_b b)
Calculated the value of the first argument times log of the second argument while behaving properly w...
DoubleVectorView allocates double values to be used as intermediate values.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
return_type< T_rate >::type poisson_log(const T_n &n, const T_rate &lambda)
fvar< T > lgamma(const fvar< T > &x)
A variable implementation that stores operands and derivatives with respect to the variable...
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
double value_of(const T x)
Return the value of the specified scalar argument converted to a double value.
bool isinf(const stan::agrad::var &v)
Checks if the given number is infinite.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
return_type< T_rate >::type poisson_cdf_log(const T_n &n, const T_rate &lambda)
int poisson_rng(const double lambda, RNG &rng)
return_type< T_log_rate >::type poisson_log_log(const T_n &n, const T_log_rate &alpha)
fvar< T > gamma_q(const fvar< T > &x1, const fvar< T > &x2)
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result)
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result)
size_t max_size(const T1 &x1, const T2 &x2)
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is nan.
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
int size(const std::vector< T > &x)
return_type< T_rate >::type poisson_ccdf_log(const T_n &n, const T_rate &lambda)
return_type< T_rate >::type poisson_cdf(const T_n &n, const T_rate &lambda)
VectorView< double *, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
fvar< T > log(const fvar< T > &x)
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
fvar< T > exp(const fvar< T > &x)
double negative_infinity()
Return negative infinity.