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 // REQUIRES: long_tests 11 12 // <random> 13 14 // template<class RealType = double> 15 // class extreme_value_distribution 16 17 // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm); 18 19 #include <random> 20 #include <cassert> 21 #include <vector> 22 #include <numeric> 23 24 template <class T> 25 inline 26 T 27 sqr(T x) 28 { 29 return x * x; 30 } 31 32 int main() 33 { 34 { 35 typedef std::extreme_value_distribution<> D; 36 typedef D::param_type P; 37 typedef std::mt19937 G; 38 G g; 39 D d(-0.5, 1); 40 P p(0.5, 2); 41 const int N = 1000000; 42 std::vector<D::result_type> u; 43 for (int i = 0; i < N; ++i) 44 { 45 D::result_type v = d(g, p); 46 u.push_back(v); 47 } 48 double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size(); 49 double var = 0; 50 double skew = 0; 51 double kurtosis = 0; 52 for (int i = 0; i < u.size(); ++i) 53 { 54 double d = (u[i] - mean); 55 double d2 = sqr(d); 56 var += d2; 57 skew += d * d2; 58 kurtosis += d2 * d2; 59 } 60 var /= u.size(); 61 double dev = std::sqrt(var); 62 skew /= u.size() * dev * var; 63 kurtosis /= u.size() * var * var; 64 kurtosis -= 3; 65 double x_mean = p.a() + p.b() * 0.577215665; 66 double x_var = sqr(p.b()) * 1.644934067; 67 double x_skew = 1.139547; 68 double x_kurtosis = 12./5; 69 assert(std::abs((mean - x_mean) / x_mean) < 0.01); 70 assert(std::abs((var - x_var) / x_var) < 0.01); 71 assert(std::abs((skew - x_skew) / x_skew) < 0.01); 72 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01); 73 } 74 { 75 typedef std::extreme_value_distribution<> D; 76 typedef D::param_type P; 77 typedef std::mt19937 G; 78 G g; 79 D d(-0.5, 1); 80 P p(1, 2); 81 const int N = 1000000; 82 std::vector<D::result_type> u; 83 for (int i = 0; i < N; ++i) 84 { 85 D::result_type v = d(g, p); 86 u.push_back(v); 87 } 88 double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size(); 89 double var = 0; 90 double skew = 0; 91 double kurtosis = 0; 92 for (int i = 0; i < u.size(); ++i) 93 { 94 double d = (u[i] - mean); 95 double d2 = sqr(d); 96 var += d2; 97 skew += d * d2; 98 kurtosis += d2 * d2; 99 } 100 var /= u.size(); 101 double dev = std::sqrt(var); 102 skew /= u.size() * dev * var; 103 kurtosis /= u.size() * var * var; 104 kurtosis -= 3; 105 double x_mean = p.a() + p.b() * 0.577215665; 106 double x_var = sqr(p.b()) * 1.644934067; 107 double x_skew = 1.139547; 108 double x_kurtosis = 12./5; 109 assert(std::abs((mean - x_mean) / x_mean) < 0.01); 110 assert(std::abs((var - x_var) / x_var) < 0.01); 111 assert(std::abs((skew - x_skew) / x_skew) < 0.01); 112 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01); 113 } 114 { 115 typedef std::extreme_value_distribution<> D; 116 typedef D::param_type P; 117 typedef std::mt19937 G; 118 G g; 119 D d(-0.5, 1); 120 P p(1.5, 3); 121 const int N = 1000000; 122 std::vector<D::result_type> u; 123 for (int i = 0; i < N; ++i) 124 { 125 D::result_type v = d(g, p); 126 u.push_back(v); 127 } 128 double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size(); 129 double var = 0; 130 double skew = 0; 131 double kurtosis = 0; 132 for (int i = 0; i < u.size(); ++i) 133 { 134 double d = (u[i] - mean); 135 double d2 = sqr(d); 136 var += d2; 137 skew += d * d2; 138 kurtosis += d2 * d2; 139 } 140 var /= u.size(); 141 double dev = std::sqrt(var); 142 skew /= u.size() * dev * var; 143 kurtosis /= u.size() * var * var; 144 kurtosis -= 3; 145 double x_mean = p.a() + p.b() * 0.577215665; 146 double x_var = sqr(p.b()) * 1.644934067; 147 double x_skew = 1.139547; 148 double x_kurtosis = 12./5; 149 assert(std::abs((mean - x_mean) / x_mean) < 0.01); 150 assert(std::abs((var - x_var) / x_var) < 0.01); 151 assert(std::abs((skew - x_skew) / x_skew) < 0.01); 152 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01); 153 } 154 { 155 typedef std::extreme_value_distribution<> D; 156 typedef D::param_type P; 157 typedef std::mt19937 G; 158 G g; 159 D d(-0.5, 1); 160 P p(3, 4); 161 const int N = 1000000; 162 std::vector<D::result_type> u; 163 for (int i = 0; i < N; ++i) 164 { 165 D::result_type v = d(g, p); 166 u.push_back(v); 167 } 168 double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size(); 169 double var = 0; 170 double skew = 0; 171 double kurtosis = 0; 172 for (int i = 0; i < u.size(); ++i) 173 { 174 double d = (u[i] - mean); 175 double d2 = sqr(d); 176 var += d2; 177 skew += d * d2; 178 kurtosis += d2 * d2; 179 } 180 var /= u.size(); 181 double dev = std::sqrt(var); 182 skew /= u.size() * dev * var; 183 kurtosis /= u.size() * var * var; 184 kurtosis -= 3; 185 double x_mean = p.a() + p.b() * 0.577215665; 186 double x_var = sqr(p.b()) * 1.644934067; 187 double x_skew = 1.139547; 188 double x_kurtosis = 12./5; 189 assert(std::abs((mean - x_mean) / x_mean) < 0.01); 190 assert(std::abs((var - x_var) / x_var) < 0.01); 191 assert(std::abs((skew - x_skew) / x_skew) < 0.01); 192 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01); 193 } 194 } 195