%run "Retropy_framework.ipynb"
Installing requirements .. Done
from pyfinance import *
>>> s = np.random.randn(400) / 100 + 0.0008
>>> idx = pd.date_range(start='2016', periods=len(s)) # default daily freq.
>>> ts = TSeries(s, index=idx)
ts
2016-01-01 0.01 2016-01-02 0.01 2016-01-03 0.02 2016-01-04 0.01 2016-01-05 -0.00 2016-01-06 -0.02 2016-01-07 0.00 2016-01-08 -0.00 2016-01-09 0.02 2016-01-10 0.01 2016-01-11 -0.01 2016-01-12 -0.02 2016-01-13 0.01 2016-01-14 0.01 2016-01-15 0.02 2016-01-16 0.01 2016-01-17 -0.01 2016-01-18 -0.01 2016-01-19 0.00 2016-01-20 0.01 2016-01-21 0.02 2016-01-22 0.01 2016-01-23 0.00 2016-01-24 -0.00 2016-01-25 0.01 2016-01-26 -0.01 2016-01-27 -0.01 2016-01-28 -0.00 2016-01-29 -0.00 2016-01-30 0.01 2016-01-31 -0.01 2016-02-01 -0.01 2016-02-02 0.01 2016-02-03 -0.00 2016-02-04 0.00 2016-02-05 -0.01 2016-02-06 -0.00 2016-02-07 0.00 2016-02-08 0.00 2016-02-09 0.02 2016-02-10 -0.00 2016-02-11 -0.01 2016-02-12 -0.00 2016-02-13 -0.00 2016-02-14 -0.01 2016-02-15 -0.01 2016-02-16 -0.01 2016-02-17 -0.01 2016-02-18 0.01 2016-02-19 0.01 2016-02-20 0.01 2016-02-21 -0.01 2016-02-22 -0.01 2016-02-23 0.01 2016-02-24 0.01 2016-02-25 0.02 2016-02-26 -0.00 2016-02-27 0.02 2016-02-28 -0.00 2016-02-29 -0.01 2016-03-01 0.01 2016-03-02 -0.01 2016-03-03 0.01 2016-03-04 -0.00 2016-03-05 0.00 2016-03-06 -0.01 2016-03-07 0.00 2016-03-08 -0.01 2016-03-09 -0.03 2016-03-10 0.01 2016-03-11 0.01 2016-03-12 -0.00 2016-03-13 -0.00 2016-03-14 -0.01 2016-03-15 0.01 2016-03-16 0.01 2016-03-17 0.00 2016-03-18 0.01 2016-03-19 -0.01 2016-03-20 -0.00 2016-03-21 -0.01 2016-03-22 0.01 2016-03-23 -0.00 2016-03-24 0.01 2016-03-25 -0.00 2016-03-26 0.00 2016-03-27 -0.00 2016-03-28 0.00 2016-03-29 -0.00 2016-03-30 -0.00 2016-03-31 -0.01 2016-04-01 -0.01 2016-04-02 -0.00 2016-04-03 0.00 2016-04-04 -0.00 2016-04-05 -0.01 2016-04-06 0.01 2016-04-07 0.00 2016-04-08 0.00 2016-04-09 0.02 2016-04-10 0.01 2016-04-11 0.00 2016-04-12 0.00 2016-04-13 -0.01 2016-04-14 0.00 2016-04-15 -0.03 2016-04-16 0.01 2016-04-17 0.00 2016-04-18 0.00 2016-04-19 0.01 2016-04-20 -0.00 2016-04-21 0.01 2016-04-22 -0.01 2016-04-23 -0.01 2016-04-24 0.01 2016-04-25 0.01 2016-04-26 -0.01 2016-04-27 0.01 2016-04-28 -0.01 2016-04-29 0.01 2016-04-30 0.01 2016-05-01 -0.01 2016-05-02 -0.01 2016-05-03 -0.01 2016-05-04 0.01 2016-05-05 0.01 2016-05-06 -0.00 2016-05-07 0.00 2016-05-08 0.00 2016-05-09 -0.01 2016-05-10 0.01 2016-05-11 0.00 2016-05-12 0.01 2016-05-13 -0.00 2016-05-14 -0.00 2016-05-15 -0.00 2016-05-16 -0.00 2016-05-17 -0.00 2016-05-18 -0.01 2016-05-19 0.00 2016-05-20 -0.00 2016-05-21 0.03 2016-05-22 0.00 2016-05-23 -0.03 2016-05-24 -0.00 2016-05-25 0.02 2016-05-26 0.01 2016-05-27 0.02 2016-05-28 0.02 2016-05-29 -0.00 2016-05-30 0.00 2016-05-31 -0.01 2016-06-01 -0.01 2016-06-02 0.00 2016-06-03 0.00 2016-06-04 0.00 2016-06-05 -0.00 2016-06-06 0.01 2016-06-07 -0.01 2016-06-08 0.00 2016-06-09 0.00 2016-06-10 0.00 2016-06-11 0.01 2016-06-12 0.01 2016-06-13 -0.01 2016-06-14 -0.01 2016-06-15 0.01 2016-06-16 0.01 2016-06-17 -0.01 2016-06-18 -0.01 2016-06-19 0.01 2016-06-20 0.01 2016-06-21 0.00 2016-06-22 0.02 2016-06-23 -0.01 2016-06-24 0.01 2016-06-25 -0.00 2016-06-26 -0.00 2016-06-27 0.02 2016-06-28 -0.02 2016-06-29 -0.01 2016-06-30 -0.01 2016-07-01 0.01 2016-07-02 0.00 2016-07-03 -0.01 2016-07-04 -0.02 2016-07-05 -0.02 2016-07-06 -0.00 2016-07-07 0.01 2016-07-08 -0.01 2016-07-09 0.00 2016-07-10 -0.02 2016-07-11 -0.01 2016-07-12 -0.00 2016-07-13 0.01 2016-07-14 0.00 2016-07-15 0.00 2016-07-16 -0.02 2016-07-17 -0.00 2016-07-18 -0.01 2016-07-19 -0.00 2016-07-20 0.00 2016-07-21 0.00 2016-07-22 0.00 2016-07-23 -0.01 2016-07-24 0.01 2016-07-25 0.01 2016-07-26 -0.00 2016-07-27 0.01 2016-07-28 0.01 2016-07-29 0.00 2016-07-30 -0.01 2016-07-31 -0.02 2016-08-01 0.01 2016-08-02 0.00 2016-08-03 -0.00 2016-08-04 -0.01 2016-08-05 -0.01 2016-08-06 0.02 2016-08-07 -0.01 2016-08-08 0.00 2016-08-09 -0.01 2016-08-10 0.00 2016-08-11 0.01 2016-08-12 -0.01 2016-08-13 0.01 2016-08-14 -0.01 2016-08-15 -0.00 2016-08-16 0.01 2016-08-17 0.00 2016-08-18 0.00 2016-08-19 -0.00 2016-08-20 -0.01 2016-08-21 0.01 2016-08-22 0.00 2016-08-23 0.00 2016-08-24 0.02 2016-08-25 0.00 2016-08-26 0.01 2016-08-27 -0.02 2016-08-28 0.00 2016-08-29 -0.00 2016-08-30 -0.01 2016-08-31 -0.00 2016-09-01 -0.00 2016-09-02 -0.01 2016-09-03 -0.01 2016-09-04 0.00 2016-09-05 0.01 2016-09-06 0.02 2016-09-07 0.02 2016-09-08 -0.01 2016-09-09 0.01 2016-09-10 0.02 2016-09-11 0.01 2016-09-12 -0.00 2016-09-13 -0.02 2016-09-14 -0.01 2016-09-15 -0.00 2016-09-16 0.00 2016-09-17 0.01 2016-09-18 0.00 2016-09-19 -0.01 2016-09-20 0.00 2016-09-21 -0.02 2016-09-22 -0.01 2016-09-23 -0.01 2016-09-24 -0.00 2016-09-25 -0.00 2016-09-26 -0.00 2016-09-27 -0.00 2016-09-28 -0.01 2016-09-29 -0.02 2016-09-30 0.02 2016-10-01 -0.02 2016-10-02 0.01 2016-10-03 -0.01 2016-10-04 -0.01 2016-10-05 -0.01 2016-10-06 0.02 2016-10-07 0.00 2016-10-08 -0.00 2016-10-09 0.00 2016-10-10 -0.01 2016-10-11 -0.00 2016-10-12 0.01 2016-10-13 0.01 2016-10-14 -0.01 2016-10-15 -0.01 2016-10-16 0.01 2016-10-17 0.01 2016-10-18 -0.00 2016-10-19 0.00 2016-10-20 0.02 2016-10-21 0.00 2016-10-22 -0.01 2016-10-23 0.00 2016-10-24 -0.00 2016-10-25 0.01 2016-10-26 0.00 2016-10-27 0.01 2016-10-28 0.01 2016-10-29 -0.01 2016-10-30 -0.02 2016-10-31 -0.01 2016-11-01 -0.02 2016-11-02 -0.00 2016-11-03 0.01 2016-11-04 -0.00 2016-11-05 -0.01 2016-11-06 -0.01 2016-11-07 0.00 2016-11-08 0.01 2016-11-09 0.01 2016-11-10 0.00 2016-11-11 0.01 2016-11-12 -0.00 2016-11-13 -0.00 2016-11-14 0.01 2016-11-15 -0.02 2016-11-16 -0.00 2016-11-17 0.00 2016-11-18 -0.00 2016-11-19 -0.00 2016-11-20 0.00 2016-11-21 0.01 2016-11-22 0.01 2016-11-23 0.01 2016-11-24 -0.02 2016-11-25 0.02 2016-11-26 0.00 2016-11-27 -0.01 2016-11-28 0.00 2016-11-29 0.01 2016-11-30 -0.01 2016-12-01 0.00 2016-12-02 -0.01 2016-12-03 0.01 2016-12-04 -0.01 2016-12-05 0.02 2016-12-06 0.00 2016-12-07 0.01 2016-12-08 -0.00 2016-12-09 0.02 2016-12-10 -0.00 2016-12-11 0.01 2016-12-12 -0.00 2016-12-13 -0.02 2016-12-14 0.00 2016-12-15 0.02 2016-12-16 0.01 2016-12-17 0.01 2016-12-18 -0.01 2016-12-19 -0.00 2016-12-20 0.02 2016-12-21 -0.00 2016-12-22 0.00 2016-12-23 -0.01 2016-12-24 0.01 2016-12-25 0.01 2016-12-26 0.02 2016-12-27 -0.00 2016-12-28 0.00 2016-12-29 0.02 2016-12-30 0.01 2016-12-31 -0.01 2017-01-01 0.02 2017-01-02 0.01 2017-01-03 -0.01 2017-01-04 0.01 2017-01-05 0.00 2017-01-06 0.01 2017-01-07 -0.02 2017-01-08 0.02 2017-01-09 0.01 2017-01-10 -0.01 2017-01-11 -0.00 2017-01-12 -0.01 2017-01-13 0.01 2017-01-14 -0.01 2017-01-15 0.01 2017-01-16 -0.01 2017-01-17 0.01 2017-01-18 0.00 2017-01-19 0.01 2017-01-20 -0.00 2017-01-21 0.00 2017-01-22 0.02 2017-01-23 -0.00 2017-01-24 0.01 2017-01-25 0.00 2017-01-26 -0.01 2017-01-27 0.00 2017-01-28 -0.01 2017-01-29 0.01 2017-01-30 -0.00 2017-01-31 0.01 2017-02-01 0.02 2017-02-02 0.01 2017-02-03 0.01 Freq: D, dtype: float64
>>> from pandas_datareader import DataReader
>>> syms = {
... 'TWEXBMTH': 'usd',
... 'T10Y2YM': 'term_spread',
... 'PCOPPUSDM': 'copper'
... }
>>> data = DataReader(syms.keys(), data_source='fred',
... start='2000-01-01', end='2016-12-31')\
... .pct_change()\
... .dropna()\
... .rename(columns=syms)
>>> y = data.pop('usd')
>>> data.head()
>>> y.head()
term_spread | copper | |
---|---|---|
DATE | ||
2000-02-01 | -1.41 | -0.02 |
2000-03-01 | 2.00 | -0.04 |
2000-04-01 | 0.52 | -0.03 |
2000-05-01 | -0.10 | 0.06 |
2000-06-01 | 0.03 | -0.02 |
DATE 2000-02-01 0.01 2000-03-01 -0.00 2000-04-01 0.01 2000-05-01 0.02 2000-06-01 -0.01 Freq: MS, Name: usd, dtype: float64
>>> from pyfinance import ols
>>> model = ols.OLS(y=y, x=data)
>>> model.alpha # the intercept - a scalar
>>> model.beta # the coefficients
>>> model.fstat
# Residuals and predicted y values are NumPy arrays
# with the same shape as `y`.
>>> model.resids.shape
>>> model.predicted.shape
Fetching OLS from AV .. Failed to fetch OLS from AV, trying from Y .. Fetching OLS from Y .. Failed to fetch implied symbol: OLS
0.001230320443416746
array([-0.00062647, -0.09490566])
33.42923069295481
(203,)
(203,)
365.0
def show_rolling_beta(target, sources, window=None, rsq=True, betaSum=False, pvalue=False, freq=None):
if not isinstance(sources, list):
sources = [sources]
target = get(target)
sources = get(sources)
names = [s.name for s in sources]
target = logret(target)
sources = lmap(logret, sources)
target = unwrap(target)
sources = lmap(unwrap, sources)
sources = pd.DataFrame(sources).T.dropna()
target, sources = sync(target, sources)
if freq:
target = target.asfreq(freq)
sources = sources.asfreq(freq)
if window is None:
window = int(get_anlz_factor(freq))
else:
if window is None:
window = 365
rolling = ols.PandasRollingOLS(y=target, x=sources, window=window)
#rolling.beta.head()
#rolling.alpha.head()
#rolling.pvalue_alpha.head()
#type(rolling.beta["feature1"])
res = []
if pvalue:
_pvalue = rolling.pvalue_beta
_pvalue.columns = [s + " pvalue" for s in names]
res += [_pvalue, 0.05]
if rsq:
rsq = rolling.rsq
rsq.name = "R^2"
res += [rsq]
_beta = rolling.beta
_beta.columns = [s + " beta" for s in names]
res += [_beta]
if betaSum and len(names) > 1:
_betaSum = rolling.beta.sum(axis=1)
_betaSum.name = "beta sum"
res += [_betaSum]
res += [-1, 0, 1]
show(res, ta=False)
#show_rolling_beta(PERM, [lc, gold, lgb], pvalue=False)
#show_rolling_beta(VT, [VTI, VXUS])
#show_rolling_beta(SVXY, ZIV, window=60, pvalue=True)
show_rolling_beta(NEAR, MINT, freq="M")
#show(NEAR, MINT)
show(logret(NEAR.asfreq("M")) / logret(MINT.asfreq("M")), ta=False)
plt.scatter(*sync(logret(NEAR.asfreq("M")), logret(MINT.asfreq("M"))))
plt.xlim(-0.002, 0.005)
plt.ylim(-0.002, 0.005)
#MINT.asfreq("M")
<matplotlib.collections.PathCollection at 0x7fba3fefeef0>
(-0.002, 0.005)
(-0.002, 0.005)
# be sure to save before you publish
# when done, you will get a public link to an HTML file with the results
publish()