From 0e41e79999fbbdf24e65d91d810331b0c450f223 Mon Sep 17 00:00:00 2001 From: Connor Skennerton Date: Tue, 5 Jan 2021 21:10:02 -0800 Subject: [PATCH 1/7] Create Math-Statistics package and add harmonic mean geometric mean and mode. --- src/Math-Statistics/Collection.extension.st | 37 +++++++++++++++++++ src/Math-Statistics/package.st | 1 + .../PMStatisticsTests.class.st | 35 ++++++++++++++++++ src/Math-Tests-Statistics/package.st | 1 + 4 files changed, 74 insertions(+) create mode 100644 src/Math-Statistics/Collection.extension.st create mode 100644 src/Math-Statistics/package.st create mode 100644 src/Math-Tests-Statistics/PMStatisticsTests.class.st create mode 100644 src/Math-Tests-Statistics/package.st diff --git a/src/Math-Statistics/Collection.extension.st b/src/Math-Statistics/Collection.extension.st new file mode 100644 index 00000000..e4202a2f --- /dev/null +++ b/src/Math-Statistics/Collection.extension.st @@ -0,0 +1,37 @@ +Extension { #name : #Collection } + +{ #category : #'*Math-Statistics' } +Collection >> geometricMean [ + "Answer with the geometric mean of the collection" + "#(1 1 2 3 4 5 5 6 6 7 8 9) geometricMean >>> 3.8583980015011208" + "{ 4. 1. 1 / 32} geometricMean >>> (1/2)" + "#(3.14 1 4.56 0.333) geometricMean >>> 1.4776945822943937" + + ^(self reduce: [ :a :b | a * b ] ) raisedToFraction: 1 / self size. +] + +{ #category : #'*Math-Statistics' } +Collection >> harmonicMean [ + "Answer with the harmonic mean of the data." + "#(2.5, 3, 10) harmonicMean >>> 3.6" + | sum | + sum := 0. + + self do: [ :i | i < 0 + ifTrue: [Error new signal: 'The harmonic mean cannot be calculated on negative numbers'] ifFalse: [ sum := (sum + (1 / i))] + ]. + ^self size / sum. + +] + +{ #category : #'*Math-Statistics' } +Collection >> 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." + "#(1 2 2 2 3 4 5) mode >>> 2" + "#(5 5 1 1 2 3 4) mode >>> 1" + + ^(self asBag sortedCounts at: 1) value. +] diff --git a/src/Math-Statistics/package.st b/src/Math-Statistics/package.st new file mode 100644 index 00000000..fe61ac1c --- /dev/null +++ b/src/Math-Statistics/package.st @@ -0,0 +1 @@ +Package { #name : #'Math-Statistics' } diff --git a/src/Math-Tests-Statistics/PMStatisticsTests.class.st b/src/Math-Tests-Statistics/PMStatisticsTests.class.st new file mode 100644 index 00000000..bb0063ca --- /dev/null +++ b/src/Math-Tests-Statistics/PMStatisticsTests.class.st @@ -0,0 +1,35 @@ +Class { + #name : #PMStatisticsTests, + #superclass : #TestCase, + #category : #'Math-Tests-Statistics' +} + +{ #category : #tests } +PMStatisticsTests >> testGeometricMean [ + self assert: #(1 1 2 3 4 5 5 6 6 7 8 9) geometricMean equals: 3.8583980015011208. + + self assert: { 4. 1. 1 / 32} geometricMean equals: (1/2). + + self assert: #(3.14 1 4.56 0.333) geometricMean equals: 1.4776945822943937. +] + +{ #category : #tests } +PMStatisticsTests >> testHarmonicMean [ + self assert: (#(2.5 3 10) harmonicMean round: 1) equals: 3.6. +] + +{ #category : #tests } +PMStatisticsTests >> testMode [ + | c | + c := #(1 2 3 4 5 5). + self assert: c mode equals: 5. + + c := #(5 6 8 3 3 3 2 2 2). + self assert: c mode equals: 2. + + c := #(1.1 1.2 1.1 3.4). + self assert: c mode equals: 1.1. + + c := #(dat1 dat2 dat4 dat1 dat4 dat4 dat2 dat9 dat8). + self assert: c mode equals: 'dat4'. +] diff --git a/src/Math-Tests-Statistics/package.st b/src/Math-Tests-Statistics/package.st new file mode 100644 index 00000000..2aba6d6e --- /dev/null +++ b/src/Math-Tests-Statistics/package.st @@ -0,0 +1 @@ +Package { #name : #'Math-Tests-Statistics' } From 098a7058063a46fdb5a7c5d8b91af58b397de12f Mon Sep 17 00:00:00 2001 From: Connor Skennerton Date: Wed, 6 Jan 2021 20:39:23 -0800 Subject: [PATCH 2/7] Refactor harmonicMean to use a guard clause. Also add in a test to make sure that passing in a negative number causes an error. --- src/Math-Statistics/Collection.extension.st | 8 ++++---- src/Math-Tests-Statistics/PMStatisticsTests.class.st | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Math-Statistics/Collection.extension.st b/src/Math-Statistics/Collection.extension.st index e4202a2f..faff967b 100644 --- a/src/Math-Statistics/Collection.extension.st +++ b/src/Math-Statistics/Collection.extension.st @@ -15,11 +15,11 @@ Collection >> harmonicMean [ "Answer with the harmonic mean of the data." "#(2.5, 3, 10) harmonicMean >>> 3.6" | sum | - sum := 0. - self do: [ :i | i < 0 - ifTrue: [Error new signal: 'The harmonic mean cannot be calculated on negative numbers'] ifFalse: [ sum := (sum + (1 / i))] - ]. + self detect: [ :i | i < 0 ] ifFound: [ Error new signal: 'The harmonic mean cannot be calculated on negative numbers' ] . + + sum := 0. + self do: [ :i | sum := (sum + (1 / i))]. ^self size / sum. ] diff --git a/src/Math-Tests-Statistics/PMStatisticsTests.class.st b/src/Math-Tests-Statistics/PMStatisticsTests.class.st index bb0063ca..cc186d8e 100644 --- a/src/Math-Tests-Statistics/PMStatisticsTests.class.st +++ b/src/Math-Tests-Statistics/PMStatisticsTests.class.st @@ -16,6 +16,8 @@ PMStatisticsTests >> testGeometricMean [ { #category : #tests } PMStatisticsTests >> testHarmonicMean [ self assert: (#(2.5 3 10) harmonicMean round: 1) equals: 3.6. + + self should: [ #(3 1.1 -1 ) harmonicMean ] raise: Error. ] { #category : #tests } From 1278ab1622ca3b4002daf27b3acf40610ada030c Mon Sep 17 00:00:00 2001 From: Connor Skennerton Date: Thu, 7 Jan 2021 21:12:42 -0800 Subject: [PATCH 3/7] 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. --- src/Math-Statistics/Collection.extension.st | 13 ++++++++++--- .../PMStatisticsTests.class.st | 8 +++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Math-Statistics/Collection.extension.st b/src/Math-Statistics/Collection.extension.st index faff967b..af35c5e6 100644 --- a/src/Math-Statistics/Collection.extension.st +++ b/src/Math-Statistics/Collection.extension.st @@ -3,11 +3,18 @@ Extension { #name : #Collection } { #category : #'*Math-Statistics' } Collection >> geometricMean [ "Answer with the geometric mean of the collection" - "#(1 1 2 3 4 5 5 6 6 7 8 9) geometricMean >>> 3.8583980015011208" - "{ 4. 1. 1 / 32} geometricMean >>> (1/2)" + "#(1 1 2 3 4 5 5 6 6 7 8 9) geometricMean >>> 3.8583980015011217" + "{ 4. 1. 1 / 32} geometricMean >>> 0.49999999999999994" "#(3.14 1 4.56 0.333) geometricMean >>> 1.4776945822943937" + "{1/3. 2/3. 8/3. 16/3} geometricMean >>> 1.3333333333333335" - ^(self reduce: [ :a :b | a * b ] ) raisedToFraction: 1 / self size. + self detect: [ :i | i < 0 ] ifFound: [ Error new signal: 'The geometric mean should not be calculated on negative 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." + ^((self collect: #ln) sum / self size) exp ] { #category : #'*Math-Statistics' } diff --git a/src/Math-Tests-Statistics/PMStatisticsTests.class.st b/src/Math-Tests-Statistics/PMStatisticsTests.class.st index cc186d8e..30b05db0 100644 --- a/src/Math-Tests-Statistics/PMStatisticsTests.class.st +++ b/src/Math-Tests-Statistics/PMStatisticsTests.class.st @@ -6,11 +6,13 @@ Class { { #category : #tests } PMStatisticsTests >> testGeometricMean [ - self assert: #(1 1 2 3 4 5 5 6 6 7 8 9) geometricMean equals: 3.8583980015011208. + self assert: (#(1 1 2 3 4 5 5 6 6 7 8 9) geometricMean round: 4) equals: 3.8584. - self assert: { 4. 1. 1 / 32} geometricMean equals: (1/2). + self assert: ({ 4. 1. 1 / 32} geometricMean round: 4) equals: 0.5. - self assert: #(3.14 1 4.56 0.333) geometricMean equals: 1.4776945822943937. + self assert: (#(3.14 1 4.56 0.333) geometricMean round: 4 ) equals: 1.4777. + + self should: [ #(-1 5 6.78) geometricMean ] raise: Error. ] { #category : #tests } From 6c81b07e731ac26b18ee490f86137f00846faab9 Mon Sep 17 00:00:00 2001 From: Connor Skennerton Date: Sun, 7 Mar 2021 23:17:36 -0800 Subject: [PATCH 4/7] Convert extension methods into new class: PMStatisticalSample --- src/Math-Statistics/Collection.extension.st | 44 ------ .../PMStatisticalSample.class.st | 135 ++++++++++++++++++ .../PMStatisticsTests.class.st | 63 +++++--- 3 files changed, 180 insertions(+), 62 deletions(-) delete mode 100644 src/Math-Statistics/Collection.extension.st create mode 100644 src/Math-Statistics/PMStatisticalSample.class.st diff --git a/src/Math-Statistics/Collection.extension.st b/src/Math-Statistics/Collection.extension.st deleted file mode 100644 index af35c5e6..00000000 --- a/src/Math-Statistics/Collection.extension.st +++ /dev/null @@ -1,44 +0,0 @@ -Extension { #name : #Collection } - -{ #category : #'*Math-Statistics' } -Collection >> geometricMean [ - "Answer with the geometric mean of the collection" - "#(1 1 2 3 4 5 5 6 6 7 8 9) geometricMean >>> 3.8583980015011217" - "{ 4. 1. 1 / 32} geometricMean >>> 0.49999999999999994" - "#(3.14 1 4.56 0.333) geometricMean >>> 1.4776945822943937" - "{1/3. 2/3. 8/3. 16/3} geometricMean >>> 1.3333333333333335" - - self detect: [ :i | i < 0 ] ifFound: [ Error new signal: 'The geometric mean should not be calculated on negative 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." - ^((self collect: #ln) sum / self size) exp -] - -{ #category : #'*Math-Statistics' } -Collection >> harmonicMean [ - "Answer with the harmonic mean of the data." - "#(2.5, 3, 10) harmonicMean >>> 3.6" - | sum | - - self detect: [ :i | i < 0 ] ifFound: [ Error new signal: 'The harmonic mean cannot be calculated on negative numbers' ] . - - sum := 0. - self do: [ :i | sum := (sum + (1 / i))]. - ^self size / sum. - -] - -{ #category : #'*Math-Statistics' } -Collection >> 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." - "#(1 2 2 2 3 4 5) mode >>> 2" - "#(5 5 1 1 2 3 4) mode >>> 1" - - ^(self asBag sortedCounts at: 1) value. -] diff --git a/src/Math-Statistics/PMStatisticalSample.class.st b/src/Math-Statistics/PMStatisticalSample.class.st new file mode 100644 index 00000000..cce9f074 --- /dev/null +++ b/src/Math-Statistics/PMStatisticalSample.class.st @@ -0,0 +1,135 @@ +" +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: aCollection [ + "Calculate the geometric mean of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric." + + ^ (self newFrom: aCollection) geometricMean +] + +{ #category : #information } +PMStatisticalSample class >> harmonicMean: aCollection [ + "Calculate the harmonic mean of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric." + + ^ (self newFrom: aCollection) harmonicMean +] + +{ #category : #information } +PMStatisticalSample class >> mode: aCollection [ + "Calculate the mode of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric." + + ^ (self newFrom: aCollection) mode +] + +{ #category : #'instance creation' } +PMStatisticalSample class >> newFrom: aCollection [ + "Create a new PMStatisticalSample with aCollection as the data" + + | ss | + ss := self new. + ss data: aCollection. + ^ ss +] + +{ #category : #accessing } +PMStatisticalSample >> data [ + "Get the collection that this StatisticalSample is calculated against" + + ^data +] + +{ #category : #accessing } +PMStatisticalSample >> data: aCollection [ + "Set the collection of data points that statistical samples will be made against" + + data := aCollection +] + +{ #category : #information } +PMStatisticalSample >> geometricMean [ + "Answer with the geometric mean of the collection" + + "(StatisticalSample new data: #(1 1 2 3 4 5 5 6 6 7 8 9)) geometricMean >>> 3.8583980015011217" + + "(StatisticalSample new data: { 4. 1. 1 / 32}) geometricMean >>> 0.49999999999999994" + + "(StatisticalSample new data: #(3.14 1 4.56 0.333)) geometricMean >>> 1.4776945822943937" + + "(StatisticalSample 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." + + "(StatisticalSample 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 >> 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." + + "(StatisticalSample new data: #(1 2 2 2 3 4 5)) mode >>> 2" + + "(StatisticalSample new data: #(5 5 1 1 2 3 4)) mode >>> 1" + + ^ (data asBag sortedCounts at: 1) value +] diff --git a/src/Math-Tests-Statistics/PMStatisticsTests.class.st b/src/Math-Tests-Statistics/PMStatisticsTests.class.st index 30b05db0..0fe7dc30 100644 --- a/src/Math-Tests-Statistics/PMStatisticsTests.class.st +++ b/src/Math-Tests-Statistics/PMStatisticsTests.class.st @@ -6,34 +6,61 @@ Class { { #category : #tests } PMStatisticsTests >> testGeometricMean [ - self assert: (#(1 1 2 3 4 5 5 6 6 7 8 9) geometricMean round: 4) equals: 3.8584. - - self assert: ({ 4. 1. 1 / 32} geometricMean round: 4) equals: 0.5. - - self assert: (#(3.14 1 4.56 0.333) geometricMean round: 4 ) equals: 1.4777. - - self should: [ #(-1 5 6.78) geometricMean ] raise: Error. + self + assert: + ((PMStatisticalSample new data: #(1 1 2 3 4 5 5 6 6 7 8 9)) + geometricMean round: 4) + equals: 3.8584. + self + assert: + ((PMStatisticalSample new + data: + {4. + 1. + (1 / 32)}) geometricMean round: 4) + equals: 0.5. + self + assert: + ((PMStatisticalSample new data: #(3.14 1 4.56 0.333)) geometricMean + round: 4) + equals: 1.4777. + self + should: [ (PMStatisticalSample new data: #(-1 5 6.78)) geometricMean ] + raise: Error. + self + assert: + ((PMStatisticalSample geometricMean: + {4. + 1. + (1 / 32)}) round: 4) + equals: 0.5. ] { #category : #tests } PMStatisticsTests >> testHarmonicMean [ - self assert: (#(2.5 3 10) harmonicMean round: 1) equals: 3.6. - - self should: [ #(3 1.1 -1 ) harmonicMean ] raise: Error. + self + assert: ((PMStatisticalSample new data: #(2.5 3 10)) harmonicMean round: 1) + equals: 3.6. + self + assert: ((PMStatisticalSample harmonicMean: #(2.5 3 10)) round: 1) + equals: 3.6. + self + should: [ (PMStatisticalSample new data: #(3 1.1 -1)) harmonicMean ] + raise: Error ] { #category : #tests } PMStatisticsTests >> testMode [ | c | - c := #(1 2 3 4 5 5). + c := PMStatisticalSample new data: #(1 2 3 4 5 5). self assert: c mode equals: 5. - - c := #(5 6 8 3 3 3 2 2 2). + c := PMStatisticalSample mode: #(1 2 3 4 5 5). + self assert: c equals: 5. + c := PMStatisticalSample new data: #(5 6 8 3 3 3 2 2 2). self assert: c mode equals: 2. - - c := #(1.1 1.2 1.1 3.4). + c := PMStatisticalSample new data: #(1.1 1.2 1.1 3.4). self assert: c mode equals: 1.1. - - c := #(dat1 dat2 dat4 dat1 dat4 dat4 dat2 dat9 dat8). - self assert: c mode equals: 'dat4'. + c := PMStatisticalSample new + data: #(dat1 dat2 dat4 dat1 dat4 dat4 dat2 dat9 dat8). + self assert: c mode equals: 'dat4' ] From c7bde455aa656724edbde08c2127137747e6b2a1 Mon Sep 17 00:00:00 2001 From: Connor Skennerton Date: Fri, 2 Apr 2021 17:21:12 -0700 Subject: [PATCH 5/7] Add in aliases in PMStatisticalSample to the basic methods in Collection. Adds in average, mean, median, stdev, variance messages --- .../PMStatisticalSample.class.st | 49 ++++++++++++++++--- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/src/Math-Statistics/PMStatisticalSample.class.st b/src/Math-Statistics/PMStatisticalSample.class.st index cce9f074..ad6fe67f 100644 --- a/src/Math-Statistics/PMStatisticalSample.class.st +++ b/src/Math-Statistics/PMStatisticalSample.class.st @@ -66,6 +66,12 @@ PMStatisticalSample class >> newFrom: aCollection [ ^ ss ] +{ #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" @@ -84,13 +90,13 @@ PMStatisticalSample >> data: aCollection [ PMStatisticalSample >> geometricMean [ "Answer with the geometric mean of the collection" - "(StatisticalSample new data: #(1 1 2 3 4 5 5 6 6 7 8 9)) geometricMean >>> 3.8583980015011217" + "(PMStatisticalSample new data: #(1 1 2 3 4 5 5 6 6 7 8 9)) geometricMean >>> 3.8583980015011217" - "(StatisticalSample new data: { 4. 1. 1 / 32}) geometricMean >>> 0.49999999999999994" + "(PMStatisticalSample new data: { 4. 1. 1 / 32}) geometricMean >>> 0.49999999999999994" - "(StatisticalSample new data: #(3.14 1 4.56 0.333)) geometricMean >>> 1.4776945822943937" + "(PMStatisticalSample new data: #(3.14 1 4.56 0.333)) geometricMean >>> 1.4776945822943937" - "(StatisticalSample new data: {1/3. 2/3. 8/3. 16/3}) geometricMean >>> 1.3333333333333335" + "(PMStatisticalSample new data: {1/3. 2/3. 8/3. 16/3}) geometricMean >>> 1.3333333333333335" data detect: [ :i | i <= 0 ] @@ -108,7 +114,7 @@ PMStatisticalSample >> geometricMean [ PMStatisticalSample >> harmonicMean [ "Answer with the harmonic mean of the data." - "(StatisticalSample new data: #(2.5 3 10)) harmonicMean >>> 3.6" + "(PMStatisticalSample new data: #(2.5 3 10)) harmonicMean >>> 3.6" | sum | data @@ -120,6 +126,23 @@ PMStatisticalSample >> harmonicMean [ ^ 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. @@ -127,9 +150,21 @@ PMStatisticalSample >> mode [ If there are values that are equally common then the one that is smallest is returned." - "(StatisticalSample new data: #(1 2 2 2 3 4 5)) mode >>> 2" + "(PMStatisticalSample new data: #(1 2 2 2 3 4 5)) mode >>> 2" - "(StatisticalSample new data: #(5 5 1 1 2 3 4)) mode >>> 1" + "(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 +] From 77e417fd71bb1b30eef983397ee5e2fe99411145 Mon Sep 17 00:00:00 2001 From: Connor Skennerton Date: Fri, 2 Apr 2021 17:29:23 -0700 Subject: [PATCH 6/7] Add in shortcut class methods for mean, median, stdev, and variance --- .../PMStatisticalSample.class.st | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Math-Statistics/PMStatisticalSample.class.st b/src/Math-Statistics/PMStatisticalSample.class.st index ad6fe67f..1c946e6c 100644 --- a/src/Math-Statistics/PMStatisticalSample.class.st +++ b/src/Math-Statistics/PMStatisticalSample.class.st @@ -18,6 +18,7 @@ Now you can get statistical measures of the data using any of the following mess 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) @@ -49,6 +50,20 @@ PMStatisticalSample class >> harmonicMean: aCollection [ ^ (self newFrom: aCollection) harmonicMean ] +{ #category : #information } +PMStatisticalSample class >> mean: aCollection [ + "Calculate the mean of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric." + + ^ (self newFrom: aCollection) mean +] + +{ #category : #information } +PMStatisticalSample class >> median: aCollection [ + "Calculate the median of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric." + + ^ (self newFrom: aCollection) median +] + { #category : #information } PMStatisticalSample class >> mode: aCollection [ "Calculate the mode of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric." @@ -66,6 +81,20 @@ PMStatisticalSample class >> newFrom: aCollection [ ^ ss ] +{ #category : #information } +PMStatisticalSample class >> stdev: aCollection [ + "Calculate the standard deviation of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric." + + ^ (self newFrom: aCollection) stdev +] + +{ #category : #information } +PMStatisticalSample class >> variance: aCollection [ + "Calculate the variance of a collection. This shortcut method will create a new instance of PMStatisticalSample and return the desired metric." + + ^ (self newFrom: aCollection) variance +] + { #category : #information } PMStatisticalSample >> average [ "An alias for the arithmetic mean of the sample" From d9ea19855a8082c926ccdb38c76506fd9911ed94 Mon Sep 17 00:00:00 2001 From: Connor Skennerton Date: Sat, 17 Apr 2021 15:57:32 -0700 Subject: [PATCH 7/7] Change aCollection to aSample --- .../PMStatisticalSample.class.st | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Math-Statistics/PMStatisticalSample.class.st b/src/Math-Statistics/PMStatisticalSample.class.st index 1c946e6c..78eb00c7 100644 --- a/src/Math-Statistics/PMStatisticalSample.class.st +++ b/src/Math-Statistics/PMStatisticalSample.class.st @@ -37,62 +37,62 @@ Class { } { #category : #information } -PMStatisticalSample class >> geometricMean: aCollection [ +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: aCollection) geometricMean + ^ (self newFrom: aSample) geometricMean ] { #category : #information } -PMStatisticalSample class >> harmonicMean: aCollection [ +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: aCollection) harmonicMean + ^ (self newFrom: aSample) harmonicMean ] { #category : #information } -PMStatisticalSample class >> mean: aCollection [ +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: aCollection) mean + ^ (self newFrom: aSample) mean ] { #category : #information } -PMStatisticalSample class >> median: aCollection [ +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: aCollection) median + ^ (self newFrom: aSample) median ] { #category : #information } -PMStatisticalSample class >> mode: aCollection [ +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: aCollection) mode + ^ (self newFrom: aSample) mode ] { #category : #'instance creation' } -PMStatisticalSample class >> newFrom: aCollection [ +PMStatisticalSample class >> newFrom: aSample [ "Create a new PMStatisticalSample with aCollection as the data" | ss | ss := self new. - ss data: aCollection. + ss data: aSample. ^ ss ] { #category : #information } -PMStatisticalSample class >> stdev: aCollection [ +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: aCollection) stdev + ^ (self newFrom: aSample) stdev ] { #category : #information } -PMStatisticalSample class >> variance: aCollection [ +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: aCollection) variance + ^ (self newFrom: aSample) variance ] { #category : #information } @@ -109,10 +109,10 @@ PMStatisticalSample >> data [ ] { #category : #accessing } -PMStatisticalSample >> data: aCollection [ +PMStatisticalSample >> data: aSample [ "Set the collection of data points that statistical samples will be made against" - data := aCollection + data := aSample ] { #category : #information }