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 weibull_distribution 14 15 // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm); 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::weibull_distribution<> D; 34 typedef D::param_type P; 35 typedef std::mt19937 G; 36 G g; 37 D d(0.5, 2); 38 P p(1, .5); 39 const int N = 1000000; 40 std::vector<D::result_type> u; 41 for (int i = 0; i < N; ++i) 42 { 43 D::result_type v = d(g, p); 44 assert(d.min() <= v); 45 u.push_back(v); 46 } 47 double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size(); 48 double var = 0; 49 double skew = 0; 50 double kurtosis = 0; 51 for (int i = 0; i < u.size(); ++i) 52 { 53 double d = (u[i] - mean); 54 double d2 = sqr(d); 55 var += d2; 56 skew += d * d2; 57 kurtosis += d2 * d2; 58 } 59 var /= u.size(); 60 double dev = std::sqrt(var); 61 skew /= u.size() * dev * var; 62 kurtosis /= u.size() * var * var; 63 kurtosis -= 3; 64 double x_mean = p.b() * std::tgamma(1 + 1/p.a()); 65 double x_var = sqr(p.b()) * std::tgamma(1 + 2/p.a()) - sqr(x_mean); 66 double x_skew = (sqr(p.b())*p.b() * std::tgamma(1 + 3/p.a()) - 67 3*x_mean*x_var - sqr(x_mean)*x_mean) / 68 (std::sqrt(x_var)*x_var); 69 double x_kurtosis = (sqr(sqr(p.b())) * std::tgamma(1 + 4/p.a()) - 70 4*x_skew*x_var*sqrt(x_var)*x_mean - 71 6*sqr(x_mean)*x_var - sqr(sqr(x_mean))) / sqr(x_var) - 3; 72 assert(std::abs((mean - x_mean) / x_mean) < 0.01); 73 assert(std::abs((var - x_var) / x_var) < 0.01); 74 assert(std::abs((skew - x_skew) / x_skew) < 0.01); 75 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01); 76 } 77 { 78 typedef std::weibull_distribution<> D; 79 typedef D::param_type P; 80 typedef std::mt19937 G; 81 G g; 82 D d(1, .5); 83 P p(2, 3); 84 const int N = 1000000; 85 std::vector<D::result_type> u; 86 for (int i = 0; i < N; ++i) 87 { 88 D::result_type v = d(g, p); 89 assert(d.min() <= v); 90 u.push_back(v); 91 } 92 double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size(); 93 double var = 0; 94 double skew = 0; 95 double kurtosis = 0; 96 for (int i = 0; i < u.size(); ++i) 97 { 98 double d = (u[i] - mean); 99 double d2 = sqr(d); 100 var += d2; 101 skew += d * d2; 102 kurtosis += d2 * d2; 103 } 104 var /= u.size(); 105 double dev = std::sqrt(var); 106 skew /= u.size() * dev * var; 107 kurtosis /= u.size() * var * var; 108 kurtosis -= 3; 109 double x_mean = p.b() * std::tgamma(1 + 1/p.a()); 110 double x_var = sqr(p.b()) * std::tgamma(1 + 2/p.a()) - sqr(x_mean); 111 double x_skew = (sqr(p.b())*p.b() * std::tgamma(1 + 3/p.a()) - 112 3*x_mean*x_var - sqr(x_mean)*x_mean) / 113 (std::sqrt(x_var)*x_var); 114 double x_kurtosis = (sqr(sqr(p.b())) * std::tgamma(1 + 4/p.a()) - 115 4*x_skew*x_var*sqrt(x_var)*x_mean - 116 6*sqr(x_mean)*x_var - sqr(sqr(x_mean))) / sqr(x_var) - 3; 117 assert(std::abs((mean - x_mean) / x_mean) < 0.01); 118 assert(std::abs((var - x_var) / x_var) < 0.01); 119 assert(std::abs((skew - x_skew) / x_skew) < 0.01); 120 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.03); 121 } 122 { 123 typedef std::weibull_distribution<> D; 124 typedef D::param_type P; 125 typedef std::mt19937 G; 126 G g; 127 D d(2, 3); 128 P p(.5, 2); 129 const int N = 1000000; 130 std::vector<D::result_type> u; 131 for (int i = 0; i < N; ++i) 132 { 133 D::result_type v = d(g, p); 134 assert(d.min() <= v); 135 u.push_back(v); 136 } 137 double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size(); 138 double var = 0; 139 double skew = 0; 140 double kurtosis = 0; 141 for (int i = 0; i < u.size(); ++i) 142 { 143 double d = (u[i] - mean); 144 double d2 = sqr(d); 145 var += d2; 146 skew += d * d2; 147 kurtosis += d2 * d2; 148 } 149 var /= u.size(); 150 double dev = std::sqrt(var); 151 skew /= u.size() * dev * var; 152 kurtosis /= u.size() * var * var; 153 kurtosis -= 3; 154 double x_mean = p.b() * std::tgamma(1 + 1/p.a()); 155 double x_var = sqr(p.b()) * std::tgamma(1 + 2/p.a()) - sqr(x_mean); 156 double x_skew = (sqr(p.b())*p.b() * std::tgamma(1 + 3/p.a()) - 157 3*x_mean*x_var - sqr(x_mean)*x_mean) / 158 (std::sqrt(x_var)*x_var); 159 double x_kurtosis = (sqr(sqr(p.b())) * std::tgamma(1 + 4/p.a()) - 160 4*x_skew*x_var*sqrt(x_var)*x_mean - 161 6*sqr(x_mean)*x_var - sqr(sqr(x_mean))) / sqr(x_var) - 3; 162 assert(std::abs((mean - x_mean) / x_mean) < 0.01); 163 assert(std::abs((var - x_var) / x_var) < 0.01); 164 assert(std::abs((skew - x_skew) / x_skew) < 0.01); 165 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.03); 166 } 167 } 168