Distributions: Exercises


7. Simulating Total Bond Payments. You have a portfolio of 100 bonds. Each bond defaults with a probability of 0.07. If a bond defaults, it pays $0 at the end of one year. Otherwise, it repays $1.10. In this exercise, you'll simulate the total repayments of the bond portfolio using the Binomial distribution.

Use the rbinom(n_sims, n_bonds, prob) function in R to simulate the number of defaults, where n_sims is the number of simulations, n_bonds is the number of bonds, and p is the default probability for a single bond.

Compute the total repayments for the bond portfolio after one year.


# Define the number of bonds. n_bonds <- ___ # Define the default probability. p <- ___ # Simulate the number of defaults among the bonds. defaults <- rbinom(1, n_bonds, p) # Calculate the total payment. total_payment <- (n_bonds - defaults) * 1.10 # Print the total payment. total_payment # Define the number of bonds. n_bonds <- 100 # Define the default probability. p <- 0.07 # Simulate the number of defaults among the bonds. defaults <- rbinom(1, n_bonds, p) # Calculate the total payment. total_payment <- (n_bonds - defaults) * 1.10 # Print the total payment. total_payment test_error() test_object("n_bonds", incorrect_msg="Check the number of bonds.") test_object("p", incorrect_msg="Check the default probability.") test_object("defaults", incorrect_msg="Make sure you simulate the number of defaults correctly.") test_object("total_payment", incorrect_msg="Check the formula to calculate total payment.") success_msg("Well done!")
Remember to use the rbinom function to simulate defaults and then calculate repayments based on the non-defaults.

Previous Exercise Next Exercise