简体   繁体   English

如何在时间序列数据中找到最大增长率?

[英]How to find max growth rate in a time series data?

I would like to know the algorithm of finding the max growth rate in a given time period.我想知道在给定时间段内找到最大增长率的算法。

Suppose we have eight(N) data points as following,假设我们有八个(N)个数据点如下,

在此处输入图像描述 在此处输入图像描述

list_x = [84,59,52,71,62,82,45,50]
def find_max(list_x):
   # return the L index, H index, ratio
   # take list_x as an example, L index: 3(52), H index: 6(82), ratio: 82/52 
   # should return (3, 6, 1.57)
   return L,H,dy/dx

A brute way would be going through O(N*N), store the ratio dicts, then sorting by ratio.一种粗暴的方法是通过 O(N*N),存储比率字典,然后按比率排序。 Any efficient algorithms?任何有效的算法? thanks谢谢

As I mentioned in a comment it looks like you are looking for H/L instead of dy/dh since your expected result is (3, 6, 1.57) .正如我在评论中提到的那样,您似乎正在寻找H/L而不是dy/dh因为您的预期结果是(3, 6, 1.57) Assuming you actually want to do H/L then you can try the following:假设您实际上想做H/L ,那么您可以尝试以下操作:

def max_result(arr):
    max_growth, cur_min, cur_min_idx = float('-inf'), float('inf'), -1
    res_l = res_h = float('-inf')
    for i, val in enumerate(arr):
        if val / cur_min > max_growth:
            max_growth = val / cur_min
            res_l, res_h = cur_min_idx, i
        if val < cur_min:
            cur_min, cur_min_idx = val, i
    return res_l + 1, res_h + 1, round(max_growth, 2)

The complexity here is O(N)这里的复杂度是O(N)

Push the nested loops to C using Numpy.使用 Numpy 将嵌套循环推送到 C。

import numpy as np

a = np.array([84,59,52,71,62,82,45,50])

Calculate the difference between each point and all other points计算每个点与所有其他点之间的差异

b = (a - a[:,None])

Calculate dy/dx for all those differences.计算所有这些差异的 dy/dx。 This assumes the x-values are contiguous and evenly spaced.这假设x 值是连续且均匀分布的。

d = np.arange(a.shape[0], dtype=float)
d = d-d[:,None]
d[d==0] = .00001
c  = b / d

Find the indices of the maximum dy/dx and use them to get the values that produced it.找到最大 dy/dx 的索引并使用它们来获取产生它的值。

max_dydx = np.triu(c).max()
indices = np.where(np.triu(c) == max_dydx)
values = a[np.concatenate(indices)]
print(indices, values)

>>>
(array([4], dtype=int64), array([5], dtype=int64)) [62 82]

Intermediate arrays - values reflect across the diagonal.中间 arrays - 值反映在对角线上。

>>> print(b.round(1))
[[  0 -25 -32 -13 -22  -2 -39 -34]
 [ 25   0  -7  12   3  23 -14  -9]
 [ 32   7   0  19  10  30  -7  -2]
 [ 13 -12 -19   0  -9  11 -26 -21]
 [ 22  -3 -10   9   0  20 -17 -12]
 [  2 -23 -30 -11 -20   0 -37 -32]
 [ 39  14   7  26  17  37   0   5]
 [ 34   9   2  21  12  32  -5   0]]
>>> print(d.round(1))
[[ 0.  1.  2.  3.  4.  5.  6.  7.]
 [-1.  0.  1.  2.  3.  4.  5.  6.]
 [-2. -1.  0.  1.  2.  3.  4.  5.]
 [-3. -2. -1.  0.  1.  2.  3.  4.]
 [-4. -3. -2. -1.  0.  1.  2.  3.]
 [-5. -4. -3. -2. -1.  0.  1.  2.]
 [-6. -5. -4. -3. -2. -1.  0.  1.]
 [-7. -6. -5. -4. -3. -2. -1.  0.]]
>>> print(c.round(1))
[[  0.  -25.  -16.   -4.3  -5.5  -0.4  -6.5  -4.9]
 [-25.    0.   -7.    6.    1.    5.8  -2.8  -1.5]
 [-16.   -7.    0.   19.    5.   10.   -1.8  -0.4]
 [ -4.3   6.   19.    0.   -9.    5.5  -8.7  -5.2]
 [ -5.5   1.    5.   -9.    0.   20.   -8.5  -4. ]
 [ -0.4   5.8  10.    5.5  20.    0.  -37.  -16. ]
 [ -6.5  -2.8  -1.8  -8.7  -8.5 -37.    0.    5. ]
 [ -4.9  -1.5  -0.4  -5.2  -4.  -16.    5.    0. ]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM