This project implements a portfolio optimization framework using Python, focusing on maximizing the Sharpe Ratio. The framework evaluates asset allocation strategies and compares the performance of the optimized portfolio against the S&P 500 (SPY).
- Portfolio Optimization: Utilizes
scipy.optimize
to find the optimal asset weights for maximizing the Sharpe Ratio. - Data Retrieval: Fetches historical price data and Federal Reserve risk-free rates using
yfinance
andfredapi
. - Financial Analysis: Calculates annualized returns, covariance matrix, and portfolio metrics.
- Performance Visualization: Uses
matplotlib
to plot cumulative returns and compare the optimized portfolio against SPY.
numpy
: Numerical computations and matrix operations.pandas
: Data manipulation and time-series analysis.scipy
: Optimization and numerical methods.yfinance
: Fetching historical asset prices.fredapi
: Retrieving Federal Reserve risk-free rates.matplotlib
: Visualizing financial data.
-
Clone the repository:
git clone <repository-url>
-
Navigate to the project directory:
cd <project-directory>
-
Install the required packages:
pip install numpy pandas scipy yfinance fredapi matplotlib
-
Set up environment variables for the Federal Reserve API key:
- Create a
.env
file in the project directory with the following content:FREDAPI_KEY=your_fred_api_key
- Alternatively, use a generic value eg. 0.20
- Create a
-
Update the
tickers
list with the assets you want to include in the portfolio. -
Specify the
start_date
for the historical data analysis in the script. -
Run the
portfolio_optimization.py
script:python portfolio_optimization.py
-
The script will:
- Fetch historical price data starting from the specified
start_date
. - Calculate log returns and covariance matrix.
- Optimize portfolio weights to maximize the Sharpe Ratio.
- Compare the optimized portfolio against SPY.
- Plot the cumulative returns of the optimized portfolio and SPY.
- Fetch historical price data starting from the specified
- Optimal Weights: Displays the asset allocation that maximizes the Sharpe Ratio.
- Performance Metrics: Shows the expected return, volatility, and Sharpe Ratio of the optimized portfolio.
- Comparison Plot: Visualizes the cumulative returns of the optimized portfolio versus SPY.