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.

259

amzn = create_lagged_series("AMZN", start, end, lags=3)

amzn.dropna(inplace=True)

# Use the first three daily lags of AMZN closing prices

# and scale the data to lie within -1 and +1 for comparison

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

y = amzn["Today"]

X = scale(X)

y = scale(y)

# 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

)

# 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)

# 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

# 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)

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

Saved successfully!

Ooh no, something went wrong!