This project implements an end-to-end deep learning approach for portfolio optimization using PyTorch. The model learns to optimize portfolio weights directly from return series data, providing a data-driven alternative to traditional portfolio optimization methods.
- Deep learning-based portfolio optimization
- End-to-end training with custom loss functions
- Support for multiple evaluation metrics
- Flexible architecture for different asset classes
- Real-time portfolio rebalancing
- Risk-adjusted return optimization
-
Score Block
- Input: Return series [B, N, T] (Batch, Number of assets, Time steps)
- Output: Asset scores [B, N]
- Architecture: LSTM-based neural network
- Purpose: Learns to score assets based on their historical returns
-
Portfolio Block
- Input: Asset scores [B, N]
- Output: Portfolio weights [B, N]
- Function: Converts scores to valid portfolio weights using softmax
- Constraint: Weights sum to 1 (fully invested portfolio)
-
Loss Functions
- Mean-Variance Portfolio Loss
- Optimizes risk-adjusted returns
- Balances expected return and portfolio variance
- Customizable risk aversion parameter
- Maximum Sharpe Ratio Loss
- Maximizes risk-adjusted returns
- Optimizes the Sharpe ratio directly
- Mean-Variance Portfolio Loss
-
Expected Return (E(R))
- Annualized portfolio return
- Calculated from weighted asset returns
-
Standard Deviation (Std(R))
- Portfolio risk measure
- Annualized volatility
-
Sharpe Ratio
- Risk-adjusted return metric
- Excess return per unit of risk
-
Sortino Ratio
- Downside risk-adjusted return
- Considers only negative returns
-
Maximum Drawdown (MDD)
- Largest peak-to-trough decline
- Worst historical loss
-
Positive Return Percentage
- Percentage of positive returns
- Win rate metric
-
Frobenius Norm
- Matrix norm for covariance
- Risk structure measure
-
Turnover
- Portfolio rebalancing cost
- Measures weight changes
- Python 3.8+
- PyTorch 1.9.0+
- NumPy 1.21.0+
- Pandas 1.3.0+
- Matplotlib 3.4.0+
- scikit-learn 0.24.0+
- Clone the repository:
git clone https://github.com/yourusername/end2end-portfolio-optimization.git
cd end2end-portfolio-optimization- Create a virtual environment (recommended):
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Run the main script:
python main.py- For training:
python train.py- Customize model parameters:
from portfolio_block import PortfolioBlock
from score_block import ScoreBlock
# Initialize blocks with custom parameters
score_block = ScoreBlock(input_dim=10, hidden_dim=64)
portfolio_block = PortfolioBlock(num_assets=10)- Use custom loss functions:
from loss import MeanVarianceLoss, SharpeRatioLoss
# Choose loss function
loss_fn = MeanVarianceLoss(risk_aversion=0.5)
# or
loss_fn = SharpeRatioLoss(risk_free_rate=0.02)The project uses S&P 500 price data for demonstration. The data includes:
- Daily closing prices
- High prices
- Low prices
Data preprocessing includes:
- Log returns calculation
- Normalization
- Time series windowing
The model combines:
- LSTM-based feature extraction
- Portfolio weight optimization
- End-to-end training with custom loss functions
The training process:
- Uses historical return windows
- Optimizes portfolio weights
- Evaluates performance metrics
- Saves training history
MIT License# P05_End2End-Portfolio-Optimization