Why does my Monte Carlo simulation not give a normal distribution?

128 Views Asked by At

Why does my python Monte Carlo simulation not produce a normal distribution?

import random
import matplotlib.pyplot as pyplot
import numpy

P = 0.1
TR = 1_000

l = []

for _ in range(TR):
    tosses = 0
    success = False
    while success == False:
        success = bool(numpy.random.binomial(1, P, 1)[0])
        tosses += 1
    l.append(tosses)

pyplot.hist(l, bins=100)
pyplot.show()

Matplotlib graph

1

There are 1 best solutions below

11
Cem Koçak On

Bernoulli trials are not normally distributed in general. I suggest you take a look at theese two links: https://byjus.com/maths/bernoulli-trials-binomial-distribution/ https://www.probabilitycourse.com/chapter3/3_1_5_special_discrete_distr.php

I assume your task is related to obtaining a normal distribution via summing the outcomes of multiple bernolli trials I think this should work for you:

P = 0.1
TR = 1000

l = []

for _ in range(TR):
    tosses = np.random.normal(loc=1/P, scale=1/P, size=1)[0]
    l.append(tosses)

plt.hist(l, bins=100)
plt.show()

enter image description here