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
DateHighLowOpenCloseVolumeAdj Close
02007-07-3076.52999975.44000275.84999875.7399982575.075.739998
12007-07-3177.16999875.66999875.69999777.0500033513.077.050003
22007-08-0177.05999874.86000177.00000075.3499983930.075.349998
32007-08-0276.20999974.26999775.22000175.7600026180.075.760002
42007-08-0376.00000074.52999975.38999974.7500004387.074.750000
........................
34122021-06-2475.76999774.51999775.37000375.55999826664.075.559998
34132021-06-2576.20999974.94999775.61000176.18000031417.076.180000
34142021-06-2876.58999674.51999776.16000474.68000027711.074.680000
34152021-06-2975.51000273.91000474.59999874.76000227711.074.760002
34162021-06-3075.16999874.77999975.13999974.77999924.074.7799

#3. Plot brent oil price history

#4. Create train and test dataset with 70%-30%
# 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)

#6. Validating the loss of train and test
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

It's very nice and better than expected!!!!

#8. Finally, predict brent oil for 30 days future from 30-Jun-2021. What happened?


Oh my Goodness! the oil price goes down! ^^
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

Popular posts from this blog

Determination of source and breakthrough mechanism of water production in a naturally fractured basement reservoir by analyzing water production data

Hybrid System of ESP and Gas Lift Application from Conceptual Design, Pilot Test to System Analysis

A Comparison of Permeability Prediction Methods Using Core Analysis Data