Stan  1.0
probability, sampling & optimization
uniform.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__UNIFORM_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__UNIFORM_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  // CONTINUOUS, UNIVARIATE DENSITIES
37  template <bool propto,
38  typename T_y, typename T_low, typename T_high,
39  class Policy>
41  uniform_log(const T_y& y, const T_low& alpha, const T_high& beta,
42  const Policy&) {
43  static const char* function = "stan::prob::uniform_log(%1%)";
44 
50 
51  // check if any vectors are zero length
52  if (!(stan::length(y)
53  && stan::length(alpha)
54  && stan::length(beta)))
55  return 0.0;
56 
57  // set up return value accumulator
58  typename return_type<T_y,T_low,T_high>::type logp(0.0);
59  if(!check_not_nan(function, y, "Random variable", &logp, Policy()))
60  return logp;
61  if (!check_finite(function, alpha, "Lower bound parameter", &logp, Policy()))
62  return logp;
63  if (!check_finite(function, beta, "Upper bound parameter", &logp, Policy()))
64  return logp;
65  if (!check_greater(function, beta, alpha, "Upper bound parameter",
66  &logp, Policy()))
67  return logp;
68  if (!(check_consistent_sizes(function,
69  y,alpha,beta,
70  "Random variable","Lower bound parameter","Upper bound parameter",
71  &logp, Policy())))
72  return logp;
73 
74 
75  // check if no variables are involved and prop-to
77  return 0.0;
78 
79  VectorView<const T_y> y_vec(y);
80  VectorView<const T_low> alpha_vec(alpha);
81  VectorView<const T_high> beta_vec(beta);
82  size_t N = max_size(y, alpha, beta);
83 
84  for (size_t n = 0; n < N; n++) {
85  const double y_dbl = value_of(y_vec[n]);
86  if (y_dbl < value_of(alpha_vec[n]) || y_dbl > value_of(beta_vec[n]))
87  return LOG_ZERO;
88  }
89 
90  for (size_t n = 0; n < N; n++) {
92  logp -= log(beta_vec[n] - alpha_vec[n]);
93  }
94  return logp;
95  }
96 
97 
98  template <bool propto,
99  typename T_y, typename T_low, typename T_high>
100  inline
102  uniform_log(const T_y& y, const T_low& alpha, const T_high& beta) {
103  return uniform_log<propto>(y,alpha,beta,stan::math::default_policy());
104  }
105 
106  template <typename T_y, typename T_low, typename T_high,
107  class Policy>
108  inline
110  uniform_log(const T_y& y, const T_low& alpha, const T_high& beta,
111  const Policy&) {
112  return uniform_log<false>(y,alpha,beta,Policy());
113  }
114 
115 
116  template <typename T_y, typename T_low, typename T_high>
117  inline
119  uniform_log(const T_y& y, const T_low& alpha, const T_high& beta) {
120  return uniform_log<false>(y,alpha,beta,stan::math::default_policy());
121  }
122 
123 
124  }
125 }
126 #endif
double value_of(const agrad::var &v)
Return the value of the specified variable.
var log(const var &a)
Return the natural log of the specified variable (cmath).
Definition: agrad.hpp:1730
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_greater(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result, const Policy &)
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_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_low, T_high >::type uniform_log(const T_y &y, const T_low &alpha, const T_high &beta, const Policy &)
The log of a uniform density for the given y, lower, and upper bound.
Definition: uniform.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
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.