Predicting monthly stock returns using momentum, volatility, and P/E ratio as features.
Built entirely in pure numpy. I used no PyTorch, no sklearn for the core model. Every forward pass, backpropagation step, and weight update is implemented from first principles.
Most neural network tutorials use MNIST or toy datasets. This project applies the same mathematical machinery to a real problem in quantitative finance. Cross-sectional return prediction, where the data is noisy, the signal is weak, and honest evaluation matters as much as implementation.
Input(3) → H1·ReLU(8) → H2·ReLU(4) → Output·Linear(1)
| Layer | Weight matrix | Shape | Bias | Parameters |
|---|---|---|---|---|
| Input → H1 | W₁ | 3 × 8 | b₁ (1×8) | 32 |
| H1 → H2 | W₂ | 8 × 4 | b₂ (1×4) | 36 |
| H2 → Output | W₃ | 4 × 1 | b₃ (1×1) | 5 |
| Total | 73 |
For each layer
where
In full for our architecture:
Dimension check (n = batch size):
Mean Squared Error:
This is identical to the OLS residual sum of squares scaled by
Backprop applies the chain rule in reverse through every layer. The goal is to compute
Hidden layer gradient (chain rule through ReLU)
ReLU derivative:
Key property: every gradient
where
We use He initialisation rather than small random values:
Why: With multiple ReLU layers, naive small random initialisation causes the variance of activations to shrink layer by layer, gradients vanish before reaching early layers. He initialisation keeps variance stable by accounting for the fact that ReLU kills roughly half the neurons.
Financial data is a time series. A random split would allow the model to train on observations from 2022 and test on 2018 — effectively leaking future information backwards. We always split by time:
|─────── train (80%) ───────|── test (20%) ──|
t=0 t=T*0.8 t=T
| Feature | Construction | Academic source |
|---|---|---|
| Momentum |
|
Jegadeesh & Titman (1993) |
| Volatility | 12-month rolling std of monthly returns | Ang et al. (2006) |
| P/E Ratio | Trailing price / earnings | Basu (1977) |
| Target y | Forward 1-month return | — |
All features normalised to zero mean and unit variance before training. Target
Low R² is expected and is not a failure of implementation. Monthly stock returns are dominated by idiosyncratic noise. The academic literature treats R² of 0.5–2% as meaningful signal in cross-sectional return prediction.
What this project validates:
- Correct forward pass dimensions throughout
- Gradients match weight shapes at every layer
- Loss decreases monotonically across epochs
- Gradient norms remain healthy (no vanishing/exploding)
Dataset covers 10 large-cap US equities from 2015–2024 (~1080 monthly observations). A production model would require broader cross-sectional coverage across the full market universe to avoid survivorship bias.
quant-nn-backprop/
│
├── data/
│ └── features.csv # generated by fetch_data.py
│
├── fetch_data.py # yfinance data pipeline + synthetic fallback
├── neural_net.py # full NN: forward, backprop, training loop
├── main.ipynb # end-to-end walkthrough with plots
└── README.md
# Clone and install dependencies
git clone https://github.com/YOUR_USERNAME/quant-nn-backprop
cd quant-nn-backprop
pip install numpy pandas matplotlib yfinance jupyter
# Fetch real data (requires internet)
python fetch_data.py
# Run notebook
jupyter notebook main.ipynbIf yfinance is unavailable, fetch_data.py automatically generates synthetic data with realistic feature distributions so the full pipeline still runs.
| Package | Purpose |
|---|---|
numpy |
All matrix operations — forward pass, backprop, weight updates |
pandas |
Data loading, feature engineering, rolling calculations |
matplotlib |
Loss curves, gradient norm plots, prediction visualisations |
yfinance |
Real stock data (optional — synthetic fallback included) |
jupyter |
Running the notebook |
- Jegadeesh, N. & Titman, S. (1993). Returns to Buying Winners and Selling Losers. Journal of Finance.
- Ang, A. et al. (2006). The Cross-Section of Volatility and Expected Returns. Journal of Finance.
- Basu, S. (1977). Investment Performance of Common Stocks in Relation to their Price-Earnings Ratios. Journal of Finance.
- He, K. et al. (2015). Delving Deep into Rectifiers. ICCV. (He initialisation)
- Rumelhart, D., Hinton, G. & Williams, R. (1986). Learning representations by back-propagating errors. Nature.