【问题标题】:Chi-Squared Probability Function in C++C++ 中的卡方概率函数
【发布时间】:2010-10-22 05:04:25
【问题描述】:

我的以下代码使用卡方的“分位数”和 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;         
}

【问题讨论】:

  • How to Calculate the Chi-Squared P-Value 在代码项目上。不过,它是在你问这个问题几年后发生的。
  • 我不确定链接上的方法是否真的有效。我来复制粘贴它,我得到了非常奇怪的值(例如,Dof=1,Cv=51)。
  • * 它给我一个近似伽马解决方案的错误,它可以使用 tgamma (用 R 语言检查)。

标签: c++ statistics chi-squared


【解决方案1】:

如果您正在寻找可以复制/粘贴的源代码,这里有一些链接:

YMMV...

【讨论】:

    【解决方案2】:

    看看Gnu Scientific library。或查看Numerical RecipesApache Commons Math 中还有一个 Java 版本,应该很容易翻译。

    【讨论】:

    • @Charlie:我的目的是独立于图书馆。 GSL 代码有很多依赖关系。
    【解决方案3】:

    我正在尝试实现此功能以避免对 Boost 的依赖。

    另一种选择是减少增强依赖,但不是避免它们。如果您减少依赖关系,您可能可以使用包含 200 或 300 个源文件的 Boost 文件夹,而不是整个 1+ GB 的材料。 (是的,200 或 300 可以是准确的 - 这是我复制出 shared_ptr 时的最终结果。

    要减少依赖性,请使用bcp (boost copy) 仅复制chi_squared.hpp 所需的文件。不好的是,您通常必须从源代码构建 bcp,因为它不分布在 ZIP 或 TARBALL 中。

    要查找有关构建 bcp 的说明,请参阅 How to checkout latest stable Boost (not development or bleeding edge)?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 2013-11-18
      • 2012-11-21
      相关资源
      最近更新 更多