Repository navigation
Expand file tree
/
Copy pathPMStatisticalSample.class.st
More file actions
198 lines (144 loc) · 5.74 KB
/
Copy pathPMStatisticalSample.class.st
File metadata and controls
198 lines (144 loc) · 5.74 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"
I am a collection of data that statistical measures can be applied to.
My input collection can be anything but most measures need numerical data to work properly.
Create me by calling new and then calling my `data:` message
sample := PMStatisticalSample new
sample data: #(1 2 3 4 5)
or using my `newFrom:` class message
sample := PMStatisticalSample newFrom: #(1 2 3 4 5)
Now you can get statistical measures of the data using any of the following messages
sample mode
sample geometricMean
sample harmonicMean
If you only want one statistical measure for a collection you can use my class methods
gMean := PMStatisticalSample geometricMean: #(1 2 3 4 5)
hMean := PMStatisticalSample harmonicMean: #(1 2 3 4 5)
mode := PMStatisticalSample mode: #(1 2 3 4 5)
"
Class {
#name : #PMStatisticalSample,
#superclass : #Object,
#instVars : [
'data'
],
#category : #'Math-Statistics'
}
{ #category : #information }
PMStatisticalSample class >> geometricMean: aSample [
"Calculate the geometric mean of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
^ (self newFrom: aSample) geometricMean
]
{ #category : #information }
PMStatisticalSample class >> harmonicMean: aSample [
"Calculate the harmonic mean of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
^ (self newFrom: aSample) harmonicMean
]
{ #category : #information }
PMStatisticalSample class >> mean: aSample [
"Calculate the mean of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
^ (self newFrom: aSample) mean
]
{ #category : #information }
PMStatisticalSample class >> median: aSample [
"Calculate the median of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
^ (self newFrom: aSample) median
]
{ #category : #information }
PMStatisticalSample class >> mode: aSample [
"Calculate the mode of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
^ (self newFrom: aSample) mode
]
{ #category : #'instance creation' }
PMStatisticalSample class >> newFrom: aSample [
"Create a new PMStatisticalSample with aCollection as the data"
| ss |
ss := self new.
ss data: aSample.
^ ss
]
{ #category : #information }
PMStatisticalSample class >> stdev: aSample [
"Calculate the standard deviation of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
^ (self newFrom: aSample) stdev
]
{ #category : #information }
PMStatisticalSample class >> variance: aSample [
"Calculate the variance of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
^ (self newFrom: aSample) variance
]
{ #category : #information }
PMStatisticalSample >> average [
"An alias for the arithmetic mean of the sample"
^ data average
]
{ #category : #accessing }
PMStatisticalSample >> data [
"Get the collection that this StatisticalSample is calculated against"
^data
]
{ #category : #accessing }
PMStatisticalSample >> data: aSample [
"Set the collection of data points that statistical samples will be made against"
data := aSample
]
{ #category : #information }
PMStatisticalSample >> geometricMean [
"Answer with the geometric mean of the collection"
"(PMStatisticalSample new data: #(1 1 2 3 4 5 5 6 6 7 8 9)) geometricMean >>> 3.8583980015011217"
"(PMStatisticalSample new data: { 4. 1. 1 / 32}) geometricMean >>> 0.49999999999999994"
"(PMStatisticalSample new data: #(3.14 1 4.56 0.333)) geometricMean >>> 1.4776945822943937"
"(PMStatisticalSample new data: {1/3. 2/3. 8/3. 16/3}) geometricMean >>> 1.3333333333333335"
data
detect: [ :i | i <= 0 ]
ifFound: [ Error new
signal: 'The geometric mean should only be calculated on positive numbers' ].
"For large collections there is a chance of Float overflow/underflow when raising to a fraction.
Instead sum the logarithms with the side effect that the returned value will be a Float and subject
to floating point arethmetic imprecision"
"^(self reduce: [ :a :b | a * b ] ) raisedToFraction: 1 / self size."
^ ((data collect: #ln) sum / data size) exp
]
{ #category : #information }
PMStatisticalSample >> harmonicMean [
"Answer with the harmonic mean of the data."
"(PMStatisticalSample new data: #(2.5 3 10)) harmonicMean >>> 3.6"
| sum |
data
detect: [ :i | i <= 0 ]
ifFound: [ Error new
signal: 'The harmonic mean should only be calculated on positive numbers' ].
sum := 0.
data do: [ :i | sum := sum + (1 / i) ].
^ data size / sum
]
{ #category : #information }
PMStatisticalSample >> mean [
"answers with the arithmetic mean of the sample."
"(PMStatisticalSample new data: #(1 2 2 2 3 4 5)) mean >>> (19/7)"
"(PMStatisticalSample new data: #(5 5 1 1 2 3 4)) mean >>> 3"
^ self average
]
{ #category : #information }
PMStatisticalSample >> median [
"The middle value of a statistical sample."
^ data median
]
{ #category : #information }
PMStatisticalSample >> mode [
"answers with the most common value in a collection.
If there are values that are equally common then the one that is
smallest is returned."
"(PMStatisticalSample new data: #(1 2 2 2 3 4 5)) mode >>> 2"
"(PMStatisticalSample new data: #(5 5 1 1 2 3 4)) mode >>> 1"
^ (data asBag sortedCounts at: 1) value
]
{ #category : #information }
PMStatisticalSample >> stdev [
"Answers with the standard deviation of the statistical sample"
^ data stdev
]
{ #category : #information }
PMStatisticalSample >> variance [
"Answers with the variance of the statistical sample."
^ self stdev squared
]