-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSummaryStats.py
executable file
·122 lines (112 loc) · 3.64 KB
/
SummaryStats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#====================================================================
# Copyright (C)2016 William H. Majoros ([email protected]).
# This is OPEN SOURxCE SOFTWARE governed by the Gnu General Public
# License (GPL) version 3, as described at www.opensource.org.
#====================================================================
from __future__ import (absolute_import, division, print_function,
unicode_literals, generators, nested_scopes, with_statement)
from builtins import (bytes, dict, int, list, object, range, str, ascii,
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
import math
######################################################################
# Attributes:
#
# Methods:
# [mean,SD,min,max]=SummaryStats.summaryStats(array)
# [mean,SD,min,max]=SummaryStats.roundedSummaryStats(array)
# sum=SummaryStats.sum(array)
# r=SummaryStats.correlation(array1,array2)
# m=SummaryStats.median(array)
# array=SummaryStats.getQuantiles(values,numQuantiles)
# (mean,SD,Min,Max)=SummaryStats.trimmedStats(array,percent)
######################################################################
class SummaryStats:
"""SummaryStats computes simple mean, variance, and correlation
statistics
"""
@classmethod
def median(self,array):
a=[]
for x in array: a.append(x)
a.sort()
n=len(a)
if(n<1): raise Exception("median is undefined for 0 elements")
halfN=int(n/2)
if(n%2==1): return a[halfN]
return (a[halfN-1]+a[halfN])/2
@classmethod
def getQuantiles(self,values,numQuantiles):
a=[]
for x in values: a.append(x)
a.sort()
n=len(a)
q=[0]
index=0
for i in range(1,numQuantiles):
index=int(float(i)/float(numQuantiles)*float(n))
q.append(a[index])
q.append(a[n-1])
return q
@classmethod
def trimmedStats(self,array,percent):
sorted=[x for x in array]
sorted.sort()
n=len(array)
keep=percent*n
omit=n-keep
first=int(omit/2)
keep=int(keep)
sorted=sorted[first:(first+keep)]
return SummaryStats.summaryStats(sorted)
@classmethod
def summaryStats(self,array):
n=len(array)
minX=None
maxX=None
sumX=0
sumXX=0
for i in range(0,n):
x=array[i]
sumX+=x
sumXX+=x*x
if(i==0): minX=maxX=x
if(x<minX): minX=x
if(x>maxX): maxX=x
meanX=sumX/n
varX=None if n<2 else (sumXX-sumX*sumX/n)/(n-1)
if(varX is not None and varX<0): varX=0
stddevX=math.sqrt(varX) if varX is not None else None
return [meanX,stddevX,minX,maxX]
@classmethod
def roundedSummaryStats(self,array):
[mean,stddev,min,max]=SummaryStats.summaryStats(array)
mean=int(100.0*mean+5.0/9.0)/100.0
stddev=int(100.0*stddev+5.0/9.0)/100.0
min=int(100.0*min+5.0/9.0)/100.0
max=int(100.0*max+5.0/9.0)/100.0
return [mean,stddev,min,max]
@classmethod
def sum(self,array):
s=0.0
n=len(array)
for i in range(0,n):
s+=array[i]
return s;
@classmethod
def correlation(self,Xs,Ys):
sumX=0.0
sumY=0.0
sumXY=0.0
sumXX=0.0
sumYY=0.0
n=len(Xs)
for i in range(0,n):
x=Xs[i]
y=Ys[i]
sumX+=x
sumY+=y
sumXY+=x*y
sumXX+=x*x
sumYY+=y*y
r=(sumXY-sumX*sumY/n)/math.sqrt((sumXX-sumX*sumX/n)*(sumYY-sumY*sumY/n))
return r