Probability is at the heart of data science. Simulation is also commonly used in algorithms such as the bootstrap. After completing this exercise, you will have a slightly stronger intuition for probability and for writing your own simulation algorithms.

Most of the problems in this set have an exact analytical solution, which is not the case for all probability problems, but they are great for practice since we can check against the exact correct answer.

To get the most out of the exercises, it pays off to read the instructions carefully and think about what the solution should be before starting to write `R`

code. Often this helps you weed out irrelevant information that can otherwise make your algorithm unnecessarily complicated.

Answers are available here.

**Exercise 1**

In 100 coin tosses, what is the probability of having the same side come up 10 times in a row?

You might want to use some of the following functions to answer this question:`sample(), rbinom(), rle()`

.

**Exercise 2**

Six kids are standing in line. What is the probability that they are in alphabetical order by name? Assume no two children have the same exact name.

**Exercise 3**

Remember the kids from the last question? There are three boys and three girls. How likely is it that all the girls come first?

**Exercise 4**

In six coin tosses, what is the probability of having a different side come up with each throw, that is, that you never get two tails or two heads in a row?

**Exercise 5**

A random five-card poker hand is dealt from a standard deck. What is the chance of a flush (all cards are the same suit)?

**Exercise 6**

In a random thirteen-card hand from a standard deck, what is the probability that none of the cards is an ace and none is a heart (♥)?

**Exercise 7**

At four parties each attended by 13, 23, 33, and 53 people respectively, how likely is it that at least two individuals share a birthday at each party? Assume there are no leap days, that all years are 365 days, and that births are uniformly distributed over the year.

**Exercise 8**

A famous coin tossing game has the following rules: The player tosses a coin repeatedly until a tail appears or tosses it a maximum of 1000 times if no tail appears. The initial stake starts at 2 dollars and is doubled every time heads appears. The first time tails appears, the game ends and the player wins whatever is in the pot. Thus the player wins 2 dollars if tails appears on the first toss, 4 dollars if heads appears on the first toss and tails on the second, 8 dollars if heads appears on the first two tosses and tails on the third, and so on. Mathematically, the player wins 2^{k} dollars, where k equals the number of tosses until the first tail. What is the probability of profit if it costs 15 dollars to participate?

**Exercise 9**

Back to coin tossing. What is the probability the pattern heads-heads-tails appears before tails-heads-heads?

**Exercise 10**

Suppose you’re on a game show, and you’re given the choice of three doors. Behind one door is a car; behind the others, goats. You pick a door, say #1, and the host, who knows what’s behind the doors, opens another door, say #3, which has a goat. He then says to you, “Do you want to pick door #2?” What is the probability of winning the car if you use the strategy of first picking a random door and then switching doors every time? Note that the host will always open a door you did not pick, and it always reveals a goat.

Chunji Wang says

I think the solution to Exercise 8 should be 1/16 rather than 1/8. Don’t know why there is a “- 1” term in the “throws_needed” variable calculation?

Ike says

It’s 1/8. I thought it was 1/16 at first also, but it starts at 2 dollars and you win the pot when a tails is thrown.

1st Heads: Keep Going [.5]

1st Tails: Stop Win $2 – NO PROFIT

2nd Heads: Keep Going [.25]

2nd Tails: Stop Win $4 – NO PROFIT

3rd Heads: Keep Going [0.125]

3rd Heads: Stop Win $8 – NO PROFIT

4th Heads: Keep Going: [0.0625]

4th Tails: Stop Win $16 – BUT PROFIT [0.0625]

5th ?? : You always profit after this point [0.0625]

Profit happens [0.0625+0.0625=0.125=1/8]

Chunji Wang says

Thank you for your answer! I forgot the 4th head situation 🙂

Dave says

You win at least 16 dollars (1 dollar profit) if heads appears on the first three tosses. Therefore, the probability of profit is 1/2 * 1/2 * 1/2 = 1/8.

Vesselin Nikov says

I don’t have a solid background in probability, but the solution for problem 4 seems wrong. There are only two possible outcomes – T,H,T,H,T,H or respectively H,T,H,T,H,T. That is 2/factorial(6) or 0.002777778

Chunji Wang says

Actually, in total there are 2^6 possible outcomes, rather than factorial(6), so the solution should be 2 / 2^6 = 1 / 2^5