【问题标题】:Find end period of maximum drawdown?找到最大回撤的结束时间?
【发布时间】:2019-11-12 06:48:54
【问题描述】:

我查看了this answer,他们有一个非常聪明的解决方案来找到最大回撤开始的点,以及最大回撤的最低点。但是,根据Wikipedia,这不是期间的结束(如答案中所述)。周期结束是当您达到回撤期开始之前的相同峰值时。

这意味着我链接到的答案中给出的图表的回撤期没有结束。我正在尝试编写一个可以解决此问题的代码,对于这两种情况{1) 它有一个结束期,2) 它没有结束期}。

如果句点永远不会结束,我只希望它返回数组的最后一个索引(基本上就是数组的长度),如果句点确实结束了,我想要正确的索引。这是一个简化的示例,我试图解决第一种情况 - 当它有一个结束期时:

import numpy as np

an_array = [21, 22, 23, 40, 19, 35, 37, 45, 42, 39, 28]
running_maximum = np.maximum.accumulate(an_array)
# running_maximum = [21, 22, 23, 40, 40, 40, 40, 45, 45, 45, 45]

bottom_index = np.argmax(running_maximum - an_array)
start_index = np.argmax(an_array[:bottom_of_period])
# bottom_index = 4, start_index = 3

difference = running_maximum - an_array
# difference = [0, 0, 0, 0, 21, 5, 3, 0, 3, 6, 17]

我计算difference 的原因是因为它可以很容易地看到end_index=7。这是因为在索引 4 处的最大回撤是 21,并且由于 difference=0 再次在索引 7 处,这意味着我已经超过(或刚刚达到)我的峰值。我尝试写 np.argmin(difference[bottom_index:]) 来获取索引,但当然这不会给我 7,因为我切片向量 difference,它给了我 3,这是不正确的。

关于我如何解决这个问题的任何提示,并使其在没有结束期的情况下返回最后一个索引,这将是惊人的。

【问题讨论】:

    标签: python-3.x numpy time-series


    【解决方案1】:

    我认为这可以解决它;

    import numpy as np
    
    an_array = [21, 22, 23, 40, 19, 35, 37, 45, 42, 39, 28]
    running_maximum = np.maximum.accumulate(an_array)
    difference = running_maximum - an_array
    
    bottom_index = np.argmax(difference)
    start_index = np.argmax(an_array[:bottom_index])
    
    if difference[bottom_index:].__contains__(0):
        end_index = len(difference[:bottom_index]) + np.argmin(difference[bottom_index:])
    else:
        end_index = len(difference)
    

    使用给定的示例数组,我得到end_index = 7。如果我更改an_array[4]=39,最大的缺点不再有结束,我得到end_index = 11。不过不确定__contains__(0) 是否有效。

    【讨论】:

      猜你喜欢
      • 2014-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      相关资源
      最近更新 更多