# Hypothesis testing on tossing the coin n times

You toss the coin n times, and you have observed 60% of times, it is heads.

How large does n need to be in order to achieve 95% confidence that it is not a fair coin?

=======

Attempt: Basically use Binomial distribution, but I have no idea how to take account of the 60% number into my calculation.

$$\mu +/- Z_\alpha \frac{S}{\sqrt(n)}, \text{so I have, } 0.6 * \frac{\sqrt(n)}{0.6*\sqrt{0.6*0.4}} = Z_{0.05} =1.96$$, so $$n$$ = 1.6

You want $$n$$ large enough that a confidence interval of the form $$\hat p \pm 1.96\sqrt{\frac{\hat p(1-\hat p)}{n}},$$ where $$X$$ is the number of heads and $$\hat p = X/n,$$ does not include $$0.5.$$

Roughly speaking the standard error is $$\sqrt{.6(.4)/n}$$ and the margin of error is about $$2\sqrt{.6(.4)/n}\approx 0.98/\sqrt{n}.$$ And you want the margin error to be less than $$0.1,$$ so something around $$n = 96$$ should suffice. I show examples with $$n=100$$ below.

n = 100;  x = 60;  z = qnorm(c(.025,.975))
CI = .6 + z*sqrt(.24/100);  CI
[1] 0.5039818 0.6960182


A superior kind of CI due to Agresti and Coull uses the point estimate $$\tilde p = (x+2)/(n+4) = 62/104 = 0.5962$$ and the endpoints are at $$\tilde p \pm 1.96\sqrt{\tilde p(1-\tilde p)/104}.$$ This interval also just misses covering $$1/2.$$

p.est=(60+2)/(10+4)
p.est + qnorm(c(.025,.975)) * sqrt( p.est*(1-p.est)/104 )
[1] 0.5018524 0.6904553


Finally, a Jeffries 95% CI uses quantiles $$0.025$$ and $$0.975$$ of the distribution $$\mathsf{BETA}(60+0.5, 40+0.5),$$ so that the interval is $$(0.5023,0.6920).$$

qbeta(c(.025,.975), 60.5, 40.5)
[1] 0.5022567 0.6920477


Depending on the kind of interval you are using and whether you want the smallest number just large enough so that the CI doesn't contain $$1/2,$$ I'll leave the rest to you.

I worked out similar math for the problem of seed germination, to estimate the population germination rate from a sample of n seeds of which k germinate.

The formula I got for the CDF is:

where x is the germination probability. Substituting your problem variables in, letting n be the number of coin flips, k is 0.6*n, so the resulting formula would be:

which you can solve for n to give you the number of total flips that will exclude a fair coin with 95% credibility. Note that this is a CREDIBLE interval instead of a CONFIDENCE interval. I don't know how this will work with an exact 60% instead of integer values for n and k, but the general CDF formula can get you there for any arbitrary credible interval. Let x equal the "fair coin probability" of 0.5, k be the number of heads, and n the total number of flips.

I actually made a Javascript calculator for the seed germination problem that you could co-opt for this use. There is also a link to a PDF of the worked-out math if you want to take a look or check it. Hope this helps!

UPDATE: I brute forced the math with my calculator and I calculate that the 95% credible interval excludes a fair coin (50% probability) at between 95 and 100 coin flips. A plot of the credible interval evolution with n is attached.