【问题标题】:Pandas autocorrelation_plot change confidence intervalPandas autocorrelation_plot 改变置信区间
【发布时间】:2020-09-05 00:03:09
【问题描述】:

我有这些来自时间序列和自相关图的数据,如下所示。

from pandas.plotting import autocorrelation_plot
import numpy as np

data = np.array([ 37.3    ,  11.9    ,  43.3    ,  23.6    ,  30.2    ,   8.4    ,
         9.1    ,  10.3    ,  74.7    ,   4.4    ,  45.7    ,  18.6    ,
        13.9    ,   7.5    ,  30.4    ,  15.5    ,  11.6    ,   3.2    ,
         6.8    ,   3.     ,   9.7    ,   4.     ,   9.8    ,  14.6    ,
         6.5    ,   7.2    ,   6.1    ,  10.3    ,   7.9    ,   3.4    ,
         3.4    ,   6.4    ,  38.5    ,  10.     ,   6.1    ,  11.7    ,
        16.9    ,   4.1    ,   8.9    ,   8.1    ,   7.6    ,  13.2    ,
        11.7    ,   7.1    ,  42.6    ,   7.2    ,  17.9    ,  42.2    ,
        18.5    ,   7.1    ,  42.1    ,  10.     , 100.1    ,   4.5    ,
        42.52905,   4.8081 ,  15.66435,  12.0056 ,   6.744  ,  96.7745 ,
        13.8    ,   8.2    ,   2.3    ,  14.8    ,  21.5    ,  11.3    ,
        10.2    ,  12.6    ,  42.7    ,  18.     ,  26.8    ,  31.9    ,
        22.047  ,   6.057  ,  20.8    ,  49.014  ,  20.788  ,   7.198  ,
         9.993  ,  19.393  ,  44.9456 ,  13.912  ,  11.404  ,  38.367  ,
        34.792  ,   7.99425,   5.37   ,  11.358  ,  16.519  ,   7.337  ,
         5.717  ,   7.248  ,  50.1475 ,  19.277  ,  41.596  ,  66.106  ])

fig, ax = plt.subplots(figsize=(8, 5))
autocorrelation_plot(data)
plt.show()

正如您在此处看到的,默认置信区间下的任何滞后之间没有任何显着相关性。As documentation here 图中显示的水平线对应于 95% 和 99% 的置信区间。虚线是 99% 置信带。

现在我需要检查不同CI下的ACF,但我找不到如何更改CI。

【问题讨论】:

    标签: python pandas plot confidence-interval autocorrelation


    【解决方案1】:

    您只需复制代码并将置信区间传递给:

    def autocorrelation(series, lower=0.95, upper=0.99, ax=None, **kwds):
        # require scipy
        import scipy
        import matplotlib.pyplot as plt
    
        n = len(series)
        data = np.asarray(series)
        if ax is None:
            ax = plt.gca(xlim=(1, n), ylim=(-1.0, 1.0))
        mean = np.mean(data)
        c0 = np.sum((data - mean) ** 2) / float(n)
    
        def r(h):
            return ((data[: n - h] - mean) * (data[h:] - mean)).sum() / float(n) / c0
    
        x = np.arange(n) + 1
        y = [r(loc) for loc in x]
    
        # customize the z's
        z95, z99 = scipy.stats.t.ppf((1 + np.array([lower,upper])) / 2., 1e9)
    
        ax.axhline(y=z99 / np.sqrt(n), linestyle="--", color="grey")
        ax.axhline(y=z95 / np.sqrt(n), color="grey")
        ax.axhline(y=0.0, color="black")
        ax.axhline(y=-z95 / np.sqrt(n), color="grey")
        ax.axhline(y=-z99 / np.sqrt(n), linestyle="--", color="grey")
        ax.set_xlabel("Lag")
        ax.set_ylabel("Autocorrelation")
        ax.plot(x, y, **kwds)
        if "label" in kwds:
            ax.legend()
        ax.grid()
        return ax
    
    
    # test
    autocorrelation(data, 0.6, 0.9)
    

    输出:

    【讨论】:

      【解决方案2】:

      我认为不可能更改 CI。According to the source code here 95 和 99 区间的 Z 分数是硬编码的。

      def autocorrelation_plot(series, ax=None, **kwds):
          import matplotlib.pyplot as plt
      
          n = len(series)
          data = np.asarray(series)
          if ax is None:
              ax = plt.gca(xlim=(1, n), ylim=(-1.0, 1.0))
          mean = np.mean(data)
          c0 = np.sum((data - mean) ** 2) / float(n)
      
          def r(h):
              return ((data[: n - h] - mean) * (data[h:] - mean)).sum() / float(n) / c0
      
          x = np.arange(n) + 1
          y = [r(loc) for loc in x]
          z95 = 1.959963984540054
          z99 = 2.5758293035489004
          ax.axhline(y=z99 / np.sqrt(n), linestyle="--", color="grey")
          ax.axhline(y=z95 / np.sqrt(n), color="grey")
          ax.axhline(y=0.0, color="black")
          ax.axhline(y=-z95 / np.sqrt(n), color="grey")
          ax.axhline(y=-z99 / np.sqrt(n), linestyle="--", color="grey")
          ax.set_xlabel("Lag")
          ax.set_ylabel("Autocorrelation")
          ax.plot(x, y, **kwds)
          if "label" in kwds:
              ax.legend()
          ax.grid()
          return ax
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-30
        • 1970-01-01
        • 2017-11-20
        • 2021-07-26
        • 2017-10-23
        • 2023-03-27
        • 1970-01-01
        • 2018-02-18
        相关资源
        最近更新 更多