Distributions: Exercises


6. Individual Bond Default Simulation with Uniform Distribution. For a given bond, there's a default probability of 0.05. Instead of using the binomial distribution, you'll simulate the default outcome by drawing from a uniform distribution.

If the value you draw from a uniform distribution is below the default probability, we will assume that the bond defaults. Use the runif(n, min, max) function in R where n is the number of draws, and min and max define the range of the uniform distribution.

Simulate a default outcome for the bond.


# Define the default probability. p <- ___ # Draw a value from the uniform distribution. drawn_value <- runif(1, 0, 1) # Determine the default outcome. default_outcome <- ifelse(drawn_value < p, 1, 0) # Print value drawn. drawn_value # Print the default outcome. default_outcome # Define the default probability. p <- 0.05 # Draw a value from the uniform distribution. drawn_value <- runif(1, 0, 1) # Determine the default outcome. default_outcome <- ifelse(drawn_value < p, 1, 0) # Print value drawn. drawn_value # Print the default outcome. default_outcome test_error() test_object("p", incorrect_msg="Check the default probability.") test_object("drawn_value", incorrect_msg="Remember to draw from a uniform distribution between 0 and 1.") test_object("default_outcome", incorrect_msg="Check whether the number drawn is less than the default probability to determine the default outcome.") success_msg("Well done!")
Use the runif function to draw from a uniform distribution and then check if it's less than the default probability.

Previous Exercise Next Exercise