In [1]:
import os
import operator
import random
import calendar
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from vixstructure.data import FuturesByMonth
from vixstructure.utils import parse_whole_directory_monthwise
mpl.rcParams["figure.figsize"] = 16, 9
In [68]:
def plot3d_loss(dataframe, zlim=None, rotation=225):
X = dataframe.index.levels[0]
Y = dataframe.index.levels[1]
X, Y = np.meshgrid(X, Y)
Z = np.reshape(
np.array(list(map(lambda x: dataframe[x[0], x[1]], np.reshape(np.dstack((X,Y)), (X.shape[0]*X.shape[1],2))))),
X.shape)
fig = plt.figure(figsize=(5, 5))
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z, linewidth=None, antialiased=True, cmap=cm.coolwarm_r)
ax.view_init(azim=rotation)
ax.set_xlabel("Depth")
ax.set_ylabel("Width")
ax.set_xlim(X[0,0], X[-1,-1])
ax.set_ylim(Y[0,0], Y[-1,-1])
ax.set_zlim(zlim)
ax.set_zlabel("Loss", rotation=90)
In [3]:
data = parse_whole_directory_monthwise("models/experiment08/")
In [4]:
min_loss = data.groupby(("depth", "width", "month")).min()
monthly_mean = min_loss.groupby(("depth", "width")).mean()
In [5]:
plot3d_loss(monthly_mean.loss, rotation=60)
plot3d_loss(monthly_mean.val_loss, rotation=60)
plt.show()
In [6]:
data1 = parse_whole_directory_monthwise("models/experiment08.1/")
min_loss1 = data1.groupby(("depth", "width", "month")).min()
monthly_mean1 = min_loss1.groupby(("depth", "width")).mean()
In [7]:
plot3d_loss(monthly_mean1.loss, rotation=60)
plot3d_loss(monthly_mean1.val_loss, rotation=60)
plt.show()
In [8]:
print(data.min())
print(data.idxmin())
In [9]:
print(data1.min())
print(data1.idxmin())
In [10]:
data1.groupby("month").min()
Out[10]:
In [11]:
data5 = parse_whole_directory_monthwise("models/experiment08.5/")
min_loss5 = data5.groupby(("depth", "width", "month")).min()
monthly_mean5 = min_loss5.groupby(("depth", "width")).mean()
In [12]:
plot3d_loss(monthly_mean5.loss, rotation=60)
plot3d_loss(monthly_mean5.val_loss, rotation=60)
plt.show()
In [13]:
min_loss5.groupby(("depth", "width")).mean().min()
Out[13]:
In [14]:
data6 = parse_whole_directory_monthwise("models/experiment08.6/")
min_loss6 = data6.groupby(("depth", "width", "month")).min()
monthly_mean6 = min_loss6.groupby(("depth", "width")).mean()
In [69]:
#plot3d_loss(monthly_mean6.loss, rotation=135)
plot3d_loss(monthly_mean6.val_loss, rotation=135)
plt.savefig("diff_yearly.pdf", format="pdf", dpi=300, bbox_inches="tight")
plt.show()
In [34]:
min_loss6.groupby(("depth", "width")).mean().min()
Out[34]:
In [38]:
min_loss6.groupby(("depth", "width")).mean()[min_loss6.groupby(("depth", "width")).mean().val_loss < 0.055]
Out[38]:
In [49]:
data6.groupby("month").min().plot()
plt.grid()
data6.groupby("month").min().plot()
plt.grid()
plt.show()
In [54]:
min_loss6.val_loss.groupby(("depth", "width")).mean().groupby("width").min().plot()
plt.show()
In [76]:
(data6.loss + data6.val_loss).groupby(("depth", "width", "month")).min().groupby(("depth", "width")).mean()
Out[76]:
In [18]:
data7 = parse_whole_directory_monthwise("models/experiment08.7/")
min_loss7 = data7.groupby(("depth", "width", "month")).min()
monthly_mean7 = min_loss7.groupby(("depth", "width")).mean()
In [19]:
plot3d_loss(monthly_mean7.loss, rotation=60)
plot3d_loss(monthly_mean7.val_loss, rotation=60)
plt.show()
In [20]:
min_loss7.groupby(("depth", "width")).mean().min()
Out[20]:
In [21]:
data8 = parse_whole_directory_monthwise("models/experiment08.8/")
min_loss8 = data8.groupby(("depth", "width", "month")).min()
monthly_mean8 = min_loss8.groupby(("depth", "width")).mean()
In [22]:
plot3d_loss(monthly_mean8.loss, rotation=60)
plot3d_loss(monthly_mean8.val_loss, rotation=60)
plt.show()
In [23]:
min_loss8.groupby(("depth", "width")).mean().min()
Out[23]:
In [24]:
data9 = parse_whole_directory_monthwise("models/experiment08.9/")
min_loss9 = data9.groupby(("depth", "width", "month")).min()
monthly_mean9 = min_loss9.groupby(("depth", "width")).mean()
In [25]:
plot3d_loss(monthly_mean9.loss, rotation=120)
plot3d_loss(monthly_mean9.val_loss, rotation=120)
plt.show()
In [26]:
min_loss9.groupby(("depth", "width")).mean().min()
Out[26]:
In [41]:
naive_prediction = []
naive_prediction_test = []
naive_prediction_train = []
for month in range(1, 13):
futures = FuturesByMonth("data/futures_per_year_and_month.h5", month, yearly=True, spreads=True)
(x_train, y_train), (x_val, y_val), (x_test, y_test) = futures.splitted_dataset()
mse = np.mean(np.square(x_val[:,month - 1] - y_val[:,0]))
naive_prediction.append(mse)
naive_prediction_test.append(np.mean(np.square(x_test[:,month - 1] - y_test[:,0])))
naive_prediction_train.append(np.mean(np.square(x_train[:,month - 1] - y_train[:,0])))
print(calendar.month_name[month], mse)
In [77]:
naive = pd.DataFrame([naive_prediction_train, naive_prediction, naive_prediction_test],
columns=calendar.month_name[1:], index=["Training", "Validation", "Test"]).T
naive.plot(figsize=(8,4))
plt.grid()
plt.xticks(np.arange(12), calendar.month_abbr[1:])
plt.title("Naive prediction MSE")
plt.show()
In [78]:
naive_prediction_test
Out[78]:
In [45]:
conct = pd.concat([#data.groupby("month").min().val_loss,
#data1.groupby("month").min().val_loss,
#data5.groupby("month").min().val_loss,
data6.groupby("month").min().val_loss,
#data7.groupby("month").min().val_loss,
#data8.groupby("month").min().val_loss,
#data9.groupby("month").min().val_loss,
pd.Series(naive_prediction, index=range(1, 13))], axis=1)
In [46]:
# Regardless of the model there are some months easier and some month harder to predict.
conct.plot(linewidth=2)
plt.legend((0, 1, 5, 6, 7, 8, 9, "naive"))
plt.show()
In [ ]: