FMS implements a financial model for stock market simulation using OCaml. It models the behaviour of a £5,000 investment over 3 years, providing an understanding of potential outcomes and risk metrics for both individual stocks and the overall portfolio.
Warning
Disclaimer: FMS is for educational purposes only! Please do not use it for actual investment decisions. Always consult a qualified financial advisor for investment choices.
- Monte Carlo simulation of stock price movements using both traditional and SR-LSTM models
- Portfolio modelling with multiple stocks (TSLA, GOOGL, NVDA, IBM, MSFT, AMZN, and many more)
- Risk metrics calculation (Value at Risk, Expected Return, Probability of Profit, Sharpe Ratio)
- Configurable parameters (investment amount, time horizon, stock characteristics)
- Dynamic Portfolio Value calculation to estimate future portfolio worth
- SR-LSTM (Self-Regulating Long Short-Term Memory) implementation for advanced stock price prediction
- Comparative analysis between traditional Monte Carlo and SR-LSTM models
- Individual stock performance metrics for both traditional and SR-LSTM models
- Identification of best-performing stocks based on Sharpe Ratio
- Calculation of the average Sharpe Ratio across the portfolio for both models
- Detailed output of overall portfolio metrics and individual stock metrics
- Extensive portfolio with a mix of established tech giants and emerging technology companies
Stock prices follow geometric Brownian motion:
where
The discrete simulation uses:
where
The portfolio value
where
VaR is calculated using the percentile method:
where
The expected return is:
where
Calculated for both individual stocks and the overall portfolio.
The Sharpe Ratio is calculated as:
where
The portfolio value
where
We incorporate a Self-Regulating Long Short-Term Memory (SR-LSTM) neural network to predict future stock prices:
-
Regulatory Factor Calculation:
$$R_t = \rho(W_r \cdot h_{t-1} + b_r)$$ Here,
$R_t$ is the regulatory factor for time step$t$ ,$W_r$ is the weight matrix,$b_r$ is the bias term, and$\rho$ is a non-linear activation function. -
Modified Forget and Input Gates:
$$f'_t = f_t \odot R_t$$ $$i'_t = i_t \odot R_t$$ We modify the standard forget gate
$f_t$ and input gate$i_t$ by the regulatory factor$R_t$ to adjust the information flow dynamically. -
Cell State and Output Gate:
The cell state update and output gate equations remain the same as in standard LSTM, but they operate on the modified gates
$f'_t$ and$i'_t$ .
-
Clone the repository and navigate to the project directory.
-
Build and run the simulation:
eval $(opam env) && make clean && make all VERBOSE=1 && ./fms
## OR ##
eval $(opam env) && make clean && make all VERBOSE=1 && make write-output
The simulation displays:
- Initial investment amount and investment horizon
- Overall portfolio metrics for both Traditional and SR-LSTM models:
- Expected Return
- Value at Risk (VaR) at 95% and 99% confidence levels
- Probability of Profit
- Sharpe Ratio
- Individual stock metrics for both Traditional and SR-LSTM models:
- Current Price
- Expected Return
- Probability of Profit
- Sharpe Ratio
- Dynamic Portfolio Value estimation after the investment horizon
- Comparison of Total Expected Returns between Traditional and SR-LSTM models
- Difference in expected returns between the two models
- Average Sharpe Ratios for both Traditional and SR-LSTM models across the portfolio
- Best Performing Stocks based on Sharpe Ratio for both models
Modify fms.ml
to adjust:
- Initial investment and investment horizon
- Stock portfolio composition and characteristics
- Number of Monte Carlo simulations
a. Singular Value Decomposition (SVD)
SVD decomposes a matrix
Where:
-
$A$ is an$m \times n$ matrix -
$U$ is an$m \times m$ orthogonal matrix -
$\Sigma$ is an$m \times n$ diagonal matrix with non-negative real numbers on the diagonal -
$V^T$ is the transpose of an$n \times n$ orthogonal matrix
The diagonal entries
Where
Benefit: We could use SVD to analyse and potentially reduce the dimensionality of our stock data. It would be beneficial if we expanded to a larger number of stocks.
b. QR Decomposition
QR decomposition factors a matrix
Where:
-
$A$ is an$m \times n$ matrix -
$Q$ is an$m \times m$ orthogonal matrix -
$R$ is an$m \times n$ upper triangular matrix
For solving least squares problems
Enables the system to solve efficiently due to
Benefit: We can use it to solve least squares problems more efficiently, which might be useful for fitting models to historical stock price data.
c. Trust Region Methods
Trust region methods solve optimisation problems of the form:
At each iteration, we solve the subproblem:
Where:
-
$m_k(p)$ is a quadratic approximation of$f(x_k + p)$ :
-
$g_k$ is the gradient of$f$ at$x_k$ -
$B_k$ is an approximation of the Hessian of$f$ at$x_k$ -
$\Delta_k$ is the trust region radius
The next iterate is then:
Where
Benefit: It could potentially improve the optimization of our portfolio allocation, especially if we're dealing with non-convex objective functions.
This project is licensed under the BSD 3-Clause License.
@misc{fmsafo2024,
author = {Oketunji, A.F.},
title = {FMS — Financial Modelling System},
year = 2024,
version = {0.0.4},
publisher = {Zenodo},
doi = {10.5281/zenodo.13910428},
url = {https://doi.org/10.5281/zenodo.13910428}
}
(c) 2024 Finbarrs Oketunji. All Rights Reserved.