Automatic Differentiation
 
Loading...
Searching...
No Matches
loglogistic_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_LOGLOGISTIC_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_LOGLOGISTIC_LPDF_HPP
3
17#include <cmath>
18
19namespace stan {
20namespace math {
21
42template <bool propto, typename T_y, typename T_scale, typename T_shape,
44 T_y, T_scale, T_shape>* = nullptr>
46 const T_scale& alpha,
47 const T_shape& beta) {
48 using T_partials_return = partials_return_t<T_y, T_scale, T_shape>;
49 using T_y_ref = ref_type_if_not_constant_t<T_y>;
50 using T_scale_ref = ref_type_if_not_constant_t<T_scale>;
51 using T_shape_ref = ref_type_if_not_constant_t<T_shape>;
52 using std::pow;
53 static constexpr const char* function = "loglogistic_lpdf";
54 check_consistent_sizes(function, "Random variable", y, "Scale parameter",
55 alpha, "Shape parameter", beta);
56
57 T_y_ref y_ref = y;
58 T_scale_ref alpha_ref = alpha;
59 T_shape_ref beta_ref = beta;
60
61 decltype(auto) y_val = to_ref(as_value_column_array_or_scalar(y_ref));
62 decltype(auto) alpha_val = to_ref(as_value_column_array_or_scalar(alpha_ref));
63 decltype(auto) beta_val = to_ref(as_value_column_array_or_scalar(beta_ref));
64
65 check_positive_finite(function, "Random variable", y_val);
66 check_positive_finite(function, "Scale parameter", alpha_val);
67 check_positive_finite(function, "Shape parameter", beta_val);
68
69 if (size_zero(y, alpha, beta)) {
70 return 0.0;
71 }
73 return 0.0;
74 }
75
76 auto ops_partials = make_partials_propagator(y_ref, alpha_ref, beta_ref);
77
78 const auto& inv_alpha
79 = to_ref_if<!is_constant_all<T_y, T_scale>::value>(inv(alpha_val));
80 const auto& y_div_alpha
81 = to_ref_if<!is_constant_all<T_shape>::value>(y_val * inv_alpha);
82 const auto& y_div_alpha_pow_beta
83 = to_ref_if<!is_constant_all<T_shape>::value>(pow(y_div_alpha, beta_val));
84 const auto& log1_arg
85 = to_ref_if<!is_constant_all<T_y, T_scale, T_shape>::value>(
86 1 + y_div_alpha_pow_beta);
87 const auto& log_y = to_ref_if<!is_constant_all<T_shape>::value>(log(y_val));
88 const auto& log_alpha
89 = to_ref_if<include_summand<propto, T_scale, T_shape>::value>(
90 log(alpha_val));
91 const auto& beta_minus_one
93 || !is_constant_all<T_y>::value)>(beta_val - 1.0);
94
95 size_t N = max_size(y, alpha, beta);
96 size_t N_alpha_beta = max_size(alpha, beta);
97
98 T_partials_return logp = sum(beta_minus_one * log_y - 2.0 * log(log1_arg));
99
101 logp += sum(N * (log(beta_val) - log_alpha - beta_minus_one * log_alpha)
102 / N_alpha_beta);
103 }
104
106 const auto& two_inv_log1_arg
107 = to_ref_if<!is_constant_all<T_y>::value
110 >= 2>(2.0 * inv(log1_arg));
112 const auto& y_pow_beta = to_ref_if<!is_constant_all<T_y, T_scale>::value>(
113 pow(y_val, beta_val));
114 const auto& inv_alpha_pow_beta
115 = to_ref_if < !is_constant_all<T_y>::value
116 && !is_constant_all<T_scale>::value > (pow(inv_alpha, beta_val));
117
119 const auto& inv_y = inv(y_val);
120 const auto& y_deriv = beta_minus_one * inv_y
121 - two_inv_log1_arg
122 * (beta_val * inv_alpha_pow_beta)
123 * y_pow_beta * inv_y;
124 partials<0>(ops_partials) = y_deriv;
125 }
127 const auto& alpha_deriv = -beta_val * inv_alpha
128 - two_inv_log1_arg * y_pow_beta * (-beta_val)
129 * inv_alpha_pow_beta * inv_alpha;
130 partials<1>(ops_partials) = alpha_deriv;
131 }
132 }
134 const auto& beta_deriv
135 = (1.0 * inv(beta_val)) + log_y - log_alpha
136 - two_inv_log1_arg * y_div_alpha_pow_beta * log(y_div_alpha);
137 partials<2>(ops_partials) = beta_deriv;
138 }
139 }
140 return ops_partials.build(logp);
141}
142
143template <typename T_y, typename T_scale, typename T_shape>
145 const T_y& y, const T_scale& alpha, const T_shape& beta) {
146 return loglogistic_lpdf<false>(y, alpha, beta);
147}
148
149} // namespace math
150} // namespace stan
151#endif
require_all_not_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_not_nonscalar_prim_or_rev_kernel_expression_t
Require none of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
return_type_t< T_y, T_scale, T_shape > loglogistic_lpdf(const T_y &y, const T_scale &alpha, const T_shape &beta)
The log of the loglogistic density for the specified scalar(s) given the specified scales(s) and shap...
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
T to_ref_if(T &&a)
No-op that should be optimized away.
Definition to_ref.hpp:29
auto pow(const T1 &x1, const T2 &x2)
Definition pow.hpp:32
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
auto as_value_column_array_or_scalar(T &&a)
Extract the value from an object and for eigen vectors and std::vectors convert to an eigen column ar...
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
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.
typename ref_type_if<!is_constant< T >::value, T >::type ref_type_if_not_constant_t
Definition ref_type.hpp:62
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...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...