Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
exponential.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__EXPONENTIAL_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__EXPONENTIAL_HPP__
3 
4 #include <boost/random/exponential_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
6 
7 #include <stan/agrad.hpp>
10 #include <stan/meta/traits.hpp>
11 #include <stan/prob/constants.hpp>
12 #include <stan/prob/traits.hpp>
13 
14 namespace stan {
15 
16  namespace prob {
17 
44  template <bool propto,
45  typename T_y, typename T_inv_scale,
46  class Policy>
47  typename return_type<T_y,T_inv_scale>::type
48  exponential_log(const T_y& y, const T_inv_scale& beta,
49  const Policy&) {
50  static const char* function = "stan::prob::exponential_log(%1%)";
51 
52  // check if any vectors are zero length
53  if (!(stan::length(y)
54  && stan::length(beta)))
55  return 0.0;
56 
62 
63  double logp(0.0);
64  if(!check_not_nan(function, y, "Random variable", &logp, Policy()))
65  return logp;
66  if(!check_finite(function, beta, "Inverse scale parameter", &logp, Policy()))
67  return logp;
68  if(!check_positive(function, beta, "Inverse scale parameter", &logp, Policy()))
69  return logp;
70 
71  if (!(check_consistent_sizes(function,
72  y,beta,
73  "Random variable","Inverse scale parameter",
74  &logp, Policy())))
75  return logp;
76 
77 
78  // set up template expressions wrapping scalars into vector views
79  VectorView<const T_y> y_vec(y);
80  VectorView<const T_inv_scale> beta_vec(beta);
81  size_t N = max_size(y, beta);
82 
85  is_vector<T_inv_scale>::value> log_beta(length(beta));
86  for (size_t i = 0; i < length(beta); i++)
87  if (include_summand<propto,T_inv_scale>::value)
88  log_beta[i] = log(value_of(beta_vec[i]));
89 
90  agrad::OperandsAndPartials<T_y,T_inv_scale> operands_and_partials(y, beta);
91 
92  for (size_t n = 0; n < N; n++) {
93  const double beta_dbl = value_of(beta_vec[n]);
94  const double y_dbl = value_of(y_vec[n]);
95  if (include_summand<propto,T_inv_scale>::value)
96  logp += log_beta[n];
98  logp -= beta_dbl * y_dbl;
99 
101  operands_and_partials.d_x1[n] -= beta_dbl;
103  operands_and_partials.d_x2[n] += 1 / beta_dbl - y_dbl;
104  }
105  return operands_and_partials.to_var(logp);
106  }
107 
108  template <bool propto,
109  typename T_y, typename T_inv_scale>
110  inline
112  exponential_log(const T_y& y, const T_inv_scale& beta) {
113  return exponential_log<propto>(y,beta,stan::math::default_policy());
114  }
115 
116  template <typename T_y, typename T_inv_scale,
117  class Policy>
118  inline
120  exponential_log(const T_y& y, const T_inv_scale& beta, const Policy&) {
121  return exponential_log<false>(y,beta,Policy());
122  }
123 
124  template <typename T_y, typename T_inv_scale>
125  inline
127  exponential_log(const T_y& y, const T_inv_scale& beta) {
128  return exponential_log<false>(y,beta,stan::math::default_policy());
129  }
130 
131 
132 
146  template <typename T_y,
147  typename T_inv_scale,
148  class Policy>
149  typename boost::math::tools::promote_args<T_y,T_inv_scale>::type
150  exponential_cdf(const T_y& y,
151  const T_inv_scale& beta,
152  const Policy&) {
153 
154  static const char* function = "stan::prob::exponential_cdf(%1%)";
155 
159  using boost::math::tools::promote_args;
160 
161  typename promote_args<T_y,T_inv_scale>::type lp;
162  if(!check_not_nan(function, y, "Random variable", &lp, Policy()))
163  return lp;
164  if(!check_finite(function, beta, "Inverse scale parameter", &lp, Policy()))
165  return lp;
166  if(!check_positive(function, beta, "Inverse scale parameter", &lp, Policy()))
167  return lp;
168 
169  if (y < 0)
170  return 1.0;
171 
172  return 1.0 - exp(-beta * y);
173  }
174 
175  template <typename T_y,
176  typename T_inv_scale>
177  inline
178  typename boost::math::tools::promote_args<T_y,T_inv_scale>::type
179  exponential_cdf(const T_y& y,
180  const T_inv_scale& beta) {
182  }
183 
184  template <class RNG>
185  inline double
186  exponential_rng(const double beta,
187  RNG& rng) {
188  using boost::variate_generator;
189  using boost::exponential_distribution;
190  variate_generator<RNG&, exponential_distribution<> >
191  exp_rng(rng, exponential_distribution<>(beta));
192  return exp_rng();
193  }
194  }
195 }
196 
197 #endif

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