diff --git a/src/Math-Core-Distribution/PMNormalDistribution.class.st b/src/Math-Core-Distribution/PMNormalDistribution.class.st index e0a948d7a..bced833aa 100644 --- a/src/Math-Core-Distribution/PMNormalDistribution.class.st +++ b/src/Math-Core-Distribution/PMNormalDistribution.class.st @@ -8,9 +8,22 @@ Class { #classVars : [ 'NextRandom' ], - #category : 'Math-Core-Distribution' + #category : #'Math-Core-Distribution' } +{ #category : #information } +PMNormalDistribution class >> boxMullerTransform [ +|v1 v2 w y| + [ v1 := Number random * 2 - 1. + v2 := Number random * 2 - 1. + w := v1 squared + v2 squared. + w > 1 ] whileTrue. + y := (w ln * 2 negated / w) sqrt. + v1 := y * v1. + NextRandom := y * v2. + ^v1. +] + { #category : #information } PMNormalDistribution class >> distributionName [ @@ -41,20 +54,15 @@ PMNormalDistribution class >> new: aNumber1 sigma: aNumber2 [ { #category : #information } PMNormalDistribution class >> random [ "Answer a random number distributed according to a (0,1) normal distribution." - | v1 v2 w y | - NextRandom isNil - ifTrue: [ [ v1 := Number random * 2 - 1. - v2 := Number random * 2 - 1. - w := v1 squared + v2 squared. - w > 1 ] whileTrue: []. - y := ( ( w ln * 2 negated) / w) sqrt. - v1 := y * v1. - NextRandom := y * v2. - ] - ifFalse:[ v1 :=NextRandom. - NextRandom := nil. - ]. - ^v1 + + | v1 | + NextRandom + ifNil: [ + + v1 := self boxMullerTransform ] + ifNotNil: [ v1 := NextRandom. + NextRandom := nil ]. + ^ v1 ] { #category : #information } @@ -78,10 +86,8 @@ PMNormalDistribution >> distributionValue: aNumber [ { #category : #initialization } PMNormalDistribution >> initialize: aNumber1 sigma: aNumber2 [ - mu := aNumber1. - sigma := aNumber2. - ^self + sigma := aNumber2 ] { #category : #information } @@ -98,8 +104,9 @@ PMNormalDistribution >> parameters [ { #category : #information } PMNormalDistribution >> random [ - "Answer a random number distributed accroding to the receiver." - ^self class random * sigma + mu + "Answer a random number distributed according to the receiver." + + ^ self class random * sigma + mu ] { #category : #information } diff --git a/src/Math-Numerical/Number.extension.st b/src/Math-Numerical/Number.extension.st index 31ef61b6a..a16408381 100644 --- a/src/Math-Numerical/Number.extension.st +++ b/src/Math-Numerical/Number.extension.st @@ -70,15 +70,16 @@ Number >> productWithVector: aVector [ ] { #category : #'*Math-Numerical' } -Number >> random [ - "Answers a random number distributed between 0 and the receiver." - ^self class random * self +Number class >> random [ + "Answers a floating random number between 0 and 1 excluded" + + ^ PMMitchellMooreGenerator new floatValue ] { #category : #'*Math-Numerical' } -Number class >> random [ - "Answers a random number between 0 and the receiver." - ^ PMMitchellMooreGenerator new floatValue +Number >> random [ + "Answers a random number distributed between 0 and the receiver." + ^self class random * self ] { #category : #'*Math-Numerical' } diff --git a/src/Math-Numerical/PMCauchyDistribution.class.st b/src/Math-Numerical/PMCauchyDistribution.class.st index 50e2bffbc..6b8d05e11 100644 --- a/src/Math-Numerical/PMCauchyDistribution.class.st +++ b/src/Math-Numerical/PMCauchyDistribution.class.st @@ -59,10 +59,10 @@ PMCauchyDistribution >> distributionValue: aNumber [ { #category : #initialization } PMCauchyDistribution >> initialize: aNumber1 scale: aNumber2 [ - "Private - Initialize the parameters of the receiver. " + "Initialize the parameters of the receiver. " + mu := aNumber1. - beta := aNumber2. - ^self + beta := aNumber2 ] { #category : #information } diff --git a/src/Math-Numerical/PMExponentialDistribution.class.st b/src/Math-Numerical/PMExponentialDistribution.class.st index 495f5e3ac..962727adb 100644 --- a/src/Math-Numerical/PMExponentialDistribution.class.st +++ b/src/Math-Numerical/PMExponentialDistribution.class.st @@ -66,8 +66,7 @@ PMExponentialDistribution >> initialize: aNumber [ aNumber > 0 ifFalse: [ self error: 'Illegal distribution parameters' ]. - beta := aNumber. - ^ self + beta := aNumber ] { #category : #information } diff --git a/src/Math-Numerical/PMFisherTippettDistribution.class.st b/src/Math-Numerical/PMFisherTippettDistribution.class.st index fa9e30269..39a78a546 100644 --- a/src/Math-Numerical/PMFisherTippettDistribution.class.st +++ b/src/Math-Numerical/PMFisherTippettDistribution.class.st @@ -68,12 +68,12 @@ PMFisherTippettDistribution >> distributionValue: aNumber [ { #category : #initialization } PMFisherTippettDistribution >> initialize: aNumber1 scale: aNumber2 [ - "Private - Initialize the parameters of the receiver." - aNumber2 > 0 - ifFalse: [ self error: 'Illegal distribution parameters']. + "Initialize the parameters of the receiver." + + aNumber2 <= 0 + ifTrue: [ self error: 'Illegal distribution parameters' ]. alpha := aNumber1. - beta := aNumber2. - ^self + beta := aNumber2 ] { #category : #information } @@ -104,11 +104,12 @@ PMFisherTippettDistribution >> parameters [ { #category : #information } PMFisherTippettDistribution >> random [ - "Answer a random number distributed according to the receiver." + "Answer a random number distributed according to the receiver." + | t | [ t := flatGenerator floatValue ln negated. - t > 0] whileFalse: []. - ^t ln negated * beta + alpha + t > 0 ] whileFalse. + ^ t ln negated * beta + alpha ] { #category : #information }