Use R do a vector 120 values containin a daily data on the simple returns for an asset Suppose you have a portfolio consisting only of this asset and that the curent portfolio value is E170Using Basic
Assuming that the daily data on simple returns for the asset is stored in a vector named "returns", we can use the following code to calculate the portfolio's 99% Value at Risk (VaR) using Basic Historical Simulation:
# Calculate portfolio value at risk (VaR) using basic historical simulation
portfolio_value <- 170 # Current portfolio value
returns <- sample(returns, 120, replace = TRUE) # Randomly sample 120 returns from the vector
sorted_returns <- sort(returns) # Sort returns in ascending order
percentile <- round(0.99 * length(sorted_returns)) # Index of 99th percentile return
VaR <- portfolio_value * sorted_returns[percentile] # Calculate VaR
# Print the result
cat("The portfolio's 99% VaR is", round(VaR, 2))
This code first sets the current portfolio value to E170. It then randomly samples 120 returns from the "returns" vector using the "sample" function with the "replace" argument set to TRUE to allow for repeated sampling. The sampled returns are sorted in ascending order using the "sort" function.
Next, the code calculates the index of the 99th percentile return using the "round" function to round down to the nearest integer. The 99th percentile return is the return that is greater than or equal to 99% of the other returns. Finally, the VaR is calculated as the product of the portfolio value and the 99th percentile return.
The resulting VaR is printed using the "cat" function with the "round" function to round the VaR to two decimal places.
原文地址: https://www.cveoy.top/t/topic/ZAU 著作权归作者所有。请勿转载和采集!