//===----------------------------------------------------------------------===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // template // class chi_squared_distribution // template result_type operator()(_URNG& g); #include #include #include #include template inline T sqr(T x) { return x * x; } int main() { { typedef std::chi_squared_distribution<> D; typedef D::param_type P; typedef std::minstd_rand G; G g; D d(0.5); const int N = 100000; std::vector u; for (int i = 0; i < N; ++i) u.push_back(d(g)); D::result_type mean = std::accumulate(u.begin(), u.end(), D::result_type(0)) / u.size(); D::result_type var = 0; for (int i = 0; i < u.size(); ++i) var += sqr(u[i] - mean); var /= u.size(); D::result_type x_mean = d.n(); D::result_type x_var = 2*d.n(); assert(std::abs(mean - x_mean) / x_mean < 0.02); assert(std::abs(var - x_var) / x_var < 0.02); } { typedef std::chi_squared_distribution<> D; typedef D::param_type P; typedef std::minstd_rand G; G g; D d(1); const int N = 100000; std::vector u; for (int i = 0; i < N; ++i) u.push_back(d(g)); D::result_type mean = std::accumulate(u.begin(), u.end(), D::result_type(0)) / u.size(); D::result_type var = 0; for (int i = 0; i < u.size(); ++i) var += sqr(u[i] - mean); var /= u.size(); D::result_type x_mean = d.n(); D::result_type x_var = 2*d.n(); assert(std::abs(mean - x_mean) / x_mean < 0.02); assert(std::abs(var - x_var) / x_var < 0.02); } { typedef std::chi_squared_distribution<> D; typedef D::param_type P; typedef std::minstd_rand G; G g; D d(2); const int N = 100000; std::vector u; for (int i = 0; i < N; ++i) u.push_back(d(g)); D::result_type mean = std::accumulate(u.begin(), u.end(), D::result_type(0)) / u.size(); D::result_type var = 0; for (int i = 0; i < u.size(); ++i) var += sqr(u[i] - mean); var /= u.size(); D::result_type x_mean = d.n(); D::result_type x_var = 2*d.n(); assert(std::abs(mean - x_mean) / x_mean < 0.02); assert(std::abs(var - x_var) / x_var < 0.02); } }