This repository contains a Python script for calculating and visualizing a stock portfolio's Value at Risk (VaR) using three different methods: Variance-Covariance, Historical Simulation, and Monte Carlo Simulation.
- Fetch Historical Stock Data: Retrieve historical stock data for multiple tickers using the
yfinance
library. - Calculate VaR:
- Variance-Covariance Method: An analytical approach is based on returns' mean and standard deviation.
- Historical Simulation Method: Non-parametric approach based on historical returns.
- Monte Carlo Simulation Method: Simulation-based approach using randomly generated returns.
- Visualize Return Distributions: Plot the distribution of returns and indicate the VaR threshold.
- Python 3.x
- pandas
- numpy
- yfinance
- scipy
- matplotlib
Install the required libraries using:
pip install pandas numpy yfinance scipy matplotlib
Clone the repository:
git clone https://github.com/ejb1987/Portfolio_VaR.git
cd Portfolio_VaR
Run the script:
python VaR.ipynb
Follow the prompts to enter your portfolio details:
Here's a brief example of how to run the script and what to expect:
Enter the stock tickers (comma-separated): AAPL, MSFT, META
Enter the initial investment amounts (comma-separated): 1000, 2000, 250
Enter the number of days: 10
Enter the confidence level (e.g., 0.95 for 95%): .95
[*********************100%%**********************] 3 of 3 completed
Portfolio Value at Risk (VaR) using the Variance-Covariance method at 95.00% confidence level is:
9.05%
$294.05
Portfolio Historical Value at Risk (VaR) at 95.00% confidence level is:
7.55%
$245.40
Portfolio Monte Carlo Value at Risk (VaR) at 95.00% confidence level is:
7.44%
$241.66
This project is licensed under the AGPL-3.0 license. See the LICENSE file for details.
Contributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.
For any questions or issues, please open an issue in this repository or contact [email protected].