Search code examples
c++distributionprobabilitygaussiannormal-distribution

Using the gaussian probability density function in C++


First, is this the correct C++ representation of the pdf gaussian function ?

float pdf_gaussian = ( 1 / ( s * sqrt(2*M_PI) ) ) * exp( -0.5 * pow( (x-m)/s, 2.0 ) );

Second, does it make sense of we do something like this ?

if(pdf_gaussian < uniform_random())
   do something
else
   do other thing

EDIT: An example of what exactly are you trying to achieve:

Say I have a data called Y1. Then a new data called Xi arrive. I want to see if I should associated Xi to Y1 or if I should keep Xi as a new data data that will be called Y2. This is based on the distance between the new data Xi and the existing data Y1. If Xi is "far" from Y1 then Xi will not be associated to Y1, otherwise if it is "not far", it will be associated to Y1. Now I want to model this "far" or "not far" using a gaussian probability based on the mean and stdeviation of distances between Y and the data that where already associated to Y in the past.


Solution

  • yes. boost::random has gaussian distribution.

    See, for example, this question: How to use boost normal distribution classes?

    As an alternative, there's a standard way of converting two uniformly distributed random numbers into two normally distributed numbers.

    See, e.g. this question: Generate random numbers following a normal distribution in C/C++

    In response to your last edit (note that the question is completely different as edited, hence my answer to an original one is irrelevant). I think you'd better off first formulating for yourself what exactly do you mean to mean by "modelling far or not far using a gaussian distribution". Then reformulate that understanding in math terms and only then start programming. As it stands, I think the problem is underspecified.