C++ 中的卡方概率函数
我的以下代码使用卡方的“分位数”和 Boost 的概率函数计算置信区间。
我正在尝试实现此功能以避免对 Boost 的依赖。 有什么资源可以在哪里找到这样的实现吗?
#include <boost/math/distributions/chi_squared.hpp>
#include <boost/cstdint.hpp>
using namespace std;
using boost::math::chi_squared;
using boost::math::quantile;
vector <double> ConfidenceInterval(double x) {
vector <double> ConfInts;
// x is an estimated value in which
// we want to derive the confidence interval.
chi_squared distl(2);
chi_squared distu((x+1)*2);
double alpha = 0.90;
double lower_limit = 0;
if (x != 0) {
chi_squared distl(x*2);
lower_limit = (quantile(distl,((1-alpha)/2)))/2;
}
double upper_limit = (quantile(distu,1-((1-alpha)/2)))/2;
ConfInts.push_back(lower_limit);
ConfInts.push_back(upper_limit);
return ConfInts;
}
The following code of mine computes the confidence interval using Chi-square's 'quantile' and probability function from Boost.
I am trying to implement this function as to avoid dependency to Boost. Is there any resource where can I find such implementation?
#include <boost/math/distributions/chi_squared.hpp>
#include <boost/cstdint.hpp>
using namespace std;
using boost::math::chi_squared;
using boost::math::quantile;
vector <double> ConfidenceInterval(double x) {
vector <double> ConfInts;
// x is an estimated value in which
// we want to derive the confidence interval.
chi_squared distl(2);
chi_squared distu((x+1)*2);
double alpha = 0.90;
double lower_limit = 0;
if (x != 0) {
chi_squared distl(x*2);
lower_limit = (quantile(distl,((1-alpha)/2)))/2;
}
double upper_limit = (quantile(distu,1-((1-alpha)/2)))/2;
ConfInts.push_back(lower_limit);
ConfInts.push_back(upper_limit);
return ConfInts;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在寻找可以复制/粘贴的源代码,这里有一些链接:
YMMV.. 。
If you're looking for source code you can copy/paste, here are some links:
YMMV...
查看 Gnu 科学库。 或者查看数值食谱。 Apache Commons Math 中还有一个 Java 版本,应该很容易翻译。
Have a look at the Gnu Scientific library. Or look in Numerical Recipes. There's also a Java version in Apache Commons Math, which should be straightforward to translate.
另一种选择是减少 Boost 依赖项,但不是避免它们。 如果减少依赖性,您可能可以使用包含 200 或 300 个源文件的
Boost
文件夹,而不是整个 1+ GB 的材料。 (是的,200 或 300 可能是准确的 - 这是我复制shared_ptr
时得到的结果)。要减少依赖性,请使用
bcp
(增强复制) 仅复制chi_squared.hpp
所需的文件。 坏事是,您通常必须从源代码构建 bcp,因为它不是以 ZIP 或 TARBALL 形式分发的。要查找有关构建
bcp
的说明,请参阅如何检查最新的稳定 Boost(不是开发或前沿)?Another option is to reduce Boost dependencies, but not avoid them. If you reduce the dependency, you might be able to use a
Boost
folder with say, 200 or 300 source files rather than the entire 1+ GB of material. (Yes, 200 or 300 can be accurate - its what I ended up with when copying outshared_ptr
).To reduce the dependency, use
bcp
(boost copy) to copy out just the files needed forchi_squared.hpp
. The bad thing is, you usually have to buildbcp
from sources because its not distributed in the ZIPs or TARBALLs.To find instructions on building
bcp
, see How to checkout latest stable Boost (not development or bleeding edge)?