2010-05-11 19:42:16 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-05-11 21:36:01 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
2010-05-11 19:42:16 +00:00
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// <random>
|
|
|
|
|
|
|
|
// class bernoulli_distribution
|
|
|
|
|
|
|
|
// template<class _URNG> result_type operator()(_URNG& g);
|
|
|
|
|
|
|
|
#include <random>
|
2010-05-15 21:36:23 +00:00
|
|
|
#include <numeric>
|
|
|
|
#include <vector>
|
2010-05-11 19:42:16 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
2010-05-15 21:36:23 +00:00
|
|
|
template <class T>
|
|
|
|
inline
|
|
|
|
T
|
|
|
|
sqr(T x)
|
|
|
|
{
|
|
|
|
return x * x;
|
|
|
|
}
|
|
|
|
|
2010-05-11 19:42:16 +00:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
{
|
2010-05-11 23:26:59 +00:00
|
|
|
typedef std::bernoulli_distribution D;
|
2010-05-15 21:36:23 +00:00
|
|
|
typedef std::minstd_rand G;
|
2010-05-11 19:42:16 +00:00
|
|
|
G g;
|
|
|
|
D d(.75);
|
2010-05-15 21:36:23 +00:00
|
|
|
const int N = 100000;
|
|
|
|
std::vector<D::result_type> u;
|
|
|
|
for (int i = 0; i < N; ++i)
|
|
|
|
u.push_back(d(g));
|
|
|
|
double mean = std::accumulate(u.begin(), u.end(),
|
|
|
|
double(0)) / u.size();
|
|
|
|
double var = 0;
|
2010-05-16 17:56:20 +00:00
|
|
|
double skew = 0;
|
|
|
|
double kurtosis = 0;
|
2010-05-15 21:36:23 +00:00
|
|
|
for (int i = 0; i < u.size(); ++i)
|
2010-05-16 17:56:20 +00:00
|
|
|
{
|
|
|
|
double d = (u[i] - mean);
|
|
|
|
double d2 = sqr(d);
|
|
|
|
var += d2;
|
|
|
|
skew += d * d2;
|
|
|
|
kurtosis += d2 * d2;
|
|
|
|
}
|
2010-05-15 21:36:23 +00:00
|
|
|
var /= u.size();
|
2010-05-16 17:56:20 +00:00
|
|
|
double dev = std::sqrt(var);
|
|
|
|
skew /= u.size() * dev * var;
|
|
|
|
kurtosis /= u.size() * var * var;
|
|
|
|
kurtosis -= 3;
|
2010-05-15 21:36:23 +00:00
|
|
|
double x_mean = d.p();
|
|
|
|
double x_var = d.p()*(1-d.p());
|
2010-05-16 17:56:20 +00:00
|
|
|
double x_skew = (1 - 2 * d.p())/std::sqrt(x_var);
|
|
|
|
double x_kurtosis = (6 * sqr(d.p()) - 6 * d.p() + 1)/x_var;
|
2010-05-15 21:36:23 +00:00
|
|
|
assert(std::abs(mean - x_mean) / x_mean < 0.01);
|
|
|
|
assert(std::abs(var - x_var) / x_var < 0.01);
|
2010-05-16 17:56:20 +00:00
|
|
|
assert(std::abs(skew - x_skew) / x_skew < 0.01);
|
|
|
|
assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.01);
|
2010-05-15 21:36:23 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
typedef std::bernoulli_distribution D;
|
|
|
|
typedef std::minstd_rand G;
|
|
|
|
G g;
|
|
|
|
D d(.25);
|
|
|
|
const int N = 100000;
|
|
|
|
std::vector<D::result_type> u;
|
|
|
|
for (int i = 0; i < N; ++i)
|
|
|
|
u.push_back(d(g));
|
|
|
|
double mean = std::accumulate(u.begin(), u.end(),
|
|
|
|
double(0)) / u.size();
|
|
|
|
double var = 0;
|
2010-05-16 17:56:20 +00:00
|
|
|
double skew = 0;
|
|
|
|
double kurtosis = 0;
|
2010-05-15 21:36:23 +00:00
|
|
|
for (int i = 0; i < u.size(); ++i)
|
2010-05-16 17:56:20 +00:00
|
|
|
{
|
|
|
|
double d = (u[i] - mean);
|
|
|
|
double d2 = sqr(d);
|
|
|
|
var += d2;
|
|
|
|
skew += d * d2;
|
|
|
|
kurtosis += d2 * d2;
|
|
|
|
}
|
2010-05-15 21:36:23 +00:00
|
|
|
var /= u.size();
|
2010-05-16 17:56:20 +00:00
|
|
|
double dev = std::sqrt(var);
|
|
|
|
skew /= u.size() * dev * var;
|
|
|
|
kurtosis /= u.size() * var * var;
|
|
|
|
kurtosis -= 3;
|
2010-05-15 21:36:23 +00:00
|
|
|
double x_mean = d.p();
|
|
|
|
double x_var = d.p()*(1-d.p());
|
2010-05-16 17:56:20 +00:00
|
|
|
double x_skew = (1 - 2 * d.p())/std::sqrt(x_var);
|
|
|
|
double x_kurtosis = (6 * sqr(d.p()) - 6 * d.p() + 1)/x_var;
|
2010-05-15 21:36:23 +00:00
|
|
|
assert(std::abs(mean - x_mean) / x_mean < 0.01);
|
|
|
|
assert(std::abs(var - x_var) / x_var < 0.01);
|
2010-05-16 17:56:20 +00:00
|
|
|
assert(std::abs(skew - x_skew) / x_skew < 0.01);
|
|
|
|
assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.01);
|
2010-05-11 19:42:16 +00:00
|
|
|
}
|
|
|
|
}
|