I’m writing a small function to generate values from the Normal distribution using Box-Muller method, but I’m getting negative values.
Here is my source code
import random
def generate_normal(mu, sigma):
u = random.random()
v = random.random()
z1 = sqrt(-2 * log(u)) * sin(2 * pi * v)
z2 = sqrt(-2 * log(u)) * cos(2 * pi * v)
x1 = mu + z1 * sigma
x2 = mu + z2 * sigma
return x2
Waht am I missing? I’m getting negative values in both x1 and x2. For example:
mu: 400
sigma: 150
u: 7.27333176449e-05
v: 0.642384573173
z1: -3.40497345242
x1: -110.746017863
and:
x2: -9.79324023117
The unit normal distribution is centred on zero, and two-sided with small tails out to plus and minus infinity. 99.7% of your values will lie within three standard deviations, the other 0.3% won’t.
In this example, with a mean of 400 and a standard deviation of 150, 99.7% of your values will fall within three standard deviations of the mean – the interval [-50,850], which includes negative numbers. So expect negative numbers right off the bat.
As for the other 0.3% of values, remember that’s 3/1000 of your numbers – not uncommon at all.
If you want a “bell-curvey” distribution with finite support, try the beta distribution.
Finally, unless this is an academic exercise, there’s no need to roll your own equivalent to
numpy.random.normal().