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.

293

Figure 20.5: Test MSE curves for multiple k-fold cross-validation folds for a Linear Regression

with polynomial features of increasing degree

import datetime

import pprint

import numpy as np

import pandas as pd

import pandas_datareader.data as web

import pylab as plt

import sklearn

from sklearn.cross_validation import train_test_split, KFold

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import PolynomialFeatures

def create_lagged_series(symbol, start_date, end_date, lags=5):

"""

This creates a pandas DataFrame that stores

the percentage returns of the adjusted closing

value of a stock obtained from Yahoo Finance,

along with a number of lagged returns from the

prior trading days (lags defaults to 5 days).

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

Saved successfully!

Ooh no, something went wrong!