I need to implement a class in Python, that represents a Univariate (for now) Normal Distribution. What I have in mind is as follows
class Norm():
def __init__(self, mu=0, sigma_sq=1):
self.mu = mu
self.sigma_sq = sigma_sq
# some initialization if necessary
def sample(self):
# generate a sample, where the probability of the value
# of the sample being generated is distributed according
# a normal distribution with a particular mean and variance
pass
N = Norm()
N.sample()
The generated samples should be distributed according to the following probability density function
I know that scipy.stats
and Numpy
provide functions to do this, but I need to understand how these functions are implemented. Any help would be appreciated, thanks :)
I ended up using the advice by @sascha. I looked at both this wikipedia article and the Numpy source and found this randomkit.c file that implemented the functions
rk_gauss
(which implements the Box Muller Transform),rk_double
andrk_random
(which implements the Mersenne Twister Random Number Generator that simulates a Uniformly Distributed Random Variable, required by the Box Muller Transform).I then adapted the Mersenne Twister Generator from here and implemented the Box Muller Transform to simulate a gaussian (more information about Random Twister Generator here).
Here is the code I ended up writing:
This works perfectly, and generates a pretty good Gaussian