16 std::uniform_real_distribution<double> d(0.0, 1.0);
20 double uniform(
const double a,
const double b) {
21 std::uniform_real_distribution<double> d(a,b);
35 std::binomial_distribution<int> bd(n,p);
40 std::normal_distribution<float> normal(0.0, 1.0);
55 const T linv_sqrt_2pi = -0.5*log(2*
pi*sd*sd);
56 const T z = (x-mu)/sd;
57 return linv_sqrt_2pi - z*z / 2.0;
63 return std::erfc(-z/std::sqrt(2))/2;
68 std::student_t_distribution<double> dist(nu);
75 log(1+x*x/nu)*(-(nu+1)/2);
99 return -log(
pi) - log(gamma) - log(1+((x-loc)/gamma)*((x-loc)/gamma));
114 return -log(2*b) - std::abs(x-mu)/b;
145 return log(p) + (k-1)*log(1-p);
149 std::gamma_distribution<double> g(a,b);
162 std::gamma_distribution<t> g(a, 1.0);
163 std::vector<t> out(len);
165 for(
size_t i =0;i<len;i++){
169 for(
size_t i=0;i<len;i++){
183 std::uniform_int_distribution<T> r(0, max-1);
196 std::uniform_int_distribution<T> r(min,max-1);
207 std::vector<int> out; out.reserve(n);
208 for(
size_t i=0;i<n;i++) {
223 std::vector<bool> myout(n,
false);
224 for(
size_t i=0;i<n;i++) {
237 template<
typename t,
typename T>
238 double sample_z(
const T& s,
const std::function<
double(
const t&)>& f) {
249 if(not std::isnan(fx)) {
250 assert(fx >= 0.0 &&
"*** Cannot use sample/sample_z with negative probabilities. Did you mean to use sample_lp/sample_lp_z?");
257 template<
typename t,
typename T>
258 std::pair<t*,double>
sample(
const T& s,
double z,
const std::function<
double(
const t&)>& f = [](
const t& v){
return 1.0;}) {
265 assert(z > 0 &&
"*** Cannot call sample with zero normalizer -- is s empty?");
270 if(std::isnan(fx))
continue;
274 return std::make_pair(const_cast<t*>(&x), log(fx)-log(z));
280 template<
typename t,
typename T>
281 std::pair<t*,double>
sample(
const T& s,
const std::function<
double(
const t&)>& f = [](
const t& v){
return 1.0;}) {
283 return sample<t,T>(s, sample_z<t,T>(s,f), f);
287 template<
typename t,
typename T>
288 double sample_lp_z(
const T& s,
const std::function<
double(
const t&)>& f) {
297 if(not std::isnan(fx))
303 template<
typename t,
typename T>
304 std::pair<t*,double>
sample_lp(
const T& s,
double z,
const std::function<
double(
const t&)>& f = [](
const t& v){
return 0.0;}) {
306 assert(z > -
infinity &&
"*** Cannot call sample with zero (-infinity) normalizer -- is s empty?");
311 if(std::isnan(fx))
continue;
313 if(tot > r)
return std::make_pair(const_cast<t*>(&x), log(fx)-log(z));
319 template<
typename t,
typename T>
320 std::pair<t*,double>
sample_lp(
const T& s,
const std::function<
double(
const t&)>& f = [](
const t& v){
return 0.0;}) {
322 return sample_lp<t,T>(s, sample_lp_z<t,T>(s,f), f);
330 std::pair<int,double>
sample_int(
unsigned int max,
const std::function<
double(
const int)>& f = [](
const int v){
return 1.0;}) {
333 for(
size_t i=0;i<max;i++){
339 for(
size_t i=0;i<max;i++){
341 if(std::isnan(fx))
continue;
345 return std::make_pair(i, log(fx)-log(z));
352 std::pair<int,double>
sample_int_lp(
unsigned int max,
const std::function<
double(
const int)>& f = [](
const int v){
return 1.0;}) {
355 for(
size_t i=0;i<max;i++){
357 if(std::isnan(fx))
continue;
364 for(
size_t i=0;i<max;i++){
366 if(std::isnan(fx))
continue;
370 return std::make_pair(i, fx-z);
377 template<
typename t,
typename T>
378 std::pair<size_t,double>
arg_max(
const T& s,
const std::function<
double(
const t&)>& f) {
391 return std::make_pair(max_idx, mx);
395 std::pair<size_t,double>
arg_max_int(
unsigned int max,
const std::function<
double(
const int)>& f) {
398 for(
size_t i=0;i<max;i++){
400 if(std::isnan(fx))
continue;
406 return std::make_pair(max_idx, mx);
409 template<
typename t,
typename T>
410 std::pair<t*,double>
max_of(
const T& s,
const std::function<
double(
const t&)>& f) {
418 out =
const_cast<t*
>(&x);
421 return std::make_pair(out, mx);
424 template<
typename t,
typename T>
425 std::pair<t*,double>
sample(
const T& s,
double(*f)(
const t&)) {
426 std::function sf = f;
427 return sample<t,T>(s,sf);
430 template<
typename t,
typename T>
431 double lp_sample_eq(
const t& x,
const T& s, std::function<
double(
const t&)>& f = [](
const t& v){
return 1.0;}) {
436 for(
const auto& y : s) {
443 return log(px)-log(z);
446 template<
typename t,
typename T>
448 std::function sf = f;
449 return lp_sample_eq<t,T>(x,s,sf);
452 template<
typename t,
typename T>
453 double lp_sample_one(
const t& x,
const T& s, std::function<
double(
const t&)>& f = [](
const t& v){
return 1.0;}) {
458 return log(f(x))-log(z);
460 template<
typename t,
typename T>
462 std::function sf = f;
463 return lp_sample_one<t,T>(x,s,sf);
std::vector< bool > random_nonempty_subset(const size_t n, const double p)
Returns a random nonempty subset of n elements, as a vector<bool> of length n with trues for elements...
Definition: Random.h:220
T myrandom(T max)
Definition: Random.h:176
double lp_sample_one(const t &x, const T &s, std::function< double(const t &)> &f=[](const t &v){return 1.0;})
Definition: Random.h:453
T random_t(T nu)
Definition: Random.h:67
std::pair< size_t, double > arg_max(const T &s, const std::function< double(const t &)> &f)
Definition: Random.h:378
T t_lpdf(T x, T nu)
Definition: Random.h:73
std::pair< t *, double > sample(const T &s, double z, const std::function< double(const t &)> &f=[](const t &v){return 1.0;})
Definition: Random.h:258
double uniform()
Definition: Random.h:11
constexpr double pi
Definition: Numerics.h:22
std::pair< int, double > sample_int_lp(unsigned int max, const std::function< double(const int)> &f=[](const int v){return 1.0;})
Definition: Random.h:352
int rbinomial(const int n, const double p)
Definition: Random.h:34
double mylgamma(double v)
Definition: Numerics.h:216
double sample_lp_z(const T &s, const std::function< double(const t &)> &f)
Definition: Random.h:288
T laplace_lpdf(T x, T mu=0.0, T b=1.0)
Definition: Random.h:104
std::pair< int, double > sample_int(unsigned int max, const std::function< double(const int)> &f=[](const int v){return 1.0;})
Definition: Random.h:330
double geometric_lpdf(size_t k, double p)
Definition: Random.h:144
double random_gamma(double a, double b)
Definition: Random.h:148
bool flip(float p=0.5)
Definition: Random.h:25
std::vector< int > random_int_vector(int min, int max, size_t n)
Definition: Random.h:200
T random_exponential(T l=1.0)
Definition: Random.h:119
std::vector< t > random_multinomial(t a, size_t len)
Definition: Random.h:154
T random_laplace(T mu=0.0, T b=1.0)
Definition: Random.h:132
double random_normal(double mu=0, double sd=1.0)
Definition: Random.h:39
double random_cauchy()
Definition: Random.h:79
constexpr double infinity
Definition: Numerics.h:20
T logplusexp(const T a, const T b)
Definition: Numerics.h:131
double lp_sample_eq(const t &x, const T &s, std::function< double(const t &)> &f=[](const t &v){return 1.0;})
Definition: Random.h:431
std::pair< t *, double > max_of(const T &s, const std::function< double(const t &)> &f)
Definition: Random.h:410
T normal_cdf(T x, T mu, T sd)
Definition: Random.h:61
std::pair< t *, double > sample_lp(const T &s, double z, const std::function< double(const t &)> &f=[](const t &v){return 0.0;})
Definition: Random.h:304
T cauchy_lpdf(T x, T loc=0.0, T gamma=1.0)
Definition: Random.h:90
std::pair< size_t, double > arg_max_int(unsigned int max, const std::function< double(const int)> &f)
Definition: Random.h:395
T normal_lpdf(T x, T mu=0.0, T sd=1.0)
Definition: Random.h:45
double sample_z(const T &s, const std::function< double(const t &)> &f)
Definition: Random.h:238