5. Bond Default Simulations with Binomial Distribution. You hold a portfolio of 100 bonds and each has a default probability of 0.05. To understand the variability in possible default outcomes, you will simulate default in the portfolio 10 times using the Binomial distribution and plot the results as a histogram.
To simulate default counts multiple times, you can use the rbinom(n_sims, n_bonds, prob)
function in R
, 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.
Simulate bond default 10 times and visualize the results using a histogram.
# Define the number of bonds.
n_bonds <- ___
# Define the default probability.
p <- ___
# Define the number of simulations.
n_sims <- ___
# Simulate default counts for the portfolio.
defaults <- rbinom(n_sims, ___, ___)
# Plot the histogram.
hist(defaults, breaks = max(defaults) - min(defaults) + 1, main = "Histogram of Defaults", xlab = "Number of Defaults")
# Define the number of bonds.
n_bonds <- 100
# Define the default probability.
p <- 0.05
# Define the number of simulations.
n_sims <- 10
# Simulate default counts for the portfolio.
defaults <- rbinom(n_sims, n_bonds, p)
# Plot the histogram.
hist(defaults, breaks = max(defaults) - min(defaults) + 1, main = "Histogram of Defaults", xlab = "Number of Defaults")
test_error()
test_object("n_bonds", incorrect_msg="Check the number of bonds.")
test_object("p", incorrect_msg="Check the default probability.")
test_object("n_sims", incorrect_msg="Check the number of simulations.")
success_msg("Excellent work!")
rbinom
function to simulate bond defaults. Don't forget to define all necessary variables.