Stan  1.0
probability, sampling & optimization
student_t.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__STUDENT_T_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__STUDENT_T_HPP__
3 
4 #include <stan/agrad.hpp>
7 #include <stan/meta/traits.hpp>
9 #include <stan/prob/traits.hpp>
10 
11 namespace stan {
12 
13  namespace prob {
14 
40  template <bool propto, typename T_y, typename T_dof,
41  typename T_loc, typename T_scale,
42  class Policy>
44  student_t_log(const T_y& y, const T_dof& nu, const T_loc& mu,
45  const T_scale& sigma,
46  const Policy&) {
47  static const char* function = "stan::prob::student_t_log(%1%)";
48 
53 
54  // check if any vectors are zero length
55  if (!(stan::length(y)
56  && stan::length(nu)
57  && stan::length(mu)
58  && stan::length(sigma)))
59  return 0.0;
60 
62 
63  // validate args (here done over var, which should be OK)
64  if (!check_not_nan(function, y, "Random variable", &logp, Policy()))
65  return logp;
66  if(!check_finite(function, nu, "Degrees of freedom parameter", &logp, Policy()))
67  return logp;
68  if(!check_positive(function, nu, "Degrees of freedom parameter", &logp, Policy()))
69  return logp;
70  if (!check_finite(function, mu, "Location parameter",
71  &logp, Policy()))
72  return logp;
73  if (!check_finite(function, sigma, "Scale parameter",
74  &logp, Policy()))
75  return logp;
76  if (!check_positive(function, sigma, "Scale parameter",
77  &logp, Policy()))
78  return logp;
79 
80 
81  if (!(check_consistent_sizes(function,
82  y,nu,mu,sigma,
83  "Random variable","Degrees of freedom parameter","Location parameter","Scale parameter",
84  &logp, Policy())))
85  return logp;
86 
87  // check if no variables are involved and prop-to
89  return 0.0;
90 
91  VectorView<const T_y> y_vec(y);
92  VectorView<const T_dof> nu_vec(nu);
93  VectorView<const T_loc> mu_vec(mu);
94  VectorView<const T_scale> sigma_vec(sigma);
95  size_t N = max_size(y, nu, mu, sigma);
96 
97  using stan::math::square;
98  using boost::math::lgamma;
99 
100  using std::log;
101 
102  for (size_t n = 0; n < N; n++) {
104  logp += lgamma( (nu_vec[n] + 1.0) / 2.0) - lgamma(nu_vec[n] / 2.0);
106  logp += NEG_LOG_SQRT_PI;
108  logp -= 0.5 * log(nu_vec[n]);
110  logp -= log(sigma_vec[n]);
112  logp -= ((nu_vec[n] + 1.0) / 2.0)
113  * log1p( square(((y_vec[n] - mu_vec[n]) / sigma_vec[n])) / nu_vec[n]);
114  }
115  return logp;
116  }
117 
118  template <bool propto,
119  typename T_y, typename T_dof, typename T_loc, typename T_scale>
120  inline
122  student_t_log(const T_y& y, const T_dof& nu, const T_loc& mu,
123  const T_scale& sigma) {
124  return student_t_log<propto>(y,nu,mu,sigma,stan::math::default_policy());
125  }
126 
127  template <typename T_y, typename T_dof, typename T_loc, typename T_scale,
128  class Policy>
129  inline
131  student_t_log(const T_y& y, const T_dof& nu, const T_loc& mu,
132  const T_scale& sigma,
133  const Policy&) {
134  return student_t_log<false>(y,nu,mu,sigma,Policy());
135  }
136 
137  template <typename T_y, typename T_dof, typename T_loc, typename T_scale>
138  inline
140  student_t_log(const T_y& y, const T_dof& nu, const T_loc& mu,
141  const T_scale& sigma) {
142  return student_t_log<false>(y,nu,mu,sigma,stan::math::default_policy());
143  }
144 
145 
146  }
147 }
148 #endif
var square(const var &x)
Return the square of the input variable.
var log1p(const stan::agrad::var &a)
The log (1 + x) function for variables (C99).
var lgamma(const stan::agrad::var &a)
The log gamma function for variables (C99).
var log(const var &a)
Return the natural log of the specified variable (cmath).
Definition: agrad.hpp:1730
T square(T x)
Return the square of the specified argument.
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is nan.
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result, const Policy &)
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is finite.
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
return_type< T_y, T_dof, T_loc, T_scale >::type student_t_log(const T_y &y, const T_dof &nu, const T_loc &mu, const T_scale &sigma, const Policy &)
The log of the Student-t density for the given y, nu, mean, and scale parameter.
Definition: student_t.hpp:44
Probability, optimization and sampling library.
Definition: agrad.cpp:6
size_t length(const T &x)
Definition: traits.hpp:111
size_t max_size(const T1 &x1, const T2 &x2)
Definition: traits.hpp:148
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:33
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
Definition: traits.hpp:368

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