Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations 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 
11 #include <stan/meta/traits.hpp>
12 #include <stan/prob/constants.hpp>
13 #include <stan/prob/traits.hpp>
14 
15 namespace stan {
16 
17  namespace prob {
18 
19  // CONTINUOUS, UNIVARIATE DENSITIES
41  template <bool propto,
42  typename T_y, typename T_low, typename T_high>
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  static const char* function = "stan::prob::uniform_log(%1%)";
46 
52 
53  // check if any vectors are zero length
54  if (!(stan::length(y)
55  && stan::length(alpha)
56  && stan::length(beta)))
57  return 0.0;
58 
59  // set up return value accumulator
60  double logp(0.0);
61  check_not_nan(function, y, "Random variable", &logp);
62  check_finite(function, alpha, "Lower bound parameter", &logp);
63  check_finite(function, beta, "Upper bound parameter", &logp);
64  check_greater(function, beta, alpha, "Upper bound parameter", &logp);
65  check_consistent_sizes(function,
66  y,alpha,beta,
67  "Random variable","Lower bound parameter",
68  "Upper bound parameter",
69  &logp);
70 
71  // check if no variables are involved and prop-to
73  return 0.0;
74 
75  VectorView<const T_y> y_vec(y);
76  VectorView<const T_low> alpha_vec(alpha);
77  VectorView<const T_high> beta_vec(beta);
78  size_t N = max_size(y, alpha, beta);
79 
80  for (size_t n = 0; n < N; n++) {
81  const double y_dbl = value_of(y_vec[n]);
82  if (y_dbl < value_of(alpha_vec[n])
83  || y_dbl > value_of(beta_vec[n]))
84  return LOG_ZERO;
85  }
86 
89  inv_beta_minus_alpha(max_size(alpha,beta));
90  for (size_t i = 0; i < max_size(alpha,beta); i++)
92  inv_beta_minus_alpha[i]
93  = 1.0 / (value_of(beta_vec[i]) - value_of(alpha_vec[i]));
96  log_beta_minus_alpha(max_size(alpha,beta));
97  for (size_t i = 0; i < max_size(alpha,beta); i++)
99  log_beta_minus_alpha[i]
100  = log(value_of(beta_vec[i]) - value_of(alpha_vec[i]));
101 
103  operands_and_partials(y,alpha,beta);
104  for (size_t n = 0; n < N; n++) {
106  logp -= log_beta_minus_alpha[n];
107 
109  operands_and_partials.d_x2[n] += inv_beta_minus_alpha[n];
111  operands_and_partials.d_x3[n] -= inv_beta_minus_alpha[n];
112  }
113  return operands_and_partials.to_var(logp);
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);
121  }
122 
123  template <typename T_y, typename T_low, typename T_high>
125  uniform_cdf(const T_y& y, const T_low& alpha, const T_high& beta) {
126  static const char* function = "stan::prob::uniform_cdf(%1%)";
127 
131  using stan::math::value_of;
133 
134  // check if any vectors are zero length
135  if (!(stan::length(y)
136  && stan::length(alpha)
137  && stan::length(beta)))
138  return 1.0;
139 
140  // set up return value accumulator
141  double cdf(1.0);
142  check_not_nan(function, y, "Random variable", &cdf);
143  check_finite(function, alpha, "Lower bound parameter", &cdf);
144  check_finite(function, beta, "Upper bound parameter", &cdf);
145  check_greater(function, beta, alpha, "Upper bound parameter", &cdf);
146  check_consistent_sizes(function,
147  y,alpha,beta,
148  "Random variable","Lower bound parameter",
149  "Upper bound parameter",
150  &cdf);
151 
152  VectorView<const T_y> y_vec(y);
153  VectorView<const T_low> alpha_vec(alpha);
154  VectorView<const T_high> beta_vec(beta);
155  size_t N = max_size(y, alpha, beta);
156 
157  for (size_t n = 0; n < N; n++) {
158  const double y_dbl = value_of(y_vec[n]);
159  if (y_dbl < value_of(alpha_vec[n])
160  || y_dbl > value_of(beta_vec[n]))
161  return 0.0;
162  }
163 
165  operands_and_partials(y,alpha,beta);
166  for (size_t n = 0; n < N; n++) {
167  const double y_dbl = value_of(y_vec[n]);
168  const double alpha_dbl = value_of(alpha_vec[n]);
169  const double beta_dbl = value_of(beta_vec[n]);
170  const double b_min_a = beta_dbl - alpha_dbl;
171  const double cdf_ = (y_dbl - alpha_dbl) / b_min_a;
172 
173  //cdf
174  cdf *= cdf_;
175 
176  //gradients
178  operands_and_partials.d_x1[n] += 1.0 / b_min_a / cdf_;
180  operands_and_partials.d_x2[n] += (y_dbl - beta_dbl) / b_min_a
181  / b_min_a / cdf_;
183  operands_and_partials.d_x3[n] -= 1.0 / b_min_a;
184  }
185 
187  for (size_t n = 0; n < stan::length(y); ++n)
188  operands_and_partials.d_x1[n] *= cdf;
190  for (size_t n = 0; n < stan::length(alpha); ++n)
191  operands_and_partials.d_x2[n] *= cdf;
193  for (size_t n = 0; n < stan::length(beta); ++n)
194  operands_and_partials.d_x3[n] *= cdf;
195 
196  return operands_and_partials.to_var(cdf);
197  }
198 
199  template <typename T_y, typename T_low, typename T_high>
201  uniform_cdf_log(const T_y& y, const T_low& alpha, const T_high& beta) {
202  static const char* function = "stan::prob::uniform_cdf_log(%1%)";
203 
207  using stan::math::value_of;
209 
210  // check if any vectors are zero length
211  if (!(stan::length(y)
212  && stan::length(alpha)
213  && stan::length(beta)))
214  return 0.0;
215 
216  // set up return value accumulator
217  double cdf_log(0.0);
218  check_not_nan(function, y, "Random variable", &cdf_log);
219  check_finite(function, alpha, "Lower bound parameter", &cdf_log);
220  check_finite(function, beta, "Upper bound parameter", &cdf_log);
221  check_greater(function, beta, alpha, "Upper bound parameter", &cdf_log);
222  check_consistent_sizes(function,
223  y,alpha,beta,
224  "Random variable","Lower bound parameter",
225  "Upper bound parameter",
226  &cdf_log);
227 
228  VectorView<const T_y> y_vec(y);
229  VectorView<const T_low> alpha_vec(alpha);
230  VectorView<const T_high> beta_vec(beta);
231  size_t N = max_size(y, alpha, beta);
232 
234  operands_and_partials(y,alpha,beta);
235 
236  for (size_t n = 0; n < N; n++) {
237  const double y_dbl = value_of(y_vec[n]);
238  if (y_dbl < value_of(alpha_vec[n])
239  || y_dbl > value_of(beta_vec[n]))
241  if (y_dbl == value_of(beta_vec[n]))
242  return operands_and_partials.to_var(0.0);
243  }
244 
245  for (size_t n = 0; n < N; n++) {
246  const double y_dbl = value_of(y_vec[n]);
247  const double alpha_dbl = value_of(alpha_vec[n]);
248  const double beta_dbl = value_of(beta_vec[n]);
249  const double b_min_a = beta_dbl - alpha_dbl;
250  const double cdf_log_ = (y_dbl - alpha_dbl) / b_min_a;
251 
252  //cdf_log
253  cdf_log += log(cdf_log_);
254 
255  //gradients
257  operands_and_partials.d_x1[n] += 1.0 / b_min_a / cdf_log_;
259  operands_and_partials.d_x2[n] += (y_dbl - beta_dbl) / b_min_a
260  / b_min_a / cdf_log_;
262  operands_and_partials.d_x3[n] -= 1.0 / b_min_a;
263  }
264 
265  return operands_and_partials.to_var(cdf_log);
266  }
267 
268  template <typename T_y, typename T_low, typename T_high>
270  uniform_ccdf_log(const T_y& y, const T_low& alpha, const T_high& beta) {
271  static const char* function = "stan::prob::uniform_ccdf_log(%1%)";
272 
276  using stan::math::value_of;
278 
279  // check if any vectors are zero length
280  if (!(stan::length(y)
281  && stan::length(alpha)
282  && stan::length(beta)))
283  return 0.0;
284 
285  // set up return value accumulator
286  double ccdf_log(0.0);
287  check_not_nan(function, y, "Random variable", &ccdf_log);
288  check_finite(function, alpha, "Lower bound parameter", &ccdf_log);
289  check_finite(function, beta, "Upper bound parameter", &ccdf_log);
290  check_greater(function, beta, alpha, "Upper bound parameter", &ccdf_log);
291  check_consistent_sizes(function,
292  y,alpha,beta,
293  "Random variable","Lower bound parameter",
294  "Upper bound parameter",
295  &ccdf_log);
296 
297  VectorView<const T_y> y_vec(y);
298  VectorView<const T_low> alpha_vec(alpha);
299  VectorView<const T_high> beta_vec(beta);
300  size_t N = max_size(y, alpha, beta);
301 
302  for (size_t n = 0; n < N; n++) {
303  const double y_dbl = value_of(y_vec[n]);
304  if (y_dbl < value_of(alpha_vec[n])
305  || y_dbl > value_of(beta_vec[n]))
306  return 0.0;
307  if (y_dbl == value_of(beta_vec[n]))
308  return LOG_ZERO;
309  }
310 
312  operands_and_partials(y,alpha,beta);
313  for (size_t n = 0; n < N; n++) {
314  const double y_dbl = value_of(y_vec[n]);
315  const double alpha_dbl = value_of(alpha_vec[n]);
316  const double beta_dbl = value_of(beta_vec[n]);
317  const double b_min_a = beta_dbl - alpha_dbl;
318  const double ccdf_log_ = 1.0 - (y_dbl - alpha_dbl) / b_min_a;
319 
320  //ccdf_log
321  ccdf_log += log(ccdf_log_);
322 
323  //gradients
325  operands_and_partials.d_x1[n] -= 1.0 / b_min_a / ccdf_log_;
327  operands_and_partials.d_x2[n] -= (y_dbl - beta_dbl) / b_min_a
328  / b_min_a / ccdf_log_;
330  operands_and_partials.d_x3[n] += (y_dbl - alpha_dbl) / b_min_a
331  / b_min_a / ccdf_log_;
332  }
333 
334  return operands_and_partials.to_var(ccdf_log);
335  }
336 
337  template <class RNG>
338  inline double
339  uniform_rng(const double alpha,
340  const double beta,
341  RNG& rng) {
342  using boost::variate_generator;
343  using boost::random::uniform_real_distribution;
344 
345  static const char* function = "stan::prob::uniform_rng(%1%)";
346 
349 
350  check_finite(function, alpha, "Lower bound parameter", (double*)0);
351  check_finite(function, beta, "Upper bound parameter", (double*)0);
352  check_greater(function, beta, alpha, "Upper bound parameter", (double*)0);
353 
354  variate_generator<RNG&, uniform_real_distribution<> >
355  uniform_rng(rng, uniform_real_distribution<>(alpha, beta));
356  return uniform_rng();
357  }
358  }
359 }
360 #endif
return_type< T_y, T_low, T_high >::type uniform_log(const T_y &y, const T_low &alpha, const T_high &beta)
The log of a uniform density for the given y, lower, and upper bound.
Definition: uniform.hpp:44
bool check_greater(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result)
T_return_type to_var(double logp)
size_t length(const T &)
Definition: traits.hpp:159
DoubleVectorView allocates double values to be used as intermediate values.
Definition: traits.hpp:358
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is finite.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
return_type< T_y, T_low, T_high >::type uniform_ccdf_log(const T_y &y, const T_low &alpha, const T_high &beta)
Definition: uniform.hpp:270
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
double uniform_rng(const double alpha, const double beta, RNG &rng)
Definition: uniform.hpp:339
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.
return_type< T_y, T_low, T_high >::type uniform_cdf(const T_y &y, const T_low &alpha, const T_high &beta)
Definition: uniform.hpp:125
VectorView< double *, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
VectorView< double *, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
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
return_type< T_y, T_low, T_high >::type uniform_cdf_log(const T_y &y, const T_low &alpha, const T_high &beta)
Definition: uniform.hpp:201
double negative_infinity()
Return negative infinity.
Definition: constants.hpp:123

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