-
-
Notifications
You must be signed in to change notification settings - Fork 40
Some basic cleaning on various Distributions classes #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f3b94bd
Some basic cleaning on various Distributions classes
SergeStinckwich df0a1a8
- Remove whileTrue:[] and whileFalse: []
SergeStinckwich 2b89664
Typos/fix comment
SergeStinckwich c83319d
Ad d a comment about Box-Muller transform
SergeStinckwich ea87d33
Extract boxMullerTransform to be more intuition revealing
SergeStinckwich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 } | ||
|
|
@@ -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 } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no because
PMNormalDistribution class >> randomis a class method.