| Stan Math Library 2.12.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 namespace math {
25
26 // BetaBinomial(n|alpha, beta) [alpha > 0; beta > 0; n >= 0]
27 template <bool propto,
28 typename T_n, typename T_N,
29 typename T_size1, typename T_size2>
30 typename return_type<T_size1, T_size2>::type
31 beta_binomial_log(const T_n& n,
32 const T_N& N,
33 const T_size1& alpha,
34 const T_size2& beta) {
35 static const char* function("beta_binomial_log");
36 typedef typename stan::partials_return_type<T_size1, T_size2>::type
37 T_partials_return;
38
39 if (!(stan::length(n)
40 && stan::length(N)
41 && stan::length(alpha)
42 && stan::length(beta)))
43 return 0.0;
44
45 T_partials_return logp(0.0);
46 check_nonnegative(function, "Population size parameter", N);
47 check_positive_finite(function,
48 "First prior sample size parameter", alpha);
49 check_positive_finite(function,
50 "Second prior sample size parameter", beta);
51 check_consistent_sizes(function,
52 "Successes variable", n,
53 "Population size parameter", N,
54 "First prior sample size parameter", alpha,
55 "Second prior sample size parameter", beta);
56
57 if (!include_summand<propto, T_size1, T_size2>::value)
58 return 0.0;
59
60 OperandsAndPartials<T_size1, T_size2>
61 operands_and_partials(alpha, beta);
62
63 VectorView<const T_n> n_vec(n);
64 VectorView<const T_N> N_vec(N);
65 VectorView<const T_size1> alpha_vec(alpha);
66 VectorView<const T_size2> beta_vec(beta);
67 size_t size = max_size(n, N, alpha, beta);
68
69 for (size_t i = 0; i < size; i++) {
70 if (n_vec[i] < 0 || n_vec[i] > N_vec[i])
71 return operands_and_partials.value(LOG_ZERO);
72 }
73
74 VectorBuilder<include_summand<propto>::value,
75 T_partials_return, T_n, T_N>
76 normalizing_constant(max_size(N, n));
77 for (size_t i = 0; i < max_size(N, n); i++)
78 if (include_summand<propto>::value)
79 normalizing_constant[i]
80 = binomial_coefficient_log(N_vec[i], n_vec[i]);
81
82 VectorBuilder<include_summand<propto, T_size1, T_size2>::value,
83 T_partials_return, T_n, T_N, T_size1, T_size2>
84 lbeta_numerator(size);
85 for (size_t i = 0; i < size; i++)
86 if (include_summand<propto, T_size1, T_size2>::value)
87 lbeta_numerator[i] = lbeta(n_vec[i] + value_of(alpha_vec[i]),
88 N_vec[i] - n_vec[i]
89 + value_of(beta_vec[i]));
90
91 VectorBuilder<include_summand<propto, T_size1, T_size2>::value,
92 T_partials_return, T_size1, T_size2>
93 lbeta_denominator(max_size(alpha, beta));
94 for (size_t i = 0; i < max_size(alpha, beta); i++)
95 if (include_summand<propto, T_size1, T_size2>::value)
96 lbeta_denominator[i] = lbeta(value_of(alpha_vec[i]),
97 value_of(beta_vec[i]));
98
99 VectorBuilder<!is_constant_struct<T_size1>::value,
100 T_partials_return, T_n, T_size1>
101 digamma_n_plus_alpha(max_size(n, alpha));
102 for (size_t i = 0; i < max_size(n, alpha); i++)
103 if (!is_constant_struct<T_size1>::value)
104 digamma_n_plus_alpha[i]
105 = digamma(n_vec[i] + value_of(alpha_vec[i]));
106
107 VectorBuilder<contains_nonconstant_struct<T_size1, T_size2>::value,
108 T_partials_return, T_N, T_size1, T_size2>
109 digamma_N_plus_alpha_plus_beta(max_size(N, alpha, beta));
110 for (size_t i = 0; i < max_size(N, alpha, beta); i++)
111 if (contains_nonconstant_struct<T_size1, T_size2>::value)
112 digamma_N_plus_alpha_plus_beta[i]
113 = digamma(N_vec[i] + value_of(alpha_vec[i])
114 + value_of(beta_vec[i]));
115
116 VectorBuilder<contains_nonconstant_struct<T_size1, T_size2>::value,
117 T_partials_return, T_size1, T_size2>
118 digamma_alpha_plus_beta(max_size(alpha, beta));
119 for (size_t i = 0; i < max_size(alpha, beta); i++)
120 if (contains_nonconstant_struct<T_size1, T_size2>::value)
121 digamma_alpha_plus_beta[i]
122 = digamma(value_of(alpha_vec[i]) + value_of(beta_vec[i]));
123
124 VectorBuilder<!is_constant_struct<T_size1>::value,
125 T_partials_return, T_size1> digamma_alpha(length(alpha));
126 for (size_t i = 0; i < length(alpha); i++)
127 if (!is_constant_struct<T_size1>::value)
128 digamma_alpha[i] = digamma(value_of(alpha_vec[i]));
129
130 VectorBuilder<!is_constant_struct<T_size2>::value,
131 T_partials_return, T_size2>
132 digamma_beta(length(beta));
133 for (size_t i = 0; i < length(beta); i++)
134 if (!is_constant_struct<T_size2>::value)
135 digamma_beta[i] = digamma(value_of(beta_vec[i]));
136
137 for (size_t i = 0; i < size; i++) {
138 if (include_summand<propto>::value)
139 logp += normalizing_constant[i];
140 if (include_summand<propto, T_size1, T_size2>::value)
141 logp += lbeta_numerator[i] - lbeta_denominator[i];
142
143 if (!is_constant_struct<T_size1>::value)
144 operands_and_partials.d_x1[i]
145 += digamma_n_plus_alpha[i]
146 - digamma_N_plus_alpha_plus_beta[i]
147 + digamma_alpha_plus_beta[i]
148 - digamma_alpha[i];
149 if (!is_constant_struct<T_size2>::value)
150 operands_and_partials.d_x2[i]
151 += digamma(value_of(N_vec[i]-n_vec[i]+beta_vec[i]))
152 - digamma_N_plus_alpha_plus_beta[i]
153 + digamma_alpha_plus_beta[i]
154 - digamma_beta[i];
155 }
156 return operands_and_partials.value(logp);
157 }
158
159 template <typename T_n,
160 typename T_N,
161 typename T_size1,
162 typename T_size2>
163 typename return_type<T_size1, T_size2>::type
164 beta_binomial_log(const T_n& n, const T_N& N,
165 const T_size1& alpha, const T_size2& beta) {
166 return beta_binomial_log<false>(n, N, alpha, beta);
167 }
168
169 }
170 }
171 #endif
stan::math::OperandsAndPartials::d_x2
VectorView< T_return_type, false, true > d_x2
Definition: OperandsAndPartials.hpp:39
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:15
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:15
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:67
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:172
stan::math::include_summand
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: include_summand.hpp:35
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:37
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:31
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:64
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:38
stan::math::digamma
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:15

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