Stan  1.0
probability, sampling & optimization
chi_square.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__CHI_SQUARE_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__CHI_SQUARE_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 
34  template <bool propto,
35  typename T_y, typename T_dof,
36  class Policy>
38  chi_square_log(const T_y& y, const T_dof& nu, const Policy&) {
39  static const char* function = "stan::prob::chi_square_log(%1%)";
40 
41  // check if any vectors are zero length
42  if (!(stan::length(y)
43  && stan::length(nu)))
44  return 0.0;
45 
51 
52  typename return_type<T_y,T_dof>::type logp(0);
53  if (!check_not_nan(function, y, "Random variable", &logp, Policy()))
54  return logp;
55  if (!check_finite(function, nu, "Degrees of freedom parameter", &logp, Policy()))
56  return logp;
57  if (!check_positive(function, nu, "Degrees of freedom parameter", &logp, Policy()))
58  return logp;
59 
60  if (!(check_consistent_sizes(function,
61  y,nu,
62  "Random variable","Degrees of freedom parameter",
63  &logp, Policy())))
64  return logp;
65 
66 
67  // set up template expressions wrapping scalars into vector views
68  VectorView<const T_y> y_vec(y);
69  VectorView<const T_dof> nu_vec(nu);
70  size_t N = max_size(y, nu);
71 
72  for (size_t n = 0; n < length(y); n++)
73  if (value_of(y_vec[n]) < 0)
74  return LOG_ZERO;
75 
76  // check if no variables are involved and prop-to
78  return 0.0;
79 
80  using boost::math::lgamma;
82 
83  for (size_t n = 0; n < N; n++) {
85  logp += nu_vec[n] * NEG_LOG_TWO_OVER_TWO - lgamma(0.5 * nu_vec[n]);
87  logp += multiply_log(0.5*nu_vec[n]-1.0, y_vec[n]);
89  logp -= 0.5 * y_vec[n];
90  }
91  return logp;
92  }
93 
94 
95  template <bool propto,
96  typename T_y, typename T_dof>
97  inline
99  chi_square_log(const T_y& y, const T_dof& nu) {
100  return chi_square_log<propto>(y,nu,stan::math::default_policy());
101  }
102 
103 
104  template <typename T_y, typename T_dof,
105  class Policy>
106  inline
108  chi_square_log(const T_y& y, const T_dof& nu, const Policy&) {
109  return chi_square_log<false>(y,nu,Policy());
110  }
111 
112 
113  template <typename T_y, typename T_dof>
114  inline
116  chi_square_log(const T_y& y, const T_dof& nu) {
117  return chi_square_log<false>(y,nu,stan::math::default_policy());
118  }
119 
129  /*template <typename T_y, typename T_dof,
130  class Policy>
131  typename return_type<T_y,T_dof>::type
132  chi_square_cdf(const T_y& y, const T_dof& nu, const Policy&) {
133  static const char* function = "stan::prob::chi_square_cdf(%1%)";
134 
135  using stan::math::check_positive;
136  using stan::math::check_finite;
137  using stan::math::check_not_nan;
138  using return_type;
139 
140  typename return_type<T_y,T_dof>::type lp;
141  if (!check_not_nan(function, y, "Random variable", &lp, Policy()))
142  return lp;
143  if (!check_finite(function, nu, "Degrees of freedom parameter", &lp, Policy()))
144  return lp;
145  if (!check_positive(function, nu, "Degrees of freedom parameter", &lp, Policy()))
146  return lp;
147 
148  // FIXME: include when gamma_cdf() is ready
149  return stan::prob::gamma_cdf(y,nu/2,0.5,Policy());
150  }
151 
152  template <typename T_y, typename T_dof>
153  typename return_type<T_y,T_dof>::type
154  chi_square_cdf(const T_y& y, const T_dof& nu) {
155  return chi_square_cdf(y, nu, stan::math::default_policy());
156  }*/
157  }
158 }
159 
160 #endif
161 
var multiply_log(const var &a, const var &b)
Return the value of a*log(b).
double value_of(const agrad::var &v)
Return the value of the specified variable.
var lgamma(const stan::agrad::var &a)
The log gamma function for variables (C99).
boost::math::tools::promote_args< T_a, T_b >::type multiply_log(T_a a, T_b b)
double value_of(T x)
Return the value of the specified scalar argument converted to a double value.
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 >::type chi_square_log(const T_y &y, const T_dof &nu, const Policy &)
The log of a chi-squared density for y with the specified degrees of freedom parameter.
Definition: chi_square.hpp:38
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.