1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__UNIFORM_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__UNIFORM_HPP
4 #include <boost/random/uniform_real_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
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%)";
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);
67 "Random variable",
"Lower bound parameter",
68 "Upper bound parameter",
80 for (
size_t n = 0; n < N; n++) {
81 const double y_dbl =
value_of(y_vec[n]);
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]
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]
103 operands_and_partials(y,alpha,beta);
104 for (
size_t n = 0; n < N; n++) {
106 logp -= log_beta_minus_alpha[n];
109 operands_and_partials.
d_x2[n] += inv_beta_minus_alpha[n];
111 operands_and_partials.
d_x3[n] -= inv_beta_minus_alpha[n];
113 return operands_and_partials.
to_var(logp);
116 template <
typename T_y,
typename T_low,
typename T_high>
119 uniform_log(
const T_y& y,
const T_low& alpha,
const T_high& beta) {
120 return uniform_log<false>(y,alpha,beta);
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%)";
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);
148 "Random variable",
"Lower bound parameter",
149 "Upper bound parameter",
155 size_t N =
max_size(y, alpha, beta);
157 for (
size_t n = 0; n < N; n++) {
158 const double y_dbl =
value_of(y_vec[n]);
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;
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
183 operands_and_partials.
d_x3[n] -= 1.0 / b_min_a;
188 operands_and_partials.
d_x1[n] *= cdf;
191 operands_and_partials.
d_x2[n] *= cdf;
194 operands_and_partials.
d_x3[n] *= cdf;
196 return operands_and_partials.
to_var(cdf);
199 template <
typename T_y,
typename T_low,
typename T_high>
202 static const char*
function =
"stan::prob::uniform_cdf_log(%1%)";
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);
224 "Random variable",
"Lower bound parameter",
225 "Upper bound parameter",
231 size_t N =
max_size(y, alpha, beta);
234 operands_and_partials(y,alpha,beta);
236 for (
size_t n = 0; n < N; n++) {
237 const double y_dbl =
value_of(y_vec[n]);
242 return operands_and_partials.
to_var(0.0);
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;
253 cdf_log +=
log(cdf_log_);
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;
265 return operands_and_partials.
to_var(cdf_log);
268 template <
typename T_y,
typename T_low,
typename T_high>
271 static const char*
function =
"stan::prob::uniform_ccdf_log(%1%)";
286 double ccdf_log(0.0);
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);
293 "Random variable",
"Lower bound parameter",
294 "Upper bound parameter",
300 size_t N =
max_size(y, alpha, beta);
302 for (
size_t n = 0; n < N; n++) {
303 const double y_dbl =
value_of(y_vec[n]);
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;
321 ccdf_log +=
log(ccdf_log_);
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_;
334 return operands_and_partials.
to_var(ccdf_log);
342 using boost::variate_generator;
343 using boost::random::uniform_real_distribution;
345 static const char*
function =
"stan::prob::uniform_rng(%1%)";
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);
354 variate_generator<RNG&, uniform_real_distribution<> >
355 uniform_rng(rng, uniform_real_distribution<>(alpha, beta));
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.
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)
DoubleVectorView allocates double values to be used as intermediate values.
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.
return_type< T_y, T_low, T_high >::type uniform_ccdf_log(const T_y &y, const T_low &alpha, const T_high &beta)
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
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
double value_of(const T x)
Return the value of the specified scalar argument converted to a double value.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
VectorView< double *, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
double uniform_rng(const double alpha, const double beta, RNG &rng)
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)
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)
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)
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
return_type< T_y, T_low, T_high >::type uniform_cdf_log(const T_y &y, const T_low &alpha, const T_high &beta)
double negative_infinity()
Return negative infinity.