Some nice clips, proofs, simulations + a rare picture of me
##################################################
# Expectations of Geometric R.V's with Heads-Tails patterns #
# (C) Albert Dorador, November 2016 #
##################################################
We'll perform a Monte Carlo simulation and, by the Law of Large Numbers, we'll obtain a good approximation to the true expected value of the number of coin tosses needed to achieve the specified pattern. R code below.
hat.Exp.Geom = function(objective.seq, reps = 1e5){
toss.vector = integer(reps) #integer is 4 bytes, numeric is 8.
len.seq = length(objective.seq)
for (i in 1:reps){
seq_1 = rbinom(n=len.seq,size=1,prob=0.5)
tosses= len.seq #at least we'll need n tosses
if (all(seq_1 == objective.seq)){ #maybe we are lucky
success = T #we forbid entering the loop
}else{
success = F
seq_i = seq_1 #so that later in the while loop we call seq_i, instead of seq_1
while(!success){
xi = rbinom(n=1,size=1,prob=0.5) #1 Bernoulli experiment
tosses = tosses + 1
seq_i = seq_i[-1] #objective.seq has length n, clearly a sequence of n+1 cannot be correct
seq_i = append(seq_i, xi)
if (all(seq_i == objective.seq)){
success = T #only in case all characters coincide we give permission to exit the while loop
}
}
}
toss.vector[i] = tosses
}
hat.Expect = mean(toss.vector) #Law of Large Numbers
return(hat.Expect)
}
# Let's test
set.seed(123)
hat.Exp.Geom(c(1, 0)) # 1 is Heads, 0 is Tails
# 4.0, correct
hat.Exp.Geom(c(1, 0, 1))
# 10.0, correct
# Now let's try something that would take us hours to compute analytically (and with some chance of error):
set.seed(321)
hat.Exp.Geom(c(1, 0, 0, 1, 0, 1, 1, 1, 0))
# 516.0
Felipe VI, King of Spain, handing me the certificate of the "la Caixa" fellowship for postgraduate studies in North America, on May 28, 2019
Fun fact: I was once close to signing with a modest (yet historic) pro soccer team!
In 2019, during my last couple months before leaving Frankfurt to start my PhD in the US, I trained with Germany's 9-th division soccer team Germania 1894. I would have signed with them but the US was calling. Who knows if I closed the door on a promisng footballing career (just kidding)! But one thing is for sure: my German would have had to improve since I could only really understand the directions from the assistant coach, who, oddly enough, could speak Italian (and I, having worked at the ECB for nearly two years at the time, had a decent level of Italian too). I was very impressed with the quality of the team. Maybe next time...