Ric*_*ing 6 c++ random chi-squared
根据这个问题的答案,我试图<random>通过使用来改变分布的参数.param().下面是一个玩具示例,我正在尝试这样做.
对于卡方和正态分布,我有一个生成两个值的函数,第二个参数已被更改.param().我多次运行这两个函数并打印出两者的平均结果.正如预期的那样,正常函数产生0和10的平均结果.出乎意料的是,卡方函数产生4和4的平均结果,而不是我对4和3的预期.为什么我对卡方分布的预期有所偏差?
#include <iostream>
#include <random>
#include <vector>
using namespace std;
vector<double> chisqtest(mt19937_64 &gen)
{
vector<double> res(2);
chi_squared_distribution<double> chisq_dist(4);
res[0] = chisq_dist(gen);
chisq_dist.param(std::chi_squared_distribution<double>::param_type (3));
res[1] = chisq_dist(gen);
return res;
}
vector<double> normtest(mt19937_64 &gen)
{
vector<double> res(2);
normal_distribution<double> norm_dist(0,1);
res[0] = norm_dist(gen);
norm_dist.param(std::normal_distribution<double>::param_type (10,1));
res[1] = norm_dist(gen);
return res;
}
int main() {
unsigned int n = 100000;
mt19937_64 gen(1);
vector<double> totals = {0,0}, res(2);
for(unsigned int i = 0; i < n; i++){
res = chisqtest(gen);
totals[0] += res[0];
totals[1] += res[1];
}
cout << totals[0]/n << " " << totals[1]/n << "\n";
vector<double> totals2 = {0,0}, res2;
for(unsigned int i = 0; i < n; i++){
res2 = normtest(gen);
totals2[0] += res2[0];
totals2[1] += res2[1];
}
cout << totals2[0]/n << " " << totals2[1]/n << "\n";
}
Run Code Online (Sandbox Code Playgroud)