<jupyter_start><jupyter_text>Graphics.<jupyter_code>import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import pandas as pd
from numpy import (
    array,
    column_stack,
    dot,
    hstack,
    logical_and,
    mean,
    ones,
    sqrt,
    squeeze,
    var,
    zeros,
)
from numpy.linalg import lstsq

# Loading and transforming the data
sp500_data = pd.read_csv("sp500.csv", index_col="Date", parse_dates=True)
ftse_data = pd.read_csv("ftse.csv", index_col="Date", parse_dates=True)
sp500 = sp500_data["Adj Close"]
sp500_dates = sp500_data.index
ftse = ftse_data["Adj Close"]
ftse_dates = ftse_data.index<jupyter_output><empty_output><jupyter_text>Exercise 1<jupyter_code>fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(sp500_dates, sp500)
fmt = mdates.DateFormatter("%Y")
ax.xaxis.set_major_formatter(fmt)
fig.autofmt_xdate()
plt.draw()<jupyter_output><empty_output><jupyter_text>Exercise 2<jupyter_code>sp500_rets = sp500.resample("W-FRI").last().pct_change().dropna()
ftse_rets = ftse.resample("W-FRI").last().pct_change().dropna()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(sp500_rets)
plt.draw()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(sp500_rets, bins=20)
plt.draw()<jupyter_output><empty_output><jupyter_text>Exercise 3<jupyter_code>bands = [(-1.0, -0.02), (-0.02, 0.0), (0.0, 0.02), (0.02, 1.0)]
percs = zeros(len(bands))
i = 0
for b in bands:
    percs[i] = mean(logical_and(sp500_rets > b[0], sp500_rets <= b[1]))
    i += 1
fig = plt.figure()
ax = fig.add_subplot(111)
labels = ["<-2%", ">-2% and <0", ">0 and <2%", ">2%"]
ax.pie(percs, labels=labels)
plt.draw()<jupyter_output><empty_output><jupyter_text>Exercise 4<jupyter_code>fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(sp500_rets, ftse_rets)
ax.set_xlabel("S&P 500 returns")
ax.set_ylabel("ftse 100 returns")
plt.draw()<jupyter_output><empty_output><jupyter_text>Exercise 5<jupyter_code>x = column_stack((ones(sp500_rets.shape), sp500_rets))
y = column_stack((ftse_rets,))
x, y
out = lstsq(x, y, rcond=None)
b = out[0]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(sp500_rets, ftse_rets)
ax.set_xlabel("S&P 500 returns")
ax.set_ylabel("FTSE 100 returns")
x = ax.get_xlim()
x = array(x)
x.shape = -1, 1
x = hstack((ones(x.shape), x))
fit = dot(x, b)
b = squeeze(b)
ax.plot(x[:, 1], fit, color="#800000")
ax.text(0, -0.15, f"y = {b[0]:0.2f} + {b[1]:0.2f}x")
plt.draw()<jupyter_output><empty_output><jupyter_text>Exercise 6<jupyter_code>T = sp500_rets.size
ewma = zeros((T, 2))
r = column_stack((sp500_rets, ftse_rets))
ewma[0] = var(r, axis=0)
for i in range(1, T):
    ewma[i] = 0.97 * ewma[i - 1] + 0.03 * r[i - 1] ** 2
ewma = 100 * sqrt(252 * ewma)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(sp500_rets.index, ewma[:, 0], label="S&P 500 ewma Vol (Annualized)")
ax.plot(ftse_rets.index, ewma[:, 1], label="ftse 100 ewma Vol (Annualized)")
ax.legend(loc=0)
ax.set_title("Annualized Volatility (%)")
fmt = mdates.DateFormatter("%Y")
ax.xaxis.set_major_formatter(fmt)
fig.autofmt_xdate()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17923920051579625, 'fraction_numerical': 0.05867182462927144, 'mean_word_length': 5.424430641821946, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '145478', 'license': 'CC0-1.0', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1372, 'n_words': 240, 'path': 'solutions/chapter15/graphics_solutions.ipynb', 'repo_name': 'bashtage/python-for-econometrics-statistics-data-analysis', 'size': '3102'}