Skip to content

Instantly share code, notes, and snippets.

@netsatsawat
Created September 21, 2020 11:59
Show Gist options
  • Save netsatsawat/e2a6153c12738a5864f7940f1569389c to your computer and use it in GitHub Desktop.
Save netsatsawat/e2a6153c12738a5864f7940f1569389c to your computer and use it in GitHub Desktop.
Python script to generate the stock time series data specifically for Hidden Markov Model example
import numpy as np
import pandas as pd
import pandas_datareader.data as web
import scipy.stats as scs
import matplotlib as mpl
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.dates import YearLocator, MonthLocator
import seaborn as sns
import os, gc, sys
import datetime
SEED = 515
mkt = 'GE' # stock to watch
f1 = 'TEDRATE' # ted spread (ref: https://fred.stlouisfed.org/series/TEDRATE)
f2 = 'T10Y2Y' # constant maturity ten yer - 2 year
f3 = 'T10Y3M' # constant maturity 10yr - 3m
f4 = 'BAMLHYH0A0HYM2TRIV' # ICE BofA US High Yield Index Total Return Index Value
start = datetime.date(2010, 1, 1)
end = datetime.date.today()
mkt_df = web.DataReader([mkt], 'yahoo', start, end)['Adj Close']\
.rename(columns={mkt: mkt})\
.assign(sret=lambda x: np.log(x[mkt] / x[mkt].shift(1)))\
.dropna()
data_df = web.DataReader([f1, f2, f3, f4], 'fred', start, end)\
.join(mkt_df, how='inner')\
.dropna()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment