How to predict price movements using sklearn

We continue the price prediction example but now we binarize the outcome variable so that it takes on the value 1 whenever the 10-day return is positive and 0 otherwise; see the notebook logistic_regression.ipynb in the sub directory stock_price_prediction:

target = 'Returns10D'
label = (y[target] > 0).astype(int).to_frame(target)

With this new categorical outcome variable, we can now train a logistic regression using the default L2 regularization. For logistic regression, the regularization is formulated inversely to linear regression: higher values for λ imply less regularization and vice versa. We evaluate 11 parameter values using cross validation as follows:

nfolds = 250
Cs = np.logspace(-5, 5, 11)
scaler = StandardScaler()

logistic_results, logistic_coeffs = pd.DataFrame(), pd.DataFrame()
for C in Cs:
coeffs = []
log_reg = LogisticRegression(C=C)
for i, (train_dates, test_dates) in enumerate(time_series_split(dates, nfolds=nfolds)):
X_train = model_data.loc[idx[train_dates], features]
y_train = model_data.loc[idx[train_dates], target]
log_reg.fit(X=scaler.fit_transform(X_train), y=y_train)

X_test = model_data.loc[idx[test_dates], features]
y_test = model_data.loc[idx[test_dates], target]
y_pred = log_reg.predict_proba(scaler.transform(X_test))[:, 1]

coeffs.append(log_reg.coef_.squeeze())
logistic_results = (logistic_results
.append(y_test
.to_frame('actuals')
.assign(predicted=y_pred, C=C)))
logistic_coeffs[C] = np.mean(coeffs, axis=0)

We then use the roc_auc_score discussed in the previous chapter to compare the predictive accuracy across the various regularization parameters:

auc_by_C = logistic_results.groupby('C').apply(lambda x: roc_auc_score(y_true=x.actuals.astype(int), 
y_score=x.predicted))

We can again plot the AUC result for the range of hyperparameter values alongside the coefficient path that shows the improvements in predictive accuracy as the coefficients are a bit shrunk at the optimal regularization value 102:

AUC and Logistic Ridge path
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
54.211.203.45