如何将卡方分布与 C++ 结合使用增强库?

发布于 2024-08-17 23:11:14 字数 159 浏览 5 评论 0原文

我已经检查了 Boost 网站上的示例,但它们不是我要找的。

简单来说,我想看看骰子上的某个数字是否受欢迎,使用 600 次掷骰,因此每个数字(1 到 6)的平均出现次数应该是 100。

并且我想使用卡方分布来检查是否骰子是公平的。

求助!请问我该怎么做?

I've checked the examples in the Boost website, but they are not what I'm looking for.

To put it simple, I want to see if a number on a die is favored, using 600 rolls, so the average appearances of every number (1 through 6) should be 100.

And I want to use the chi square distribution to check if the die is fair.

Help!, how would I do this please ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-08-24 23:11:14

假设 e[i] 和 o[i] 是数组,保存 6 种可能性中每种可能性的预期和观察到的掷骰数。在你的例子中,e[i] 是每个 bin 的 100,o[i] 是 i 在你的 600 次试验中滚动的次数。

然后,您可以通过对 (e[i]-o[i])2/e[i] 求和来计算卡方统计量
6 个垃圾箱。假设您的 o[i] 数组的结果为 105、95、102、98、98 和 102
进行 600 次试验后才算。

chi2 = 52/100 + 52/100 + 22/100 + 2 2/100 + 22/100 + 22/100 = .660

您有五个自由度(箱数减 1)。所以你要
有一个声明,例如

boost::math::chi_squared mydist(5);

创建表示您的卡方分布的 Boost 对象。

此时,您将使用 Boost 库中的 cdf 访问器函数(累积分布函数)来查找与具有五个自由度的 0.660 卡方得分相对应的 p 值。

p = boost::math::cdf(mydist,.660);

您应该得到接近 0.015 的值,如果假设原假设(骰子是公平的),则观察卡方分数至少为 0.660 的极端概率为 (1 - .015) = 98.5% ) 成立。因此,对于这组数据,不能以任何合理的置信水平拒绝原假设。 (免责声明:未经测试的代码!但是如果我正确理解了 Boost 文档,那么它应该是这样工作的。)

Suppose e[i] and o[i] are arrays holding the expected and observed count of rolls for each of the 6 possibilities. In your case, e[i] is 100 for each bin, and o[i] is the number of times i was rolled in your 600 trials.

You then calculate the chi-squared statistic by summing (e[i]-o[i])2/e[i] over
the 6 bins. Lets say your o[i] array came out with 105, 95, 102, 98, 98, and 102
counts after doing your 600 trials.

chi2 = 52/100 + 52/100 + 22/100 + 22/100 + 22/100 + 22/100 = .660

You have five degrees of freedom (number of bins minus 1). So you're going to
have a declaration like

boost::math::chi_squared mydist(5);

to create the Boost object representing your chi-square distribution.

At this point you would use the cdf accessor function (cumulative distribution function) from the Boost library to look up the p-value corresponding to a chi-squared score of .660 with five degrees of freedom.

p = boost::math::cdf(mydist,.660);

You should get something close to 0.015, which would be interpreted as a (1 - .015) = 98.5% probability of observing a chi-squared score at least as extreme as 0.660, if one assumes the null hypothesis (that the die is fair) holds. So for this set of data, the null hypothesis cannot be rejected with any reasonable confidence level. (Disclaimer: untested code! But if I understand the Boost documentation correctly, this is how it should work.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文