-1

I wrote below code to use binomial distribution CDF (by using scipy.stats.binom.cdf) to estimate the probability of having NO MORE THAN k heads out of 100 tosses, where k = 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100. and then I tried to plot it using hist().

import scipy
import matplotlib.pyplot as plt
def binomcdf():
    p = 0.5
    n = 100
    x = 0
    for a in range(10):
        print(scipy.stats.binom.cdf(x, n, p))
        x += 10

plt.hist(binomcdf())
plt.show()

but I don't know why my plot returns empty, and I receive below error, can anyone help please!

TypeError: 'NoneType' object is not iterable

Hashmatullah Noorzai
  • 771
  • 3
  • 12
  • 34

4 Answers4

3

You printed your values, but did not return them. The default return value is None, which produced your error.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
1

I would save x and the corresponding cdf output for each associated x to a list, then return that list. Then use the data in the list to make plot.

William
  • 83
  • 8
1

You forgot to return calculated values...so you returning None Should work like this- see below- if I got your intent right :)

import scipy
import matplotlib.pyplot as plt
def binomcdf():
    p = 0.5
    n = 100
    x = 0
    result = []
    for a in range(10):
        result.append(scipy.stats.binom.cdf(x, n, p))
        x += 10
    return result

plt.hist(binomcdf())
plt.show()
DariaS
  • 51
  • 2
  • For posterity (or next time I read this answer): Modern scipy wants import scipy.stats not just import scipy. See https://stackoverflow.com/questions/22108017/cannot-use-scipy-stats#22118676 – Ron Jensen Oct 29 '21 at 18:35
0

To plot a binomial distribution for every k, you need to store each cdf in an list and return the same list and the same can be used to plot the histogram.

Smaurya
  • 167
  • 9