Stan  1.0
probability, sampling & optimization
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 <stan/agrad.hpp>
7 #include <stan/meta/traits.hpp>
9 #include <stan/prob/traits.hpp>
10 
11 namespace stan {
12 
13  namespace prob {
14 
41  template <bool propto,
42  typename T_y, typename T_inv_scale,
43  class Policy>
45  exponential_log(const T_y& y, const T_inv_scale& beta,
46  const Policy&) {
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  typename return_type<T_y,T_inv_scale>::type logp(0.0);
60  if(!check_not_nan(function, y, "Random variable", &logp, Policy()))
61  return logp;
62  if(!check_finite(function, beta, "Inverse scale parameter", &logp, Policy()))
63  return logp;
64  if(!check_positive(function, beta, "Inverse scale parameter", &logp, Policy()))
65  return logp;
66 
67  if (!(check_consistent_sizes(function,
68  y,beta,
69  "Random variable","Inverse scale parameter",
70  &logp, Policy())))
71  return logp;
72 
73 
74  // set up template expressions wrapping scalars into vector views
75  VectorView<const T_y> y_vec(y);
76  VectorView<const T_inv_scale> beta_vec(beta);
77  size_t N = max_size(y, beta);
78 
79  for (size_t n = 0; n < N; n++) {
81  logp += log(beta_vec[n]);
83  logp -= beta_vec[n] * y_vec[n];
84  }
85  return logp;
86  }
87 
88  template <bool propto,
89  typename T_y, typename T_inv_scale>
90  inline
92  exponential_log(const T_y& y, const T_inv_scale& beta) {
93  return exponential_log<propto>(y,beta,stan::math::default_policy());
94  }
95 
96  template <typename T_y, typename T_inv_scale,
97  class Policy>
98  inline
100  exponential_log(const T_y& y, const T_inv_scale& beta, const Policy&) {
101  return exponential_log<false>(y,beta,Policy());
102  }
103 
104  template <typename T_y, typename T_inv_scale>
105  inline
107  exponential_log(const T_y& y, const T_inv_scale& beta) {
108  return exponential_log<false>(y,beta,stan::math::default_policy());
109  }
110 
111 
112 
126  template <typename T_y,
127  typename T_inv_scale,
128  class Policy>
129  typename boost::math::tools::promote_args<T_y,T_inv_scale>::type
130  exponential_cdf(const T_y& y,
131  const T_inv_scale& beta,
132  const Policy&) {
133 
134  static const char* function = "stan::prob::exponential_cdf(%1%)";
135 
139  using boost::math::tools::promote_args;
140 
141  typename promote_args<T_y,T_inv_scale>::type lp;
142  if(!check_not_nan(function, y, "Random variable", &lp, Policy()))
143  return lp;
144  if(!check_finite(function, beta, "Inverse scale parameter", &lp, Policy()))
145  return lp;
146  if(!check_positive(function, beta, "Inverse scale parameter", &lp, Policy()))
147  return lp;
148 
149  if (y < 0)
150  return 1.0;
151 
152  return 1.0 - exp(-beta * y);
153  }
154 
155  template <typename T_y,
156  typename T_inv_scale>
157  inline
158  typename boost::math::tools::promote_args<T_y,T_inv_scale>::type
159  exponential_cdf(const T_y& y,
160  const T_inv_scale& beta) {
162  }
163 
164 
165 
166  }
167 }
168 
169 #endif
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
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.
return_type< T_y, T_inv_scale >::type exponential_log(const T_y &y, const T_inv_scale &beta, const Policy &)
The log of an exponential density for y with the specified inverse scale parameter.
Definition: exponential.hpp:45
boost::math::tools::promote_args< T_y, T_inv_scale >::type exponential_cdf(const T_y &y, const T_inv_scale &beta, const Policy &)
Calculates the exponential cumulative distribution function for the given y and beta.
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.