0

I have made a pretty simple program that is supposed to get the quantiles of different distributions. In my main file I create a class object for each distribution and then use a setup void inside the class to set the parameters for each distribution (for instance for the normal distribution I want to set the mean and variance). Then I want to the the quantile value by calling GetDraw with the confidence interval specified when I call the double.

I use the boost package for the distributions.

The problem is that I get different values for some distribution if I use the class instead of printing the value directly to the console (the first value is the value I get from the class, the second value is the value I get without the class):

Normal: 1.64485
Normal: 1.64485
Binomal: 1
Binomal: 422
Beta: 0.95
Beta: 1
exponential_distribution: 2.99573
exponential_distribution: 1.49787

Do you have any idea why I get this strange result?

This is my main.cpp:

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

#include "Random.h"

int main()
    {
        double q;

        //NORMAL
        Normaldist RandomNormal;
        RandomNormal.Setup(0, 1);
        q = RandomNormal.GetDraw(0.95);
        std::cout << "Normal: " << q << std::endl;

        boost::math::normal dist(0.0, 1.0); //https://stackoverflow.com/questions/5565228/quantile-functions-in-boost-c
        q = quantile(dist, 0.95);
        std::cout << "Normal: " << q << std::endl;


        //Binomial
        Binomialdist RandomBinomial;
        RandomBinomial.Setup(509, 0.8);
        q = RandomBinomial.GetDraw(0.95);
        std::cout << "Binomal: " << q << std::endl;

        q = quantile(boost::math::binomial(509, 0.8), 0.95);
        std::cout << "Binomal: " << q << std::endl;


        //BETA
        Betadist RandomBeta;
        RandomBeta.Setup(1, 0.1);
        q = RandomBeta.GetDraw(0.95);
        std::cout << "Beta: " << q << std::endl;

        q = boost::math::ibeta_inv(1, 0.1, 0.95);
        std::cout << "Beta: " << q << std::endl;


        //Exponential
        Exponentialdist RandomExp;
        RandomExp.Setup(2);
        q = RandomExp.GetDraw(0.95);
        std::cout << "exponential_distribution: " << q << std::endl;

        const auto lbda = 2.0;
        q = quantile(boost::math::exponential_distribution<> { lbda }, 0.95);
        std::cout << "exponential_distribution: " << q << std::endl;

    return(0);
    }

This is my Random.h:

#ifndef RANDOM_H_INCLUDED
#define RANDOM_H_INCLUDED

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

class DistributionClass {
            protected:
                boost::math::normal_distribution<> NormalDistribution; //https://stackoverflow.com/questions/5565228/quantile-functions-in-boost-c
                boost::math::binomial_distribution<> BinomialDistribution; //https://valelab4.ucsf.edu/svn/3rdpartypublic/boost/libs/math/doc/sf_and_dist/html/math_toolkit/dist/dist_ref/dists/binomial_dist.html
                boost::math::beta_distribution<> BetaDistribution;
                boost::math::exponential_distribution<> ExponentialDistribution;
        };

        class Normaldist: public DistributionClass {
            public:
                void Setup(double mean, double variance);
                double GetDraw(double r);
        };

        class Binomialdist: public DistributionClass {
            public:
                void Setup(double n, double p);
                double GetDraw(double r);
        };

        class Betadist: public DistributionClass {
            public:
                void Setup(double a, double b);
                double GetDraw(double r);
        };

        class Exponentialdist: public DistributionClass {
            public:
                void Setup(double lbda);
                double GetDraw(double r);
        };

This is my Random.cpp:

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

#include "Random.h"


    void Normaldist::Setup(double mean, double variance) {
        boost::math::normal_distribution<> NormalDistribution(mean, variance);
    }

    double Normaldist::GetDraw(double r) {
        return(quantile(NormalDistribution, r));
    }


    void Binomialdist::Setup(double n, double p) {
        boost::math::binomial_distribution<> BinomialDistribution(n, p);
    }

    double Binomialdist::GetDraw(double r) {
        return(quantile(BinomialDistribution, r)); //boost::math::binomial(509, 0.8)
    }


    void Betadist::Setup(double a, double b) { //boost.org/doc/libs/1_51_0/libs/math/doc/sf_and_dist/html/math_toolkit/dist/dist_ref/dists/beta_dist.html
        boost::math::beta_distribution<> BetaDistribution(a, b);
    }

    double Betadist::GetDraw(double r) {
        return(quantile(BetaDistribution, r));
    }


    void Exponentialdist::Setup(double lbda) {
        boost::math::exponential_distribution<> ExponentialDistribution(lbda);
    }

    double Exponentialdist::GetDraw(double r) { //https://stackoverflow.com/questions/38201740/generate-a-exponential-distribution-for-a-set-of-data-using-boost-c
        return(quantile(ExponentialDistribution, r));
    }

    #endif // RANDOM_H_INCLUDED
KGB91
  • 630
  • 2
  • 6
  • 24
  • Your setup functions aren't doing anything. You seem to be making this more convoluted than it needs to be. – Taekahn Nov 27 '19 at 15:41
  • I am trying to set the parameters so that I can access the distribution later on without having to specify them another time. – KGB91 Nov 27 '19 at 15:58

0 Answers0