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.

236

The goal of the exercise will be to estimate the slope and intercept values on the test set

solely using a trained linear regression model based on the training data. Scikit-Learn possesses

a clean API to carry this out, which will be demonstrated below.

The first task is to import the necessary libraries. Matplotlib and Seaborn are imported

for plotting. Importing Seaborn is not strictly necessary, but it does provide more aestheticallypleasing

plots. NumPy is imported to provide random number generation, while the linear_model

object is imported from Scikit-Learn:

# lin_reg_sklearn.py

import matplotlib.pyplot as plt

import numpy as np

import seaborn as sns

from sklearn import linear_model

In the __main__ function all of the parameters are set. The code is commented liberally, so

it should be straightforward to ascertain the parameters from the code:

# Create N values, with 80% used for training

# and 20% used for testing/evaluation

N = 500

split = int(0.8*N)

# Set the intercept and slope of the univariate

# linear regression simulated data

alpha = 2.0

beta = 3.0

# Set the mean and variance of the randomly

# distributed noise in the simulated dataset

eps_mu = 0.0

eps_sigma = 30.0

# Set the mean and variance of the X data

X_mu = 0.0

X_sigma = 10.0

The next step is to randomly simulate some data. Firstly an array of errors is created, which

is stored in the eps variable. Then normally-distributed feature values, x i , are created and used

to create linear responses y i with error. The final line X = X.reshape(-1, 1) is needed to

avoid a deprecation warning in earlier versions of Scikit-Learn. Leaving it out will actually cause

a ValueError in version 0.19. Be warned!

# Create the error/noise, X and y data

eps = np.random.normal(loc=eps_mu, scale=eps_sigma, size=N)

X = np.random.normal(loc=X_mu, scale=X_sigma, size=N)

y = alpha + beta*X + eps

X = X.reshape(-1, 1) # Needed to avoid deprecation warning

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

Saved successfully!

Ooh no, something went wrong!