Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Probability Density Function using the standard library?

Being able to use the std <random> to generate random numbers of different prob distributions is great... Now, Is there any way to compute the probability of a set of numbers given a distribution and its parameters using the standard library?

I am aware that I can code the Probability Density & Mass Functions for whatever distributions on my own (see example below for a single random variable), but I'd rather use the standard library if I could.

long double exponential_pdf(long double x, long double rate) {

    if ( x < 0.0 ) {
        return 0.0;
    }
    if ( rate < 0.0 ) {
        return NOT_A_NUMBER;
    }
    auto    pdf = rate * exp( - rate * x);
    return  pdf;
}
like image 245
dudu Avatar asked Oct 15 '13 17:10

dudu


People also ask

How do you find the probability density function?

How to Find the Mean of Probability Density Function? The expected value is also known as the mean. The mean of the probability density function is given by the formula μ=∫∞−∞xf(x)dx μ = ∫ − ∞ ∞ x f ( x ) d x .

How do you draw a probability density function in Python?

You first create a plot object ax . Here, you can specify the number of bins in the histogram, specify the color of the histogram and specify density plot option with kde and linewidth option with hist_kws . You can also set labels for x and y axis using the xlabel and ylabel arguments.

Is standard normal probability density function?

The probability density formula simplifies quite easily. A normal distribution with a mean of zero, and a standard deviation of 1, is known as a standard normal distribution. This is the normal distribution that is used for statistical tables.


1 Answers

Since C++11, the Standard Library contains a bunch of distributions that can be used for random number generation. Unfortunately, there is no functionality for computing probability densities, cumulative distributions or quantile functions in the Standard Library.

As for a rationale why this was not included in the Standard Library, see the working paper N1398 (emphasis mine)

Some libraries provide the probability density function of a given distribution as part of that distribution's interface. While this may be useful occasionally, this proposal does not provide for such a feature. One reason is separation of concerns: The distribution class templates might benefit from precomputing large tables of values depending on the distribution parameters, while the computation of the probability density function does not. Also, the function representation is often straightforward, so the user can easily code it himself.

I don't quite agree with this reasoning: while it is true that the density functions are easy enough to code, the same does not hold for the cumulative distribution or quantile functions.

As a workaround, you could go to the Boost Math Toolkit. It uses the same names as the Standard Library for the distributions, but allows you to compute pdf() and many more properties for these distributions. Boost libraries often find their way into the Standard, and if not, they are at least platform independent and widely available. Your example would be something like:

#include <iostream>
#include <boost/math/distributions/exponential.hpp>

using namespace boost::math;

int main() 
{
    auto const lambda = 1.0;
    auto d = exponential_distribution<>{lambda};
    std::cout << pdf(d, 0) << "\n"; // exp(0) = 1
    std::cout << pdf(d, 1) << "\n"; // exp(-1) = 0.367879
}

Live Example.

like image 102
TemplateRex Avatar answered Sep 17 '22 22:09

TemplateRex