Open Side Menu Go to the Top
Register
Coin Toss Game Coin Toss Game

11-23-2017 , 02:48 AM
I was playing a very simple game with my son.

We each toss three coins and then count a running tally for how many heads we each throw. I wanted to show introduce him to random walks.

Im now wondering if anyone knows or can derive a formula for the expected number of lead changes and its standard deviation over n throws.
Coin Toss Game Quote
11-23-2017 , 07:00 PM
This sounds potentially interesting.

Are you asking about lead changes when single flips are alternated between two people or when batches of three flips are alternated between two people?

And can a proper lead change only occur after a complete round (both people) is completed?
Coin Toss Game Quote
11-24-2017 , 03:34 AM
Quote:
Originally Posted by whosnext
This sounds potentially interesting.

Are you asking about lead changes when single flips are alternated between two people or when batches of three flips are alternated between two people?

And can a proper lead change only occur after a complete round (both people) is completed?
I think we would best be starting with just a single coin flip each. My son throws first and I follow, that is one round. A lead change can only occur when a round is complete.

A sample of counts could look like;

Son; 0,1,2,2,2,3,3,4,4,
me; 0,0,1,2,3,3,3,4,5,

I could then look at son minus me

difference is; 0,1,1,0,-1,0,0,1,0,-1

I count four lead changes, the second round, the fifth, eighth and tenth.

If you look at three sequential rounds and the sum of the difference of our scores across those three rounds is 0 then you usually have a lead change (apart from 0,0,0...).

the sequence of differences is just a random walk.

C(k+1) = C(k)+ rand()

that rand function, produces, 0 50% and each of +1,-1 25%

Last edited by akkopower1; 11-24-2017 at 03:43 AM.
Coin Toss Game Quote
11-24-2017 , 06:45 AM
Interesting question. I qualitatively guess that the expected number of lead changes being ~ 1/4 * expected number of times of being at 0 for large N. To have a change lead you must pass by zero and, when in zero, you have a 1/4 probability of getting a change. Maybe the expected number of 0 is easier to calculate/already known in literature (didn't think about it yet).
Coin Toss Game Quote
11-24-2017 , 07:54 AM
Quote:
Originally Posted by nickthegeek
Interesting question. I qualitatively guess that the expected number of lead changes being ~ 1/4 * expected number of times of being at 0 for large N. To have a change lead you must pass by zero and, when in zero, you have a 1/4 probability of getting a change. Maybe the expected number of 0 is easier to calculate/already known in literature (didn't think about it yet).
Agreed.

C(k+1) = C(k)+ rand()
C(0)=0

The rand function, produces, 0 50% and each of +1,-1 25%.

should be a relatively simple combinations problem counting the number of ways C(n)=0.

You could get p zeros and q +1's and q-1's. P+2q=n........ thats every single way it could be done.
Coin Toss Game Quote
11-24-2017 , 11:50 AM
Say you have M rounds. We start by calculating how big is the probability of being at zero at round M. As you said, you have three results for each round: -1, 0 and 1 with 0.25, 0.5 and 0.25 probabilities respectively. In order to arrive at zero, one must obtain an equal number (call it k) of +1 and -1, while the remaining can be zeroes. Of course, k can range from 0 to M/2 (/ being the integer division). So you have M-2*k zeroes, k +1s and k -1s. You can arrange your zeroes whenever you want in your M rounds and next you arrange k +1s in the 2*k remaining spots. You finally sum for k ranging from zero to M/2. In formula:

P_0(M) = Sum_{K=0}^{M/2} C(M,M-2K) * (1/2)^(M-2K) * C(2K,K) * (1/4)^(2K)

With a little algebra, and by noting that the ratio P_0(M+1)/P_0(M) is equal to 1-1/(2M), we arrive to:

P_0(M) = Prod_{i=1}^{M} (1-1/(2M)) = Gamma(M+1/2)/sqrt(pi)/Gamma(M+1)

The last step has been obtained with the help of Wolfram Alpha. Gamma is the gamma function.

We now consider the expected number of total times we are at zero in M rounds. Call this quantity E_T[M]. We can exploit linearity of the expected value without having to deal with mutual dependencies:

E_T[M] = Sum_{i=1}^M E_0[i] = Sum_{i=0}^M P_0(i)

where E_0[i] is the expected number of times the i-th round is zero, which is obviously equal to P_0(i). Luckily for us, even the sum above can be simplified using the properties of the gamma function (and Wolfram Alpha of course), leading to:

E_T[M] = 2 * (M+1) * Gamma(M+3/2) / sqrt(pi) / Gamma(M+2) - 1

Now to lead changes. It must be noted that:

1 - the first non zero value always represents a lead change;
2 - for every zero outside an eventual initial sequence of zeroes and not in the last spot, the probability of having a lead change is 1/4.

In formula:

E_LC[M] = E_L0[M] + 1/4*(E_T(M-1)-E_L0[M])

where E_L0[M] is the expected number of leading zeroes and E_LC[M] is the expected number of lead changes in M rounds. Here is an implementation in the R language:

Code:
expectedzeroes<-function(M) {
	exp(log(2)+log(M+1)+lgamma(M+3/2)-0.5*log(pi)-lgamma(M+2))-1
}
expectedLeadingZeros<-function(M) {
	2*(1-(1/2)^(M+1))-1
}
expectedChanges<-function(M) {
	3/4*expectedLeadingZeros(M) + 1/4*expectedzeroes(M-1)
}
Also, some routine for a simulation:

Code:
simulationLeadChanges<-function(n,nsim=100000) {
	simula<-replicate(nsim,sample(-1:1,n,prob=c(1,2,1),replace=TRUE))
	simula2<-sign(apply(simula,2,cumsum))
	res<-apply(simula2,2,function(x) length(rle(x[x!=0])$values))
	c(mean(res),sd(res))
}
And a test (it will took awhile for every simulation to run):

Code:
n<-seq(10,1000,by=10)
calc<-expectedChanges(n)
fromsim<-matrix(0,nrow=length(n),ncol=2)
for (i in seq_len(nrow(fromsim))) {
	fromsim[i,]<-simulationLeadChanges(n[i],nsim=10000)
	cat("Done n:",n[i],"\n")
}

plot(n,calc,ty="l",lwd=2)
points(n,fromsim[,1],col="blue")
Coin Toss Game Quote
11-24-2017 , 05:17 PM
Great work nick.

I have not read the derivation but I did copy your functions into my R.

Your program seems to say that:

Expected number of Lead Changes with 1 Round = 0.375

Unless I am missing something or did something wrong in copying your functions, shouldn't that be 0.5 (presuming we are treating a leader after one round to be a lead change from the initial state).

Same thing happens with other small number of rounds.
Coin Toss Game Quote
11-24-2017 , 07:36 PM
Quote:
Originally Posted by whosnext
Great work nick.

I have not read the derivation but I did copy your functions into my R.

Your program seems to say that:

Expected number of Lead Changes with 1 Round = 0.375

Unless I am missing something or did something wrong in copying your functions, shouldn't that be 0.5 (presuming we are treating a leader after one round to be a lead change from the initial state).

Same thing happens with other small number of rounds.
Is that simply because rounds should be even?

what does it give for rounds 1-10?

the expected number of lead changes in 2 rounds, should be; both players roll the same in round 1 and then different in round 2 and they roll different in round 1 and same in round two.

Which I get 0.5

Last edited by akkopower1; 11-24-2017 at 07:47 PM.
Coin Toss Game Quote
11-24-2017 , 10:56 PM
For 2 rounds isn't it 0.75?

0.5 for round 1 and then 0.5 for round 2 (when round 1 is not a lead change), so it becomes 0.5+0.5*0.5 = 0.75

R function gives 0.6875.

To repeat, I am probably misunderstanding what is going on here or am not executing the R functions properly.
Coin Toss Game Quote
11-25-2017 , 01:20 AM
I was incorrect before.

Yep 0.75.

n=1, 0.5 expected lead changes
n=2, 0.75
n=3, 0.9375 (60/64)
Coin Toss Game Quote
11-25-2017 , 01:53 AM
For 3 rounds, I think it is pretty clear that the distribution of the number of lead changes is:

8 have 0
54 have 1
2 have 2

Which yields an EV of 58/64 = 0.90625

R function that I have gives EV(3) = 0.875
Coin Toss Game Quote
11-25-2017 , 01:53 AM
n=1, 0.5 expected lead changes
n=2, 0.75
n=3, 0.9375 (60/64)
n=4, 1.03125 264/256(?? not certain)

A sequence like;

h,t,t,h,h,t,t,h,h,t,t,h,h
t,h,h,t,t,h,h,t,t,h,h,t,t

would produce the largest number of lead changes.
when n=1 you can have 1 lead change
n=2, 1 lead change
n=3, 2
n=4, 2
n=5, 3

looks like the max number of possible lead changes would be ceiling(n/2)

when n=2 you can have one lead change two different ways.
when n=3, zero lead changes occurs when you tie, 8 ways, 2 lead changes occur 4 ways, and 1 occurs 52 ways.

when n=4, there are 256 combos,
16 of these produce zero lead changes

I count 12 ways of having two lead changes
so, 240 ways of having 1 lead change
Coin Toss Game Quote
11-25-2017 , 02:21 AM
Since this is easy to code up the brute force scenarios:

For 4 rounds, the distribution of the number of lead changes is:

16 have 0
220 have 1
20 have 2
0 have 3
0 have 4

Which yields an EV of 260/256 = 1.015625

R function that I have gives EV(4) = 1.0

Last edited by whosnext; 11-25-2017 at 02:29 AM.
Coin Toss Game Quote
11-25-2017 , 02:30 AM
Since this is easy to code up the brute force scenarios:

For 5 rounds, the distribution of the number of lead changes is:

32 have 0
860 have 1
130 have 2
2 have 3
0 have 4
0 have 5

Which yields an EV of 1126/1024 = 1.099609375

R function that I have gives EV(5) = 1.091796875 (which is 1118/1024)


Edit to add: I will stop here since I may be using an incorrect definition of what constitutes a "lead change".
Coin Toss Game Quote
11-25-2017 , 05:18 AM
Quote:
Originally Posted by whosnext
Since this is easy to code up the brute force scenarios:

For 5 rounds, the distribution of the number of lead changes is:

32 have 0
860 have 1
130 have 2
2 have 3
0 have 4
0 have 5

Which yields an EV of 1126/1024 = 1.099609375

R function that I have gives EV(5) = 1.091796875 (which is 1118/1024)


Edit to add: I will stop here since I may be using an incorrect definition of what constitutes a "lead change".
Great catch and thanks for the detailed tests. You are using the right definition. My idea of breaking between leading zeroes and the last round has a bias: what happens if the initial sequence of zeroes lasts for every round? Luckily, this is pretty easy to correct:

Code:
expectedChanges<-function(M) {
	3/4*expectedLeadingZeros(M) + 1/4*expectedzeroes(M-1)+(1/2)^(M+2)
}
Note the last term that takes into account the effect you noticed. For large M, it is pretty meaningless (that's why I didn't notice in my test), but it's important for small M.

Some results:

Code:
setNames(expectedChanges(1:8),1:8)
       1        2        3        4        5        6        7        8 
0.500000 0.750000 0.906250 1.015625 1.099609 1.168945 1.229248 1.283569
Coin Toss Game Quote
11-25-2017 , 06:08 AM
What happens for large M? 20,50,100+?

surely it cant converge.
Coin Toss Game Quote
11-25-2017 , 06:42 AM
Quote:
Originally Posted by akkopower1
What happens for large M? 20,50,100+?

surely it cant converge.
You have every ingredient to answer this question. You could download R from here, install it, run the code and see the results for large M. Or, you can dig into the formula I provided in post #6, see the gamma function properties here and derive the asymptotic behaviour by yourself.

Spoiler:
It doesn't converge; it grows like M^(1/2) for large M.
Coin Toss Game Quote
11-25-2017 , 09:00 AM
Starting with

E_LC[M] = E_L0[M] + 1/4*(E_T(M-1)-E_L0[M])

and using some of the gamma function properties, I get,

E_LC[M] =3/4*(1/2)^m+m*(2m)!/(2*4^m*(m!)^2)-1/4

Not too surprisingly,using Wolfram in an attempt to reduce (2m)!/(m!)^2, gave back the original gamma functions

ignoring a few terms

E_LC[M] =m*(2m)!/(4^m*(m!)^2), clearly diverges........ nothing much to see here
Coin Toss Game Quote
11-25-2017 , 09:24 AM
I initially told my son, no matter how big a lead you get I will always overtake you at some stage....... Given a lead of Q, in how many turns do you expect to lose your lead?


Given a lead of Q there is a;
(1/4)^(Q+1) chance you will lose that lead in Q+1 turns
C(Q+2,1)*(1/2)*(1/4)^(Q+1) chance you will lose that lead in Q+2 turns
C(Q+3,2)*(1/2)^2*(1/4)^(Q+1) chance you will lose that lead in Q+3 turns

these are the simple cases, as the lead player is only ever throwing heads.

Next one is getting tricky, as you need to start counting the times you lose the flip.

Last edited by akkopower1; 11-25-2017 at 09:47 AM.
Coin Toss Game Quote
11-27-2017 , 07:16 PM
So how many lead changes will there be if you play forever and heads is 51%? (Or x percent not 50.)
Coin Toss Game Quote
11-28-2017 , 03:06 AM
Quote:
Originally Posted by David Sklansky
So how many lead changes will there be if you play forever and heads is 51%? (Or x percent not 50.)
I'm way too lazy to redo the calculation in post #6 for this case. Considering that the game stays symmetric (P(-1) = P(1)), I still expect (but this is just a feeling, very likely I'm wrong) the number of lead changes to diverge (aside from the trivial case in which heads is 100% or 0%), possibly like M^(1-P(0)), where P(0) = p^2 + (1-p)^2, p being the probability of heads.

I expect a different asymptotic behaviour if just one player had a rigged coin (giving P(1) != P(-1)). In this case, in every path, sooner or later, a player will retain the lead forever.
Coin Toss Game Quote
11-29-2017 , 03:42 PM
Typing out a derivation and formulas is a massive PITA on here but I arrived at

EV [# of lead changes in m turns] =

SUM[.5^i + .25*SUM[(i choose 2j)*.25^2j*.5^(i-2j)]]

where the outer SUM is indexed on i and runs from i = 1 to m, and the inner SUM is indexed on j and runs from j = 1 to (i-1)/2

Last edited by TiltedDonkey; 11-29-2017 at 03:59 PM.
Coin Toss Game Quote
11-29-2017 , 04:34 PM
Quote:
Originally Posted by TiltedDonkey
Typing out a derivation and formulas is a massive PITA on here but I arrived at

EV [# of lead changes in m turns] =

SUM[.5^i + .5*SUM[(i-1 choose 2j)*.25^2j*.5^(i-1-2j)]]

where the outer SUM is indexed on i and runs from i = 1 to m, and the inner SUM is indexed on j and runs from j = 1 to (i-1)/2
Some errors, fixed.

Code, VBA
Code:
Public Function NumLeadChanges(numTurns As Integer) As Double

Dim i       As Integer
Dim j       As Integer
Dim result As Double
Dim temp As Double

result = 0

For i = 1 To numTurns
    result = result + 0.5 ^ i
    
    For j = 1 To Application.WorksheetFunction.Floor((i - 1) / 2, 1)
        result = result + (0.5 * Application.WorksheetFunction.Combin(i - 1, 2 * j) * (0.25 ^ (2 * j)) * (0.5 ^ (i - 1 - (2 * j))))
    Next
Next

NumLeadChanges = result

End Function
Results to 50 trials
Code:
NumTurns	Expected Lead Changes
1	0.500000000000000
2	0.750000000000000
3	0.906250000000000
4	1.015625000000000
5	1.095703125000000
6	1.155273437500000
7	1.199829101562500
8	1.233215332031250
9	1.258247375488280
10	1.277019500732420
11	1.291098117828360
12	1.301656961441040
13	1.309576064348220
14	1.315515384078020
15	1.319969872012730
16	1.323310737498100
17	1.325816386495720
18	1.327695623214820
19	1.329105050746880
20	1.330162121394100
21	1.330954924379060
22	1.331549526617660
23	1.331995478296590
24	1.332329942055780
25	1.332580789875170
26	1.332768925739710
27	1.332910027638120
28	1.333015854061920
29	1.333095223879770
30	1.333154751243160
31	1.333199396765710
32	1.333232880907610
33	1.333257994014040
34	1.333276828843870
35	1.333290954966230
36	1.333301549558010
37	1.333309495501840
38	1.333315454959710
39	1.333319924553120
40	1.333323276748170
41	1.333325790894460
42	1.333327676504180
43	1.333329090711470
44	1.333330151366940
45	1.333330946858540
46	1.333331543477240
47	1.333331990941260
48	1.333332326539280
49	1.333332578237790
50	1.333332767011680
Coin Toss Game Quote
11-29-2017 , 05:23 PM
I do not know what your formula is purported to show.

If this is a formula for the expected number of lead changes with a fair coin (50% heads) as discussed in this thread, it is clearly wrong.

Examples of demonstrably correct percents are given above which your formula does not agree with.

Edit to add:

I have tabulated the following results (there is nothing difficult about coding up a brute force computer program for N rounds in the 50% heads case and tallying the number of lead changes).

E(1) = 0.5
E(2) = 0.75
E(3) = 0.90625
E(4) = 1.015625
E(5) = 1.099609375
E(6) = 1.1689453125
E(7) = 1.229248046875
E(8) = 1.2835693359375
E(9) = 1.33364105224609375

Of course, nickthegeek's formula given above agrees with these results


Last edited by whosnext; 11-29-2017 at 06:59 PM. Reason: added edit at bottom, added another for good measure
Coin Toss Game Quote
11-29-2017 , 05:28 PM
Quote:
Originally Posted by nickthegeek
I'm way too lazy to redo the calculation in post #6 for this case. Considering that the game stays symmetric (P(-1) = P(1)), I still expect (but this is just a feeling, very likely I'm wrong) the number of lead changes to diverge (aside from the trivial case in which heads is 100% or 0%), possibly like M^(1-P(0)), where P(0) = p^2 + (1-p)^2, p being the probability of heads.

I expect a different asymptotic behaviour if just one player had a rigged coin (giving P(1) != P(-1)). In this case, in every path, sooner or later, a player will retain the lead forever.
It seems pretty clear to me to be divergent, as it is essentially the same as a standard random walk where the number expected number of returns to origin is known to diverge.

The same applies for convergence in a weighted random walk. If one player has an unfair coin the number of lead changes must converge.
Coin Toss Game Quote

      
m