Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views3 pages

Problem Set A

The document is a problem set for MTH208, focusing on R programming tasks. It includes various exercises such as defining vectors and matrices, writing functions for simulations, and calculating probabilities. The problems range from basic R functions to more complex simulations involving random events.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Problem Set A

The document is a problem set for MTH208, focusing on R programming tasks. It includes various exercises such as defining vectors and matrices, writing functions for simulations, and calculating probabilities. The problems range from basic R functions to more complex simulations involving random events.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MTH208: Problem Set

Problem Set Questions


Last Updated: August 12, 2025

Practice R Questions
1. Define an R vector of the first 1000 odd integers.

2. Define an R vector of the first 500 Fibonacci numbers.

3. Write an R function that rolls a fair die and returns 1 if the die turns out to be even.

4. Write an R function that tosses a fair coin 15 times. The function should return "lose" if the number
heads is less than 8 and "win" if the number of heads is more than or equal to 8.

5. Define a 5 × 5 matrix of all elements being 1.

6. Define a 5 × 5 diagonal matrix with the diagonals being 1:5.

7. Define a 10 × 10 matrix where each entry is the result of a random roll of a fair die.

8. Write an R function that takes inputs n and rho. The function should return an 𝑛 × 𝑛 matrix with 1s
on the diagonals and rho on the off-diagonals.

9. Write an R function that takes inputs n and rho. The function should return an 𝑛 × 𝑛 matrix with the
𝑖, 𝑗 the element being 𝜌|𝑖−𝑗| .

10. Write an R function that takes a matrix input and returns a smaller matrix with only the odd columns
of the original matrix.

11. Define an 4 dimensional arrays with dimensions 10 × 4 × 6 × 5 where the entries are all 1s.

Worksheet Based Questions


Basic and Simulation Questions

1. Write an R function to calculate the area of a circle of radius 𝑟 for a user-given value of 𝑟.

2. Write an R function that returns the larger of two inputs 𝑥 and 𝑦.

1
3. Write an R program that saves the output of 1000 rolls of a fair die and returns the number of time
the output was an even number.

4. Draw 1000 random number between [0, 1] and calculate the proportion of numbers between 0.1 and 0.2.

5. A popular chips company came out with a new strategy. They were going to put different Harry Potter
toys in their chips packets. You and your friends are very excited; you want to be the first to collect
all figurines! The chips company has declared that they will put the toys in the packets based on the
popularity of the character. So more popular characters are more likely to be in the packet. Below in
the table is the probability of a packet of chips containing the type of toys.

Toys Harry Dumbledore Hermione Ron Neville Mcgonagall Dobby

Prob .25 .20 .20 .15 .10 .05 .05

Write an R function to calculate the number of chips packets that need to be bought to collect all toys.
Repeat the simulation 1000 times and calculate the average number of chips packets that need to be
bought to collect all toys.

6. You take half of a vitamin every morning. The vitamins are sold in a bottle of 100 (whole) tablets, so
at first you have to cut the tablets in half. Every day you randomly pull one thing from the bottle. If
it’s a whole tablet, you cut it in half and put the leftover half back in the bottle. If it’s a half-tablet,
you take the vitamin. You just bought a fresh bottle. How many days, on average, will it be before
you pull a half-tablet out of the bottle?

7. You are on a game show, being asked to choose between three doors. One door has a car, and the
other two have goats. After you choose a door, the host, Monty Hall, opens one of the other doors,
which he knows has a goat behind it. Monty then asks whether you would like to switch your choice
of door to the other remaining door. Do you choose to switch or not to switch?

Whether you switch or not depends on which action has the largest probability of winning the car.

1. Write an R function, MontyHall() to simulate this game show for when the contestant decides to
switch doors. The R function should return 1 if the contestant wins and 0 if they lose.

2. Repeat the simulation 1000 times to estimate the probability of winning if the contestant switches.

8. Write an R function to simulate a random walk of n steps. Each step is determined by a fair coin
toss: heads means step forward (+1), tails means step backward (-1). The function should return
the final position after n steps. Repeat the simulation 1000 times for n=100 steps and calculate the
average final position (beware of the first and last step, you cannot move before first and after last step).

2
9. Write an R function to simulate the following scenario: In a large city, people visit a popular mall
on random days throughout the year. The function should estimate the probability that at least two
people in a group of n people visit the mall on the same day. Assume there are 365 days in a year.
Repeat the simulation 1000 times for n=23 and calculate the estimated probability.

10. Write an R function that simulates a geometric random variable with a given probability of success p.
The function should return the number of trials needed to get the first success. Repeat the simulation
1000 times and calculate the average number of trials needed.

You might also like