Stan  1.0
probability, sampling & optimization
beta.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__BETA_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__BETA_HPP__
3 
4 #include <stan/agrad.hpp>
7 #include <stan/meta/traits.hpp>
9 #include <stan/prob/traits.hpp>
10 
11 namespace stan {
12 
13  namespace prob {
14 
36  template <bool propto,
37  typename T_y, typename T_scale_succ, typename T_scale_fail,
38  class Policy>
40  beta_log(const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta,
41  const Policy&) {
42  static const char* function = "stan::prob::beta_log(%1%)";
43 
50  using stan::math::log1m;
53  using boost::math::digamma;
54 
55  // check if any vectors are zero length
56  if (!(stan::length(y)
57  && stan::length(alpha)
58  && stan::length(beta)))
59  return 0.0;
60 
61  // set up return value accumulator
62  double logp(0.0);
63 
64  // validate args (here done over var, which should be OK)
65  if (!check_finite(function, alpha,
66  "First shape parameter",
67  &logp, Policy()))
68  return logp;
69  if (!check_positive(function, alpha,
70  "First shape parameter",
71  &logp, Policy()))
72  return logp;
73  if (!check_finite(function, beta,
74  "Second shape parameter",
75  &logp, Policy()))
76  return logp;
77  if (!check_positive(function, beta,
78  "Second shape parameter",
79  &logp, Policy()))
80  return logp;
81  if (!check_not_nan(function, y, "Random variable", &logp, Policy()))
82  return logp;
83  if (!(check_consistent_sizes(function,
84  y,alpha,beta,
85  "Random variable","First shape parameter","Second shape parameter",
86  &logp, Policy())))
87  return logp;
88 
89  // check if no variables are involved and prop-to
91  return 0.0;
92 
93  VectorView<const T_y> y_vec(y);
94  VectorView<const T_scale_succ> alpha_vec(alpha);
95  VectorView<const T_scale_fail> beta_vec(beta);
96  size_t N = max_size(y, alpha, beta);
97 
98  for (size_t n = 0; n < N; n++) {
99  const double y_dbl = value_of(y_vec[n]);
100  if (y_dbl < 0 || y_dbl > 1)
101  return LOG_ZERO;
102  }
103 
104  // set up template expressions wrapping scalars into vector views
105  agrad::OperandsAndPartials<T_y, T_scale_succ, T_scale_fail> operands_and_partials(y, alpha, beta);
106 
107 
110 
111  for (size_t n = 0; n < length(y); n++) {
113  log_y[n] = log(value_of(y_vec[n]));
115  log1m_y[n] = log1m(value_of(y_vec[n]));
116  }
117 
118  DoubleVectorView<include_summand<propto,T_scale_succ>::value,T_scale_succ> lgamma_alpha(length(alpha));
119  DoubleVectorView<!is_constant_struct<T_scale_succ>::value,T_scale_succ> digamma_alpha(length(alpha));
120  for (size_t n = 0; n < length(alpha); n++) {
122  lgamma_alpha[n] = lgamma(value_of(alpha_vec[n]));
124  digamma_alpha[n] = digamma(value_of(alpha_vec[n]));
125  }
126 
127 
129  DoubleVectorView<!is_constant_struct<T_scale_fail>::value,T_scale_fail> digamma_beta(length(beta));
130  for (size_t n = 0; n < length(beta); n++) {
132  lgamma_beta[n] = lgamma(value_of(beta_vec[n]));
134  digamma_beta[n] = digamma(value_of(beta_vec[n]));
135  }
136 
138  double,
140  lgamma_alpha_beta(max_size(alpha,beta));
142  double,
144  digamma_alpha_beta(max_size(alpha,beta));
145  for (size_t n = 0; n < max_size(alpha,beta); n++) {
146  const double alpha_beta = value_of(alpha_vec[n]) + value_of(beta_vec[n]);
148  lgamma_alpha_beta[n] = lgamma(alpha_beta);
150  digamma_alpha_beta[n] = digamma(alpha_beta);
151  }
152 
153  for (size_t n = 0; n < N; n++) {
154  // pull out values of arguments
155  const double y_dbl = value_of(y_vec[n]);
156  const double alpha_dbl = value_of(alpha_vec[n]);
157  const double beta_dbl = value_of(beta_vec[n]);
158 
159  // log probability
161  logp += lgamma_alpha_beta[n];
163  logp -= lgamma_alpha[n];
165  logp -= lgamma_beta[n];
167  logp += (alpha_dbl-1.0) * log_y[n];
169  logp += (beta_dbl-1.0) * log1m_y[n];
170 
171  // gradients
173  operands_and_partials.d_x1[n] += (alpha_dbl-1)/y_dbl + (beta_dbl-1)/(y_dbl-1);
175  operands_and_partials.d_x2[n] += log_y[n] + digamma_alpha_beta[n] - digamma_alpha[n];
177  operands_and_partials.d_x3[n] += log1m_y[n] + digamma_alpha_beta[n] - digamma_beta[n];
178  }
179  return operands_and_partials.to_var(logp);
180  }
181 
182  template <bool propto,
183  typename T_y, typename T_scale_succ, typename T_scale_fail>
184  inline
186  beta_log(const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
187  return beta_log<propto>(y,alpha,beta,stan::math::default_policy());
188  }
189 
190  template <typename T_y, typename T_scale_succ, typename T_scale_fail,
191  class Policy>
193  beta_log(const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta,
194  const Policy&) {
195  return beta_log<false>(y,alpha,beta,Policy());
196  }
197 
198  template <typename T_y, typename T_scale_succ, typename T_scale_fail>
199  inline
201  beta_log(const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
202  return beta_log<false>(y,alpha,beta,stan::math::default_policy());
203  }
204 
205 
219  template <typename T_y, typename T_scale_succ, typename T_scale_fail,
220  class Policy>
222  beta_cdf(const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta,
223  const Policy&) {
224  static const char* function = "stan::prob::beta_cdf(%1%)";
225 
229  using boost::math::tools::promote_args;
230 
231  typename promote_args<T_y,T_scale_succ,T_scale_fail>::type lp;
232  if (!check_finite(function, alpha,
233  "First shape parameter",
234  &lp, Policy()))
235  return lp;
236  if (!check_positive(function, alpha,
237  "First shape parameter",
238  &lp, Policy()))
239  return lp;
240  if (!check_finite(function, beta,
241  "Second shape parameter",
242  &lp, Policy()))
243  return lp;
244  if (!check_positive(function, beta,
245  "Second shape parameter",
246  &lp, Policy()))
247  return lp;
248  if (!check_not_nan(function, y, "Random variable", &lp, Policy()))
249  return lp;
250 
251  if (y < 0.0)
252  return 0;
253  if (y > 1.0)
254  return 1.0;
255 
256  return stan::math::ibeta(alpha, beta, y);
257  }
258 
259  template <typename T_y, typename T_scale_succ, typename T_scale_fail>
261  beta_cdf(const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
262  return beta_cdf(y,alpha,beta,stan::math::default_policy());
263  }
264  }
265 }
266 #endif
var log1m(const stan::agrad::var &a)
The log (1 - x) function for variables.
double value_of(const agrad::var &v)
Return the value of the specified variable.
var lgamma(const stan::agrad::var &a)
The log gamma function for variables (C99).
var log(const var &a)
Return the natural log of the specified variable (cmath).
Definition: agrad.hpp:1730
double ibeta(const double &a, const double &b, const double &x)
The normalized incomplete beta function of a, b, and x.
boost::math::tools::promote_args< T >::type log1m(T x)
Return the natural logarithm of one minus the specified value.
boost::math::tools::promote_args< T_a, T_b >::type multiply_log(T_a a, T_b b)
double value_of(T x)
Return the value of the specified scalar argument converted to a double value.
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is nan.
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result, const Policy &)
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is finite.
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
return_type< T_y, T_scale_succ, T_scale_fail >::type beta_log(const T_y &y, const T_scale_succ &alpha, const T_scale_fail &beta, const Policy &)
The log of the beta density for the specified scalar(s) given the specified sample size(s).
Definition: beta.hpp:40
return_type< T_y, T_scale_succ, T_scale_fail >::type beta_cdf(const T_y &y, const T_scale_succ &alpha, const T_scale_fail &beta, const Policy &)
Calculates the beta cumulative distribution function for the given variate and scale variables.
Definition: beta.hpp:222
Probability, optimization and sampling library.
Definition: agrad.cpp:6
size_t length(const T &x)
Definition: traits.hpp:111
size_t max_size(const T1 &x1, const T2 &x2)
Definition: traits.hpp:148
A variable implementation that stores operands and derivatives with respect to the variable.
VectorView< double *, is_vector< T1 >::value > d_x1
VectorView< double *, is_vector< T2 >::value > d_x2
T_return_type to_var(double logp)
VectorView< double *, is_vector< T3 >::value > d_x3
Metaprogram to determine if a type has a base scalar type that can be assigned to type double.
Definition: traits.hpp:39
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:33
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: traits.hpp:368

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