Stan  1.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
inv_chi_square.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DIST__UNI__CONTINUOUS__INV_CHI_SQUARE_HPP__
2 #define __STAN__PROB__DIST__UNI__CONTINUOUS__INV_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>
37  typename return_type<T_y,T_dof>::type
38  inv_chi_square_log(const T_y& y, const T_dof& nu,
39  const Policy&) {
40  static const char* function = "stan::prob::inv_chi_square_log(%1%)";
41 
42  // check if any vectors are zero length
43  if (!(stan::length(y)
44  && stan::length(nu)))
45  return 0.0;
46 
52 
53  typename return_type<T_y,T_dof>::type logp(0.0);
54  if (!check_finite(function, nu, "Degrees of freedom parameter", &logp, Policy()))
55  return logp;
56  if (!check_positive(function, nu, "Degrees of freedom parameter", &logp, Policy()))
57  return logp;
58  if (!check_not_nan(function, y, "Random variable", &logp, Policy()))
59  return logp;
60 
61  if (!(check_consistent_sizes(function,
62  y,nu,
63  "Random variable","Degrees of freedom parameter",
64  &logp, Policy())))
65  return logp;
66 
67 
68  // set up template expressions wrapping scalars into vector views
69  VectorView<const T_y> y_vec(y);
70  VectorView<const T_dof> nu_vec(nu);
71  size_t N = max_size(y, nu);
72 
73  for (size_t n = 0; n < length(y); n++)
74  if (value_of(y_vec[n]) <= 0)
75  return LOG_ZERO;
76 
77  using boost::math::lgamma;
79  for (size_t n = 0; n < N; n++) {
81  logp += nu_vec[n] * NEG_LOG_TWO_OVER_TWO - lgamma(0.5 * nu_vec[n]);
83  logp -= multiply_log(0.5*nu_vec[n]+1.0, y_vec[n]);
85  logp -= 0.5 / y_vec[n];
86  }
87  return logp;
88  }
89 
90  template <bool propto,
91  typename T_y, typename T_dof>
92  inline
94  inv_chi_square_log(const T_y& y, const T_dof& nu) {
95  return inv_chi_square_log<propto>(y,nu,stan::math::default_policy());
96  }
97 
98  template <typename T_y, typename T_dof,
99  class Policy>
100  inline
102  inv_chi_square_log(const T_y& y, const T_dof& nu,
103  const Policy&) {
104  return inv_chi_square_log<false>(y,nu,Policy());
105  }
106 
107 
108  template <typename T_y, typename T_dof>
109  inline
111  inv_chi_square_log(const T_y& y, const T_dof& nu) {
112  return inv_chi_square_log<false>(y,nu,stan::math::default_policy());
113  }
114 
115  }
116 }
117 
118 #endif
119 

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