Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations 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 
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, typename T_y, typename T_inv_scale>
45  typename return_type<T_y,T_inv_scale>::type
46  exponential_log(const T_y& y, const T_inv_scale& beta) {
47  static const char* function = "stan::prob::exponential_log(%1%)";
48 
49  // check if any vectors are zero length
50  if (!(stan::length(y)
51  && stan::length(beta)))
52  return 0.0;
53 
58 
59  double logp(0.0);
60  check_not_nan(function, y, "Random variable", &logp);
61  check_positive_finite(function, beta, "Inverse scale parameter", &logp);
62  check_consistent_sizes(function,
63  y,beta,
64  "Random variable","Inverse scale parameter",
65  &logp);
66 
67 
68  // set up template expressions wrapping scalars into vector views
69  VectorView<const T_y> y_vec(y);
70  VectorView<const T_inv_scale> beta_vec(beta);
71  size_t N = max_size(y, beta);
72 
75  is_vector<T_inv_scale>::value> log_beta(length(beta));
76  for (size_t i = 0; i < length(beta); i++)
77  if (include_summand<propto,T_inv_scale>::value)
78  log_beta[i] = log(value_of(beta_vec[i]));
79 
80  agrad::OperandsAndPartials<T_y,T_inv_scale> operands_and_partials(y, beta);
81 
82  for (size_t n = 0; n < N; n++) {
83  const double beta_dbl = value_of(beta_vec[n]);
84  const double y_dbl = value_of(y_vec[n]);
85  if (include_summand<propto,T_inv_scale>::value)
86  logp += log_beta[n];
88  logp -= beta_dbl * y_dbl;
89 
91  operands_and_partials.d_x1[n] -= beta_dbl;
93  operands_and_partials.d_x2[n] += 1 / beta_dbl - y_dbl;
94  }
95  return operands_and_partials.to_var(logp);
96  }
97 
98  template <typename T_y, typename T_inv_scale>
99  inline
101  exponential_log(const T_y& y, const T_inv_scale& beta) {
102  return exponential_log<false>(y,beta);
103  }
104 
105 
106 
120  template <typename T_y, typename T_inv_scale>
122  exponential_cdf(const T_y& y, const T_inv_scale& beta) {
123 
124  static const char* function = "stan::prob::exponential_cdf(%1%)";
125 
129  using boost::math::tools::promote_args;
130  using stan::math::value_of;
131 
132  double cdf(1.0);
133  // check if any vectors are zero length
134  if (!(stan::length(y)
135  && stan::length(beta)))
136  return cdf;
137 
138  check_not_nan(function, y, "Random variable", &cdf);
139  check_nonnegative(function, y, "Random variable", &cdf);
140  check_positive_finite(function, beta, "Inverse scale parameter", &cdf);
141 
143  operands_and_partials(y, beta);
144 
145  VectorView<const T_y> y_vec(y);
146  VectorView<const T_inv_scale> beta_vec(beta);
147  size_t N = max_size(y, beta);
148  for (size_t n = 0; n < N; n++) {
149  const double beta_dbl = value_of(beta_vec[n]);
150  const double y_dbl = value_of(y_vec[n]);
151  const double one_m_exp = 1.0 - exp(-beta_dbl * y_dbl);
152 
153  // cdf
154  cdf *= one_m_exp;
155  }
156 
157  for(size_t n = 0; n < N; n++) {
158  const double beta_dbl = value_of(beta_vec[n]);
159  const double y_dbl = value_of(y_vec[n]);
160  const double one_m_exp = 1.0 - exp(-beta_dbl * y_dbl);
161 
162  // gradients
163  double rep_deriv = exp(-beta_dbl * y_dbl) / one_m_exp;
165  operands_and_partials.d_x1[n] += rep_deriv * beta_dbl * cdf;
167  operands_and_partials.d_x2[n] += rep_deriv * y_dbl * cdf;
168  }
169 
170  return operands_and_partials.to_var(cdf);
171  }
172 
173  template <typename T_y, typename T_inv_scale>
175  exponential_cdf_log(const T_y& y, const T_inv_scale& beta) {
176 
177  static const char* function = "stan::prob::exponential_cdf_log(%1%)";
178 
182  using boost::math::tools::promote_args;
183  using stan::math::value_of;
184 
185  double cdf_log(0.0);
186  // check if any vectors are zero length
187  if (!(stan::length(y)
188  && stan::length(beta)))
189  return cdf_log;
190 
191  check_not_nan(function, y, "Random variable", &cdf_log);
192  check_nonnegative(function, y, "Random variable", &cdf_log);
193  check_positive_finite(function, beta, "Inverse scale parameter",
194  &cdf_log);
195 
197  operands_and_partials(y, beta);
198 
199  VectorView<const T_y> y_vec(y);
200  VectorView<const T_inv_scale> beta_vec(beta);
201  size_t N = max_size(y, beta);
202  for (size_t n = 0; n < N; n++) {
203  const double beta_dbl = value_of(beta_vec[n]);
204  const double y_dbl = value_of(y_vec[n]);
205  double one_m_exp = 1.0 - exp(-beta_dbl * y_dbl);
206  // log cdf
207  cdf_log += log(one_m_exp);
208 
209  // gradients
210  double rep_deriv = -exp(-beta_dbl * y_dbl) / one_m_exp;
212  operands_and_partials.d_x1[n] -= rep_deriv * beta_dbl;
214  operands_and_partials.d_x2[n] -= rep_deriv * y_dbl;
215  }
216  return operands_and_partials.to_var(cdf_log);
217  }
218 
219  template <typename T_y, typename T_inv_scale>
221  exponential_ccdf_log(const T_y& y, const T_inv_scale& beta) {
222 
223  static const char* function = "stan::prob::exponential_ccdf_log(%1%)";
224 
228  using boost::math::tools::promote_args;
229  using stan::math::value_of;
230 
231  double ccdf_log(0.0);
232  // check if any vectors are zero length
233  if (!(stan::length(y)
234  && stan::length(beta)))
235  return ccdf_log;
236 
237  check_not_nan(function, y, "Random variable", &ccdf_log);
238  check_nonnegative(function, y, "Random variable", &ccdf_log);
239  check_positive_finite(function, beta, "Inverse scale parameter",
240  &ccdf_log);
241 
243  operands_and_partials(y, beta);
244 
245  VectorView<const T_y> y_vec(y);
246  VectorView<const T_inv_scale> beta_vec(beta);
247  size_t N = max_size(y, beta);
248  for (size_t n = 0; n < N; n++) {
249  const double beta_dbl = value_of(beta_vec[n]);
250  const double y_dbl = value_of(y_vec[n]);
251  // log ccdf
252  ccdf_log += -beta_dbl * y_dbl;
253 
254  // gradients
256  operands_and_partials.d_x1[n] -= beta_dbl;
258  operands_and_partials.d_x2[n] -= y_dbl;
259  }
260  return operands_and_partials.to_var(ccdf_log);
261  }
262 
263  template <class RNG>
264  inline double
265  exponential_rng(const double beta,
266  RNG& rng) {
267  using boost::variate_generator;
268  using boost::exponential_distribution;
269 
270  static const char* function = "stan::prob::exponential_rng(%1%)";
271 
273 
274  check_positive_finite(function, beta, "Inverse scale parameter",
275  (double*)0);
276 
277  variate_generator<RNG&, exponential_distribution<> >
278  exp_rng(rng, exponential_distribution<>(beta));
279  return exp_rng();
280  }
281  }
282 }
283 
284 #endif
return_type< T_y, T_inv_scale >::type exponential_ccdf_log(const T_y &y, const T_inv_scale &beta)
T_return_type to_var(double logp)
bool check_positive_finite(const char *function, const T_y &y, const char *name, T_result *result)
return_type< T_y, T_inv_scale >::type exponential_cdf_log(const T_y &y, const T_inv_scale &beta)
size_t length(const T &)
Definition: traits.hpp:159
DoubleVectorView allocates double values to be used as intermediate values.
Definition: traits.hpp:358
double exponential_rng(const double beta, RNG &rng)
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
A variable implementation that stores operands and derivatives with respect to the variable...
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:406
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
Definition: traits.hpp:57
double value_of(const T x)
Return the value of the specified scalar argument converted to a double value.
Definition: value_of.hpp:24
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:35
VectorView< double *, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result)
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result)
size_t max_size(const T1 &x1, const T2 &x2)
Definition: traits.hpp:191
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is nan.
VectorView< double *, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: traits.hpp:275
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:16
return_type< T_y, T_inv_scale >::type exponential_cdf(const T_y &y, const T_inv_scale &beta)
Calculates the exponential cumulative distribution function for the given y and beta.
return_type< T_y, T_inv_scale >::type exponential_log(const T_y &y, const T_inv_scale &beta)
The log of an exponential density for y with the specified inverse scale parameter.
Definition: exponential.hpp:46

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