几种平均数(Mean function)

前言:平均数是用来表示数据整体趋势的一个统计指标,本文参考wiki,采用一些简明的例子,主要是总结。

  • 算术平均数(Arithmetic Mean)
  1. 计算公式
  2. 优点:相比于中位数、众数,更少收到随机因素的影响
  3. 缺点:更容易收到极端值(biased value)的影响
  4. 例子:
    • data = [1, 2, 3, 4, 5, 6,  7, 8, 9, 10]. result = 5.5.
  • 几何平均数(Geometric Mean)
  1. 计算公式
  2. 优点:适用于对比率数据的平均,主要用于计算数据平均增长率
  3. 例子:
    • data = [1, 2, 3, 4, 5, 6,  7, 8, 9, 10]. result = 4.5287.
  • 调和平均数(Harmonic Mean)
  1. 计算公式
  2. 优点:计算平均速率,感觉很多paper都在用,用于计算平均速率
  3. 例子:
    • data = [1, 2, 3, 4, 5, 6,  7, 8, 9, 10]. result = 3.414.
  • 平方平均数(Quadratic Mean)
  1. 计算公式:
  2. 优点:是2次方的广义平均数的表达式。可以定义在连续区间。常用来计算一组数据与某个数据之间的平均差。
  3. 例子:
    • data = [1, 2, 3, 4, 5, 6,  7, 8, 9, 10]. result = 6.204.

注意到各种算法得到的平均值有所差别,比如在这个例子之中,调和平均数偏小,平方平均数偏大,当然我没有严格formulate,不能说是一个通用的结论。在看一篇Sigcomm的文章时,prediction的结果一般较为乐观(偏大)时,error比较大,作者采用了Harmonic mean。

附python实现代码(CalculateMeans.py)

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 from math import *
 5 
 6 def ArithmeticMean(data):
 7     len_d = float(len(data))
 8     result = sum(data) / len_d
 9     return result
10 
11 def GeometricMean(data):
12     len_d = len(data)
13     product = 1.0
14     for i in range(len_d):
15         product = product * data[i]
16     # the next line is equal to calculate the n-root
17     result = product ** (1.0/len_d)
18     return result
19 
20 def HarmonicMean(data):
21     len_d = len(data)
22     x = [1.0/data[i] for i in range(len_d)]
23     result = float(len_d) / sum(x)
24     return result
25 
26 def QuadraticMean(data):
27     len_d = len(data)
28     x = [data[i] * data[i] for i in range(len_d)]
29     result = sqrt(sum(x) / float(len_d))
30     return result
31 
32 
33 if __name__ == "__main__":
34     data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
35     print "Scenario used in the test:", data
36     print "The arithmetic mean:", ArithmeticMean(data)
37     print "The geometric mean:", GeometricMean(data)
38     print "The Harmonic mean:", HarmonicMean(data)
39     print "The Quadratic mean:", QuadraticMean(data)

实验结果:

参考:

https://zh.wikipedia.org/wiki/%E8%B0%83%E5%92%8C%E5%B9%B3%E5%9D%87%E6%95%B0

https://zh.wikipedia.org/wiki/%E5%87%A0%E4%BD%95%E5%B9%B3%E5%9D%87%E6%95%B0

https://zh.wikipedia.org/wiki/%E5%B9%B3%E6%96%B9%E5%B9%B3%E5%9D%87%E6%95%B0

https://zh.wikipedia.org/wiki/%E7%AE%97%E6%9C%AF%E5%B9%B3%E5%9D%87%E6%95%B0

原文地址:https://www.cnblogs.com/loadofleaf/p/6665824.html