Automatic Differentiation
 
Loading...
Searching...
No Matches
beta_proportion_lccdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BETA_PROPORTION_LCCDF_HPP
2#define STAN_MATH_PRIM_PROB_BETA_PROPORTION_LCCDF_HPP
3
18#include <cmath>
19
20namespace stan {
21namespace math {
22
43template <typename T_y, typename T_loc, typename T_prec>
45 const T_loc& mu,
46 const T_prec& kappa) {
47 using T_partials_return = partials_return_t<T_y, T_loc, T_prec>;
48 using std::exp;
49 using std::log;
50 using std::pow;
51 using T_y_ref = ref_type_t<T_y>;
52 using T_mu_ref = ref_type_t<T_loc>;
53 using T_kappa_ref = ref_type_t<T_prec>;
54 static constexpr const char* function = "beta_proportion_lccdf";
55 check_consistent_sizes(function, "Random variable", y, "Location parameter",
56 mu, "Precision parameter", kappa);
57 if (size_zero(y, mu, kappa)) {
58 return 0;
59 }
60
61 T_y_ref y_ref = y;
62 T_mu_ref mu_ref = mu;
63 T_kappa_ref kappa_ref = kappa;
64 check_positive(function, "Location parameter", value_of(mu_ref));
65 check_less(function, "Location parameter", value_of(mu_ref), 1.0);
66 check_positive_finite(function, "Precision parameter", value_of(kappa_ref));
67 check_bounded(function, "Random variable", value_of(y_ref), 0.0, 1.0);
68
69 T_partials_return ccdf_log(0.0);
70 auto ops_partials = make_partials_propagator(y_ref, mu_ref, kappa_ref);
71
72 scalar_seq_view<T_y_ref> y_vec(y_ref);
73 scalar_seq_view<T_mu_ref> mu_vec(mu_ref);
74 scalar_seq_view<T_kappa_ref> kappa_vec(kappa_ref);
75 size_t size_kappa = stan::math::size(kappa);
76 size_t size_mu_kappa = max_size(mu, kappa);
77 size_t N = max_size(y, mu, kappa);
78
80 T_loc, T_prec>
81 digamma_mukappa(size_mu_kappa);
83 T_loc, T_prec>
84 digamma_kappa_mukappa(size_mu_kappa);
86 T_prec>
87 digamma_kappa(size_kappa);
88
90 for (size_t i = 0; i < size_mu_kappa; i++) {
91 const T_partials_return kappa_dbl = kappa_vec.val(i);
92 const T_partials_return mukappa_dbl = mu_vec.val(i) * kappa_dbl;
93 digamma_mukappa[i] = digamma(mukappa_dbl);
94 digamma_kappa_mukappa[i] = digamma(kappa_dbl - mukappa_dbl);
95 }
96
97 for (size_t i = 0; i < size_kappa; i++) {
98 digamma_kappa[i] = digamma(kappa_vec.val(i));
99 }
100 }
101
102 for (size_t n = 0; n < N; n++) {
103 const T_partials_return y_dbl = y_vec.val(n);
104 const T_partials_return mu_dbl = mu_vec.val(n);
105 const T_partials_return kappa_dbl = kappa_vec.val(n);
106 const T_partials_return mukappa_dbl = mu_dbl * kappa_dbl;
107 const T_partials_return kappa_mukappa_dbl = kappa_dbl - mukappa_dbl;
108 const T_partials_return betafunc_dbl = beta(mukappa_dbl, kappa_mukappa_dbl);
109 const T_partials_return Pn
110 = 1 - inc_beta(mukappa_dbl, kappa_mukappa_dbl, y_dbl);
111
112 ccdf_log += log(Pn);
113
114 const T_partials_return inv_Pn
116
118 partials<0>(ops_partials)[n] -= pow(1 - y_dbl, kappa_mukappa_dbl - 1)
119 * pow(y_dbl, mukappa_dbl - 1) * inv_Pn
120 / betafunc_dbl;
121 }
122
123 T_partials_return g1 = 0;
124 T_partials_return g2 = 0;
125
127 grad_reg_inc_beta(g1, g2, mukappa_dbl, kappa_mukappa_dbl, y_dbl,
128 digamma_mukappa[n], digamma_kappa_mukappa[n],
129 digamma_kappa[n], betafunc_dbl);
130 }
132 partials<1>(ops_partials)[n] -= kappa_dbl * (g1 - g2) * inv_Pn;
133 }
135 partials<2>(ops_partials)[n]
136 -= (g1 * mu_dbl + g2 * (1 - mu_dbl)) * inv_Pn;
137 }
138 }
139 return ops_partials.build(ccdf_log);
140}
141
142} // namespace math
143} // namespace stan
144#endif
VectorBuilder allocates type T1 values to be used as intermediate values.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
return_type_t< T_y, T_loc, T_prec > beta_proportion_lccdf(const T_y &y, const T_loc &mu, const T_prec &kappa)
Returns the beta log complementary cumulative distribution function for specified probability,...
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
auto pow(const T1 &x1, const T2 &x2)
Definition pow.hpp:32
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
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.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition inc_beta.hpp:19
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
void check_less(const char *function, const char *name, const T_y &y, const T_high &high, Idxs... idxs)
Throw an exception if y is not strictly less than high.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
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
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:13
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition digamma.hpp:23
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
typename partials_return_type< Args... >::type partials_return_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...