Search code examples
c++mathboostprobabilitydistribution

Sample random variates from a Normal Inverse Gaussian (NIG) distribution


How do I sample random variates from a Normal Inverse Gaussian (NIG) distribution?

I need to generate 100 numbers from the NIG distribution.

I use boost::math::inverse_gaussian but it does not have an operator() member function like std::normal_distribution

Edit: Hörmann, W., Leydold have been doing some research into this topic:

  1. Paper Hörmann, W., Leydold, J. Generating generalized inverse Gaussian random variates. Stat Comput 24, 547–557 (2014). https://doi.org/10.1007/s11222-013-9387-3[https://doi.org/10.1007/s11222-013-9387-3][3]
  2. Slides UNU.RAN
  3. An Implementation in C Universal Non-Uniform RANdom number generators

Solution

  • I don't find the inverse Gaussian distribution in Boost.Random.

    You can use the so-called inverse transform sampling technique. That is, you take the inverse cdf (i.e. the quantile function) of the inverse Gaussian distribution, and you apply it to a sample of uniformly random numbers in (0,1).

    Something like that:

    boost::math::inverse_gaussian my_ig(2, 3);
    double inverseCDFig(double p){
      return boost::math::quantile(my_ig, p);
    }
    

    Then you use std::uniform_real_distribution to generate uniformly random numbers between 0 and 1, say u[i] for i = 0; i < N, and you compute inverseCDFig(u[i]) for every i. In this way you get a random sample from the inverse Gaussian distribution.