区間ごとに数える
平均と標準偏差は1000個のリターンを2つの数に押し込めます。ヒストグラムは形を残します。値の範囲を区間(ビン)に区切り、各区間に入る観測数を数えます。中央が高く両端が低ければおなじみの釣鐘型で、偏った形や裾の重い形は警告です。np.histogram(values, bins=edges) は配列全体を処理して度数を返します。区間 i は edges[i] 以上 edges[i + 1] 未満の値を取り、最後の区間だけは右端も含みます。
import numpy as np
returns = np.array([-0.021, -0.012, -0.008, -0.004, -0.002, 0.000, 0.001, 0.003, 0.004, 0.006, 0.009, 0.015, 0.032])
edges = np.array([-0.03, -0.01, 0.0, 0.01, 0.03]) # four bins: -3%..-1%, -1%..0%, 0%..1%, 1%..3%
counts, _ = np.histogram(returns, bins=edges)
print(counts.tolist(), counts.sum(), len(returns)) # [2, 3, 6, 1] 12 13 the 3.2% return is outside every bin
# bins=4 asks numpy for 4 equal-width bins between the minimum and the maximum
counts, edges4 = np.histogram(returns, bins=4)
print(counts.tolist(), edges4.round(4).tolist()) # [3, 6, 3, 1] [-0.021, -0.0078, 0.0055, 0.0188, 0.032]度数か密度か
度数は観測数と区間の幅に左右されるので、標本サイズの違う2つのヒストグラムは比べられません。密度はこれを解決します。各度数を数えた合計と区間の幅で割り、棒の面積の合計が1になるようにします。すると、正規分布の釣鐘のような理論曲線や別の標本と比較できます。np.histogram(values, bins=edges, density=True) がそれを行い、counts / (counts.sum() * np.diff(edges)) でも同じです。
counts, _ = np.histogram(returns, bins=edges)
density = counts / (counts.sum() * np.diff(edges))
print(density.round(2).tolist()) # [8.33, 25.0, 50.0, 4.17]
print((density * np.diff(edges)).sum()) # 1.0 the areas add up to 1描画と裾の読み取り
ax.hist(values, bins=10) は数えて描くまでを1回で行い、区間ごとに棒が1本(ax.patches)でき、その高さが度数です。軸には必ずラベルを付けましょう。「日次リターン」という表示のないリターンのヒストグラムは意味を持ちません。大切なのは形で、痛いのは裾です。平均から標準偏差の k 倍より離れた観測の割合 np.mean(np.abs(x - mean) > k * std) を数えれば、極端な値がどれだけ起きるかがわかります。正規分布では約4.6%が2標準偏差の外、0.27%が3標準偏差の外にあります。市場はそれより悪く、次のトピックで見ます。
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.hist(returns, bins=5)
ax.set_xlabel("Daily return")
ax.set_ylabel("Frequency")
print(len(ax.patches), [round(p.get_height()) for p in ax.patches]) # 5 [2, 4, 5, 1, 1] one bar per bin; the heights are the counts
# How often are the returns more than 1 standard deviation from the mean?
mean, std = returns.mean(), returns.std(ddof=1)
print(round(float(np.mean(np.abs(returns - mean) > 1 * std)), 3)) # 0.308 about 4 of 13 days# The number of bins changes the picture: compare a few
for bins in (2, 4, 8):
print(bins, np.histogram(returns, bins=bins)[0].tolist())
# 2 [9, 4]
# 4 [3, 6, 3, 1]
# 8 [1, 2, 2, 4, 2, 1, 0, 1]np.histogram(values, bins=edges)は各区間の値を数える。最後の区間は右端を含む- 密度は数えた合計と区間の幅で割る。面積の合計が1になり、標本どうしを比較できる
ax.hist(values, bins=n)で描き、np.mean(np.abs(x - mean) > k * std)で裾を測る。軸にラベルを付け、同じ境界で比べる
次はリターンの平均と分散です。分布を要約する2つの数を、定義から計算します。
ラボ:採点される内容
bin_countsで各区間のリターン数を数える25点densityで度数を密度に変換する25点fraction_beyondで裾の割合を測る25点plot_histogramでヒストグラムを描く25点