RNN Activity: Predicting the stock market

[!NOTE] I do not recommend making any kind of financial decisions based on RNNs.
Setup
To fetch the data, we’ll need the yfinance package. Activate your virtual environment, then run pip install yfinance. The starter notebook shows how to download some data.
Steps
- As usual, we need to split the data. For time series data, it is very important that you split chronologically, not randomly. The
yfinancedata has aDateTimeIndex, which means that we can index it with dates directly, e.g:
This also assumes that we’re interested in just the daily closing price. I’d suggest at least a year’s worth of data for both test and validation, with the rest for training.train_end = '2023-12-31' train = data['Close'][:train_end].values - Decide on a scaling factor and rescale your data to the 1-ish range. This should not be informed by the test or validation data!
- Follow the various TODO items to get the single-prediction RNN working.
- Modify both the
TimeSeriesDatasetand theSimpleRnnModelto predict multiple days in the future instead of just one.[!TIP] You may need to
squeezeand/orexpandyour data at some point in the process to ensure the data dimensions are correct
Further Exercises and Questions
- What impact does the scaling have on the model? Can you trigger exploding/vanishing gradients?
- Can you improve the model? Try playing around with LSTM/GRU instead of RNN, number of hidden units, activation functions, etc
- What happens if you pass a longer time window to the model? You will need to create a new
torch.FloatTensorfrom the numpy data to do this instead of using theTimeSeriesDatasetclass.