価格比の対数
対数リターンは2つの価格の比の自然対数です。P_t / P_{t-1} - 1 の代わりに ln(P_t / P_{t-1}) を計算します。小さな変動ではほぼ同じ値になりますが(2%の上昇は対数で0.0198)、対数版には統計やモデリングの標準になっている性質があります。pandas では価格の Series 全体に np.log をかけてから diff を取ります。対数の引き算は価格の割り算だからです。
import numpy as np
import pandas as pd
prices = pd.Series([100.0, 102.0, 100.98, 103.0], index=["Mon", "Tue", "Wed", "Thu"])
log_ret = np.log(prices).diff()
print(log_ret.round(4))
# Mon NaN
# Tue 0.0198
# Wed -0.0101
# Thu 0.0198
# dtype: float64
print(prices.pct_change().round(4).tolist()) # [nan, 0.02, -0.01, 0.02] the simple returns, for comparison対数リターンは時間方向に足し算できる
これが使う最大の理由です。ln(a) - ln(c) = (ln(a) - ln(b)) + (ln(b) - ln(c)) なので、複数期間の対数リターンは期間ごとの対数リターンの合計にすぎず、複利計算は不要です。2日間のリターンは日次2つの和、月次は日次の和で、exp(total) - 1 で単純リターンに戻せます。対数リターンは対称でもあります。価格が2倍になって半分に戻ると、対数リターンは +0.6931 と -0.6931 で打ち消し合いますが、単純リターンは +100% と -50% でした。
# The sum of the daily log returns is the log return over the whole period
print(round(log_ret.sum(), 6)) # 0.029559
print(round(np.log(prices.iloc[-1] / prices.iloc[0]), 6)) # 0.029559 end price over start price
# exp(total) - 1 is the simple return of the whole period
print(round(np.exp(log_ret.sum()) - 1, 6)) # 0.03# Symmetry: the price doubles, then halves
doubling = pd.Series([100.0, 200.0, 100.0])
print(doubling.pct_change().tolist()) # [nan, 1.0, -0.5]
print(np.log(doubling).diff().round(4).tolist()) # [nan, 0.6931, -0.6931]変換と複数日の値動き
単純リターンに戻すには np.exp(r) - 1 を使います(極めて小さな値では np.expm1(r) のほうが正確です)。小さな変動ではよく一致し、大きな変動では差が開きます。単純リターン +50% は対数で 0.4055 です。np.log と np.exp は Series 全体に働くのでループは要りません。複数日の値動きを測るには、diff に期間数を渡します。np.log(prices).diff(5) は5日間の対数リターンで、日次の対数リターン5つの合計に等しくなります。
# Small moves agree with the simple return, big moves do not
for simple in [0.01, 0.1, 0.5, -0.5]:
print(simple, round(np.log1p(simple), 4)) # log1p(r) is ln(1 + r)
# 0.01 0.01
# 0.1 0.0953
# 0.5 0.4055
# -0.5 -0.6931
# Back to simple returns, on the whole Series at once
print((np.exp(log_ret) - 1).round(4).tolist()) # [nan, 0.02, -0.01, 0.02]
# The log return over 2 days: diff takes a number of periods
print(np.log(prices).diff(2).round(4).tolist()) # [nan, nan, 0.0098, 0.0098]- 対数リターンは
ln(価格 / 前の価格):np.log(prices).diff()。最初の要素はNaN - 時間方向では単純に足せる:
log_ret.sum()。np.exp(total) - 1で単純リターンになる diff(k)はk期間の値動きを返す。資産間の加重は対数ではなく単純リターンで行う
次は DatetimeIndex と時間によるインデックスです。価格の系列に本物の日付を持たせ、月や曜日で切り出します。
ラボ:採点される内容
log_returnsで価格を対数リターンに変換する25点simple_from_logで対数リターンを単純リターンに戻す25点total_log_returnで期間ごとの対数リターンを合計する25点multi_day_log_returnで複数日の値動きを測る25点