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.

255

The same approach is carried out for Random Forests. However, since Random Forests

implicitly use a regression tree as their base estimators, there is no need to specify it in the

ensemble constructor:

# Estimate the Random Forest MSE over the full number

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

for i in range(0, axis_step):

print("Random Forest Estimator: %d of %d..." % (

step_factor*(i+1), n_estimators)

)

rf = RandomForestRegressor(

n_estimators=step_factor*(i+1),

n_jobs=n_jobs,

random_state=random_state

)

rf.fit(X_train, y_train)

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

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

rf_mse[i] = mse

Similarly for the AdaBoost boosting algorithm, with the exception that n_jobs is not present

due to the lack of parallelisability of boosting techniques. The learning rate, or shrinkage factor,

λ has been set to 0.01. Adjusting this value has a large impact on the absolute MSE calculated

for each estimator total:

# Estimate the AdaBoost MSE over the full number

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

for i in range(0, axis_step):

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

step_factor*(i+1), n_estimators)

)

boosting = AdaBoostRegressor(

DecisionTreeRegressor(),

n_estimators=step_factor*(i+1),

random_state=random_state,

learning_rate=0.01

)

boosting.fit(X_train, y_train)

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

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

boosting_mse[i] = mse

The final snippet of code simply plots these arrays against each other using Matplotlib but

with Seaborn’s default colour scheme. This is more visually pleasing than the Matplotlib defaults:

# Plot the chart of MSE versus number of estimators

plt.figure(figsize=(8, 8))

plt.title(’Bagging, Random Forest and Boosting comparison’)

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

Saved successfully!

Ooh no, something went wrong!