Open Side Menu Go to the Top
Register
Coin flips question Coin flips question

10-01-2018 , 07:00 PM
Friend asked me this. Hope it makes sense.

If 100 people flip a coin once a day for a year, what percentage can be expected to have a wider spread than 52.38/47.62 heads/tails ratio?
Coin flips question Quote
10-01-2018 , 08:19 PM
Quote:
Originally Posted by wait
Friend asked me this. Hope it makes sense.

If 100 people flip a coin once a day for a year, what percentage can be expected to have a wider spread than 52.38/47.62 heads/tails ratio?
I did a quick simulation of your question (written in R and the code is below in case any users wanted to play around with parameters). I had a person flip a coin 365 times, calculated the absolute difference of heads and tails, and then counted how many reached the threshold you asked for (which came out to a difference 17.374. So a success is any trial where the person flipped at least 17.37 more of one than the other). I repeated this 20,000 times.

The probability of one person getting a difference of at least 52.38% on one outcome or the other was about 34.06%. Since coin flips are all independent, that would mean that on average 34 out of 100 people would hit the threshold.

Here's a histogram of the difference in all 20,000 sims (red line is >= 52.38%):


Code:
#Number of days a coin is flipped
days.sample = 365

#Calculating what the difference in heads from tails would need to be to hit the threshold
ratio.spread = 52.38 - 47.62
ratio.target = days.sample * ratio.spread/100

#Simulation trials
n.trials = 20000

#initialize results vector for ratio of heads to tails
ratios = c()

for(trial in 1:n.trials){
  #In every trial, a person flips a coin days.sample days in a row. The loop stores the number of heads and tails in the sample and then repeats

  heads=c()
  tails=c()

  for(day in 1:days.sample){
    
    #flip coin
    coin = sample(0:1,1)
    
    if(coin == 1){
      heads[day] = 1
      tails[day] = 0
      
    } else{
      heads[day] = 0
      tails[day] = 1
    }
    
  }
  ratios[trial] = abs(sum(heads) - sum(tails))
}

#Number of successes in sample
successes = sum(ratios >= ratio.target) 

#Likelihood of a person reaching the difference threshhold of heads from tails
successes / n.trials

ggplot(data.frame(ratios), aes(x=ratios)) + geom_histogram() + geom_vline(xintercept = ratio.target, col="red")

Last edited by mkind0516; 10-01-2018 at 08:25 PM.
Coin flips question Quote
10-01-2018 , 08:27 PM
[Sum of C(365, k) from k=192 to k=365] / 2^365

≈ 17.3%

Or twice that if it can also be .5238 tails to heads.
Coin flips question Quote
10-01-2018 , 08:29 PM
Thanks
Coin flips question Quote
10-01-2018 , 08:32 PM
Quote:
Originally Posted by heehaww
[Sum of C(365, k) from k=192 to k=365] / 2^365

≈ 17.3%

Or twice that if it can also be .5238 tails to heads.
Just to clarify, I assumed it didn't matter if it was .5238 heads or tails. If it had to be heads> tails, then you'd cut my simulation result in half, which would be 17.03%.

So at the very least since heehaww solved it formally I now know that I didn't screw up my simulation!
Coin flips question Quote
10-03-2018 , 12:04 PM
Quote:
Originally Posted by mkind0516
I did a quick simulation of your question (written in R ...)
Your simulation is highly inefficient. Just vectorize. After defining the initial parameters, without any loop you can just do this:

Code:
sims<-matrix(rbinom(n.trials*days.sample,1,0.5),ncol=days.sample)
ratios<-abs(2*rowSums(sims)-ncol(sims))
Still better is just to simulate all the days.sample together for each simulation:

Code:
sims<-rbinom(n.trials,days.sample,0.5)
ratios<-abs(2*sims-days.sample)
Coin flips question Quote
11-18-2018 , 04:41 PM
Quote:
Originally Posted by nickthegeek
Your simulation is highly inefficient. Just vectorize.
thanks for sharing this
many do want to lower the time to run some code (as long as they can also understand it)
some do not care how long it takes

on my machine (win10 64bit, i7 quad-core. R version 3.4.4 64-bit)
the original R code using the for(), where many can easily follow and understand the code
> successes / n.trials
[1] 0.3455
> end.time <- Sys.time()
> print(end.time - start.time)
Time difference of 1.063789 mins

1st change
> successes / n.trials
[1] 0.34175
> end.time <- Sys.time()
> print(end.time - start.time)
Time difference of 0.880959 secs

2nd change
> successes / n.trials
[1] 0.348
> end.time <- Sys.time()
> print(end.time - start.time)
Time difference of 0.224988 secs
*****
anyone pick up on the reason for the OP?
the 52.38
looks to be the sportsbetting break even percentage for -110 wagers

I like seeing the calculated version also
just as a check
using pari/gp calculator
here online
pari/gp calculator
sum(k=192,365,binomial(365,k)/2^365.)

gp > x=sum(k=192,365,binomial(365,k)/2^365.);
gp > x
%2 = 0.17306096977676108864963708535391833549
(just what it prints)

gp also can give an exact result (to many is meaningless)
Code:
%4 = 203220528468429389799192225657901180620928791059635491607434982152500715968661876726960266553086443136419117/1174271291386916613944740298394668513687841274454159935353645485766104512557304221731849499192384351515967488

Last edited by kraps2312; 11-18-2018 at 04:54 PM. Reason: fix spelling errors
Coin flips question Quote

      
m