Created
May 20, 2020 12:47
-
-
Save jitenspin/de828f2e7426eaa16761b7ab8b78d79f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import numpy as np | |
import csv | |
def yahoo_finance_close_chart(filename, label=None): | |
with open(filename) as f: | |
reader = csv.reader(f, delimiter=",", doublequote=True, | |
lineterminator="\r\n", quotechar='"', skipinitialspace=True) | |
rows = [row for row in reader] | |
rows = rows[1:] | |
series = [float(row[4]) for row in rows] | |
returns = [] | |
for i in range(len(series)): | |
if i != 0: | |
returns.append(np.log(series[i] / series[i - 1])) | |
return returns | |
def correlation(xs, ys): | |
return np.corrcoef(np.array([xs, ys]))[0][1] | |
spy = yahoo_finance_close_chart('./SPY.csv', 'SPY') | |
tlt = yahoo_finance_close_chart('./TLT.csv', 'TLT') | |
assert(len(tlt) == len(spy)) | |
""" | |
xs = [] | |
ys = [] | |
co = [] | |
for i in range(len(tlt)): | |
if i != 0 and i % 100 == 0: | |
co.append(correlation(xs, ys)) | |
xs = [] | |
ys = [] | |
xs.append(spy[i]) | |
ys.append(tlt[i]) | |
""" | |
# SPY のみ | |
co = [] | |
xs = [] | |
for i in range(len(tlt)): | |
if i != 0 and i % 20 == 0: | |
co.append(np.std(xs)) | |
xs = [] | |
xs.append(spy[i]) # * 0.5 + tlt[i] * 0.5) | |
# SPY と TLT が5割5割 | |
co2 = [] | |
xs = [] | |
for i in range(len(tlt)): | |
if i != 0 and i % 20 == 0: | |
co2.append(np.std(xs)) | |
xs = [] | |
xs.append(spy[i] * 0.5 + tlt[i] * 0.5) | |
# TLT のみ | |
co3 = [] | |
xs = [] | |
for i in range(len(tlt)): | |
if i != 0 and i % 20 == 0: | |
co3.append(np.std(xs)) | |
xs = [] | |
xs.append(tlt[i]) | |
# 今月の分散が最小になるように今月の比率を決める (現実には不可能) | |
co4 = [] | |
xss = [] | |
for r in range(101): | |
xss.append([]) | |
for i in range(len(tlt)): | |
if i != 0 and i % 20 == 0: | |
co4.append(min([ np.std(xs) for xs in xss ])) | |
xss = [] | |
for r in range(101): | |
xss.append([]) | |
for r in range(101): | |
xss[r].append(spy[i] * r / 100 + tlt[i] * (1 - r / 100)) | |
# 今月の分散が最小になるように次の月の比率を決める | |
co5 = [] | |
xss = [] | |
for r in range(101): | |
xss.append([]) | |
nextr = 50 | |
for i in range(len(tlt)): | |
if i != 0 and i % 20 == 0: | |
stds = [ np.std(xs) for xs in xss ] | |
co5.append(stds[nextr]) | |
for r in range(101): | |
stds[r] | |
if stds[r] < stds[nextr]: | |
nextr = r | |
xss = [] | |
for r in range(101): | |
xss.append([]) | |
for r in range(101): | |
xss[r].append(spy[i] * r / 100 + tlt[i] * (1 - r / 100)) | |
plt.plot(co, label="SPY : TLT = 10 : 0") | |
plt.plot(co2, label="SPY : TLT = 5 : 5") | |
plt.plot(co3, label="SPY : TLT = 0 : 10") | |
plt.plot(co4, label="minimum variance weight by this month (impossible)") | |
plt.plot(co5, label="minimum variance weight by previous month") | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment