Stan Math Library: stan/math/prim/scal/prob/beta_binomial_log.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

beta_binomial_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_LOG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_LOG_HPP
3
4 #include <stan/math/prim/scal/meta/is_constant_struct.hpp>
5 #include <stan/math/prim/scal/meta/partials_return_type.hpp>
6 #include <stan/math/prim/scal/meta/OperandsAndPartials.hpp>
7 #include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
8 #include <stan/math/prim/scal/err/check_nonnegative.hpp>
9 #include <stan/math/prim/scal/err/check_positive_finite.hpp>
10 #include <stan/math/prim/scal/fun/constants.hpp>
11 #include <stan/math/prim/scal/fun/lbeta.hpp>
12 #include <stan/math/prim/scal/fun/digamma.hpp>
13 #include <stan/math/prim/scal/fun/lgamma.hpp>
14 #include <stan/math/prim/scal/fun/binomial_coefficient_log.hpp>
15 #include <stan/math/prim/scal/fun/value_of.hpp>
16 #include <stan/math/prim/scal/meta/contains_nonconstant_struct.hpp>
17 #include <stan/math/prim/scal/meta/VectorBuilder.hpp>
18 #include <stan/math/prim/scal/meta/include_summand.hpp>
19 #include <stan/math/prim/scal/prob/beta_rng.hpp>
20 #include <stan/math/prim/scal/fun/F32.hpp>
21 #include <stan/math/prim/scal/fun/grad_F32.hpp>
22
23 namespace stan {
24
25 namespace math {
26
27 // BetaBinomial(n|alpha, beta) [alpha > 0; beta > 0; n >= 0]
28 template <bool propto,
29 typename T_n, typename T_N,
30 typename T_size1, typename T_size2>
31 typename return_type<T_size1, T_size2>::type
32 beta_binomial_log(const T_n& n,
33 const T_N& N,
34 const T_size1& alpha,
35 const T_size2& beta) {
36 static const char* function("stan::math::beta_binomial_log");
37 typedef typename stan::partials_return_type<T_size1, T_size2>::type
38 T_partials_return;
39
40 using stan::math::check_positive_finite;
41 using stan::math::check_nonnegative;
42 using stan::math::value_of;
43 using stan::math::check_consistent_sizes;
44 using stan::math::include_summand;
45
46 // check if any vectors are zero length
47 if (!(stan::length(n)
48 && stan::length(N)
49 && stan::length(alpha)
50 && stan::length(beta)))
51 return 0.0;
52
53 T_partials_return logp(0.0);
54 check_nonnegative(function, "Population size parameter", N);
55 check_positive_finite(function,
56 "First prior sample size parameter", alpha);
57 check_positive_finite(function,
58 "Second prior sample size parameter", beta);
59 check_consistent_sizes(function,
60 "Successes variable", n,
61 "Population size parameter", N,
62 "First prior sample size parameter", alpha,
63 "Second prior sample size parameter", beta);
64
65 // check if no variables are involved and prop-to
66 if (!include_summand<propto, T_size1, T_size2>::value)
67 return 0.0;
68
69 OperandsAndPartials<T_size1, T_size2>
70 operands_and_partials(alpha, beta);
71
72 VectorView<const T_n> n_vec(n);
73 VectorView<const T_N> N_vec(N);
74 VectorView<const T_size1> alpha_vec(alpha);
75 VectorView<const T_size2> beta_vec(beta);
76 size_t size = max_size(n, N, alpha, beta);
77
78 for (size_t i = 0; i < size; i++) {
79 if (n_vec[i] < 0 || n_vec[i] > N_vec[i])
80 return operands_and_partials.value(LOG_ZERO);
81 }
82
83 using stan::math::lbeta;
84 using stan::math::binomial_coefficient_log;
85 using stan::math::digamma;
86
87 VectorBuilder<include_summand<propto>::value,
88 T_partials_return, T_n, T_N>
89 normalizing_constant(max_size(N, n));
90 for (size_t i = 0; i < max_size(N, n); i++)
91 if (include_summand<propto>::value)
92 normalizing_constant[i]
93 = binomial_coefficient_log(N_vec[i], n_vec[i]);
94
95 VectorBuilder<include_summand<propto, T_size1, T_size2>::value,
96 T_partials_return, T_n, T_N, T_size1, T_size2>
97 lbeta_numerator(size);
98 for (size_t i = 0; i < size; i++)
99 if (include_summand<propto, T_size1, T_size2>::value)
100 lbeta_numerator[i] = lbeta(n_vec[i] + value_of(alpha_vec[i]),
101 N_vec[i] - n_vec[i]
102 + value_of(beta_vec[i]));
103
104 VectorBuilder<include_summand<propto, T_size1, T_size2>::value,
105 T_partials_return, T_size1, T_size2>
106 lbeta_denominator(max_size(alpha, beta));
107 for (size_t i = 0; i < max_size(alpha, beta); i++)
108 if (include_summand<propto, T_size1, T_size2>::value)
109 lbeta_denominator[i] = lbeta(value_of(alpha_vec[i]),
110 value_of(beta_vec[i]));
111
112 VectorBuilder<!is_constant_struct<T_size1>::value,
113 T_partials_return, T_n, T_size1>
114 digamma_n_plus_alpha(max_size(n, alpha));
115 for (size_t i = 0; i < max_size(n, alpha); i++)
116 if (!is_constant_struct<T_size1>::value)
117 digamma_n_plus_alpha[i]
118 = digamma(n_vec[i] + value_of(alpha_vec[i]));
119
120 VectorBuilder<contains_nonconstant_struct<T_size1, T_size2>::value,
121 T_partials_return, T_N, T_size1, T_size2>
122 digamma_N_plus_alpha_plus_beta(max_size(N, alpha, beta));
123 for (size_t i = 0; i < max_size(N, alpha, beta); i++)
124 if (contains_nonconstant_struct<T_size1, T_size2>::value)
125 digamma_N_plus_alpha_plus_beta[i]
126 = digamma(N_vec[i] + value_of(alpha_vec[i])
127 + value_of(beta_vec[i]));
128
129 VectorBuilder<contains_nonconstant_struct<T_size1, T_size2>::value,
130 T_partials_return, T_size1, T_size2>
131 digamma_alpha_plus_beta(max_size(alpha, beta));
132 for (size_t i = 0; i < max_size(alpha, beta); i++)
133 if (contains_nonconstant_struct<T_size1, T_size2>::value)
134 digamma_alpha_plus_beta[i]
135 = digamma(value_of(alpha_vec[i]) + value_of(beta_vec[i]));
136
137 VectorBuilder<!is_constant_struct<T_size1>::value,
138 T_partials_return, T_size1> digamma_alpha(length(alpha));
139 for (size_t i = 0; i < length(alpha); i++)
140 if (!is_constant_struct<T_size1>::value)
141 digamma_alpha[i] = digamma(value_of(alpha_vec[i]));
142
143 VectorBuilder<!is_constant_struct<T_size2>::value,
144 T_partials_return, T_size2>
145 digamma_beta(length(beta));
146 for (size_t i = 0; i < length(beta); i++)
147 if (!is_constant_struct<T_size2>::value)
148 digamma_beta[i] = digamma(value_of(beta_vec[i]));
149
150 for (size_t i = 0; i < size; i++) {
151 if (include_summand<propto>::value)
152 logp += normalizing_constant[i];
153 if (include_summand<propto, T_size1, T_size2>::value)
154 logp += lbeta_numerator[i] - lbeta_denominator[i];
155
156 if (!is_constant_struct<T_size1>::value)
157 operands_and_partials.d_x1[i]
158 += digamma_n_plus_alpha[i]
159 - digamma_N_plus_alpha_plus_beta[i]
160 + digamma_alpha_plus_beta[i]
161 - digamma_alpha[i];
162 if (!is_constant_struct<T_size2>::value)
163 operands_and_partials.d_x2[i]
164 += digamma(value_of(N_vec[i]-n_vec[i]+beta_vec[i]))
165 - digamma_N_plus_alpha_plus_beta[i]
166 + digamma_alpha_plus_beta[i]
167 - digamma_beta[i];
168 }
169 return operands_and_partials.value(logp);
170 }
171
172 template <typename T_n,
173 typename T_N,
174 typename T_size1,
175 typename T_size2>
176 typename return_type<T_size1, T_size2>::type
177 beta_binomial_log(const T_n& n, const T_N& N,
178 const T_size1& alpha, const T_size2& beta) {
179 return beta_binomial_log<false>(n, N, alpha, beta);
180 }
181
182 }
183 }
184 #endif
stan::math::OperandsAndPartials::d_x2
VectorView< T_return_type, false, true > d_x2
Definition: OperandsAndPartials.hpp:40
lgamma.hpp
stan::math::binomial_coefficient_log
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
Definition: binomial_coefficient_log.hpp:16
check_positive_finite.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::value_of
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
stan::math::lbeta
fvar< T > lbeta(const fvar< T > &x1, const fvar< T > &x2)
Definition: lbeta.hpp:16
digamma.hpp
stan::math::OperandsAndPartials::value
T_return_type value(double value)
Returns a T_return_type with the value specified with the partial derivatves.
Definition: OperandsAndPartials.hpp:68
contains_nonconstant_struct.hpp
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
binomial_coefficient_log.hpp
is_constant_struct.hpp
stan::math::LOG_ZERO
const double LOG_ZERO
Definition: constants.hpp:175
stan::math::include_summand
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: include_summand.hpp:36
F32.hpp
stan::return_type::type
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
Definition: return_type.hpp:27
include_summand.hpp
check_nonnegative.hpp
stan::is_constant_struct
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
Definition: is_constant_struct.hpp:13
beta_rng.hpp
stan::math::OperandsAndPartials
This class builds partial derivatives with respect to a set of operands.
Definition: OperandsAndPartials.hpp:38
stan::math::beta_binomial_log
return_type< T_size1, T_size2 >::type beta_binomial_log(const T_n &n, const T_N &N, const T_size1 &alpha, const T_size2 &beta)
Definition: beta_binomial_log.hpp:32
stan::max_size
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
VectorBuilder.hpp
partials_return_type.hpp
OperandsAndPartials.hpp
stan::contains_nonconstant_struct
Definition: contains_nonconstant_struct.hpp:14
grad_F32.hpp
stan::VectorBuilder
VectorBuilder allocates type T1 values to be used as intermediate values.
Definition: VectorBuilder.hpp:28
constants.hpp
stan::math::size
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
stan::math::check_consistent_sizes
bool check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Return true if the dimension of x1 is consistent with x2.
Definition: check_consistent_sizes.hpp:31
lbeta.hpp
stan::math::check_nonnegative
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
Definition: check_nonnegative.hpp:66
value_of.hpp
stan::VectorView
VectorView is a template expression that is constructed with a container or scalar, which it then allows to be used as an array using operator[].
Definition: VectorView.hpp:48
stan::partials_return_type::type
boost::math::tools::promote_args< typename partials_type< typename scalar_type< T1 >::type >::type, typename partials_type< typename scalar_type< T2 >::type >::type, typename partials_type< typename scalar_type< T3 >::type >::type, typename partials_type< typename scalar_type< T4 >::type >::type, typename partials_type< typename scalar_type< T5 >::type >::type, typename partials_type< typename scalar_type< T6 >::type >::type >::type type
Definition: partials_return_type.hpp:26
check_consistent_sizes.hpp
stan::math::check_positive_finite
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.
Definition: check_positive_finite.hpp:28
stan::math::OperandsAndPartials::d_x1
VectorView< T_return_type, false, true > d_x1
Definition: OperandsAndPartials.hpp:39
stan::math::digamma
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:16

[ Stan Home Page ] © 2011–2016, Stan Development Team.