13.08.2022 Views

advanced-algorithmic-trading

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

254

X = amzn[["Lag1", "Lag2", "Lag3"]]

y = amzn["Today"]

X = scale(X)

y = scale(y)

The data is split into a training set and a test set, with 70% of the data forming the training

data and the remaining 30% performing the test set. Once again, note that financial time series

data is serially correlated, so this procedure will introduce some error by not accounting for it,

however it serves the purpose for comparison across procedures in this chapter:

# Use the training-testing split with 70% of data in the

# training data with the remaining 30% of data in the testing

X_train, X_test, y_train, y_test = train_test_split(

X, y, test_size=0.3, random_state=random_state

)

The following NumPy arrays store the number of estimators at each axis step, as well as the

actual associated MSE for each of the three ensemble methods. They are all initially set to zero

and are filled in subsequently:

# Pre-create the arrays which will contain the MSE for

# each particular ensemble method

estimators = np.zeros(axis_step)

bagging_mse = np.zeros(axis_step)

rf_mse = np.zeros(axis_step)

boosting_mse = np.zeros(axis_step)

The first ensemble method to be utilised is the bagging procedure. The code iterates over

the total number of estimators (1-1000 in this case, with a step-size of 10), defines the ensemble

model with the correct base model (in this case a regression tree), fits it to the training data

and then calculates the Mean Squared Error on the test data. This MSE is then added to the

bagging MSE array:

# Estimate the Bagging MSE over the full number

# of estimators, across a step size ("step_factor")

for i in range(0, axis_step):

print("Bagging Estimator: %d of %d..." % (

step_factor*(i+1), n_estimators)

)

bagging = BaggingRegressor(

DecisionTreeRegressor(),

n_estimators=step_factor*(i+1),

n_jobs=n_jobs,

random_state=random_state

)

bagging.fit(X_train, y_train)

mse = mean_squared_error(y_test, bagging.predict(X_test))

estimators[i] = step_factor*(i+1)

bagging_mse[i] = mse

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

Saved successfully!

Ooh no, something went wrong!