Stan  1.0
probability, sampling & optimization
weibull.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__WEIBULL_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__WEIBULL_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 
15  // Weibull(y|sigma,alpha) [y >= 0; sigma > 0; alpha > 0]
16  // FIXME: document
17  template <bool propto,
18  typename T_y, typename T_shape, typename T_scale,
19  class Policy>
21  weibull_log(const T_y& y, const T_shape& alpha, const T_scale& sigma,
22  const Policy&) {
23  static const char* function = "stan::prob::weibull_log(%1%)";
24 
30 
31  // check if any vectors are zero length
32  if (!(stan::length(y)
33  && stan::length(alpha)
34  && stan::length(sigma)))
35  return 0.0;
36 
37  // set up return value accumulator
38  typename return_type<T_y,T_shape,T_scale>::type logp(0.0);
39  if(!check_finite(function, y, "Random variable", &logp, Policy()))
40  return logp;
41  if(!check_finite(function, alpha, "Shape parameter",
42  &logp, Policy()))
43  return logp;
44  if(!check_positive(function, alpha, "Shape parameter",
45  &logp, Policy()))
46  return logp;
47  if(!check_not_nan(function, sigma, "Scale parameter",
48  &logp, Policy()))
49  return logp;
50  if(!check_positive(function, sigma, "Scale parameter",
51  &logp, Policy()))
52  return logp;
53  if (!(check_consistent_sizes(function,
54  y,alpha,sigma,
55  "Random variable","Shape parameter","Scale parameter",
56  &logp, Policy())))
57  return logp;
58 
59  // check if no variables are involved and prop-to
61  return 0.0;
62 
63  VectorView<const T_y> y_vec(y);
64  VectorView<const T_shape> alpha_vec(alpha);
65  VectorView<const T_scale> sigma_vec(sigma);
66  size_t N = max_size(y, alpha, sigma);
67 
68  for (size_t n = 0; n < N; n++) {
69  const double y_dbl = value_of(y_vec[n]);
70  if (y_dbl < 0)
71  return LOG_ZERO;
72  }
73 
75 
76  for (size_t n = 0; n < N; n++) {
78  logp += log(alpha_vec[n]);
80  logp += multiply_log(alpha_vec[n]-1.0, y_vec[n]);
82  logp -= multiply_log(alpha_vec[n], sigma_vec[n]);
84  logp -= pow(y_vec[n] / sigma_vec[n], alpha_vec[n]);
85  }
86  return logp;
87  }
88 
89 
90  template <bool propto,
91  typename T_y, typename T_shape, typename T_scale>
92  inline
94  weibull_log(const T_y& y, const T_shape& alpha, const T_scale& sigma) {
95  return weibull_log<propto>(y,alpha,sigma,stan::math::default_policy());
96  }
97 
98 
99  template <typename T_y, typename T_shape, typename T_scale,
100  class Policy>
101  inline
103  weibull_log(const T_y& y, const T_shape& alpha, const T_scale& sigma,
104  const Policy&) {
105  return weibull_log<false>(y,alpha,sigma,Policy());
106  }
107 
108 
109  template <typename T_y, typename T_shape, typename T_scale>
110  inline
112  weibull_log(const T_y& y, const T_shape& alpha, const T_scale& sigma) {
113  return weibull_log<false>(y,alpha,sigma,stan::math::default_policy());
114  }
115 
116 
117 
118 
119  template <typename T_y, typename T_shape, typename T_scale,
120  class Policy>
121  typename boost::math::tools::promote_args<T_y,T_shape,T_scale>::type
122  weibull_cdf(const T_y& y, const T_shape& alpha, const T_scale& sigma,
123  const Policy&) {
124 
125  static const char* function = "stan::prob::weibull_cdf(%1%)";
126 
129  using boost::math::tools::promote_args;
130 
131  typename promote_args<T_y,T_shape,T_scale>::type lp;
132  if (!check_finite(function, alpha, "Shape parameter",
133  &lp, Policy()))
134  return lp;
135  if (!check_positive(function, alpha, "Shape parameter",
136  &lp, Policy()))
137  return lp;
138  if (!check_finite(function, sigma, "Scale parameter",
139  &lp, Policy()))
140  return lp;
141  if (!check_positive(function, sigma, "Scale parameter",
142  &lp, Policy()))
143  return lp;
144 
145  if (y < 0.0)
146  return 0.0;
147  return 1.0 - exp(-pow(y / sigma, alpha));
148  }
149 
150  template <typename T_y, typename T_shape, typename T_scale>
151  inline
152  typename boost::math::tools::promote_args<T_y,T_shape,T_scale>::type
153  weibull_cdf(const T_y& y, const T_shape& alpha, const T_scale& sigma) {
154  return weibull_cdf(y,alpha,sigma,stan::math::default_policy());
155  }
156 
157  }
158 }
159 #endif
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 pow(const var &base, const var &exponent)
Return the base raised to the power of the exponent (cmath).
Definition: agrad.hpp:1778
var log(const var &a)
Return the natural log of the specified variable (cmath).
Definition: agrad.hpp:1730
var exp(const var &a)
Return the exponentiation of the specified variable (cmath).
Definition: agrad.hpp:1716
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.
boost::math::tools::promote_args< T_y, T_shape, T_scale >::type weibull_cdf(const T_y &y, const T_shape &alpha, const T_scale &sigma, const Policy &)
Definition: weibull.hpp:122
return_type< T_y, T_shape, T_scale >::type weibull_log(const T_y &y, const T_shape &alpha, const T_scale &sigma, const Policy &)
Definition: weibull.hpp:21
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.