Gaps in histogram for geometric distribution

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:  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()

pd.Series(simresults).hist(bins=200)

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

• 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. Oct 10 '19 at 0:42

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

example:

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)
plt.show()

you will get the idea from plot yourself

• Ah.. so it's an artifact due to the way Python plots histograms. Thanks for clarifying! Oct 9 '19 at 17:57
• @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 Oct 9 '19 at 18:01
• @ShirishKulhari it's more problem of algorithm counting bins, you can always count them yourself Oct 9 '19 at 20:25