import numpy as np
import pandas as pd
from bqplot import pyplot as plt
from bqplot import GridHeatMap, Figure, ColorScale, ColorAxis, HeatMap
from ipywidgets import HBox
## get prices of Dow Jones constituents
index_prices = pd.read_csv('./data_files/indu_const_prices.csv', index_col=0, parse_dates=[0])
## get prices of index
price_data = pd.read_csv('./data_files/indu_prices.csv', index_col=0, parse_dates=[0])
returns_clean = price_data.pct_change()[1:].dropna(axis=1)
price_data = price_data.loc[returns_clean.index]
corr_mat = np.corrcoef(returns_clean.T)
def event_handler(name, value):
sel = price_line.selected
if sel is None or len(sel) < 2:
_corr = np.corrcoef(returns_clean.T)
else:
_corr = np.corrcoef(returns_clean.iloc[sel[0]:sel[-1], :].T)
corr_mat.color = _corr
col_sc = ColorScale(scheme='Reds', min=0., max=1.)
corr_mat = GridHeatMap(color=np.corrcoef(returns_clean.T), scales={'color': col_sc})
col_ax = ColorAxis(scale=col_sc, label='Correlation')
corr_fig = Figure(marks=[corr_mat], axes=[col_ax])
price_fig = plt.figure(title='DOW Jones IA Price Series')
price_line = plt.plot(index_prices.index.values, index_prices.values)
brush = plt.int_selector(event_handler)
plt.axes()['x'].tick_format = '%b-%Y'
plt.axes()['x'].num_ticks = 5
HBox([price_fig, corr_fig])
Failed to display Jupyter Widget of type HBox
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.