| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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/OperandsAndPartials.hpp>
5 #include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
6 #include <stan/math/prim/scal/err/check_nonnegative.hpp>
7 #include <stan/math/prim/scal/err/check_positive_finite.hpp>
8 #include <stan/math/prim/scal/fun/constants.hpp>
9 #include <stan/math/prim/scal/fun/lbeta.hpp>
10 #include <stan/math/prim/scal/fun/digamma.hpp>
11 #include <stan/math/prim/scal/fun/lgamma.hpp>
12 #include <stan/math/prim/scal/fun/binomial_coefficient_log.hpp>
13 #include <stan/math/prim/scal/fun/value_of.hpp>
14 #include <stan/math/prim/scal/meta/contains_nonconstant_struct.hpp>
15 #include <stan/math/prim/scal/meta/VectorBuilder.hpp>
16 #include <stan/math/prim/scal/meta/include_summand.hpp>
17 #include <stan/math/prim/scal/prob/beta_rng.hpp>
18 #include <stan/math/prim/scal/fun/F32.hpp>
19 #include <stan/math/prim/scal/fun/grad_F32.hpp>
20
21 namespace stan {
22
23 namespace math {
24
25 // BetaBinomial(n|alpha, beta) [alpha > 0; beta > 0; n >= 0]
26 template <bool propto,
27 typename T_n, typename T_N,
28 typename T_size1, typename T_size2>
29 typename return_type<T_size1, T_size2>::type
30 beta_binomial_log(const T_n& n,
31 const T_N& N,
32 const T_size1& alpha,
33 const T_size2& beta) {
34 static const char* function("stan::math::beta_binomial_log");
35 typedef typename stan::partials_return_type<T_size1, T_size2>::type
36 T_partials_return;
37
38 using stan::math::check_positive_finite;
39 using stan::math::check_nonnegative;
40 using stan::math::value_of;
41 using stan::math::check_consistent_sizes;
42 using stan::math::include_summand;
43
44 // check if any vectors are zero length
45 if (!(stan::length(n)
46 && stan::length(N)
47 && stan::length(alpha)
48 && stan::length(beta)))
49 return 0.0;
50
51 T_partials_return logp(0.0);
52 check_nonnegative(function, "Population size parameter", N);
53 check_positive_finite(function,
54 "First prior sample size parameter", alpha);
55 check_positive_finite(function,
56 "Second prior sample size parameter", beta);
57 check_consistent_sizes(function,
58 "Successes variable", n,
59 "Population size parameter", N,
60 "First prior sample size parameter", alpha,
61 "Second prior sample size parameter", beta);
62
63 // check if no variables are involved and prop-to
64 if (!include_summand<propto, T_size1, T_size2>::value)
65 return 0.0;
66
67 OperandsAndPartials<T_size1, T_size2>
68 operands_and_partials(alpha, beta);
69
70 VectorView<const T_n> n_vec(n);
71 VectorView<const T_N> N_vec(N);
72 VectorView<const T_size1> alpha_vec(alpha);
73 VectorView<const T_size2> beta_vec(beta);
74 size_t size = max_size(n, N, alpha, beta);
75
76 for (size_t i = 0; i < size; i++) {
77 if (n_vec[i] < 0 || n_vec[i] > N_vec[i])
78 return operands_and_partials.to_var(LOG_ZERO, alpha, beta);
79 }
80
81 using stan::math::lbeta;
82 using stan::math::binomial_coefficient_log;
83 using stan::math::digamma;
84
85 VectorBuilder<include_summand<propto>::value,
86 T_partials_return, T_n, T_N>
87 normalizing_constant(max_size(N, n));
88 for (size_t i = 0; i < max_size(N, n); i++)
89 if (include_summand<propto>::value)
90 normalizing_constant[i]
91 = binomial_coefficient_log(N_vec[i], n_vec[i]);
92
93 VectorBuilder<include_summand<propto, T_size1, T_size2>::value,
94 T_partials_return, T_n, T_N, T_size1, T_size2>
95 lbeta_numerator(size);
96 for (size_t i = 0; i < size; i++)
97 if (include_summand<propto, T_size1, T_size2>::value)
98 lbeta_numerator[i] = lbeta(n_vec[i] + value_of(alpha_vec[i]),
99 N_vec[i] - n_vec[i]
100 + value_of(beta_vec[i]));
101
102 VectorBuilder<include_summand<propto, T_size1, T_size2>::value,
103 T_partials_return, T_size1, T_size2>
104 lbeta_denominator(max_size(alpha, beta));
105 for (size_t i = 0; i < max_size(alpha, beta); i++)
106 if (include_summand<propto, T_size1, T_size2>::value)
107 lbeta_denominator[i] = lbeta(value_of(alpha_vec[i]),
108 value_of(beta_vec[i]));
109
110 VectorBuilder<!is_constant_struct<T_size1>::value,
111 T_partials_return, T_n, T_size1>
112 digamma_n_plus_alpha(max_size(n, alpha));
113 for (size_t i = 0; i < max_size(n, alpha); i++)
114 if (!is_constant_struct<T_size1>::value)
115 digamma_n_plus_alpha[i]
116 = digamma(n_vec[i] + value_of(alpha_vec[i]));
117
118 VectorBuilder<contains_nonconstant_struct<T_size1, T_size2>::value,
119 T_partials_return, T_N, T_size1, T_size2>
120 digamma_N_plus_alpha_plus_beta(max_size(N, alpha, beta));
121 for (size_t i = 0; i < max_size(N, alpha, beta); i++)
122 if (contains_nonconstant_struct<T_size1, T_size2>::value)
123 digamma_N_plus_alpha_plus_beta[i]
124 = digamma(N_vec[i] + value_of(alpha_vec[i])
125 + value_of(beta_vec[i]));
126
127 VectorBuilder<contains_nonconstant_struct<T_size1, T_size2>::value,
128 T_partials_return, T_size1, T_size2>
129 digamma_alpha_plus_beta(max_size(alpha, beta));
130 for (size_t i = 0; i < max_size(alpha, beta); i++)
131 if (contains_nonconstant_struct<T_size1, T_size2>::value)
132 digamma_alpha_plus_beta[i]
133 = digamma(value_of(alpha_vec[i]) + value_of(beta_vec[i]));
134
135 VectorBuilder<!is_constant_struct<T_size1>::value,
136 T_partials_return, T_size1> digamma_alpha(length(alpha));
137 for (size_t i = 0; i < length(alpha); i++)
138 if (!is_constant_struct<T_size1>::value)
139 digamma_alpha[i] = digamma(value_of(alpha_vec[i]));
140
141 VectorBuilder<!is_constant_struct<T_size2>::value,
142 T_partials_return, T_size2>
143 digamma_beta(length(beta));
144 for (size_t i = 0; i < length(beta); i++)
145 if (!is_constant_struct<T_size2>::value)
146 digamma_beta[i] = digamma(value_of(beta_vec[i]));
147
148 for (size_t i = 0; i < size; i++) {
149 if (include_summand<propto>::value)
150 logp += normalizing_constant[i];
151 if (include_summand<propto, T_size1, T_size2>::value)
152 logp += lbeta_numerator[i] - lbeta_denominator[i];
153
154 if (!is_constant_struct<T_size1>::value)
155 operands_and_partials.d_x1[i]
156 += digamma_n_plus_alpha[i]
157 - digamma_N_plus_alpha_plus_beta[i]
158 + digamma_alpha_plus_beta[i]
159 - digamma_alpha[i];
160 if (!is_constant_struct<T_size2>::value)
161 operands_and_partials.d_x2[i]
162 += digamma(value_of(N_vec[i]-n_vec[i]+beta_vec[i]))
163 - digamma_N_plus_alpha_plus_beta[i]
164 + digamma_alpha_plus_beta[i]
165 - digamma_beta[i];
166 }
167 return operands_and_partials.to_var(logp, alpha, beta);
168 }
169
170 template <typename T_n,
171 typename T_N,
172 typename T_size1,
173 typename T_size2>
174 typename return_type<T_size1, T_size2>::type
175 beta_binomial_log(const T_n& n, const T_N& N,
176 const T_size1& alpha, const T_size2& beta) {
177 return beta_binomial_log<false>(n, N, alpha, beta);
178 }
179
180 }
181 }
182 #endif
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::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
contains_nonconstant_struct.hpp
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
binomial_coefficient_log.hpp
stan::math::OperandsAndPartials::to_var
T_return_type to_var(T_partials_return logp, const T1 &x1=0, const T2 &x2=0, const T3 &x3=0, const T4 &x4=0, const T5 &x5=0, const T6 &x6=0)
Definition: OperandsAndPartials.hpp:322
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
stan::math::OperandsAndPartials::d_x1
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
Definition: OperandsAndPartials.hpp:253
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:16
beta_rng.hpp
stan::math::OperandsAndPartials
A variable implementation that stores operands and derivatives with respect to the variable...
Definition: OperandsAndPartials.hpp:238
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:30
OperandsAndPartials.hpp
stan::max_size
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
VectorBuilder.hpp
stan::contains_nonconstant_struct
Definition: contains_nonconstant_struct.hpp:14
grad_F32.hpp
stan::VectorBuilder
Definition: VectorBuilder.hpp:61
constants.hpp
stan::math::size
int size(const std::vector< T > &x)
Definition: size.hpp:11
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::OperandsAndPartials::d_x2
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
Definition: OperandsAndPartials.hpp:256
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 metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
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::digamma
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:16

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