- Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-4)
- Probability functions beginner
- Combinations Exercises
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory

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.

- Answer probability questions with simulation
- Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-4)
- Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-6)
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory

Note: We are going to use random number functions and random process functions in R such as `runif`

, a problem with these functions is that every time you run them you will obtain a different value. To make your results reproducible you can specify the value of the seed using `set.seed(‘any number’)`

before calling a random function. (If you are not familiar with seeds, think of them as the tracking number of your random numbers). For this set of exercises we will use `set.seed(1)`

, don’t forget to specify it before every random exercise.

Answers to the exercises are available here

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

**Exercise 1**

**Generating random numbers. ** Set your seed to 1 and generate 10 random numbers using `runif`

and save it in an object called `random_numbers`

.

**Exercise 2**

Using the function `ifelse`

and the object `random_numbers`

simulate coin tosses. Hint: If `random_numbers`

is bigger than .5 then the result is head, otherwise is tail.

Another way of generating random coin tosses is by using the `rbinom`

function. Set the seed again to 1 and simulate with this function 10 coin tosses. Note: The value you will obtain is the total number of heads of those 10 coin tosses.

**Exercise 3**

Using the function `rbinom`

to generate 10 unfair coin tosses with probability success of 0.3. Set the seed to 1.

**Exercise 4**

We can simulate rolling a die in R with `runif`

. Save in an object called `die_roll`

1 random number with `min = 0`

and `max = 6`

. This mean that we will generate a random number between 1 and 6.

Apply the function `ceiling`

to `die_roll`

. Don’t forget to set the seed to 1 before calling `runif`

.

**Exercise 5**

Simulate normal distribution values. Imagine a population in which the average height is 1.70 m with an standard deviation of 0.1, using `rnorm`

simulate the height of 100 people and save it in an object called `heights`

.

To get an idea of the values of heights applying the function `summary`

to it.

**Exercise 6**

a) What’s the probability that a person will be smaller or equal to 1.90 m ? Use `pnorm`

b) What’s the probability that a person will be taller or equal to 1.60 m? Use `pnorm`

**Exercise 7**

The waiting time (in minutes) at a doctor’s clinic follows an exponential distribution with a rate parameter of 1/50. Use the function `rexp`

to simulate the waiting time of 30 people at the doctor’s office.

**Exercise 8**

What’s the probability that a person will wait less than 10 minutes? Use `pexp`

**Exercise 9**

What’s the waiting time average?

**Exercise 10**

Let’s assume that patients with a waiting time bigger than 60 minutes leave. Out of 100 patients that arrive to the clinic how many are expected to leave? Use `pexp`