Skip to content

Predicting closing stock returns and performing portfolio optimization. All in Jupyter Notebooks (Python)

License

Notifications You must be signed in to change notification settings

vicdotcom/Stock-Market-Prediction-Portfolio-Optimization

Repository files navigation

Stock Returns Prediction & Portfolio Optimization

A research thesis entailing the prediction of stock returns using Long Short Term Memory (LSTM) neural network designs and portfolio optimization. Specifically, this study compares the prediction performance of a univariate and multivariate LSTM after which the return predictions from both models are fed into a mean variance optimizer and the final portfolios constructed are evaluated.

Problem Statement

Classical portfolio optimization techniques often use historical prices and returns as model inputs which has limited influence on future stock behaviour therefore obtaining inaccurate estimates on future expected returns.

Additionally, with the exponential increase in volumes of data, stock market analysts and particpants currently rely on non-AI methods (such as technical and fundamental analysis) for stock selection which are often arbitrary due to their limited ability to analyze raw data.

Data

10 year daily stock data (Open, Close, High, Low) from January 2013 to November 2022 for stocks listed on the Nairobi Securities Exchange (NSE).

Methodology

  1. Data Cleaning and Preprocessing
    • This entails ensuring the stock data has full price information for the 10 year period and removal of null or outlier values.
    • An Augmented Dickey Fuller (ADF) test was applied to the stock returns to ensure time series stationarity.
    • Data is then normalized within the range (0,1) and split into 80% for training and 20% for testing.
  2. LSTM Model Construction
    • A Keras random search methodology was applied for both univariate and multivariate models to select the most optimal combination of LSTM hyperparameters.
    • For the multivariate LSTM model, 167 features consisting of lagged versions of returns and technical indicators were generated.
    • 25 features were selected as model inputs using Recursive Feature Elimination (RFE) via a Random Forest regressor.
  3. Portfolio Construction
    • 10 stocks with the highest return predictions were selected for portfolio optimization. Here, 30-day return forecasts from each model were fed into a mean-variance optimization model. Portfolio performance metrics generated by each model's preditions were then compared.

Results

LSTM Prediction Results

The multivariate LSTM outperforms the univariate LSTM in returns prediction as evidenced by lower root mean squared error per predicted stock as well as by simply looking at the return prediction visualizations.

SCOM univariate predictions Fig. 1: Univariate LSTM Return Predictions for Safaricom Data

SCOM multivariate predictions Fig. 2: Multivariate LSTM Return Predictions for Safaricom Data

The univariate LSTM model can be better used in classification and trend forecasting whereas the multifeature LSTM is more superior in actual value prediction.

Portfolio Performance

The univariate LSTM return-based portfolio model shows superior performance with a higher annualized Sharpe ratio of 5.592 compared to 0.667 for the multivariate return-based portfolio.

Optimum Uni-Portfolio Frontier

Fig. 3: Efficient Frontier for Univariate LSTM-Based Portfolio

Optimum Multi-Portfolio Frontier

Fig. 4: Efficient Frontier for Multivariate LSTM-Based Portfolio

While achieving excess returns excess returns to risk is paramount for any portfolio past studies emphasize on prediction accuracy as a crucial aspect for asset preselection. On the basis of having more accurate stock return predictions, the study recommends the multifeature LSTM for stock forecasting and subsequent portfolio optimization.

About

Predicting closing stock returns and performing portfolio optimization. All in Jupyter Notebooks (Python)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published