Blog of Andrés Aravena

Exercises for Makeup

08 June 2019. Deadline: Makeup exam day.

The makeup is coming, be prepared. There are two parts on the course: deterministic systems and random systems. The main idea behind both parts is computational thinking. You should be able to

In the exam, most of the errors were in the last part. Many people forgot the rules of R:

You can read all the previous classes, and even watch some very good YouTube videos. But that does not make you learn, even if you feel like learning. To really learn, you need to practice.

I like to tell this analogy. Let’s say we are studying to be a musician. You read about music, listen a lot of music, and you can recognize every author, style and instruments. You can talk about music, even be a music critic, but you will not be a musician. (This is my case). If instead you play an instrument, and practice regularly, you will remember songs and themes, and you will be able to perform as a cover band. (Some people in our course is at this level). What we really need to do is to make your own music. We cannot be successful if we always play the same songs with the same instruments. You need to make your own music. Of course, you are not starting from zero in this course. We give you the questions and the expected answers. It is much easier than real life.

The following exercises are hard. Much harder than the makeup exam. To solve them, you need all the tools we have used before. It is a triathlon. Each partial answer will be a step towards success. If you work on these exercises, you have much better chance with the makeup exam.

Today I’m giving you open questions without hints, so you have the chance of finding your own solutions. Also, giving you more hints will take longer, and it is better that you start exercising soon.

Game of dice

My friend Vincent, from France, showed me this game that he studies with first-year students. The game is played by tossing two blue dice and one red dice. You win the game if the sum of the two blue dice is equal to the value of the red one.

  1. What is the probability of each sum-of-two-dice value? Simulate the blue dice 10000 times and draw a barplot of the frequency of each sum value.
  2. What is the probability of winning the game? Write a function one_game() to represent one game. The function should simulate three dice and return TRUE when the sum of the two blue dice is equal to the value of the red dice. Then calculate the average number of games when the result is TRUE.
  3. If the game is played 100 times, what is the probability of never winning? Now you should make a function hundred_games() that plays one_game() one hundred times, and returns TRUE when all the games were lost. Finally, simulate hundred_games() for 10000 times and calculate the average number of times the result is TRUE.
  4. Some players (around 10%) cheat and use a loaded blue dice that always gives the value 1. What is the probability of winning in this case?
  5. (Bonus) Someone just won one game. What is the probability that the player was cheating?

Hot hand in Basketball

Fanatics of basketball, and TV sports speakers, believe that when a player shots a successful ball, then it is more probable that he will have another success. This is called “hot hand”, or “success breed success”. But this may be just chance. To test this, we will assume that the player has 80% chance of a successful shot.

  1. What do you believe is the chance of a good shot after two bad shoots? What about the chance of a good shot after two good shots? Which one is better?
  2. Create a vector called shots, with the result of simulating 10000 random shots. Something like sample(c(TRUE,FALSE), size=10000, replace=TRUE, prob=c(0.8, 0.2))
  3. Write a function that takes the shots vector and counts how many times there is a TRUE or FALSE value, but only after two FALSE values.
  4. Write another function to count the number of TRUE or FALSE values, only after two TRUE values.
  5. Compare the last two results. Does the result match your prediction?
  6. What do you believe is the probability of having a strike of 3 or more good shots? 5 or more? 10 or more?
  7. Write a function that takes the shots vector and returns a list with the length of each strike of TRUE. You can do it with a for(){} loop. You need to prepare a list called ans and a variable called count, that initially has value 0.If shots[i] is TRUE, then count must increase by 1. If shots[i] is FALSE, then the value of count must be stored in ans[j], and after that count must be set to zero. You do not need to store count zero on ans, but it is not a problem if you do. Notice that you need an index i for shots[i] and another index j for ans[j]. Be careful when you update them.
  8. Draw a barplot with the frequency of each strike length. Does this result match your prediction?

Family length

There is a big variety in the number of children in different families. For this exercise we will assume that any family can have 0 to 5 children. The probabilities for each case are the following:

Size Probability
0 64%
1 16%
2 10%
3 5%
4 3%
5 2%

We want to know the frequency of family sizes. We define family size as the number of grandchildren plus the number of children plus one, since a family without children has one person.

If the number of children in a family is 0, then the number of grandchildren is 0, and the family size is 1. Otherwise, if the number of children is 1 or more, then the number of grandchildren is the sum of the family size for each children, and the family size will be n_grandchildren + n_children + 1.

  1. Please write a recursive function to simulate a family and calculate its family size. Be careful when defining the probabilities, otherwise the function may not finish.
  2. Simulate 10000 times and draw a barplot with the result. You may notice that this case is not a normal distribution.

Recursive random tree

In Homework 6 we made some drawings of trees using Turtle Graphics. The program was something like this:

tree <- function(n, size, angle) {
        old_pos <- turtle_getpos()
        old_angle <- turtle_getangle()
        if(n>1) {
            tree(n-1, size*0.8, angle)
            tree(n-1, size*0.8, angle)
            tree(n-1, size*0.8, angle)
        turtle_setpos(old_pos[1], old_pos[2])

and then we used the function to make a tree

tree(6, 23, 25)

The problem is that these trees do not look very realistic. To improve this, you have to modify the code to have some randomness. You can see that the variable size is used 4 times, and angle is used 6 times. You should replace each one of them with random numbers from a Normal distribution.

For the random size you can use the rnorm() function, with mean=size and sd=size/10. The same idea can be used for angle. You should get something like this:

Deadline: Makeup exam day.