给出总回报和股息,矢量化隐含价格

时间:2016-11-03 22:16:00

标签: python pandas numpy quantitative-finance

考虑总回报指数的时间序列tr_idx

import pandas as pd
import numpy as np

np.random.seed([314,15])
n = 5
tidx = pd.date_range('2015-12-31', periods=n)
lognormal_kw = dict(mean=2e-4, sigma=1e-2, size=n)
tr_idx = pd.Series(np.random.lognormal(**lognormal_kw),
                   tidx, name='TRIdx')

tr_idx

2015-12-31    0.999499
2016-01-01    1.014569
2016-01-02    1.002259
2016-01-03    0.988591
2016-01-04    0.987725
Freq: D, Name: TRIdx, dtype: float64

支付股息的时间序列

div = pd.Series(1e-3, tidx, name='Div')
div

2015-12-31    0.001
2016-01-01    0.001
2016-01-02    0.001
2016-01-03    0.001
2016-01-04    0.001
Freq: D, Name: Div, dtype: float64

如何对隐含价格计算进行矢量化?

我们知道总回报可以计算为

enter image description here

我们可以退出价格计算

enter image description here

我可以在这样的循环中使用这些公式

price = pd.Series(np.nan, tidx, name='Price')

p0 = 100  # Initial Price
for i, (date, prc) in enumerate(price.iteritems()):
    p0 = price.iloc[i] = tr_idx.iloc[i] * p0 - div.iloc[i]

查看结果

pd.concat([tr_idx, div, price], axis=1)

enter image description here

2 个答案:

答案 0 :(得分:1)

这是使用broadcasting -

的矢量化方法
p0 = 100  # Initial Price
y1 = np.append(tr_idx.values,1)[::-1].cumprod()[::-1]
y2 = np.append(p0,-div.values)
price_out = np.tril(y1/y1[:,None]*y2).sum(1)[1:]

答案 1 :(得分:0)

扩大公式

enter image description here

更清楚

enter image description here

d = div.values[:]
d[0] = 100 - d[0]
t = tr_idx.values[::-1].cumprod()[::-1]
p = pd.Series((t * d).cumsum() / np.concatenate([t[1:], [1]]),
              tidx, name='PriceV')

pd.concat([tr_idx, div, price, p], axis=1)

enter image description here