11.5 p466
Let’s simulate this problem, with 10,000 trials
n = 10000
length = rep(0, n)
for (i in 1:n){
last = "Start"
k = 0
while (last != "Strong"){
p = runif(1)
if (last == "Start") {
if (p > 0.5) {
last = "Strong"
length[i] = k + 1
}
else {
last = "Average"
k = k + 1
}
} else if (last == "Average"){
if (p >= 0.75) {
last = "Strong"
length[i] = k + 1
} else if (p <= 0.25 ){
last = "Weak"
k = k + 1
} else {
last = "Average"
k = k + 1
}
}
else if (last == "Weak"){
if (p >= 2/3) {
last = "Average"
k = k + 1
}else {
last = "Weak"
k = k + 1
}
}
}
}
print(paste("The average time until the next Strong team:",mean(length)))
## [1] "The average time until the next Strong team: 4.4882"
hist(length, main = "Histogram of years until a strong team recurs")
plot(ecdf(length), main = "CDF of years until a strong team recurs")
Let’s replicate the code above, albeit with a different starting position, of a Weak team.
n = 10000
length = rep(0, n)
for (i in 1:n){
last = "Weak"
k = 0
while (last != "Strong"){
p = runif(1)
if (last == "Start") {
if (p > 0.5) {
last = "Strong"
length[i] = k + 1
}
else {
last = "Average"
k = k + 1
}
} else if (last == "Average"){
if (p >= 0.75) {
last = "Strong"
length[i] = k + 1
} else if (p <= 0.25 ){
last = "Weak"
k = k + 1
} else {
last = "Average"
k = k + 1
}
}
else if (last == "Weak"){
if (p >= 2/3) {
last = "Average"
k = k + 1
}else {
last = "Weak"
k = k + 1
}
}
}
}
print(paste("The average time until a Strong team occurs:",mean(length)))
## [1] "The average time until a Strong team occurs: 9.9899"
hist(length, main = "Histogram of years until a strong team occurs")
plot(ecdf(length), main = "CDF of years until a strong team occurs")