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.

56

5.5 Full Code

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import pymc3 as pm

import seaborn as sns

sns.set(style="darkgrid", palette="muted")

def simulate_linear_data(N, beta_0, beta_1, eps_sigma_sq):

"""

Simulate a random dataset using a noisy

linear process.

N: Number of data points to simulate

beta_0: Intercept

beta_1: Slope of univariate predictor, X

"""

# Create a pandas DataFrame with column ’x’ containing

# N uniformly sampled values between 0.0 and 1.0

df = pd.DataFrame(

{"x":

np.random.RandomState(42).choice(

map(

lambda x: float(x)/100.0,

np.arange(N)

), N, replace=False

)

}

)

# Use a linear model (y ~ beta_0 + beta_1*x + epsilon) to

# generate a column ’y’ of responses based on ’x’

eps_mean = 0.0

df["y"] = beta_0 + beta_1*df["x"] + np.random.RandomState(42).normal(

eps_mean, eps_sigma_sq, N

)

return df

def glm_mcmc_inference(df, iterations=5000):

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

Saved successfully!

Ooh no, something went wrong!