Stan  1.0
probability, sampling & optimization
gamma.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__GAMMA_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__GAMMA_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 
37  template <bool propto,
38  typename T_y, typename T_shape, typename T_inv_scale,
39  class Policy>
41  gamma_log(const T_y& y, const T_shape& alpha, const T_inv_scale& beta,
42  const Policy&) {
43  static const char* function = "stan::prob::gamma_log(%1%)";
44 
52 
53  // check if any vectors are zero length
54  if (!(stan::length(y)
55  && stan::length(alpha)
56  && stan::length(beta)))
57  return 0.0;
58 
59  // set up return value accumulator
60  double logp(0.0);
61 
62  // validate args (here done over var, which should be OK)
63  if (!check_not_nan(function, y, "Random variable", &logp, Policy()))
64  return logp;
65  if (!check_finite(function, alpha, "Shape parameter",
66  &logp, Policy()))
67  return logp;
68  if (!check_positive(function, alpha, "Shape parameter",
69  &logp, Policy()))
70  return logp;
71  if (!check_finite(function, beta, "Inverse scale parameter",
72  &logp, Policy()))
73  return logp;
74  if (!check_positive(function, beta, "Inverse scale parameter",
75  &logp, Policy()))
76  return logp;
77  if (!(check_consistent_sizes(function,
78  y,alpha,beta,
79  "Random variable","Shape parameter","Inverse scale parameter",
80  &logp, Policy())))
81  return logp;
82 
83  // check if no variables are involved and prop-to
85  return 0.0;
86 
87  // set up template expressions wrapping scalars into vector views
88  VectorView<const T_y> y_vec(y);
89  VectorView<const T_shape> alpha_vec(alpha);
90  VectorView<const T_inv_scale> beta_vec(beta);
91 
92  for (size_t n = 0; n < length(y); n++) {
93  const double y_dbl = value_of(y_vec[n]);
94  if (y_dbl < 0)
95  return LOG_ZERO;
96  }
97 
98  size_t N = max_size(y, alpha, beta);
99  agrad::OperandsAndPartials<T_y, T_shape, T_inv_scale> operands_and_partials(y, alpha, beta);
100 
101  using boost::math::lgamma;
103  using boost::math::digamma;
104 
106  log_y(length(y));
108  for(size_t n = 0; n < length(y); n++) {
109  if (value_of(y_vec[n]) > 0)
110  log_y[n] = log(value_of(y_vec[n]));
111  }
112 
114  lgamma_alpha(length(alpha));
116  digamma_alpha(length(alpha));
117  for (size_t n = 0; n < length(alpha); n++) {
119  lgamma_alpha[n] = lgamma(value_of(alpha_vec[n]));
121  digamma_alpha[n] = digamma(value_of(alpha_vec[n]));
122  }
123 
125  log_beta(length(beta));
127  for (size_t n = 0; n < length(beta); n++)
128  log_beta[n] = log(value_of(beta_vec[n]));
129 
130  for (size_t n = 0; n < N; n++) {
131  // pull out values of arguments
132  const double y_dbl = value_of(y_vec[n]);
133  const double alpha_dbl = value_of(alpha_vec[n]);
134  const double beta_dbl = value_of(beta_vec[n]);
135 
137  logp -= lgamma_alpha[n];
139  logp += alpha_dbl * log_beta[n];
141  logp += (alpha_dbl-1.0) * log_y[n];
143  logp -= beta_dbl * y_dbl;
144 
145  // gradients
147  operands_and_partials.d_x1[n] += (alpha_dbl-1)/y_dbl - beta_dbl;
149  operands_and_partials.d_x2[n] += -digamma_alpha[n] + log_beta[n] + log_y[n];
151  operands_and_partials.d_x3[n] += alpha_dbl / beta_dbl - y_dbl;
152  }
153  return operands_and_partials.to_var(logp);
154  }
155 
156  template <bool propto,
157  typename T_y, typename T_shape, typename T_inv_scale>
158  inline
160  gamma_log(const T_y& y, const T_shape& alpha, const T_inv_scale& beta) {
161  return gamma_log<propto>(y,alpha,beta,stan::math::default_policy());
162  }
163 
164  template <typename T_y, typename T_shape, typename T_inv_scale,
165  class Policy>
166  inline
168  gamma_log(const T_y& y, const T_shape& alpha, const T_inv_scale& beta,
169  const Policy&) {
170  return gamma_log<false>(y,alpha,beta,Policy());
171  }
172 
173  template <typename T_y, typename T_shape, typename T_inv_scale>
174  inline
176  gamma_log(const T_y& y, const T_shape& alpha, const T_inv_scale& beta) {
177  return gamma_log<false>(y,alpha,beta,stan::math::default_policy());
178  }
179 
180 
195  /*template <typename T_y, typename T_shape, typename T_inv_scale,
196  class Policy>
197  typename boost::math::tools::promote_args<T_y,T_shape,T_inv_scale>::type
198  gamma_cdf(const T_y& y, const T_shape& alpha, const T_inv_scale& beta,
199  const Policy&) {
200  static const char* function = "stan::prob::gamma_cdf(%1%)";
201 
202  using stan::math::check_finite;
203  using stan::math::check_positive;
204  using stan::math::check_nonnegative;
205  using boost::math::tools::promote_args;
206 
207  typename promote_args<T_y,T_shape,T_inv_scale>::type result;
208  if (!check_finite(function, y, "Random variable", &result, Policy()))
209  return result;
210  if (!check_nonnegative(function, y, "Random variable", &result,
211  Policy()))
212  return result;
213  if (!check_finite(function, alpha, "Shape parameter", &result,
214  Policy()))
215  return result;
216  if (!check_positive(function, alpha, "Shape parameter", &result,
217  Policy()))
218  return result;
219  if (!check_finite(function, beta, "Inverse scale parameter",
220  &result, Policy()))
221  return result;
222  if (!check_positive(function, beta, "Inverse scale parameter",
223  &result, Policy()))
224  return result;
225 
226  // FIXME: implement gamma cdf
227  return boost::math::gamma_p(alpha, y*beta);
228  }
229 
230  template <typename T_y, typename T_shape, typename T_inv_scale>
231  inline
232  typename boost::math::tools::promote_args<T_y,T_shape,T_inv_scale>::type
233  gamma_cdf(const T_y& y, const T_shape& alpha, const T_inv_scale& beta) {
234  return gamma_cdf(y,alpha,beta,stan::math::default_policy());
235  }
236  */
237 
238  }
239 }
240 
241 #endif
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).
var log(const var &a)
Return the natural log of the specified variable (cmath).
Definition: agrad.hpp:1730
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.
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
return_type< T_y, T_shape, T_inv_scale >::type gamma_log(const T_y &y, const T_shape &alpha, const T_inv_scale &beta, const Policy &)
The log of a gamma density for y with the specified shape and inverse scale parameters.
Definition: gamma.hpp:41
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
A variable implementation that stores operands and derivatives with respect to the variable.
VectorView< double *, is_vector< T1 >::value > d_x1
VectorView< double *, is_vector< T2 >::value > d_x2
T_return_type to_var(double logp)
VectorView< double *, is_vector< T3 >::value > d_x3
Metaprogram to determine if a type has a base scalar type that can be assigned to type double.
Definition: traits.hpp:39
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.