参考整理:https://tinycool.blog.csdn.net/article/details/124484157
一、源码及使用说明
Statistics库
mono
复制
引用方法:如果采用pia install statistics下载的话使用(参考链接)
#Include <statistics\statistics>
否则正常可以将文件放到同级目录
#Include statistics.ahk
使用时采用以下方法
ret := statistics.xxx(Arr)
源码:
; Author: Mono
; Time: 2022.09.12
; Version: 1.0.0
Class Statistics
{
; Mean
Static FMean(Arr)
{
Sum := 0
For i in Arr
Sum += i
Return Sum / Arr.Length
}
Static Geometric_Mean(Arr)
{
Mul := 1
For i in Arr
Mul *= i
Return Mul ** (1 / Arr.Length)
}
Static Harmonic_Mean(Arr)
{
Sum := 0
For i in Arr
Sum += (1 / i)
Return Arr.Length / Sum
}
Static Mean(Arr)
{
Sum := 0
For i in Arr
Sum += i
Return Sum // Arr.Length
}
; Median
Static Median(Arr)
{
Arr := Arr.Clone()
Loop Arr.Length
{
Index := A_Index
Loop Arr.Length - Index
{
if Arr[Index] > Arr[A_Index + Index]
{
Tmp := Arr[Index]
Arr[Index] := Arr[A_Index + Index]
Arr[A_Index + Index] := Tmp
}
}
}
Return Mod(Arr.Length, 2) ? Arr[Floor(Arr.Length / 2)] : (Arr[Arr.Length // 2] + Arr[Arr.Length // 2 + 1]) / 2
}
Static Median_Low(Arr)
{
Arr := Arr.Clone()
Loop Arr.Length
{
Index := A_Index
Loop Arr.Length - Index
{
if Arr[Index] > Arr[A_Index + Index]
{
Tmp := Arr[Index]
Arr[Index] := Arr[A_Index + Index]
Arr[A_Index + Index] := Tmp
}
}
}
Return Mod(Arr.Length, 2) ? Arr[Floor(Arr.Length / 2)] : Arr[Arr.Length // 2]
}
Static Median_High(Arr)
{
Arr := Arr.Clone()
Loop Arr.Length
{
Index := A_Index
Loop Arr.Length - Index
{
if Arr[Index] > Arr[A_Index + Index]
{
Tmp := Arr[Index]
Arr[Index] := Arr[A_Index + Index]
Arr[A_Index + Index] := Tmp
}
}
}
Return Arr[Arr.Length // 2 + 1]
}
Static Median_Grouped(Arr, Interval := 1)
{
if Arr.Length == 1
Return Arr[1]
Arr := Arr.Clone()
Loop Arr.Length
{
Index := A_Index
Loop Arr.Length - Index
{
if Arr[Index] > Arr[A_Index + Index]
{
Tmp := Arr[Index]
Arr[Index] := Arr[A_Index + Index]
Arr[A_Index + Index] := Tmp
}
}
}
x := Arr[Arr.Length // 2 + 1]
For i in Arr
{
if i == x
{
l1 := A_Index - 1
Break
}
}
For i in Arr
{
if i > x
{
l2 := A_Index - 2
Break
}
}
L := x - Interval / 2
cf := l1
f := l2 - l1 + 1
Return L + Interval * (Arr.Length / 2 - cf) / f
}
; Mode
Static Mode(Arr)
{
Tmp := Map()
For i in Arr
Tmp.Has(i) ? Tmp[i]++ : (Tmp[i] := 1)
For Key, Value in Tmp
isSet(MaxKey) ? ((MaxValue < Value) ? (MaxKey := Key, MaxValue := Value) : "") : (MaxKey := Key, MaxValue := Value)
Return MaxKey
}
Static MultiMode(Arr)
{
Tmp := Map()
Tmp2 := []
For i in Arr
Tmp.Has(i) ? Tmp[i]++ : (Tmp[i] := 1)
For Key, Value in Tmp
isSet(MaxValue) ? ((MaxValue < Value) ? (MaxValue := Value) : "") : (MaxValue := Value)
For Key, Value in Tmp
{
if Value == MaxValue
Tmp2.Push(Key)
}
Return Tmp2
}
; Standard deviation
Static Pstdev(Arr)
{
avg := Statistics.FMean(Arr)
Sum := 0
For i in Arr
Sum += (i - avg) ** 2
Return Sqrt(Sum / Arr.Length)
}
Static Pvariance(Arr)
{
avg := Statistics.FMean(Arr)
Sum := 0
For i in Arr
Sum += (i - avg) ** 2
Return Sum / Arr.Length
}
Static Stddev(Arr)
{
avg := Statistics.FMean(Arr)
Sum := 0
For i in Arr
Sum += (i - avg) ** 2
Return Sqrt(Sum / (Arr.Length - 1))
}
Static Variance(Arr)
{
avg := Statistics.FMean(Arr)
Sum := 0
For i in Arr
Sum += (i - avg) ** 2
Return Sum / (Arr.Length - 1)
}
; Other Function
Static Correlation(x, y)
{
Return Statistics.Covariance(x, y) / (Statistics.Stddev(x) * Statistics.Stddev(y))
}
Static Covariance(x, y)
{
Ex := Statistics.FMean(x)
Ey := Statistics.FMean(y)
xy := []
Loop x.Length
xy.Push(x[A_Index] * y[A_Index])
Exy := Statistics.FMean(xy)
Return (Exy - Ex * Ey) * x.Length / (x.Length - 1)
}
Static Linear_Regression(x, y)
{
x_ := Statistics.FMean(x)
y_ := Statistics.FMean(y)
Sum1 := 0
Sum2 := 0
Loop x.Length
{
Sum1 += x[A_Index] * y[A_Index]
Sum2 += x[A_Index] ** 2
}
b := (Sum1 - x.Length * x_ * y_) / (Sum2 - x.Length * (x_ ** 2))
a := y_ - b * x_
Return [b, a]
}
Static Quantiles(Arr, n := 4, method := "exclusive")
{
Arr := Arr.Clone()
Loop Arr.Length
{
Index := A_Index
Loop Arr.Length - Index
{
if Arr[Index] > Arr[A_Index + Index]
{
Tmp := Arr[Index]
Arr[Index] := Arr[A_Index + Index]
Arr[A_Index + Index] := Tmp
}
}
}
if method == "inclusive"
{
m := Arr.Length - 1
result := []
Loop n - 1
{
j := A_Index * m // n
delta := Mod(A_Index * m, n)
interpolated := (Arr[j + 1] * (n - delta) + Arr[j + 2] * delta) / n
result.Push(interpolated)
}
Return result
}
if method == "exclusive"
{
m := Arr.Length + 1
result := []
Loop n - 1
{
j := A_Index * m // n
j := (j < 1) ? 1 : (j > Arr.Length - 1) ? Arr.Length - 1 : j
delta := A_Index * m - j * n
interpolated := (Arr[j] * (n - delta) + Arr[j + 1] * delta) / n
result.Push(interpolated)
}
Return result
}
}
}
二、函数介绍
中心特征 | |
---|---|
平均数 | 算术平均数mean ,浮点算数平均数fmean 几何平均数 geometric_mean ,调和平均数harmonic_mean |
中位数 | median ,分组中位数median_grouped 低中位数 median_low ,高中位数median_high |
众数 | mode ,众数列表multimode |
标准差 | 总体pstdev ,样本stdev |
方差 | 总体pvariance ,样本variance |
quantiles()
将数据以相等的概率分为多个间隔;covariance()
计算样本协方差;correlation()
计算皮尔逊相关系数;linear_regression()
计算简单线性回归的斜率和截距。