Automatic Differentiation
 
Loading...
Searching...
No Matches
normal_sufficient_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_NORMAL_SUFFICIENT_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_NORMAL_SUFFICIENT_LPDF_HPP
3
19#include <cmath>
20
21namespace stan {
22namespace math {
23
52template <bool propto, typename T_y, typename T_s, typename T_n, typename T_loc,
53 typename T_scale>
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) {
58 using T_y_ref = ref_type_if_not_constant_t<T_y>;
59 using T_s_ref = ref_type_if_not_constant_t<T_s>;
60 using T_n_ref = ref_type_if_not_constant_t<T_n>;
61 using T_mu_ref = ref_type_if_not_constant_t<T_loc>;
62 using T_sigma_ref = ref_type_if_not_constant_t<T_scale>;
63 static constexpr const char* function = "normal_sufficient_lpdf";
64 check_consistent_sizes(function, "Location parameter sufficient statistic",
65 y_bar, "Scale parameter sufficient statistic",
66 s_squared, "Number of observations", n_obs,
67 "Location parameter", mu, "Scale parameter", sigma);
68
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;
72 T_mu_ref mu_ref = mu;
73 T_sigma_ref sigma_ref = sigma;
74
75 decltype(auto) y_val = to_ref(as_value_column_array_or_scalar(y_ref));
76 decltype(auto) s_squared_val
78 decltype(auto) n_obs_val_int
80 decltype(auto) n_obs_val = to_ref(
81 promote_scalar<double>(as_value_column_array_or_scalar(n_obs_ref)));
82 decltype(auto) mu_val = to_ref(as_value_column_array_or_scalar(mu_ref));
83 decltype(auto) sigma_val = to_ref(as_value_column_array_or_scalar(sigma_ref));
84
85 check_finite(function, "Location parameter sufficient statistic", y_val);
86 check_finite(function, "Scale parameter sufficient statistic", s_squared_val);
87 check_nonnegative(function, "Scale parameter sufficient statistic",
88 s_squared_val);
89 check_positive_finite(function, "Number of observations", n_obs_val);
90 check_finite(function, "Location parameter", mu_val);
91 check_positive_finite(function, "Scale parameter", sigma_val);
92
93 if (size_zero(y_bar, s_squared, n_obs, mu, sigma)) {
94 return 0.0;
95 }
97 return 0.0;
98 }
99
100 const auto& sigma_squared
101 = to_ref_if<!is_constant_all<T_y, T_loc, T_s, T_scale>::value>(
102 square(sigma_val));
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);
106
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));
110 logp += NEG_LOG_SQRT_TWO_PI * sum(n_obs_val) * N / math::size(n_obs);
111 }
113 logp -= sum(n_obs_val * log(sigma_val)) * N / max_size(n_obs, sigma);
114 }
115
116 auto ops_partials
117 = make_partials_propagator(y_ref, s_squared_ref, mu_ref, sigma_ref);
119 auto common_derivative = to_ref_if<(!is_constant_all<T_loc>::value
121 N / max_size(y_bar, mu, n_obs, sigma) * n_obs_val / sigma_squared
122 * diff);
124 partials<2>(ops_partials) = -common_derivative;
125 }
127 partials<0>(ops_partials) = std::move(common_derivative);
128 }
129 }
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);
137 } else {
139 partials<1>(ops_partials) = T_sigma_value_vector::Constant(
140 N, -0.5 / forward_as<T_sigma_value_scalar>(sigma_squared));
141 } else {
142 forward_as<internal::broadcast_array<T_partials_return>>(
143 partials<1>(ops_partials))
144 = -0.5 / sigma_squared * N / math::size(sigma);
145 }
146 }
147 }
149 edge<3>(ops_partials).partials_
150 = (cons_expr / sigma_squared - n_obs_val) / sigma_val;
151 }
152 return ops_partials.build(logp);
153}
154
155template <typename T_y, typename T_s, typename T_n, typename T_loc,
156 typename T_scale>
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);
161}
162
163} // namespace math
164} // namespace stan
165#endif
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>>.
Definition size.hpp:19
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.
Definition size_zero.hpp:19
T to_ref_if(T &&a)
No-op that should be optimized away.
Definition to_ref.hpp:29
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
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.
Definition to_ref.hpp:17
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.
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
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)
Definition square.hpp:12
typename ref_type_if<!is_constant< T >::value, T >::type ref_type_if_not_constant_t
Definition ref_type.hpp:62
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...