首页 > 解决方案 > 在 Python 中使用时间序列分解中的运行中位数提取趋势?

问题描述

我有许多不同的时间序列,它们是季节性的,包括大的异常值。

当使用运行平均值来提取趋势时(如在 STL 实现中),它可能会破坏提取的趋势和季节性成分,并在残差中引入错误的异常值。

在此处输入图像描述

解决这个问题的一种方法是使用移动中位数来提取趋势,是否有任何 python 包类似于 R 中的十进制(https://rdrr.io/cran/pastecs/man/decmedian.html)?

另一种解决方案可能是尝试将以下代码片段翻译为 Python,但我不确定如何翻译该xts部分。

decompose.median <- function(m, period) {
   trend <- rollapply(m, width = period, fill = NA, align = "center",
                 FUN = median, na.rm = TRUE)
   season <- m - trend
   figure <- numeric(period)
   l   <- length(m)
   index <- seq.int(1, l, by = period) - 1
   for (i in 1:period) figure[i] <- median(season[index + i], na.rm = TRUE)
   seasonal=xts(rep(figure, l %/% period + 1)[seq_len(l)], order.by = index(m))
   list(observed = m,
   trend = trend,
   seasonal = seasonal,
   remainder = m - trend - seasonal)
}

蟒蛇尝试

def decompose_median(df,col,period):
    m = df[col]
    trend = df[col].rolling(period).median()
    season = m - trend
    figure = np.zeros(period)
    l = len(m)
    index = np.array(range(1,100,10)) - 1
    for i in range(1,period):
        figure[i] = np.median(season.dropna()[index + i])

    
  #### Not sure how to translate this part
   seasonal=xts(rep(figure, l %/% period + 1)[seq_len(l)], order.by = 
   index(m))
   list(observed = m,
   trend = trend,
   seasonal = seasonal,
   remainder = m - trend - seasonal)
    }

标签: python

解决方案


推荐阅读