Stan Math Library  2.20.0
reverse mode automatic differentiation
student_t_lpdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_STUDENT_T_LPDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_STUDENT_T_LPDF_HPP
3 
15 #include <cmath>
16 
17 namespace stan {
18 namespace math {
19 
48 template <bool propto, typename T_y, typename T_dof, typename T_loc,
49  typename T_scale>
51  const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& sigma) {
52  static const char* function = "student_t_lpdf";
54  T_partials_return;
55 
56  if (size_zero(y, nu, mu, sigma))
57  return 0.0;
58 
59  T_partials_return logp(0.0);
60 
61  check_not_nan(function, "Random variable", y);
62  check_positive_finite(function, "Degrees of freedom parameter", nu);
63  check_finite(function, "Location parameter", mu);
64  check_positive_finite(function, "Scale parameter", sigma);
65  check_consistent_sizes(function, "Random variable", y,
66  "Degrees of freedom parameter", nu,
67  "Location parameter", mu, "Scale parameter", sigma);
68 
70  return 0.0;
71 
72  scalar_seq_view<T_y> y_vec(y);
73  scalar_seq_view<T_dof> nu_vec(nu);
74  scalar_seq_view<T_loc> mu_vec(mu);
75  scalar_seq_view<T_scale> sigma_vec(sigma);
76  size_t N = max_size(y, nu, mu, sigma);
77 
78  using std::log;
79 
81  T_partials_return, T_dof>
82  half_nu(length(nu));
83  for (size_t i = 0; i < length(nu); i++)
85  half_nu[i] = 0.5 * value_of(nu_vec[i]);
86 
88  lgamma_half_nu(length(nu));
90  lgamma_half_nu_plus_half(length(nu));
92  for (size_t i = 0; i < length(nu); i++) {
93  lgamma_half_nu[i] = lgamma(half_nu[i]);
94  lgamma_half_nu_plus_half[i] = lgamma(half_nu[i] + 0.5);
95  }
96  }
97 
98  VectorBuilder<!is_constant_all<T_dof>::value, T_partials_return, T_dof>
99  digamma_half_nu(length(nu));
100  VectorBuilder<!is_constant_all<T_dof>::value, T_partials_return, T_dof>
101  digamma_half_nu_plus_half(length(nu));
103  for (size_t i = 0; i < length(nu); i++) {
104  digamma_half_nu[i] = digamma(half_nu[i]);
105  digamma_half_nu_plus_half[i] = digamma(half_nu[i] + 0.5);
106  }
107  }
108 
109  VectorBuilder<include_summand<propto, T_dof>::value, T_partials_return, T_dof>
110  log_nu(length(nu));
111  for (size_t i = 0; i < length(nu); i++)
113  log_nu[i] = log(value_of(nu_vec[i]));
114 
116  T_scale>
117  log_sigma(length(sigma));
118  for (size_t i = 0; i < length(sigma); i++)
120  log_sigma[i] = log(value_of(sigma_vec[i]));
121 
123  T_partials_return, T_y, T_dof, T_loc, T_scale>
124  square_y_minus_mu_over_sigma__over_nu(N);
125 
127  T_partials_return, T_y, T_dof, T_loc, T_scale>
128  log1p_exp(N);
129 
130  for (size_t i = 0; i < N; i++)
132  const T_partials_return y_dbl = value_of(y_vec[i]);
133  const T_partials_return mu_dbl = value_of(mu_vec[i]);
134  const T_partials_return sigma_dbl = value_of(sigma_vec[i]);
135  const T_partials_return nu_dbl = value_of(nu_vec[i]);
136  square_y_minus_mu_over_sigma__over_nu[i]
137  = square((y_dbl - mu_dbl) / sigma_dbl) / nu_dbl;
138  log1p_exp[i] = log1p(square_y_minus_mu_over_sigma__over_nu[i]);
139  }
140 
142  sigma);
143  for (size_t n = 0; n < N; n++) {
144  const T_partials_return y_dbl = value_of(y_vec[n]);
145  const T_partials_return mu_dbl = value_of(mu_vec[n]);
146  const T_partials_return sigma_dbl = value_of(sigma_vec[n]);
147  const T_partials_return nu_dbl = value_of(nu_vec[n]);
149  logp += NEG_LOG_SQRT_PI;
151  logp += lgamma_half_nu_plus_half[n] - lgamma_half_nu[n] - 0.5 * log_nu[n];
153  logp -= log_sigma[n];
155  logp -= (half_nu[n] + 0.5) * log1p_exp[n];
156 
158  ops_partials.edge1_.partials_[n]
159  += -(half_nu[n] + 0.5) * 1.0
160  / (1.0 + square_y_minus_mu_over_sigma__over_nu[n])
161  * (2.0 * (y_dbl - mu_dbl) / square(sigma_dbl) / nu_dbl);
162  }
164  const T_partials_return inv_nu = 1.0 / nu_dbl;
165  ops_partials.edge2_.partials_[n]
166  += 0.5 * digamma_half_nu_plus_half[n] - 0.5 * digamma_half_nu[n]
167  - 0.5 * inv_nu - 0.5 * log1p_exp[n]
168  + (half_nu[n] + 0.5)
169  * (1.0 / (1.0 + square_y_minus_mu_over_sigma__over_nu[n])
170  * square_y_minus_mu_over_sigma__over_nu[n] * inv_nu);
171  }
173  ops_partials.edge3_.partials_[n]
174  -= (half_nu[n] + 0.5)
175  / (1.0 + square_y_minus_mu_over_sigma__over_nu[n])
176  * (2.0 * (mu_dbl - y_dbl) / (sigma_dbl * sigma_dbl * nu_dbl));
177  }
179  const T_partials_return inv_sigma = 1.0 / sigma_dbl;
180  ops_partials.edge4_.partials_[n]
181  += -inv_sigma
182  + (nu_dbl + 1.0) / (1.0 + square_y_minus_mu_over_sigma__over_nu[n])
183  * (square_y_minus_mu_over_sigma__over_nu[n] * inv_sigma);
184  }
185  }
186  return ops_partials.build(logp);
187 }
188 
189 template <typename T_y, typename T_dof, typename T_loc, typename T_scale>
191  const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& sigma) {
192  return student_t_lpdf<false>(y, nu, mu, sigma);
193 }
194 
195 } // namespace math
196 } // namespace stan
197 #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
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
internal::ops_partials_edge< double, Op4 > edge4_
const double NEG_LOG_SQRT_PI
Definition: constants.hpp:160
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
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:12
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.
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_
fvar< T > log1p_exp(const fvar< T > &x)
Definition: log1p_exp.hpp:12
return_type< T_y, T_dof, T_loc, T_scale >::type student_t_lpdf(const T_y &y, const T_dof &nu, const T_loc &mu, const T_scale &sigma)
The log of the Student-t density for the given y, nu, mean, and scale parameter.
fvar< T > log1p(const fvar< T > &x)
Definition: log1p.hpp:12
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.