【问题标题】:Why the MAD that is calculated with the function scipy.stats.median_absolute_deviation it's different from the function i did?为什么使用函数 scipy.stats.median_absolute_deviation 计算的 MAD 与我所做的函数不同?
【发布时间】:2020-06-14 03:42:19
【问题描述】:

接下来我展示代码,我创建了具有绝对平均偏差公式的 DMA 函数,另外两个打印计算 stats 包的 DMA 并且健壮,因为我们看到两个结果不同,我没有了解为什么函数的估计与我手动创建的有很大不同

  import numpy as np
  import scipy.stats as stats
  from statsmodels import robust    



def MAD (vector):
      MAD  = np.sum(np.abs(vector-np.mean(vector)))/len(vector)
      return(MAD )

    print("MAD ",DMA([1.5,0,4,2.5]))
    print("MAD function from stats", stats.median_absolute_deviation([1.5,0,4,2.5],axis=0))
    print("MAD function from robust", robust.mad([1.5,0,4,2.5]))

结果:

MAD 1.25
MAD function from stats 1.8532499999999998
MAD function from robust 1.8532527731320025

【问题讨论】:

    标签: python statistics statsmodels robust


    【解决方案1】:

    首先,这两个函数都应用了一个归一化常数,以使 MAD 成为标准偏差的一致估计量。如果我们通过将此因子设置为 1.0 来关闭此调整,则结果是相同的。

    其次,虽然这个特定向量的中值和均值相同,但如果您想匹配这两个函数的默认行为,则应使用向量的中值而不是均值作为中心。

    import numpy as np
    import scipy.stats as stats
    from statsmodels import robust    
    
    def MAD(vector):
        MAD = np.mean(np.abs(vector-np.median(vector)))
        return MAD
    
    print("MAD",MAD([1.5,0,4,2.5]))
    print("MAD function from stats", stats.median_absolute_deviation([1.5,0,4,2.5],axis=0,scale=1.0))
    print("MAD function from robust", robust.mad([1.5,0,4,2.5],c=1.0))
    

    疯狂 1.25

    统计 1.25 中的 MAD 函数

    来自鲁棒 1.25 的 MAD 函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多