Skip to content

Commit 22c78b9

Browse files
authored
Add in some more messages to PMStatisticalSample (#200)
* Create Math-Statistics package and add harmonic mean geometric mean and mode. * Refactor harmonicMean to use a guard clause. Also add in a test to make sure that passing in a negative number causes an error. * change the implementation of geometricMean. * Add in guard clause to prevent use with negative numbers as the behaviour is not well defined. * Prevent a potential float under/overflow by summing the logarithms as suggested by Nicolas Cellier. * Convert extension methods into new class: PMStatisticalSample * Add in aliases in PMStatisticalSample to the basic methods in Collection. Adds in average, mean, median, stdev, variance messages * Add in shortcut class methods for mean, median, stdev, and variance * Change aCollection to aSample
1 parent 28ed5fa commit 22c78b9

File tree

1 file changed

+80
-17
lines changed

1 file changed

+80
-17
lines changed

src/Math-Statistics/PMStatisticalSample.class.st

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,70 @@ Class {
3636
}
3737

3838
{ #category : #information }
39-
PMStatisticalSample class >> geometricMean: aCollection [
39+
PMStatisticalSample class >> geometricMean: aSample [
4040
"Calculate the geometric mean of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
4141

42-
^ (self newFrom: aCollection) geometricMean
42+
^ (self newFrom: aSample) geometricMean
4343
]
4444

4545
{ #category : #information }
46-
PMStatisticalSample class >> harmonicMean: aCollection [
46+
PMStatisticalSample class >> harmonicMean: aSample [
4747
"Calculate the harmonic mean of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
4848

49-
^ (self newFrom: aCollection) harmonicMean
49+
^ (self newFrom: aSample) harmonicMean
5050
]
5151

5252
{ #category : #information }
53-
PMStatisticalSample class >> mode: aCollection [
53+
PMStatisticalSample class >> mean: aSample [
54+
"Calculate the mean of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
55+
56+
^ (self newFrom: aSample) mean
57+
]
58+
59+
{ #category : #information }
60+
PMStatisticalSample class >> median: aSample [
61+
"Calculate the median of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
62+
63+
^ (self newFrom: aSample) median
64+
]
65+
66+
{ #category : #information }
67+
PMStatisticalSample class >> mode: aSample [
5468
"Calculate the mode of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
5569

56-
^ (self newFrom: aCollection) mode
70+
^ (self newFrom: aSample) mode
5771
]
5872

5973
{ #category : #'instance creation' }
60-
PMStatisticalSample class >> newFrom: aCollection [
74+
PMStatisticalSample class >> newFrom: aSample [
6175
"Create a new PMStatisticalSample with aCollection as the data"
6276

6377
| ss |
6478
ss := self new.
65-
ss data: aCollection.
79+
ss data: aSample.
6680
^ ss
6781
]
6882

83+
{ #category : #information }
84+
PMStatisticalSample class >> stdev: aSample [
85+
"Calculate the standard deviation of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
86+
87+
^ (self newFrom: aSample) stdev
88+
]
89+
90+
{ #category : #information }
91+
PMStatisticalSample class >> variance: aSample [
92+
"Calculate the variance of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric."
93+
94+
^ (self newFrom: aSample) variance
95+
]
96+
97+
{ #category : #information }
98+
PMStatisticalSample >> average [
99+
"An alias for the arithmetic mean of the sample"
100+
^ data average
101+
]
102+
69103
{ #category : #accessing }
70104
PMStatisticalSample >> data [
71105
"Get the collection that this StatisticalSample is calculated against"
@@ -74,23 +108,23 @@ PMStatisticalSample >> data [
74108
]
75109

76110
{ #category : #accessing }
77-
PMStatisticalSample >> data: aCollection [
111+
PMStatisticalSample >> data: aSample [
78112
"Set the collection of data points that statistical samples will be made against"
79113

80-
data := aCollection
114+
data := aSample
81115
]
82116

83117
{ #category : #information }
84118
PMStatisticalSample >> geometricMean [
85119
"Answer with the geometric mean of the collection"
86120

87-
"(StatisticalSample new data: #(1 1 2 3 4 5 5 6 6 7 8 9)) geometricMean >>> 3.8583980015011217"
121+
"(PMStatisticalSample new data: #(1 1 2 3 4 5 5 6 6 7 8 9)) geometricMean >>> 3.8583980015011217"
88122

89-
"(StatisticalSample new data: { 4. 1. 1 / 32}) geometricMean >>> 0.49999999999999994"
123+
"(PMStatisticalSample new data: { 4. 1. 1 / 32}) geometricMean >>> 0.49999999999999994"
90124

91-
"(StatisticalSample new data: #(3.14 1 4.56 0.333)) geometricMean >>> 1.4776945822943937"
125+
"(PMStatisticalSample new data: #(3.14 1 4.56 0.333)) geometricMean >>> 1.4776945822943937"
92126

93-
"(StatisticalSample new data: {1/3. 2/3. 8/3. 16/3}) geometricMean >>> 1.3333333333333335"
127+
"(PMStatisticalSample new data: {1/3. 2/3. 8/3. 16/3}) geometricMean >>> 1.3333333333333335"
94128

95129
data
96130
detect: [ :i | i <= 0 ]
@@ -108,7 +142,7 @@ PMStatisticalSample >> geometricMean [
108142
PMStatisticalSample >> harmonicMean [
109143
"Answer with the harmonic mean of the data."
110144

111-
"(StatisticalSample new data: #(2.5 3 10)) harmonicMean >>> 3.6"
145+
"(PMStatisticalSample new data: #(2.5 3 10)) harmonicMean >>> 3.6"
112146

113147
| sum |
114148
data
@@ -120,16 +154,45 @@ PMStatisticalSample >> harmonicMean [
120154
^ data size / sum
121155
]
122156

157+
{ #category : #information }
158+
PMStatisticalSample >> mean [
159+
"answers with the arithmetic mean of the sample."
160+
161+
"(PMStatisticalSample new data: #(1 2 2 2 3 4 5)) mean >>> (19/7)"
162+
163+
"(PMStatisticalSample new data: #(5 5 1 1 2 3 4)) mean >>> 3"
164+
165+
^ self average
166+
]
167+
168+
{ #category : #information }
169+
PMStatisticalSample >> median [
170+
"The middle value of a statistical sample."
171+
^ data median
172+
]
173+
123174
{ #category : #information }
124175
PMStatisticalSample >> mode [
125176
"answers with the most common value in a collection.
126177
127178
If there are values that are equally common then the one that is
128179
smallest is returned."
129180

130-
"(StatisticalSample new data: #(1 2 2 2 3 4 5)) mode >>> 2"
181+
"(PMStatisticalSample new data: #(1 2 2 2 3 4 5)) mode >>> 2"
131182

132-
"(StatisticalSample new data: #(5 5 1 1 2 3 4)) mode >>> 1"
183+
"(PMStatisticalSample new data: #(5 5 1 1 2 3 4)) mode >>> 1"
133184

134185
^ (data asBag sortedCounts at: 1) value
135186
]
187+
188+
{ #category : #information }
189+
PMStatisticalSample >> stdev [
190+
"Answers with the standard deviation of the statistical sample"
191+
^ data stdev
192+
]
193+
194+
{ #category : #information }
195+
PMStatisticalSample >> variance [
196+
"Answers with the variance of the statistical sample."
197+
^ self stdev squared
198+
]

0 commit comments

Comments
 (0)