In [20]:
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

In [21]:
mpl.rcParams["figure.figsize"] = 16, 9

In [22]:
data = parse_whole_directory_monthwise("models/experiment07/")

In [23]:
min_loss = data.groupby(("depth", "width", "month")).min()

In [24]:
monthly_mean = min_loss.groupby(("depth", "width")).mean()
monthly_std = min_loss.groupby(("depth", "width")).std()

In [25]:
monthly_temp = monthly_mean.join(monthly_std, rsuffix="_std")
monthly_temp[["loss", "loss_std"]].plot()
monthly_temp[["val_loss", "val_loss_std"]].plot()
plt.show()



In [26]:
print(monthly_temp.to_string())


                 loss  val_loss  loss_std  val_loss_std
depth width                                            
1     3      0.171867  0.157421  0.090178      0.089349
      6      0.130683  0.133865  0.058773      0.071299
      9      0.107800  0.136845  0.040401      0.059789
      12     0.093887  0.102733  0.041752      0.053856
      15     0.077667  0.121210  0.026561      0.063690
      18     0.076914  0.134941  0.028609      0.062674
      21     0.070039  0.112888  0.023558      0.049854
      24     0.061648  0.126027  0.021095      0.062107
      27     0.063057  0.118898  0.023643      0.052969
      30     0.056849  0.117584  0.019845      0.057529
3     3      0.169473  0.196165  0.095373      0.187970
      6      0.091796  0.143079  0.040809      0.080561
      9      0.051801  0.130402  0.015498      0.071286
      12     0.033572  0.132723  0.013145      0.066212
      15     0.024149  0.128547  0.008395      0.068696
      18     0.015301  0.131992  0.004855      0.083562
      21     0.010660  0.113688  0.002426      0.054227
      24     0.008519  0.112580  0.002953      0.058450
      27     0.006952  0.117061  0.002372      0.058625
      30     0.005332  0.110895  0.001517      0.062265
5     3      0.269018  0.147478  0.384630      0.083226
      6      0.095056  0.133972  0.054166      0.078751
      9      0.036371  0.112205  0.014605      0.062345
      12     0.020094  0.111966  0.006164      0.049899
      15     0.012479  0.120109  0.003535      0.056564
      18     0.008125  0.124039  0.001970      0.057017
      21     0.005855  0.110464  0.001657      0.051739
      24     0.004163  0.106460  0.001611      0.056313
      27     0.002864  0.115005  0.000982      0.069430
      30     0.002291  0.112879  0.001060      0.057211
7     3      0.255098  0.158407  0.292518      0.113065
      6      0.069694  0.128274  0.023154      0.058011
      9      0.032618  0.130146  0.013475      0.070174
      12     0.016706  0.108523  0.005936      0.052490
      15     0.010633  0.123609  0.005117      0.066746
      18     0.005815  0.112769  0.001613      0.062405
      21     0.004341  0.116122  0.001891      0.060439
      24     0.002712  0.113635  0.000905      0.060636
      27     0.002347  0.112372  0.000915      0.057314
      30     0.001765  0.111038  0.000958      0.062082
9     3      0.329045  0.257109  0.385003      0.291425
      6      0.085103  0.125111  0.045707      0.064284
      9      0.033364  0.122711  0.011737      0.075117
      12     0.015552  0.113752  0.003655      0.069072
      15     0.008698  0.114903  0.004007      0.059010
      18     0.005362  0.108260  0.001810      0.049564
      21     0.003882  0.113037  0.001448      0.058893
      24     0.002653  0.114212  0.000914      0.057684
      27     0.002297  0.113355  0.001029      0.070289
      30     0.001745  0.111629  0.000957      0.049596
11    3      0.255970  0.290622  0.107579      0.278759
      6      0.192078  0.130993  0.403847      0.083290
      9      0.035709  0.127331  0.010191      0.069791
      12     0.017052  0.110755  0.005934      0.048711
      15     0.008887  0.123769  0.002865      0.084225
      18     0.005113  0.132274  0.001530      0.077875
      21     0.004132  0.120892  0.001537      0.074466
      24     0.002483  0.106363  0.001180      0.057999
      27     0.002420  0.107898  0.001593      0.065213
      30     0.001865  0.102473  0.001301      0.050524
13    3      0.437867  0.302319  0.414667      0.276857
      6      0.133514  0.136759  0.112871      0.076107
      9      0.036133  0.126931  0.019482      0.083703
      12     0.016726  0.130180  0.005203      0.076610
      15     0.009335  0.124406  0.002112      0.065984
      18     0.005977  0.114939  0.002524      0.068375
      21     0.004266  0.103962  0.001604      0.056006
      24     0.003235  0.118238  0.001564      0.064808
      27     0.002414  0.111473  0.001314      0.058387
      30     0.001812  0.110739  0.000926      0.054865
15    3      0.439235  0.258961  0.418096      0.245530
      6      0.092865  0.162618  0.057757      0.104981
      9      0.036867  0.121043  0.015088      0.061262
      12     0.019700  0.124347  0.008494      0.078013
      15     0.009625  0.106826  0.004147      0.052059
      18     0.006188  0.115719  0.001803      0.059878
      21     0.004430  0.126170  0.001883      0.064076
      24     0.002937  0.110406  0.001505      0.057980
      27     0.002103  0.110588  0.001187      0.053766
      30     0.002153  0.108786  0.001168      0.060376
17    3      0.421233  0.290154  0.429037      0.283247
      6      0.342950  0.159796  0.470538      0.103563
      9      0.055349  0.128750  0.051298      0.063509
      12     0.034836  0.117115  0.055372      0.053298
      15     0.010742  0.126707  0.003184      0.067850
      18     0.005702  0.113365  0.002223      0.059337
      21     0.004576  0.113015  0.001950      0.068175
      24     0.003300  0.117553  0.001378      0.063264
      27     0.002638  0.102883  0.001254      0.052490
      30     0.002641  0.106507  0.002092      0.055422
19    3      0.476080  0.303409  0.398600      0.276278
      6      0.215085  0.211659  0.137548      0.244700
      9      0.045856  0.143126  0.038581      0.086543
      12     0.016106  0.138761  0.003778      0.087650
      15     0.011801  0.112740  0.005252      0.070220
      18     0.006201  0.112885  0.002484      0.053519
      21     0.004845  0.107326  0.002169      0.071060
      24     0.003781  0.108444  0.002066      0.056697
      27     0.002679  0.117284  0.001331      0.069234
      30     0.002456  0.105852  0.001838      0.059152

In [27]:
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=(8, 8))
    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 [28]:
plot3d_loss(monthly_mean["loss"], rotation=60)
plt.title("Training loss")
plot3d_loss(monthly_mean["val_loss"], rotation=60)
plt.title("Validation loss")
plt.show()



In [29]:
min_loss.groupby("month").min().plot()
plt.xticks(np.arange(1,13), calendar.month_abbr[1:])
plt.grid()
plt.title("Minimum losses by month")
plt.show()


Experiment 7.1

Don't include VIX


In [30]:
data1 = parse_whole_directory_monthwise("models/experiment07.1/")

In [31]:
min_loss1 = data1.groupby(("depth", "width", "month")).min()
monthly_mean1 = min_loss1.groupby(("depth", "width")).mean()
monthly_std1 = min_loss1.groupby(("depth", "width")).std()

In [32]:
plot3d_loss(monthly_mean1["loss"], rotation=60)
plt.title("Training loss")
plot3d_loss(monthly_mean1["val_loss"], rotation=60)
plt.title("Validation loss")
plt.show()



In [33]:
monthly_temp1 = monthly_mean1.join(monthly_std1, rsuffix="_std")
monthly_temp1[["loss", "loss_std"]].plot()
monthly_temp1[["val_loss", "val_loss_std"]].plot()
plt.show()



In [34]:
min_loss1.groupby("month").min().plot()
plt.xticks(np.arange(1,13), calendar.month_abbr[1:])
plt.grid()
plt.title("Minimum losses by month")
plt.show()