Stan Math Library  2.20.0
reverse mode automatic differentiation
beta_lccdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_LCCDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_LCCDF_HPP
3 
16 #include <cmath>
17 
18 namespace stan {
19 namespace math {
20 
39 template <typename T_y, typename T_scale_succ, typename T_scale_fail>
41  const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
42  typedef
44  T_partials_return;
45 
46  if (size_zero(y, alpha, beta))
47  return 0.0;
48 
49  static const char* function = "beta_lccdf";
50 
51  T_partials_return ccdf_log(0.0);
52 
53  check_positive_finite(function, "First shape parameter", alpha);
54  check_positive_finite(function, "Second shape parameter", beta);
55  check_not_nan(function, "Random variable", y);
56  check_nonnegative(function, "Random variable", y);
57  check_less_or_equal(function, "Random variable", y, 1);
58  check_consistent_sizes(function, "Random variable", y,
59  "First shape parameter", alpha,
60  "Second shape parameter", beta);
61 
62  scalar_seq_view<T_y> y_vec(y);
63  scalar_seq_view<T_scale_succ> alpha_vec(alpha);
64  scalar_seq_view<T_scale_fail> beta_vec(beta);
65  size_t N = max_size(y, alpha, beta);
66 
68  beta);
69 
70  using std::exp;
71  using std::log;
72  using std::pow;
73 
75  T_partials_return, T_scale_succ, T_scale_fail>
76  digamma_alpha_vec(max_size(alpha, beta));
78  T_partials_return, T_scale_succ, T_scale_fail>
79  digamma_beta_vec(max_size(alpha, beta));
81  T_partials_return, T_scale_succ, T_scale_fail>
82  digamma_sum_vec(max_size(alpha, beta));
83 
85  for (size_t i = 0; i < max_size(alpha, beta); i++) {
86  const T_partials_return alpha_dbl = value_of(alpha_vec[i]);
87  const T_partials_return beta_dbl = value_of(beta_vec[i]);
88 
89  digamma_alpha_vec[i] = digamma(alpha_dbl);
90  digamma_beta_vec[i] = digamma(beta_dbl);
91  digamma_sum_vec[i] = digamma(alpha_dbl + beta_dbl);
92  }
93  }
94 
95  for (size_t n = 0; n < N; n++) {
96  const T_partials_return y_dbl = value_of(y_vec[n]);
97  const T_partials_return alpha_dbl = value_of(alpha_vec[n]);
98  const T_partials_return beta_dbl = value_of(beta_vec[n]);
99  const T_partials_return betafunc_dbl
100  = stan::math::beta(alpha_dbl, beta_dbl);
101 
102  const T_partials_return Pn = 1.0 - inc_beta(alpha_dbl, beta_dbl, y_dbl);
103 
104  ccdf_log += log(Pn);
105 
107  ops_partials.edge1_.partials_[n] -= pow(1 - y_dbl, beta_dbl - 1)
108  * pow(y_dbl, alpha_dbl - 1)
109  / betafunc_dbl / Pn;
110 
111  T_partials_return g1 = 0;
112  T_partials_return g2 = 0;
113 
115  grad_reg_inc_beta(g1, g2, alpha_dbl, beta_dbl, y_dbl,
116  digamma_alpha_vec[n], digamma_beta_vec[n],
117  digamma_sum_vec[n], betafunc_dbl);
118  }
120  ops_partials.edge2_.partials_[n] -= g1 / Pn;
122  ops_partials.edge3_.partials_[n] -= g2 / Pn;
123  }
124  return ops_partials.build(ccdf_log);
125 }
126 
127 } // namespace math
128 } // namespace stan
129 #endif
void check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high)
Check if y is less or equal to high.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:17
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Definition: conjunction.hpp:14
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This template builds partial derivatives with respect to a set of operands.
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition: size_zero.hpp:18
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition: inc_beta.hpp:18
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
Definition: beta.hpp:51
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
void grad_reg_inc_beta(T &g1, T &g2, const T &a, const T &b, const T &z, const T &digammaA, const T &digammaB, const T &digammaSum, const T &betaAB)
Computes the gradients of the regularized incomplete beta function.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
VectorBuilder allocates type T1 values to be used as intermediate values.
internal::ops_partials_edge< double, Op2 > edge2_
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:16
return_type< T_y, T_scale_succ, T_scale_fail >::type beta_lccdf(const T_y &y, const T_scale_succ &alpha, const T_scale_fail &beta)
Returns the beta log complementary cumulative distribution function for the given probability...
Definition: beta_lccdf.hpp:40
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
internal::ops_partials_edge< double, Op3 > edge3_
internal::ops_partials_edge< double, Op1 > edge1_
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition: digamma.hpp:23

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