10. Mixture of Two Normal Distributions. Consider an asset that performs differently in stable and volatile periods. During stable periods, the asset has an average return of 2% with a standard deviation of 2%. During volatile periods, the asset has an average return of 10% with a standard deviation of 10%. Assume that the asset is in the stable regime 70% of the time and the volatile regime 30% of the time. Model the returns using a mixture of two normal distributions.
In R
, you can generate values from a normal distribution using the rnorm(n, mean, sd)
function, where n
is the number of observations, mean
is the mean of the distribution, and sd
is the standard deviation. After generating values for both regimes, combine them based on the mixture probabilities.
Plot the mixture to visualize the distribution of returns using the hist
function. This function creates a histogram, which is a representation of the distribution of numerical data.
# Define the parameters for the stable regime
mean_stable <- ___
sd_stable <- ___
# Define the parameters for the volatile regime
mean_volatile <- ___
sd_volatile <- ___
# Mixture probabilities
p_stable <- ___
p_volatile <- ___
# Simulate returns from both regimes
n <- 1000
stable_returns <- rnorm(n, mean_stable, sd_stable)
volatile_returns <- rnorm(n, mean_volatile, sd_volatile)
# Combine the returns based on the mixture probabilities
mixed_returns <- c(sample(stable_returns, size = p_stable * n, replace = TRUE),
sample(volatile_returns, size = p_volatile * n, replace = TRUE))
# Plot the mixture
hist(mixed_returns)
# Define the parameters for the stable regime
mean_stable <- 0.02
sd_stable <- 0.02
# Define the parameters for the volatile regime
mean_volatile <- 0.10
sd_volatile <- 0.10
# Mixture probabilities
p_stable <- 0.7
p_volatile <- 0.3
# Simulate returns from both regimes
n <- 1000
stable_returns <- rnorm(n, mean_stable, sd_stable)
volatile_returns <- rnorm(n, mean_volatile, sd_volatile)
# Combine the returns based on the mixture probabilities
mixed_returns <- c(sample(stable_returns, size = p_stable * n, replace = TRUE),
sample(volatile_returns, size = p_volatile * n, replace = TRUE))
# Plot the mixture
hist(mixed_returns)
test_error()
test_object("mean_stable", incorrect_msg="Check the mean return for the stable regime.")
test_object("sd_stable", incorrect_msg="Check the standard deviation for the stable regime.")
test_object("mean_volatile", incorrect_msg="Check the mean return for the volatile regime.")
test_object("sd_volatile", incorrect_msg="Check the standard deviation for the volatile regime.")
test_object("p_stable", incorrect_msg="Check the mixture probability for the stable regime.")
test_object("p_volatile", incorrect_msg="Check the mixture probability for the volatile regime.")
success_msg("Well done!")