且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

pandas 通过切片计算CAGR(缺失值)

更新时间:2022-04-13 03:33:01

在计算某个级别的回报时,可以使用最新的级别.例如,在计算第1行的CAGR时,我们要使用(5/7)^(1/3)-1.此外,对于第3行(9/7)^(1/3).有一个假设是,我们对所考察的所有年份都进行了年化处理.

When calculating returns from a level, it's ok to use most recent available. For example, when calculating CAGR for row 1, we want to use (5/7) ^ (1/3) - 1. Also, for row 3 (9/7) ^ (1/3). There is an assumption made that we annualize across all years looked at.

基于这些假设:

df = df.bfill(axis=1).ffill(axis=1)

然后应用链接问题中的解决方案.

Then apply solution from linked question.

df['CAGR'] = df.T.pct_change().add(1).prod().pow(1./(len(df.columns) - 1)).sub(1)

没有这个假设.唯一的其他合理选择是按非NaN观测值的数量进行年度化.因此,我需要使用以下方法进行跟踪:

With out this assumption. The only other reasonable choice would be to annualize by the number of non-NaN observations. So I need to track that with:

notnull = df.notnull().sum(axis=1)
df = df.bfill(axis=1).ffill(axis=1)
df['CAGR'] = df.T.pct_change().add(1).prod().pow(1./(notnull.sub(1))).sub(1)

实际上,这将成为更通用的解决方案,因为它也适用于没有null的情况.

In fact, this becomes the more general solution as it will work with the case with out nulls as well.