Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
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 <boost/random/uniform_real_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 
18  // CONTINUOUS, UNIVARIATE DENSITIES
40  template <bool propto,
41  typename T_y, typename T_low, typename T_high,
42  class Policy>
43  typename return_type<T_y,T_low,T_high>::type
44  uniform_log(const T_y& y, const T_low& alpha, const T_high& beta,
45  const Policy&) {
46  static const char* function = "stan::prob::uniform_log(%1%)";
47 
53 
54  // check if any vectors are zero length
55  if (!(stan::length(y)
56  && stan::length(alpha)
57  && stan::length(beta)))
58  return 0.0;
59 
60  // set up return value accumulator
61  double logp(0.0);
62  if(!check_not_nan(function, y, "Random variable", &logp, Policy()))
63  return logp;
64  if (!check_finite(function, alpha, "Lower bound parameter", &logp, Policy()))
65  return logp;
66  if (!check_finite(function, beta, "Upper bound parameter", &logp, Policy()))
67  return logp;
68  if (!check_greater(function, beta, alpha, "Upper bound parameter",
69  &logp, Policy()))
70  return logp;
71  if (!(check_consistent_sizes(function,
72  y,alpha,beta,
73  "Random variable","Lower bound parameter","Upper bound parameter",
74  &logp, Policy())))
75  return logp;
76 
77 
78  // check if no variables are involved and prop-to
80  return 0.0;
81 
82  VectorView<const T_y> y_vec(y);
83  VectorView<const T_low> alpha_vec(alpha);
84  VectorView<const T_high> beta_vec(beta);
85  size_t N = max_size(y, alpha, beta);
86 
87  for (size_t n = 0; n < N; n++) {
88  const double y_dbl = value_of(y_vec[n]);
89  if (y_dbl < value_of(alpha_vec[n]) || y_dbl > value_of(beta_vec[n]))
90  return LOG_ZERO;
91  }
92 
94  is_vector<T_low>::value | is_vector<T_high>::value> inv_beta_minus_alpha(max_size(alpha,beta));
95  for (size_t i = 0; i < max_size(alpha,beta); i++)
97  inv_beta_minus_alpha[i] = 1.0 / (value_of(beta_vec[i]) - value_of(alpha_vec[i]));
99  is_vector<T_low>::value | is_vector<T_high>::value> log_beta_minus_alpha(max_size(alpha,beta));
100  for (size_t i = 0; i < max_size(alpha,beta); i++)
102  log_beta_minus_alpha[i] = log(value_of(beta_vec[i]) - value_of(alpha_vec[i]));
103 
104  agrad::OperandsAndPartials<T_y,T_low,T_high> operands_and_partials(y,alpha,beta);
105  for (size_t n = 0; n < N; n++) {
107  logp -= log_beta_minus_alpha[n];
108 
110  operands_and_partials.d_x2[n] += inv_beta_minus_alpha[n];
112  operands_and_partials.d_x3[n] -= inv_beta_minus_alpha[n];
113  }
114  return operands_and_partials.to_var(logp);
115  }
116 
117 
118  template <bool propto,
119  typename T_y, typename T_low, typename T_high>
120  inline
122  uniform_log(const T_y& y, const T_low& alpha, const T_high& beta) {
123  return uniform_log<propto>(y,alpha,beta,stan::math::default_policy());
124  }
125 
126  template <typename T_y, typename T_low, typename T_high,
127  class Policy>
128  inline
130  uniform_log(const T_y& y, const T_low& alpha, const T_high& beta,
131  const Policy&) {
132  return uniform_log<false>(y,alpha,beta,Policy());
133  }
134 
135 
136  template <typename T_y, typename T_low, typename T_high>
137  inline
139  uniform_log(const T_y& y, const T_low& alpha, const T_high& beta) {
140  return uniform_log<false>(y,alpha,beta,stan::math::default_policy());
141  }
142 
143 
144  template <class RNG>
145  inline double
146  uniform_rng(const double alpha,
147  const double beta,
148  RNG& rng) {
149  using boost::variate_generator;
150  using boost::random::uniform_real_distribution;
151  variate_generator<RNG&, uniform_real_distribution<> >
152  uniform_rng(rng, uniform_real_distribution<>(alpha, beta));
153  return uniform_rng();
154  }
155  }
156 }
157 #endif

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