Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions src/Math-Core-Distribution/PMNormalDistribution.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@ Class {
#classVars : [
'NextRandom'
],
#category : 'Math-Core-Distribution'
#category : #'Math-Core-Distribution'
}

{ #category : #information }
PMNormalDistribution class >> boxMullerTransform [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's interesting is that this is a class method, rather than an instance one. I presume this choice was made because NextRandom is a class variable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no because PMNormalDistribution class >> random is a class method.

|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 [

Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered what kind of variable this is. It seems global. Is that right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no this is a class variable.

ifNil: [

v1 := self boxMullerTransform ]
ifNotNil: [ v1 := NextRandom.
NextRandom := nil ].
^ v1
]

{ #category : #information }
Expand All @@ -78,10 +86,8 @@ PMNormalDistribution >> distributionValue: aNumber [

{ #category : #initialization }
PMNormalDistribution >> initialize: aNumber1 sigma: aNumber2 [

mu := aNumber1.
sigma := aNumber2.
^self
sigma := aNumber2
]

{ #category : #information }
Expand All @@ -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 }
Expand Down
13 changes: 7 additions & 6 deletions src/Math-Numerical/Number.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down
6 changes: 3 additions & 3 deletions src/Math-Numerical/PMCauchyDistribution.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
3 changes: 1 addition & 2 deletions src/Math-Numerical/PMExponentialDistribution.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ PMExponentialDistribution >> initialize: aNumber [

aNumber > 0
ifFalse: [ self error: 'Illegal distribution parameters' ].
beta := aNumber.
^ self
beta := aNumber
]

{ #category : #information }
Expand Down
17 changes: 9 additions & 8 deletions src/Math-Numerical/PMFisherTippettDistribution.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 }
Expand Down