1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <random> 11 12 // template<class RealType = double> 13 // class chi_squared_distribution 14 15 // template<class _URNG> result_type operator()(_URNG& g); 16 17 #include <random> 18 #include <cassert> 19 #include <vector> 20 #include <numeric> 21 22 template <class T> 23 inline 24 T 25 sqr(T x) 26 { 27 return x * x; 28 } 29 30 int main() 31 { 32 { 33 typedef std::chi_squared_distribution<> D; 34 typedef D::param_type P; 35 typedef std::minstd_rand G; 36 G g; 37 D d(0.5); 38 const int N = 1000000; 39 std::vector<D::result_type> u; 40 for (int i = 0; i < N; ++i) 41 { 42 D::result_type v = d(g); 43 assert(d.min() < v); 44 u.push_back(v); 45 } 46 double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size(); 47 double var = 0; 48 double skew = 0; 49 double kurtosis = 0; 50 for (int i = 0; i < u.size(); ++i) 51 { 52 double d = (u[i] - mean); 53 double d2 = sqr(d); 54 var += d2; 55 skew += d * d2; 56 kurtosis += d2 * d2; 57 } 58 var /= u.size(); 59 double dev = std::sqrt(var); 60 skew /= u.size() * dev * var; 61 kurtosis /= u.size() * var * var; 62 kurtosis -= 3; 63 double x_mean = d.n(); 64 double x_var = 2 * d.n(); 65 double x_skew = std::sqrt(8 / d.n()); 66 double x_kurtosis = 12 / d.n(); 67 assert(std::abs((mean - x_mean) / x_mean) < 0.01); 68 assert(std::abs((var - x_var) / x_var) < 0.01); 69 assert(std::abs((skew - x_skew) / x_skew) < 0.01); 70 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01); 71 } 72 { 73 typedef std::chi_squared_distribution<> D; 74 typedef D::param_type P; 75 typedef std::minstd_rand G; 76 G g; 77 D d(1); 78 const int N = 1000000; 79 std::vector<D::result_type> u; 80 for (int i = 0; i < N; ++i) 81 { 82 D::result_type v = d(g); 83 assert(d.min() < v); 84 u.push_back(v); 85 } 86 double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size(); 87 double var = 0; 88 double skew = 0; 89 double kurtosis = 0; 90 for (int i = 0; i < u.size(); ++i) 91 { 92 double d = (u[i] - mean); 93 double d2 = sqr(d); 94 var += d2; 95 skew += d * d2; 96 kurtosis += d2 * d2; 97 } 98 var /= u.size(); 99 double dev = std::sqrt(var); 100 skew /= u.size() * dev * var; 101 kurtosis /= u.size() * var * var; 102 kurtosis -= 3; 103 double x_mean = d.n(); 104 double x_var = 2 * d.n(); 105 double x_skew = std::sqrt(8 / d.n()); 106 double x_kurtosis = 12 / d.n(); 107 assert(std::abs((mean - x_mean) / x_mean) < 0.01); 108 assert(std::abs((var - x_var) / x_var) < 0.01); 109 assert(std::abs((skew - x_skew) / x_skew) < 0.01); 110 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01); 111 } 112 { 113 typedef std::chi_squared_distribution<> D; 114 typedef D::param_type P; 115 typedef std::mt19937 G; 116 G g; 117 D d(2); 118 const int N = 1000000; 119 std::vector<D::result_type> u; 120 for (int i = 0; i < N; ++i) 121 { 122 D::result_type v = d(g); 123 assert(d.min() < v); 124 u.push_back(v); 125 } 126 double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size(); 127 double var = 0; 128 double skew = 0; 129 double kurtosis = 0; 130 for (int i = 0; i < u.size(); ++i) 131 { 132 double d = (u[i] - mean); 133 double d2 = sqr(d); 134 var += d2; 135 skew += d * d2; 136 kurtosis += d2 * d2; 137 } 138 var /= u.size(); 139 double dev = std::sqrt(var); 140 skew /= u.size() * dev * var; 141 kurtosis /= u.size() * var * var; 142 kurtosis -= 3; 143 double x_mean = d.n(); 144 double x_var = 2 * d.n(); 145 double x_skew = std::sqrt(8 / d.n()); 146 double x_kurtosis = 12 / d.n(); 147 assert(std::abs((mean - x_mean) / x_mean) < 0.01); 148 assert(std::abs((var - x_var) / x_var) < 0.01); 149 assert(std::abs((skew - x_skew) / x_skew) < 0.01); 150 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01); 151 } 152 } 153