Stan  1.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
scaled_inv_chi_square.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__SCALED_INV_CHI_SQUARE_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__SCALED_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, typename T_scale,
36  class Policy>
37  typename return_type<T_y,T_dof,T_scale>::type
38  scaled_inv_chi_square_log(const T_y& y, const T_dof& nu, const T_scale& s,
39  const Policy&) {
40  static const char* function
41  = "stan::prob::scaled_inv_chi_square_log(%1%)";
42 
48 
49  // check if any vectors are zero length
50  if (!(stan::length(y)
51  && stan::length(nu)
52  && stan::length(s)))
53  return 0.0;
54 
55  typename return_type<T_y,T_dof,T_scale>::type logp(0.0);
56  if (!check_not_nan(function, y, "Random variable", &logp, Policy()))
57  return logp;
58  if (!check_finite(function, nu, "Degrees of freedom parameter", &logp, Policy()))
59  return logp;
60  if (!check_positive(function, nu, "Degrees of freedom parameter", &logp, Policy()))
61  return logp;
62  if (!check_finite(function, s, "Scale parameter", &logp, Policy()))
63  return logp;
64  if (!check_positive(function, s, "Scale parameter", &logp, Policy()))
65  return logp;
66  if (!(check_consistent_sizes(function,
67  y,nu,s,
68  "Random variable","Degrees of freedom parameter","Scale parameter",
69  &logp, Policy())))
70  return logp;
71 
72  // check if no variables are involved and prop-to
74  return 0.0;
75 
76  VectorView<const T_y> y_vec(y);
77  VectorView<const T_dof> nu_vec(nu);
79  size_t N = max_size(y, nu, s);
80 
81  for (size_t n = 0; n < N; n++) {
82  if (value_of(y_vec[n]) <= 0)
83  return LOG_ZERO;
84  }
85 
87  using stan::math::square;
88 
89  for (size_t n = 0; n < N; n++) {
91  typename return_type<T_dof>::type half_nu = 0.5 * nu_vec[n];
92  logp += multiply_log(half_nu,half_nu) - lgamma(half_nu);
93  }
95  logp += nu_vec[n] * log(s_vec[n]);
97  logp -= multiply_log(nu_vec[n]*0.5+1.0, y_vec[n]);
99  logp -= nu_vec[n] * 0.5 * square(s_vec[n]) / y_vec[n];
100  }
101  return logp;
102  }
103 
104  template <bool propto,
105  typename T_y, typename T_dof, typename T_scale>
106  inline
108  scaled_inv_chi_square_log(const T_y& y, const T_dof& nu, const T_scale& s) {
109  return scaled_inv_chi_square_log<propto>(y,nu,s,
111  }
112 
113  template <typename T_y, typename T_dof, typename T_scale,
114  class Policy>
115  inline
117  scaled_inv_chi_square_log(const T_y& y, const T_dof& nu, const T_scale& s,
118  const Policy&) {
119  return scaled_inv_chi_square_log<false>(y,nu,s,Policy());
120  }
121 
122  template <typename T_y, typename T_dof, typename T_scale>
123  inline
125  scaled_inv_chi_square_log(const T_y& y, const T_dof& nu, const T_scale& s) {
126  return scaled_inv_chi_square_log<false>(y,nu,s,
128  }
129 
130  }
131 }
132 
133 #endif
134 

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