1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include <iostream> #include <iomanip> #include <cmath> // Function to calculate Value at Risk (VaR) double calculateVaR(double meanReturn, double volatility, double confidenceLevel, double investmentAmount) { // Z-score for the given confidence level double zScore = 0.0; if (confidenceLevel == 0.95) { zScore = 1.645; // Approximate Z-score for 95% confidence level } else if (confidenceLevel == 0.99) { zScore = 2.33; // Approximate Z-score for 99% confidence level } else { std::cerr << "Confidence level must be 0.95 or 0.99.\n"; return -1.0; } // Calculate Value at Risk (VaR) double VaR = investmentAmount * (meanReturn - zScore * volatility); return VaR; } int main() { double meanReturn, volatility, confidenceLevel, investmentAmount; std::cout << "Enter the expected mean return (as a decimal, e.g., 0.08 for 8%): "; std::cin >> meanReturn; std::cout << "Enter the annual volatility (standard deviation) of the investment: "; std::cin >> volatility; std::cout << "Enter the confidence level (0.95 or 0.99): "; std::cin >> confidenceLevel; std::cout << "Enter the investment amount: "; std::cin >> investmentAmount; double VaR = calculateVaR(meanReturn, volatility, confidenceLevel, investmentAmount); if (VaR != -1.0) { std::cout << std::fixed << std::setprecision(2); std::cout << "Value at Risk (VaR) for the investment: $" << VaR << "\n"; std::cout << "This represents the potential loss at the " << (confidenceLevel * 100) << "% confidence level.\n"; } return 0; } |
Explanation
- Function
calculateVaR(double meanReturn, double volatility, double confidenceLevel, double investmentAmount)
:- Purpose: Computes the Value at Risk (VaR) for an investment, which estimates the potential loss at a given confidence level.
- Parameters:
meanReturn
: Expected mean return of the investment (as a decimal).volatility
: Annual volatility (standard deviation) of the investment.confidenceLevel
: Confidence level for the VaR calculation (0.95 or 0.99).investmentAmount
: Amount invested.
- Implementation:
- Z-Score: A critical value from the standard normal distribution, used to determine the threshold for the confidence level. Approximate values are used for 95% and 99% confidence levels.
- VaR Calculation:
VaR = investmentAmount * (meanReturn - zScore * volatility)
. This formula estimates the potential loss.
- Main Function:
- User Input: Collects input for mean return, volatility, confidence level, and investment amount.
- VaR Calculation: Calls
calculateVaR
to compute the Value at Risk. - Result Display: Outputs the VaR with two decimal places of precision and explains the result in the context of the confidence level.
Usage
- Risk Management Simulation: Provides a basic simulation for evaluating the risk of an investment based on historical volatility and expected return.
- Financial Analysis: Useful for understanding potential losses and making informed investment decisions.