Predict Brent Oil Price by LSTM
#1.importing data from yahoo for 2001 to 2021
df = web.DataReader('BZ=F', data_source='yahoo', start='2001-01-01', end='2021-06-30')
df = df.reset_index()
#2. format date data to appropriate format
df['Date']=pd.to_datetime(df['Date'], format="%b %d, %Y")
df
Date | High | Low | Open | Close | Volume | Adj Close | |
---|---|---|---|---|---|---|---|
0 | 2007-07-30 | 76.529999 | 75.440002 | 75.849998 | 75.739998 | 2575.0 | 75.739998 |
1 | 2007-07-31 | 77.169998 | 75.669998 | 75.699997 | 77.050003 | 3513.0 | 77.050003 |
2 | 2007-08-01 | 77.059998 | 74.860001 | 77.000000 | 75.349998 | 3930.0 | 75.349998 |
3 | 2007-08-02 | 76.209999 | 74.269997 | 75.220001 | 75.760002 | 6180.0 | 75.760002 |
4 | 2007-08-03 | 76.000000 | 74.529999 | 75.389999 | 74.750000 | 4387.0 | 74.750000 |
... | ... | ... | ... | ... | ... | ... | ... |
3412 | 2021-06-24 | 75.769997 | 74.519997 | 75.370003 | 75.559998 | 26664.0 | 75.559998 |
3413 | 2021-06-25 | 76.209999 | 74.949997 | 75.610001 | 76.180000 | 31417.0 | 76.180000 |
3414 | 2021-06-28 | 76.589996 | 74.519997 | 76.160004 | 74.680000 | 27711.0 | 74.680000 |
3415 | 2021-06-29 | 75.510002 | 73.910004 | 74.599998 | 74.760002 | 27711.0 | 74.760002 |
3416 | 2021-06-30 | 75.169998 | 74.779999 | 75.139999 | 74.779999 | 24.0 | 74.7799 |
#3. Plot brent oil price history
# split into train and test sets
train_size = int(len(scaled_data) * 0.70)
test_size = len(scaled_data) - train_size
train, test = scaled_data[0:train_size, :], scaled_data[train_size:len(scaled_data), :]
#5. build LSTM model (Please search on Google for Long Short Term Model (LSTM))
model = Sequential()
model.add(LSTM(units = 50, return_sequences = True, input_shape = (x_train.shape[1], 1)))
model.add(Dropout(0.2))
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))
model.add(LSTM(units = 50))
model.add(Dropout(0.2))
model.add(Dense(units = 1))
model.compile(optimizer = 'adam', loss = 'mean_squared_error')
model.fit(x_train, y_train, batch_size=10,epochs=20)
Train Mean Absolute Error: 2.7676871476534477
Train Root Mean Squared Error: 3.5561004954208113
Test Mean Absolute Error: 1.6927817893998272
Test Root Mean Squared Error: 2.3757989199770857
#7. Plot the result
#8. Finally, predict brent oil for 30 days future from 30-Jun-2021. What happened?
Price next (1) days of Oil Brent : 74.98288
Price next (2) days of Oil Brent : 75.1864
Price next (3) days of Oil Brent : 75.18406
Price next (4) days of Oil Brent : 75.039986
Price next (5) days of Oil Brent : 74.830185
Price next (6) days of Oil Brent : 74.60653
Price next (7) days of Oil Brent : 74.392654
Price next (8) days of Oil Brent : 74.193634
Price next (9) days of Oil Brent : 74.00678
Price next (10) days of Oil Brent : 73.82796
Price next (11) days of Oil Brent : 73.653854
Price next (12) days of Oil Brent : 73.48243
Price next (13) days of Oil Brent : 73.31274
Price next (14) days of Oil Brent : 73.1438
Price next (15) days of Oil Brent : 72.97505
Price next (16) days of Oil Brent : 72.80588
Price next (17) days of Oil Brent : 72.63606
Price next (18) days of Oil Brent : 72.46532
Price next (19) days of Oil Brent : 72.293625
Price next (20) days of Oil Brent : 72.12104
Price next (21) days of Oil Brent : 71.94766
Price next (22) days of Oil Brent : 71.77382
Price next (23) days of Oil Brent : 71.59962
Price next (24) days of Oil Brent : 71.42495
Price next (25) days of Oil Brent : 71.25018
Price next (26) days of Oil Brent : 71.07561
Price next (27) days of Oil Brent : 70.90114
Price next (28) days of Oil Brent : 70.726906
Price next (29) days of Oil Brent : 70.55298
Price next (30) days of Oil Brent : 70.37943
Comments
Post a Comment
Many thanks for your comment!