Chapter 2 Exercises
Coin tossing
Solutions
1. Write your own BUGS code for calculating the probability of obtaining at least 15 heads in 20 tosses of a fair coin.
model {
Y ~ dbin(0.5, 20)
P <- step(Y - 14.5)
}
node mean sd MC error 2.5% median 97.5% start sample
P 0.021 0.1434 0.00158 0.0 0.0 0.0 1 10000
Y 10.02 2.244 0.02181 6.0 10.0 14.0 1 10000
![]() |
![]() |
2. Adapt your code to calculate the probability that no more than 6 patients in a group of 30 will fail to respond to a drug with a success probability of 0.7.
Modifications to the code are shown in red below. We first change the success probability from 0.5 (fair coin) to 0.7. Requiring no more than 6 failures is the same as requiring at least 24 successes. Alternatively we could calculate the number of failures, numfail , as 30 – Y, and then compute the required probability directly, as shown below ( P.alt ).
model {
Y ~ dbin( 0.7 , 30 )
P <- step(Y - 23.5 )
numfail <- 30 - Y
P.alt <- step(6.5 - numfail)
}
node mean sd MC error 2.5% median 97.5% start sample
P 0.1616 0.3681 0.003231 0.0 0.0 1.0 1 10000
P.alt 0.1616 0.3681 0.003231 0.0 0.0 1.0 1 10000
Y 21.01 2.517 0.0246 16.0 21.0 26.0 1 10000
numfail 8.992 2.517 0.0246 4.0 9.0 14.0 1 10000