Stan  1.1
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>
11 
12 namespace stan {
13 
14  namespace prob {
15 
35  template <bool propto,
36  typename T_y, typename T_dof,
37  class Policy>
38  typename return_type<T_y,T_dof>::type
39  inv_chi_square_log(const T_y& y, const T_dof& nu,
40  const Policy&) {
41  static const char* function = "stan::prob::inv_chi_square_log(%1%)";
42 
43  // check if any vectors are zero length
44  if (!(stan::length(y)
45  && stan::length(nu)))
46  return 0.0;
47 
53 
54  typename return_type<T_y,T_dof>::type logp(0.0);
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  if (!check_not_nan(function, y, "Random variable", &logp, Policy()))
60  return logp;
61 
62  if (!(check_consistent_sizes(function,
63  y,nu,
64  "Random variable","Degrees of freedom parameter",
65  &logp, Policy())))
66  return logp;
67 
68 
69  // set up template expressions wrapping scalars into vector views
70  VectorView<const T_y> y_vec(y);
71  VectorView<const T_dof> nu_vec(nu);
72  size_t N = max_size(y, nu);
73 
74  for (size_t n = 0; n < length(y); n++)
75  if (value_of(y_vec[n]) <= 0)
76  return LOG_ZERO;
77 
78  using boost::math::lgamma;
80  for (size_t n = 0; n < N; n++) {
82  logp += nu_vec[n] * NEG_LOG_TWO_OVER_TWO - lgamma(0.5 * nu_vec[n]);
84  logp -= multiply_log(0.5*nu_vec[n]+1.0, y_vec[n]);
86  logp -= 0.5 / y_vec[n];
87  }
88  return logp;
89  }
90 
91  template <bool propto,
92  typename T_y, typename T_dof>
93  inline
95  inv_chi_square_log(const T_y& y, const T_dof& nu) {
96  return inv_chi_square_log<propto>(y,nu,stan::math::default_policy());
97  }
98 
99  template <typename T_y, typename T_dof,
100  class Policy>
101  inline
103  inv_chi_square_log(const T_y& y, const T_dof& nu,
104  const Policy&) {
105  return inv_chi_square_log<false>(y,nu,Policy());
106  }
107 
108 
109  template <typename T_y, typename T_dof>
110  inline
112  inv_chi_square_log(const T_y& y, const T_dof& nu) {
113  return inv_chi_square_log<false>(y,nu,stan::math::default_policy());
114  }
115 
116  template <typename T_y, typename T_dof, class Policy>
118  inv_chi_square_cdf(const T_y& y, const T_dof& nu, const Policy&) {
119 
120  // Size checks
121  if ( !( stan::length(y) && stan::length(nu) ) ) return 0.0;
122 
123  // Error checks
124  static const char* function = "stan::prob::inv_chi_square_cdf(%1%)";
125 
130 
131  using boost::math::tools::promote_args;
132 
133  double P(1.0);
134 
135  if (!check_finite(function, nu, "Degrees of freedom parameter", &P, Policy()))
136  return P;
137 
138  if (!check_positive(function, nu, "Degrees of freedom parameter", &P, Policy()))
139  return P;
140 
141  if (!check_not_nan(function, y, "Random variable", &P, Policy()))
142  return P;
143 
144  if (!check_positive(function, y, "Random variable", &P, Policy()))
145  return P;
146 
147  if (!(check_consistent_sizes(function, y, nu,
148  "Random variable", "Degrees of freedom parameter",
149  &P, Policy())))
150  return P;
151 
152  // Wrap arguments in vectors
153  VectorView<const T_y> y_vec(y);
154  VectorView<const T_dof> nu_vec(nu);
155  size_t N = max_size(y, nu);
156 
157  agrad::OperandsAndPartials<T_y, T_dof> operands_and_partials(y, nu);
158 
159  std::fill(operands_and_partials.all_partials,
160  operands_and_partials.all_partials + operands_and_partials.nvaris, 0.0);
161 
162  // Compute CDF and its gradients
163  using stan::math::value_of;
164  using boost::math::gamma_p_derivative;
165  using boost::math::gamma_q;
166  using boost::math::tgamma;
167  using boost::math::digamma;
168 
169  // Cache a few expensive function calls if nu is a parameter
172 
174 
175  for (size_t i = 0; i < stan::length(nu); i++) {
176  const double nu_dbl = value_of(nu_vec[i]);
177  gamma_vec[i] = tgamma(nu_dbl);
178  digamma_vec[i] = digamma(nu_dbl);
179  }
180 
181  }
182 
183  // Compute vectorized CDF and gradient
184  for (size_t n = 0; n < N; n++) {
185 
186  // Pull out values
187  const double y_dbl = value_of(y_vec[n]);
188  const double y_inv_dbl = 1.0 / y_dbl;
189  const double nu_dbl = value_of(nu_vec[n]);
190 
191  // Compute
192  const double Pn = gamma_q(0.5 * nu_dbl, 0.5 * y_inv_dbl);
193 
194  P *= Pn;
195 
197  operands_and_partials.d_x1[n]
198  += 0.5 * y_inv_dbl * y_inv_dbl * gamma_p_derivative(0.5 * nu_dbl, 0.5 * y_inv_dbl) / Pn;
199 
201  operands_and_partials.d_x2[n]
202  += 0.5 * stan::math::gradRegIncGamma(0.5 * nu_dbl, 0.5 * y_inv_dbl, gamma_vec[n], digamma_vec[n]) / Pn;
203 
204  }
205 
206  for (size_t n = 0; n < N; n++) {
207 
209  operands_and_partials.d_x1[n] *= P;
210 
212  operands_and_partials.d_x2[n] *= P;
213 
214  }
215 
216  return operands_and_partials.to_var(P);
217 
218  }
219 
220  template <typename T_y, typename T_dof>
221  inline typename return_type<T_y,T_dof>::type
222  inv_chi_square_cdf(const T_y& y, const T_dof& nu) {
224  }
225 
226  }
227 }
228 
229 #endif
230 

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