Quantile functions in boost (C++)
Asked Answered
B

2

13

Judging from the documentation boost seems to offer quantile functions (inverse cdf functions) for both normal and gamma distributions, but its not clear for me how can I actually use them. Could someone paste an example please?

Bartender answered 6/4, 2011 at 10:59 Comment(1)
This page contains an example for calculating quantiles of the normal distribution. It seems pretty straightforward. Does this work for you?Talanian
K
11

The quantile calculation is implemented as a free function. Here's an example:

#include <boost/math/distributions/normal.hpp>

boost::math::normal dist(0.0, 1.0);

// 95% of distribution is below q:
double q = quantile(dist, 0.95);

You can also get the complement (quantile from the right) using:

// 95% of distribution is above qc:
double qc = quantile(complement(dist, 0.05));

There are some similar worked examples here:

http://www.boost.org/doc/libs/1_46_1/libs/math/doc/sf_and_dist/html/math_toolkit/dist/stat_tut/weg.html

Edit: don't need namespaces on the free functions thanks to ADL

Kibbutznik answered 6/4, 2011 at 14:43 Comment(2)
Any idea why there are both normal and normal_distribution classes? This is what got me confused.Bartender
I think that normal is just a typedef of normal_distribution<double>Kibbutznik
P
3

There is a workable example on QuantCorner.

// Édouard Tallent @ TaGoMa.Tech
// September 2012

#include<boost/math/distributions.hpp>
#include<iostream>
using std::cout;
using std::endl;

double inverseNormal(double prob, double mean, double sd){
        boost::math::normal_distribution<>myNormal (mean, sd);
        return quantile(myNormal, prob);
}

int main (int, char*[])
{
        try
        {                
                double myProb = 0.1;    // the 10% quantile
                double myMean = 0.07;   // a 7% mean 
                double myVol = 0.14;    // a 14% volatility 

        cout << inverseNormal(myProb, myMean, myVol)  << endl;
        }

                catch(std::exception& e)
        {
                cout << "Error message: " << e.what() << endl;
        }
return 0;
}
Privateer answered 13/9, 2012 at 8:16 Comment(1)
If that is/was your site, you should probably disclose it in your post.Prerecord

© 2022 - 2024 — McMap. All rights reserved.