4

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;         
}
3
  • How to Calculate the Chi-Squared P-Value on Code Project. It came a couple of years after you asked the question, though.
    – jww
    Commented Jul 8, 2015 at 13:09
  • I'm not sure if the method on the link works actually. I come to copy-paste it and I'm getting quite weird values with it (eg. Dof=1, Cv=51). Commented Mar 15, 2019 at 14:51
  • * It gace me an error with the approximate gamma solution, it is OK with tgamma (doble checked in R language). Commented Mar 15, 2019 at 15:27

3 Answers 3

2

If you're looking for source code you can copy/paste, here are some links:

YMMV...

2

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.

1
  • @Charlie: my intention is to be independent from library. GSL code has many dependencies.
    – neversaint
    Commented Apr 28, 2009 at 14:47
2

I am trying to implement this function as to avoid dependency to 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 out shared_ptr).

To reduce the dependency, use bcp (boost copy) to copy out just the files needed for chi_squared.hpp. The bad thing is, you usually have to build bcp 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)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.