R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

#library("MASS")

# Define input parameters
S <- 100 # initial portfolio value
r <- 0.05 # risk-free rate
sigma <- 0.2 # volatility of the factor
correlation <- 0.3 # correlation between the factor and the loan portfolio
T <- 1 # time horizon in years
N <- 100 # number of loan assets in the portfolio
recovery_rate <- 0.5 # recovery rate in case of default
default_probabilities <- runif(N, 0.01, 0.10) # generate random default probabilities for each loan

# Define the factor as a normal distribution
mu <- 0 # mean of the factor distribution
dt <- T / 365 # time step for the factor simulation
n <- as.integer(T / dt) # number of time steps
factor <- NULL #mvrnorm(n, mu = rep(0, N), Sigma = correlation) # generate random factor values

# Calculate the portfolio loss distribution
portfolio_loss <- rep(0, n)
for (i in 1:N) {
  asset_loss <- rep(0, n)
  for (j in 1:n) {
    asset_loss[j] <- S * (1 - recovery_rate) * pnorm((-log(default_probabilities[i]) + (r - 0.5*sigma^2)*j*dt) / (sigma*sqrt(j*dt))) - S * default_probabilities[i] * exp(-r*j*dt)
  }
  portfolio_loss <- portfolio_loss + asset_loss
}

# Calculate the expected loss and value at risk
expected_loss <- mean(portfolio_loss)
var_95 <- quantile(portfolio_loss, 0.95)

# Output results
print(paste("Expected loss:", expected_loss))
## [1] "Expected loss: 4493.54139602808"
print(paste("Value at risk (95%):", var_95))
## [1] "Value at risk (95%): 4504.83052762956"
# Define input parameters
S <- 100 # initial portfolio value
r <- 0.05 # risk-free rate
sigma <- 0.2 # volatility of the factor
correlation <- 0.3 # correlation between the factor and the loan portfolio
T <- 1 # time horizon in years
N <- 100 # number of loan assets in the portfolio
recovery_rate <- 0.5 # recovery rate in case of default
default_probabilities <- runif(N, 0.01, 0.10) # generate random default probabilities for each loan
attachment_point <- 0.05 # attachment point for the Aaa bond
detachment_point <- 0.01 # detachment point for the Aaa bond

# Define the factor as a normal distribution
mu <- 0 # mean of the factor distribution
dt <- T / 365 # time step for the factor simulation
n <- as.integer(T / dt) # number of time steps
factor <- NULL #mvrnorm(n, mu = rep(0, N), Sigma = correlation) # generate random factor values

# Simulate the portfolio loss distribution
portfolio_loss <- rep(0, n)
for (i in 1:N) {
  asset_loss <- rep(0, n)
  for (j in 1:n) {
    asset_loss[j] <- S * (1 - recovery_rate) * pnorm((-log(default_probabilities[i]) + (r - 0.5*sigma^2)*j*dt) / (sigma*sqrt(j*dt))) - S * default_probabilities[i] * exp(-r*j*dt)
  }
  portfolio_loss <- portfolio_loss + asset_loss
}

# Calculate the probability of reaching the attachment and detachment points
prob_default <- mean(portfolio_loss >= attachment_point)
prob_total_loss <- mean(portfolio_loss >= detachment_point)

# Calculate the expected losses for the Aaa bond
expected_loss <- (prob_default - prob_total_loss) * (attachment_point - detachment_point)

# Calculate the price of the Aaa bond
price <- S * (1 - expected_loss)

# Output results
print(paste("Aaa bond price:", price))
## [1] "Aaa bond price: 100"

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.