vendredi 17 juillet 2020

Calculate inverse CDF of distribution

I would like to use the distributions provided in <random> to compute an inverse CDF calculation. I see that normal distribution has the following method

template<class Generator>
result_type std::normal_distribution<RealType>::operator()(Generator& g);

where

g - an uniform random bit generator object

My idea was to make a custom generator that simply passed a single value (the probability that I would like to look up the inverse x value for).

class ConstantGenerator
{
public:
    ConstantGenerator(double value) : m_value(value) {}
    double min() const { return 0.0; }
    double max() const { return 1.0; }
    double operator()(){ return m_value; }
    using result_type = double;
private:
    double m_value = 0.0;
};

Example usage would be

int main()
{
    std::normal_distribution<double> dist{10.0, 1.0};
    ConstantGenerator gen{0.5};
    std::cout << dist(gen);
}

In this example, I created a normal distribution with Mean=10, StdDev=1 therefore I was hoping that my dist(gen) call with passing p=0.5 would return 10 (the mean) but instead it returns 1.46885.

How can I modify my ConstantGenerator to use the existing C++11 distributions to perform inverse CDF calculations? Or am I incorrect in my assumption that operator() performs an inverse CDF in the first place?

Aucun commentaire:

Enregistrer un commentaire