The best bid is $180.02 and the best offer is $180.03.
import yfinance as yf
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style("whitegrid")
price = yf.download("IEF", start=1990)["Adj Close"]
price.plot()
plt.ylabel("IEF")
plt.show()
[*********************100%%**********************] 1 of 1 completed
import pandas as pd
df = pd.read_csv(
'https://www.dropbox.com/s/hgwte6swx57jqcv/nominal_sbb.csv?dl=1',
index_col="Year"
)
means = df.Treasuries.rolling(20).mean()
means.plot()
plt.ylabel("Trailing 20-Year Mean")
plt.show()
df.head()
S&P 500 | TBills | Treasuries | Corporates | |
---|---|---|---|---|
Year | ||||
1928 | 0.438112 | 0.0308 | 0.008355 | 0.032196 |
1929 | -0.082979 | 0.0316 | 0.042038 | 0.030179 |
1930 | -0.251236 | 0.0455 | 0.045409 | 0.005398 |
1931 | -0.438375 | 0.0231 | -0.025589 | -0.156808 |
1932 | -0.086424 | 0.0107 | 0.087903 | 0.235896 |
means = df.Treasuries.rolling(40).mean()
means.plot()
plt.ylabel("Trailing 40-Year Mean")
plt.show()
corrs = []
for i in range(20, len(df.index)):
corr = df.iloc[(i-20):i]["S&P 500"].corr(df.Treasuries)
corrs.append(corr)
plt.plot(df.index[20:], corrs)
[<matplotlib.lines.Line2D at 0x1fdf64aa290>]
import numpy as np
rf = 0.03
mu = [0.04, 0.10, 0.10]
stdevs = [0.2, 0.2, 0.2]
corrs = [
[1., 0., 0.8],
[0., 1., 0.3],
[0.8, 0.3, 1.]
]
Sigma = np.diag(stdevs) @ corrs @ np.diag(stdevs)
# example target expected return
r = 0.08
P = Sigma
q = np.zeros(3).reshape(3, 1)
A = (mu - rf*np.ones(3)).reshape(1, 3)
b = np.array([r-rf]).reshape(1, 1)
from cvxopt import matrix
from cvxopt.solvers import qp
sol = qp(
P=matrix(P),
q=matrix(q),
A=matrix(A),
b=matrix(b)
)
pd.Series(sol["x"], index=range(1, 4)).round(3)
1 -0.497 2 0.109 3 0.676 dtype: float64
G = -np.identity(3)
h = np.zeros((3, 1))
sol = qp(
P=matrix(P),
q=matrix(q),
G=matrix(G),
h=matrix(h),
A=matrix(A),
b=matrix(b)
)
pd.Series(sol["x"], index=range(1, 4)).round(3)
pcost dcost gap pres dres 0: 7.0157e-03 -7.5505e-01 8e-01 0e+00 2e+00 1: 7.0089e-03 -1.4634e-03 8e-03 8e-17 3e-02 2: 6.7723e-03 6.5362e-03 2e-04 1e-17 3e-04 3: 6.6347e-03 6.6316e-03 3e-06 8e-17 4e-06 4: 6.6327e-03 6.6326e-03 3e-08 8e-17 4e-08 Optimal solution found.
1 0.000 2 0.357 3 0.357 dtype: float64