Stan Math Library  2.20.0
reverse mode automatic differentiation
normal_sufficient_lpdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_NORMAL_SUFFICIENT_LPDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_NORMAL_SUFFICIENT_LPDF_HPP
3 
13 
14 namespace stan {
15 namespace math {
16 
44 template <bool propto, typename T_y, typename T_s, typename T_n, typename T_loc,
45  typename T_scale>
47  const T_y& y_bar, const T_s& s_squared, const T_n& n_obs, const T_loc& mu,
48  const T_scale& sigma) {
49  static const char* function = "normal_sufficient_lpdf";
50  typedef
52  T_partials_return;
53 
54  using std::log;
55 
56  // check if any vectors are zero length
57  if (size_zero(y_bar, s_squared, n_obs, mu, sigma))
58  return 0.0;
59 
60  // set up return value accumulator
61  T_partials_return logp(0.0);
62 
63  // validate args (here done over var, which should be OK)
64  check_finite(function, "Location parameter sufficient statistic", y_bar);
65  check_finite(function, "Scale parameter sufficient statistic", s_squared);
66  check_nonnegative(function, "Scale parameter sufficient statistic",
67  s_squared);
68  check_finite(function, "Number of observations", n_obs);
69  check_positive(function, "Number of observations", n_obs);
70  check_finite(function, "Location parameter", mu);
71  check_finite(function, "Scale parameter", sigma);
72  check_positive(function, "Scale parameter", sigma);
73  check_consistent_sizes(function, "Location parameter sufficient statistic",
74  y_bar, "Scale parameter sufficient statistic",
75  s_squared, "Number of observations", n_obs,
76  "Location parameter", mu, "Scale parameter", sigma);
77  // check if no variables are involved and prop-to
79  return 0.0;
80 
81  // set up template expressions wrapping scalars into vector views
82  operands_and_partials<T_y, T_s, T_loc, T_scale> ops_partials(y_bar, s_squared,
83  mu, sigma);
84 
85  scalar_seq_view<const T_y> y_bar_vec(y_bar);
86  scalar_seq_view<const T_s> s_squared_vec(s_squared);
87  scalar_seq_view<const T_n> n_obs_vec(n_obs);
89  scalar_seq_view<const T_scale> sigma_vec(sigma);
90  size_t N = max_size(y_bar, s_squared, n_obs, mu, sigma);
91 
92  for (size_t i = 0; i < N; i++) {
93  const T_partials_return y_bar_dbl = value_of(y_bar_vec[i]);
94  const T_partials_return s_squared_dbl = value_of(s_squared_vec[i]);
95  const T_partials_return n_obs_dbl = n_obs_vec[i];
96  const T_partials_return mu_dbl = value_of(mu_vec[i]);
97  const T_partials_return sigma_dbl = value_of(sigma_vec[i]);
98  const T_partials_return sigma_squared = pow(sigma_dbl, 2);
99 
101  logp += NEG_LOG_SQRT_TWO_PI * n_obs_dbl;
102 
104  logp -= n_obs_dbl * log(sigma_dbl);
105 
106  const T_partials_return cons_expr
107  = (s_squared_dbl + n_obs_dbl * pow(y_bar_dbl - mu_dbl, 2));
108 
109  logp -= cons_expr / (2 * sigma_squared);
110 
111  // gradients
113  const T_partials_return common_derivative
114  = n_obs_dbl * (mu_dbl - y_bar_dbl) / sigma_squared;
116  ops_partials.edge1_.partials_[i] += common_derivative;
118  ops_partials.edge3_.partials_[i] -= common_derivative;
119  }
121  ops_partials.edge2_.partials_[i] -= 0.5 / sigma_squared;
123  ops_partials.edge4_.partials_[i]
124  += cons_expr / pow(sigma_dbl, 3) - n_obs_dbl / sigma_dbl;
125  }
126  return ops_partials.build(logp);
127 }
128 
129 template <typename T_y, typename T_s, typename T_n, typename T_loc,
130  typename T_scale>
132 normal_sufficient_lpdf(const T_y& y_bar, const T_s& s_squared, const T_n& n_obs,
133  const T_loc& mu, const T_scale& sigma) {
134  return normal_sufficient_lpdf<false>(y_bar, s_squared, n_obs, mu, sigma);
135 }
136 
137 } // namespace math
138 } // namespace stan
139 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:17
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Definition: conjunction.hpp:14
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
internal::ops_partials_edge< double, Op4 > edge4_
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This template builds partial derivatives with respect to a set of operands.
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition: size_zero.hpp:18
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
return_type< T_y, T_s, T_loc, T_scale >::type 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...
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
const double NEG_LOG_SQRT_TWO_PI
Definition: constants.hpp:156
internal::ops_partials_edge< double, Op2 > edge2_
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:16
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
internal::ops_partials_edge< double, Op3 > edge3_
internal::ops_partials_edge< double, Op1 > edge1_

     [ Stan Home Page ] © 2011–2018, Stan Development Team.