This repository contains a Python script that performs portfolio risk analysis on selected assets using Value at Risk (VaR) and Expected Shortfall (ES) measures. The script uses Numba to optimize the risk calculations, downloads historical data from Yahoo Finance with yfinance, and visualizes the portfolio's return distribution.
- Data Acquisition: Downloads historical stock data for specified assets.
- Data Cleaning: Handles missing data by dropping incomplete rows.
- Risk Metrics: Calculates portfolio Value at Risk (VaR) and Expected Shortfall (ES) using a Numba-optimized function.
- Visualization: Plots a histogram of daily portfolio returns with a marked VaR threshold.
- Interactivity: Provides verbose output and interactive prompts to follow each step of the analysis.
- Python 3.x
- NumPy
- Pandas
- Matplotlib
- yfinance
- Numba
-
Clone the repository:
git clone https://github.com/yourusername/your-repo-name.git cd your-repo-name -
Create a virtual environment (optional but recommended):
python -m venv venv
Activate the virtual environment:
- Install the required packages:
You can install packages from the requirements.txt file:
pip install -r requirements.txt
Run the script using Python:
python var.py
- The script will walk you through several steps:
- Configuration of assets, weights, and time period.
- Downloading and cleaning historical data.
- Calculating daily returns and portfolio returns.
- Computing VaR and Expected Shortfall.
- Visualizing the return distribution.
Follow the on-screen prompts (press Enter to continue) to observe the progress at each step.
Contributions are welcome! If you have suggestions or improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.