13.08.2022 Views

advanced-algorithmic-trading

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

292

..

We can plot these curves with the following function:

..

..

def plot_test_error_curves_kf(kf_dict, folds, degrees):

fig, ax = plt.subplots()

ds = range(1, degrees+1)

for i in range(1, folds+1):

ax.plot(

ds,

kf_dict["fold_%s" % i],

lw=2,

label=’Test MSE - Fold %s’ % i

)

..

..

ax.plot(

ds,

kf_dict["avg"],

linestyle=’--’,

color="black",

lw=3,

label=’Avg Test MSE’

)

ax.legend(loc=0)

ax.set_xlabel(’Degree of Polynomial Fit’)

ax.set_ylabel(’Mean Squared Error’)

fig.set_facecolor(’white’)

plt.show()

The output is given in Figure 20.5.

Notice that the variation among the error curves is much lower than for the validation set

approach (even accounting for the high value of Fold 4). This is the desired effect of carrying

out cross-validation.

Cross-validation generally provides a much better estimate of the true test MSE, at the expense

of some slight bias. This is usually an acceptable trade-off in machine learning applications.

20.2.7 Full Python Code

The full Python code for cross_validation.py is given below:

# cross_validation.py

from __future__ import print_function

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!