Stan Math Library  2.20.0
reverse mode automatic differentiation
scaled_inv_chi_square_lpdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_SCALED_INV_CHI_SQUARE_LPDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_SCALED_INV_CHI_SQUARE_LPDF_HPP
3 
14 #include <cmath>
15 
16 namespace stan {
17 namespace math {
18 
39 template <bool propto, typename T_y, typename T_dof, typename T_scale>
41  const T_y& y, const T_dof& nu, const T_scale& s) {
42  static const char* function = "scaled_inv_chi_square_lpdf";
44  T_partials_return;
45 
46  check_not_nan(function, "Random variable", y);
47  check_positive_finite(function, "Degrees of freedom parameter", nu);
48  check_positive_finite(function, "Scale parameter", s);
49  check_consistent_sizes(function, "Random variable", y,
50  "Degrees of freedom parameter", nu, "Scale parameter",
51  s);
52  if (size_zero(y, nu, s))
53  return 0;
55  return 0;
56  T_partials_return logp(0);
57  scalar_seq_view<T_y> y_vec(y);
58  scalar_seq_view<T_dof> nu_vec(nu);
59  scalar_seq_view<T_scale> s_vec(s);
60  size_t N = max_size(y, nu, s);
61 
62  for (size_t n = 0; n < N; n++) {
63  if (value_of(y_vec[n]) <= 0)
64  return LOG_ZERO;
65  }
66 
67  using std::log;
68 
70  T_partials_return, T_dof>
71  half_nu(length(nu));
72  for (size_t i = 0; i < length(nu); i++)
74  half_nu[i] = 0.5 * value_of(nu_vec[i]);
75 
77  T_y>
78  log_y(length(y));
79  for (size_t i = 0; i < length(y); i++)
81  log_y[i] = log(value_of(y_vec[i]));
82 
84  T_partials_return, T_y>
85  inv_y(length(y));
86  for (size_t i = 0; i < length(y); i++)
88  inv_y[i] = 1.0 / value_of(y_vec[i]);
89 
91  T_partials_return, T_scale>
92  log_s(length(s));
93  for (size_t i = 0; i < length(s); i++)
95  log_s[i] = log(value_of(s_vec[i]));
96 
98  log_half_nu(length(nu));
100  lgamma_half_nu(length(nu));
101  VectorBuilder<!is_constant_all<T_dof>::value, T_partials_return, T_dof>
102  digamma_half_nu_over_two(length(nu));
103  for (size_t i = 0; i < length(nu); i++) {
105  lgamma_half_nu[i] = lgamma(half_nu[i]);
107  log_half_nu[i] = log(half_nu[i]);
109  digamma_half_nu_over_two[i] = digamma(half_nu[i]) * 0.5;
110  }
111 
112  operands_and_partials<T_y, T_dof, T_scale> ops_partials(y, nu, s);
113  for (size_t n = 0; n < N; n++) {
114  const T_partials_return s_dbl = value_of(s_vec[n]);
115  const T_partials_return nu_dbl = value_of(nu_vec[n]);
117  logp += half_nu[n] * log_half_nu[n] - lgamma_half_nu[n];
119  logp += nu_dbl * log_s[n];
121  logp -= (half_nu[n] + 1.0) * log_y[n];
123  logp -= half_nu[n] * s_dbl * s_dbl * inv_y[n];
124 
126  ops_partials.edge1_.partials_[n]
127  += -(half_nu[n] + 1.0) * inv_y[n]
128  + half_nu[n] * s_dbl * s_dbl * inv_y[n] * inv_y[n];
129  }
131  ops_partials.edge2_.partials_[n]
132  += 0.5 * log_half_nu[n] + 0.5 - digamma_half_nu_over_two[n] + log_s[n]
133  - 0.5 * log_y[n] - 0.5 * s_dbl * s_dbl * inv_y[n];
134  }
136  ops_partials.edge3_.partials_[n]
137  += nu_dbl / s_dbl - nu_dbl * inv_y[n] * s_dbl;
138  }
139  }
140  return ops_partials.build(logp);
141 }
142 
143 template <typename T_y, typename T_dof, typename T_scale>
145 scaled_inv_chi_square_lpdf(const T_y& y, const T_dof& nu, const T_scale& s) {
146  return scaled_inv_chi_square_lpdf<false>(y, nu, s);
147 }
148 
149 } // namespace math
150 } // namespace stan
151 #endif
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition: lgamma.hpp:21
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
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.
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
Definition: length.hpp:16
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition: size_zero.hpp:18
const double LOG_ZERO
Definition: constants.hpp:150
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
return_type< T_y, T_dof, T_scale >::type scaled_inv_chi_square_lpdf(const T_y &y, const T_dof &nu, const T_scale &s)
The log of a scaled inverse chi-squared density for y with the specified degrees of freedom parameter...
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.
VectorBuilder allocates type T1 values to be used as intermediate values.
internal::ops_partials_edge< double, Op2 > edge2_
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_
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition: digamma.hpp:23

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