How to calculate p-value for coin toss?

Viewed 663

I was trying to calculate the p-value for the following coin toss example:

n = 5
h = 4  # out of 5 toss 4 are head

# calculate pvalue using equal or more extreme cases
pval = P(4H1T) + P(4T1H) + P(5H) + P(5T)
     = 5/32    + 5/32    + 1/32  + 1/32
     = 12/32
     = 0.375

But when I tried standard method:

from scipy import stats

phat = h / n
p0 = 0.5  # for unbiased coin
q0 = 1 - p0
z = (phat - p0) / sqrt(p0q0/n)      

pval = 1 - stats.norm.cdf(z)

I got:

0.08985624743949994.

Question 1

How to get the similar result using python scipy stats (answer = 0.375) ?

References

In this statquest video the author explains how to get pvalue using equal or more extreme values and we get 0.375

But here,

using the formula given we get another answer.

Question 2

Which method is good so that we can compare pvalue with alpha?

2 Answers

The z value is the standardized value of a normal distribution and it is NOT a probability. Also, the probability for an exact value in a continuous distribution is a bit more tricky. This scenario sounds optimal for binomial distribution.

from scipy.stats import binom
binom.pmf(4,5,0.5)

Which outputs:

0.15625
Related