1#ifndef STAN_MATH_PRIM_PROB_NORMAL_SUFFICIENT_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_NORMAL_SUFFICIENT_LPDF_HPP
52template <
bool propto,
typename T_y,
typename T_s,
typename T_n,
typename T_loc,
55 const T_y& y_bar,
const T_s& s_squared,
const T_n& n_obs,
const T_loc& mu,
56 const T_scale& sigma) {
63 static constexpr const char* function =
"normal_sufficient_lpdf";
65 y_bar,
"Scale parameter sufficient statistic",
66 s_squared,
"Number of observations", n_obs,
67 "Location parameter", mu,
"Scale parameter", sigma);
69 T_y_ref y_ref = y_bar;
70 T_s_ref s_squared_ref = s_squared;
71 T_n_ref n_obs_ref = n_obs;
73 T_sigma_ref sigma_ref = sigma;
76 decltype(
auto) s_squared_val
78 decltype(
auto) n_obs_val_int
80 decltype(
auto) n_obs_val =
to_ref(
85 check_finite(function,
"Location parameter sufficient statistic", y_val);
86 check_finite(function,
"Scale parameter sufficient statistic", s_squared_val);
93 if (
size_zero(y_bar, s_squared, n_obs, mu, sigma)) {
100 const auto& sigma_squared
101 = to_ref_if<!is_constant_all<T_y, T_loc, T_s, T_scale>::value>(
103 const auto& diff =
to_ref(mu_val - y_val);
104 const auto& cons_expr = to_ref_if<!is_constant_all<T_scale>::value>(
105 s_squared_val + n_obs_val * diff * diff);
107 size_t N =
max_size(y_bar, s_squared, n_obs, mu, sigma);
108 T_partials_return logp = -
sum(cons_expr / (2 * sigma_squared));
113 logp -=
sum(n_obs_val *
log(sigma_val)) * N /
max_size(n_obs, sigma);
121 N /
max_size(y_bar, mu, n_obs, sigma) * n_obs_val / sigma_squared
124 partials<2>(ops_partials) = -common_derivative;
127 partials<0>(ops_partials) = std::move(common_derivative);
131 using T_sigma_value_scalar =
scalar_type_t<
decltype(sigma_val)>;
132 using T_sigma_value_vector
133 = Eigen::Array<T_sigma_value_scalar, Eigen::Dynamic, 1>;
135 edge<1>(ops_partials).partials_
136 = -0.5 / forward_as<T_sigma_value_vector>(sigma_squared);
139 partials<1>(ops_partials) = T_sigma_value_vector::Constant(
140 N, -0.5 / forward_as<T_sigma_value_scalar>(sigma_squared));
142 forward_as<internal::broadcast_array<T_partials_return>>(
143 partials<1>(ops_partials))
144 = -0.5 / sigma_squared * N /
math::size(sigma);
149 edge<3>(ops_partials).partials_
150 = (cons_expr / sigma_squared - n_obs_val) / sigma_val;
152 return ops_partials.build(logp);
155template <
typename T_y,
typename T_s,
typename T_n,
typename T_loc,
158 const T_y& y_bar,
const T_s& s_squared,
const T_n& n_obs,
const T_loc& mu,
159 const T_scale& sigma) {
160 return normal_sufficient_lpdf<false>(y_bar, s_squared, n_obs, mu, sigma);
return_type_t< T_y, T_s, T_loc, T_scale > normal_sufficient_lpdf(const T_y &y_bar, const T_s &s_squared, const T_n &n_obs, const T_loc &mu, const T_scale &sigma)
The log of the normal density for the specified scalar(s) given the specified mean(s) and deviation(s...
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>>.
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
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.
fvar< T > log(const fvar< T > &x)
const double NEG_LOG_SQRT_TWO_PI
The value of minus the natural logarithm of the square root of , .
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.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
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 > square(const fvar< T > &x)
typename ref_type_if<!is_constant< T >::value, T >::type ref_type_if_not_constant_t
typename scalar_type< T >::type scalar_type_t
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 ...
If the input type T is either an eigen matrix with 1 column or 1 row at compile time or a standard ve...
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...