在固定利率支出后,提高寻找投资组合终值的速度
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在固定利率支出后,提高寻找投资组合终值的速度相关的知识,希望对你有一定的参考价值。
我有一个pd.DataFrame
的回报系列对应多年,固定支出率为5%。我希望在每年支出后找到结束的投资组合价值。 val_after_spending
年度t
等于t
val_before_spending
年平均值t-1
val_after_spending乘以支出率。第一年,val_after_spending
的t-1
假定为1。
我现在有一个工作实现(下面),但它非常慢。有更快的方法来实现这个吗?
import pandas as pd
import numpy as np
port_rets = pd.DataFrame({'port_ret': [.10,-.25,.15]})
spending_rate = .05
for index, row in port_rets.iterrows():
if index != 0:
port_rets.at[index, 'val_before_spending'] = port_rets['val_after_spending'][index - 1] * (1 + port_rets['port_ret'][index])
port_rets.at[index, 'spending'] = np.mean([port_rets['val_after_spending'][index - 1], port_rets['val_before_spending'][index]]) * spending_rate
else:
port_rets.at[index, 'val_before_spending'] = 1 * (1 + port_rets['port_ret'][index])
port_rets.at[index, 'spending'] = np.mean([1, port_rets['val_before_spending'][index]]) * spending_rate
port_rets.at[index, 'val_after_spending'] = port_rets['val_before_spending'][index] - port_rets['spending'][index]
# port_ret val_before_spending spending val_after_spending
#0 0.100000 1.100000 0.052500 1.047500
#1 -0.250000 0.785625 0.045828 0.739797
#2 0.150000 0.850766 0.039764 0.811002
答案
您在代码中与pandas非常密切地接触,就性能而言,这似乎是个坏主意。为了使其易于使用,大熊猫需要进行大量的簿记,这会导致性能降低。
我们在numpy中进行所有计算,然后获得所有构建块,最后构建数据帧。因此,代码转换为:
def get_vals(rates, spending_rate):
n = len(rates)
vals_after_spending = np.zeros((n+1, ))
vals_before_spending = np.zeros((n+1, ))
vals_after_spending[0] = 1.0
for i in range(n):
vals_before_spending[i+1] = vals_after_spending[i] * (1 + rates[i])
spending = np.mean(np.array([vals_after_spending[i], vals_before_spending[i+1]])) * spending_rate
vals_after_spending[i+1] = vals_before_spending[i+1] - spending
return vals_before_spending[1:], vals_after_spending[1:]
rates = np.array(port_rets["port_ret"].tolist())
vals_before_spending, vals_after_spending = get_vals(rates, spending_rate)
port_rets = pd.DataFrame({'port_ret': rates, "val_before_spending": vals_before_spending, "val_after_spending": vals_after_spending})
我们可以通过JIT编译代码来进一步改进,因为python循环很慢。下面我用numba:
import numba as nb
@nb.njit(cache=True) # as easy as putting this decorator
def get_vals(rates, spending_rate):
n = len(rates)
vals_after_spending = np.zeros((n+1, ))
vals_before_spending = np.zeros((n+1, ))
# ... code remains same, we are just compiling the function
如果我们考虑这样的随机费率列表:
port_rets = pd.DataFrame({'port_ret': np.random.uniform(low=-1.0, high=1.0, size=(100000,))})
我们得到了性能比较:
你的代码:15.758s
get_vals:1.407s
JITed get_vals:0.093s(第二次运行到折扣编译时)
以上是关于在固定利率支出后,提高寻找投资组合终值的速度的主要内容,如果未能解决你的问题,请参考以下文章