0

I am trying to understand SciPy Binomial, I am making no headway with the scipy manual.

I can simulate a coin flip with NumPy random.

import numpy as np

print('Simulating the result of a single fair coin flip ')
n = 1
p = 0.5
result = np.random.binomial(n,p)
if result == 1:
    print('1')
    print('Heads')
else:
    print('0')
    print('tails')

I also came across this code for NumPy which simulates a random probability of getting 6 heads in a row out of 20000 flips

import numpy as np
simulation = sum(np.random.binomial(6, 0.5,20000)==6)/20000
print(simulation)

However I am not looking to run a random simulation, I would like to use SciPy binomial to show me the actual probability of binomial test, e.g. 0.5 for a single coin flip

When I run the SciPy code below

from scipy.stats import binom
n, p = 1,0.5

result = binom.stats(n,p)

print(result)

I get this result

(array(0.5), array(0.25))

Can anyone help explain how I use SciPy properly to get the result of 0.5 for a single coin flip, and any other syntax I require to calculate how to use SciPy to show binomial probability of 5 heads in 20000 coin flips.

I realize that there are other mathematical ways to do this, but it is SciPy I am trying to learn.

Progman
  • 16,827
  • 6
  • 33
  • 48
Christopher
  • 427
  • 1
  • 8
  • 18

1 Answers1

2

What you need is the probability mass function, pmf. As per wiki:

In probability and statistics, a probability mass function (PMF) is a function that gives the probability that a discrete random variable is exactly equal to some value.

So in your example, to get the probability of getting 0, when the outcome can be 0 or 1, with a probability of 0.5:

binom.pmf(0,1,0.5)
0.5
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • Thank you. I am just wondering how do I calculate the probability of 5 heads in 20,000 coin flips would it be binom(5,20000,0.5) ? Is binom.pmf(0,1,0.5) the same as binom.pmf(1,1,0.5) ? – Christopher Apr 04 '21 at 00:38
  • `from scipy.stats import binom import decimal a = binom.pmf(0,1,0.5) b = binom.pmf(1,1,0.5) c = binom.pmf(5,20000,0.5) print(a) print(b) print(c)` only returns 1 decimal place in Jupyter. – Christopher Apr 04 '21 at 00:48
  • 5 heads out of 20000 is `binom.pmf(5,20000,0.5)` your probability is really small. use the log, `binom.logpmf` . Hope I've answered your questions. – StupidWolf Apr 04 '21 at 08:24
  • Thank you. I am not sure about binom.logpmf , I am getting -13818.214165216503 when I put 5, and I am getting -13853.040123646366 when I put 1. I thought at least 1 head in 20000 flips is pretty much guaranteed, I was expecting a positive number. – Christopher Apr 04 '21 at 18:02
  • if you exactly 5 heads in 20000 it is the pmf. If you need at least 5, it is the cdf. see something like https://stats.libretexts.org/Courses/Saint_Mary's_College_Notre_Dame/MATH_345__-_Probability_(Kuter)/3%3A_Discrete_Random_Variables/3.2%3A_Probability_Mass_Functions_(PMFs)_and_Cumulative_Distribution_Functions_(CDFs)_for_Discrete_Random_Variables – StupidWolf Apr 04 '21 at 19:04