Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 大熊猫的年化收益_Python_Pandas - Fatal编程技术网

Python 大熊猫的年化收益

Python 大熊猫的年化收益,python,pandas,Python,Pandas,我想确认我对年化回报公式(使用月回报)的表述是最优的 我使用的年化回报率公式(其中M是月回报率,D是月回报率的总计数),其中月回报率的计数大于12,如下所示: 或者,如果每月返回计数小于12,则该值将发生变化: 以下是我在熊猫中对这一公式的表述: ann_return = observations.apply(lambda y: y.apply(lambda x: x+1)) ann_return = (ann_return.prod() ** (np.min(12/len(ann_retu

我想确认我对年化回报公式(使用月回报)的表述是最优的

我使用的年化回报率公式(其中M是月回报率,D是月回报率的总计数),其中月回报率的计数大于12,如下所示:

或者,如果每月返回计数小于12,则该值将发生变化:

以下是我在熊猫中对这一公式的表述:

ann_return = observations.apply(lambda y: y.apply(lambda x: x+1))
ann_return = (ann_return.prod() ** (np.min(12/len(ann_return.index.values)) if len(ann_return.index.values) > 12 else 12/len(ann_return.index.values)))-1
公式
这将计算年化回报率。它既适用于单个数字,也适用于数据帧。在后一种情况下,第一个参数
percent
和第二个参数
months
可以是数据帧

使用Python 3.7.0和Pandas 0.23.4以及NumPy 1.15.2对其进行了测试

def annualize_return(percent: float, months: int) -> float:
    """Return the annualized return percentage given the holding return percentage and the number of months held.

    >>> annualize_return(1.5, 1)  # doctest: +ELLIPSIS
    19.56...
    >>> annualize_return(6.1, 3)  # doctest: +ELLIPSIS
    26.72...
    >>> annualize_return(30, 12)  # doctest: +ELLIPSIS
    30.00...
    >>> annualize_return(30, 15)  # doctest: +ELLIPSIS
    23.35...
    >>> annualize_return(float('nan'), 15)
    nan
    >>> annualize_return(0, 0)
    0

    References:
        https://en.wikipedia.org/wiki/Holding_period_return
        https://www.wikihow.com/Calculate-Annualized-Portfolio-Return
    """
    # Ref: https://stackoverflow.com/a/52618808/
    if months == 0:
        return percent
    rate = percent / 100
    years = months / 12
    rate = ((rate + 1)**(1 / years)) - 1
    percent = rate * 100
    return percent


if __name__ == '__main__':
    import doctest
    doctest.testmod(verbose=True, exclude_empty=True)

数据框中有哪些列?你能举一个数据框的例子吗?我不知道1和12之间的插入符号应该是什么意思。@piRSquared:1和12/D之间的插入符号/楔形符号表示最小值@DYZ:dataframe中只有一列,这是一个月收益列表。索引是一系列相应的每月日期。@northernthinking对所有行都是相同的,那么?谢谢您指出公式末尾缺少的减法1。python代码不也需要显示这个吗?我认为第一个公式是正确的。第二个公式是尊重你的想法,并给出一种计算方法。
-1
嵌入在布尔值
D<12
中。当
D<12
时,该
True
值将计算为
1
。当
False
时,其计算结果为
0
。这为您捕获了if语句。明白了,我相信减1是无条件的。应该是有条件的是指数计算<代码>指数=12/D,如果D>12,否则:指数=min(1,12/D)这是受以下方向启发的:,我很难看到的值。根据您的回答和进一步的审查,我认为最有效和正确的一行应该是:
(df.add(1.prod()**(12/len(df.index.values))-1
哦!你的意思是从整件事中减去一件?啊,那就更有意义了。1的减法等于指数。算了吧。我希望我现在能理解你的想法。否则,我的兴高采烈可能会让事情变得更加混乱。
def annualize_return(percent: float, months: int) -> float:
    """Return the annualized return percentage given the holding return percentage and the number of months held.

    >>> annualize_return(1.5, 1)  # doctest: +ELLIPSIS
    19.56...
    >>> annualize_return(6.1, 3)  # doctest: +ELLIPSIS
    26.72...
    >>> annualize_return(30, 12)  # doctest: +ELLIPSIS
    30.00...
    >>> annualize_return(30, 15)  # doctest: +ELLIPSIS
    23.35...
    >>> annualize_return(float('nan'), 15)
    nan
    >>> annualize_return(0, 0)
    0

    References:
        https://en.wikipedia.org/wiki/Holding_period_return
        https://www.wikihow.com/Calculate-Annualized-Portfolio-Return
    """
    # Ref: https://stackoverflow.com/a/52618808/
    if months == 0:
        return percent
    rate = percent / 100
    years = months / 12
    rate = ((rate + 1)**(1 / years)) - 1
    percent = rate * 100
    return percent


if __name__ == '__main__':
    import doctest
    doctest.testmod(verbose=True, exclude_empty=True)