1#ifndef STAN_MATH_PRIM_PROB_LOGLOGISTIC_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_LOGLOGISTIC_LPDF_HPP
42template <
bool propto,
typename T_y,
typename T_scale,
typename T_shape,
44 T_y, T_scale, T_shape>* =
nullptr>
46 const T_y& y,
const T_scale& alpha,
const T_shape&
beta) {
52 static constexpr const char* function =
"loglogistic_lpdf";
54 alpha,
"Shape parameter",
beta);
57 T_scale_ref alpha_ref = alpha;
58 T_shape_ref beta_ref =
beta;
78 = to_ref_if<is_any_autodiff_v<T_y, T_scale>>(
inv(alpha_val));
79 const auto& y_div_alpha
80 = to_ref_if<is_autodiff_v<T_shape>>(y_val * inv_alpha);
81 const auto& y_div_alpha_pow_beta
82 = to_ref_if<is_autodiff_v<T_shape>>(
pow(y_div_alpha, beta_val));
83 const auto& log1_arg = to_ref_if<is_any_autodiff_v<T_y, T_scale, T_shape>>(
84 1 + y_div_alpha_pow_beta);
85 const auto& log_y = to_ref_if<is_autodiff_v<T_shape>>(
log(y_val));
87 = to_ref_if<include_summand<propto, T_scale, T_shape>::value>(
89 const auto& beta_minus_one
91 || is_autodiff_v<T_y>)>(beta_val - 1.0);
96 T_partials_return logp =
sum(beta_minus_one * log_y - 2.0 *
log(log1_arg));
99 logp +=
sum(N * (
log(beta_val) - log_alpha - beta_minus_one * log_alpha)
103 if constexpr (is_any_autodiff_v<T_y, T_scale, T_shape>) {
104 const auto& two_inv_log1_arg =
to_ref_if<
106 T_y> + is_autodiff_v<T_scale> + is_autodiff_v<T_shape> >= 2>(
107 2.0 *
inv(log1_arg));
108 if constexpr (is_any_autodiff_v<T_y, T_scale>) {
109 const auto& y_pow_beta
110 = to_ref_if<is_any_autodiff_v<T_y, T_scale>>(
pow(y_val, beta_val));
111 const auto& inv_alpha_pow_beta
113 T_y> && is_autodiff_v<T_scale> > (
pow(inv_alpha, beta_val));
115 if constexpr (is_autodiff_v<T_y>) {
116 const auto& inv_y =
inv(y_val);
117 const auto& y_deriv = beta_minus_one * inv_y
119 * (beta_val * inv_alpha_pow_beta)
120 * y_pow_beta * inv_y;
121 partials<0>(ops_partials) = y_deriv;
123 if constexpr (is_autodiff_v<T_scale>) {
124 const auto& alpha_deriv = -beta_val * inv_alpha
125 - two_inv_log1_arg * y_pow_beta * (-beta_val)
126 * inv_alpha_pow_beta * inv_alpha;
127 partials<1>(ops_partials) = alpha_deriv;
130 if constexpr (is_autodiff_v<T_shape>) {
131 const auto& beta_deriv
132 = (1.0 *
inv(beta_val)) + log_y - log_alpha
133 - two_inv_log1_arg * y_div_alpha_pow_beta *
log(y_div_alpha);
134 partials<2>(ops_partials) = beta_deriv;
137 return ops_partials.build(logp);
140template <
typename T_y,
typename T_scale,
typename T_shape>
142 const T_y& y,
const T_scale& alpha,
const T_shape&
beta) {
143 return loglogistic_lpdf<false>(y, alpha,
beta);
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.
T to_ref_if(T &&a)
No-op that should be optimized away.
auto pow(const T1 &x1, const T2 &x2)
fvar< T > log(const fvar< T > &x)
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.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
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.
fvar< T > inv(const fvar< T > &x)
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_autodiff_v< T >, T >::type ref_type_if_not_constant_t
constexpr bool is_autodiff_v
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 ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...