I'm attempting an assignment in which we're supposed to write a function to simulate a geometric distribution with $p=0.03$. While plotting a histograms for about $100000$ simulations of the function, I got these:

enter image description here

enter image description here

The code is as follows:

import numpy as np
import pandas as pd
from random import random

def simulateGeo(p=0.03):
    n = 1
    while(random() >= p):
        n += 1
    return n

simresults = np.zeros(100000)

for i in range(100000):
    simresults[i] = simulateGeo()


I'm just curious - what's the reason for these (seemingly regularly-spaced) gaps / spikes?

  • $\begingroup$ The geometric distribution is discrete but your histogram bins don't perfectly line up with the integer-spacing of the original variable. Histograms are suited for continuous variables rather than discrete ones. If you use them on discrete variables (rather than choosing a more suitable display), it can work, but you must choose your bin origin and bin size with some care rather than relying on defaults. $\endgroup$ – Glen_b -Reinstate Monica Oct 10 at 0:42

in python hist sometimes bins tend to "cluster" and then for one value you will have observations from many values


import numpy as np
import matplotlib.pyplot as plt
q = np.random.choice([0,1,2], 1000)
plt.hist(q, bins=2)
plt.hist(q, bins=3)

you will get the idea from plot yourself

  • $\begingroup$ Ah.. so it's an artifact due to the way Python plots histograms. Thanks for clarifying! $\endgroup$ – Shirish Kulhari Oct 9 at 17:57
  • $\begingroup$ @ShirishKulhari yup, remember to set number of bins properly (as divisor of number of unique values in your data), or just cut the tail of distribution $\endgroup$ – quester Oct 9 at 18:01
  • $\begingroup$ @ShirishKulhari it's more problem of algorithm counting bins, you can always count them yourself $\endgroup$ – quester Oct 9 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.