Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
cf4acd1
[issue-105] Removed an obselete test.
hemalvarambhia May 31, 2019
51678f2
[issue-105] Improved the name of the class. The class comment says it…
hemalvarambhia May 31, 2019
b741754
[issue-105] Improved the name of the class.
hemalvarambhia May 31, 2019
d978dc1
[issue-105] Extracted duplicate code to an intention-revealing method.
hemalvarambhia May 31, 2019
457538f
[issue-105] Extracted duplicate code to a helper method.
hemalvarambhia May 31, 2019
2099f26
[issue-105] Inlined the method as it's only used in one place.
hemalvarambhia May 31, 2019
b988f04
[issue-105] Improved the name of the method.
hemalvarambhia May 31, 2019
6539d56
[issue-105] Corrected code style.
hemalvarambhia Jun 2, 2019
96a4e6a
[issue-105] Improved the name of the method.
hemalvarambhia Jun 2, 2019
bfa9746
[issue-105] Removed obselete code.
hemalvarambhia Jun 2, 2019
56926ad
[issue-105] Corrected the category.
hemalvarambhia Jun 2, 2019
b0ded6e
[issue-105] Improved the names of the methods.
hemalvarambhia Jun 3, 2019
fe8b9ea
[issue-105] Improved the names of the method.
hemalvarambhia Jun 3, 2019
6fce1ea
[issue-105] Removed obselete test.
hemalvarambhia Jun 3, 2019
5ba526a
[issue-105] Brought back a test deleted accidentally.
hemalvarambhia Jun 3, 2019
e970b16
[issue-105] Made similar code even more similar.
hemalvarambhia Jun 3, 2019
c07a279
[issue-105] Moved the code to a better place, an intention-revealing …
hemalvarambhia Jun 3, 2019
10ad8db
[issue-105] Made the code consistent with that of the other tests. Th…
hemalvarambhia Jun 3, 2019
d40f8e4
[issue-105] Made similar code look the same, improved the name of a t…
hemalvarambhia Jun 3, 2019
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
5 changes: 5 additions & 0 deletions src/Math-Random/PMBinomialGenerator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ PMBinomialGenerator class >> numberOfTrials: numberOfTrials probabilityOfSuccess
yourself
]

{ #category : #accessing }
PMBinomialGenerator >> expectedValue [
^ numberOfTrials * probability .
]

{ #category : #initialization }
PMBinomialGenerator >> initialize [
self generator: PMParkMillerMinimumRandomGenerator new.
Expand Down
2 changes: 1 addition & 1 deletion src/Math-Random/PMLehmerRandomGenerator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PMLehmerRandomGenerator new next
"
Class {
#name : #PMLehmerRandomGenerator,
#superclass : #PMRandomGenerator,
#superclass : #PMPseudoRandomNumberGenerator,
#category : #'Math-Random'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PMLinearCongruentialRandomGenerator new next.
"
Class {
#name : #PMLinearCongruentialRandomGenerator,
#superclass : #PMRandomGenerator,
#superclass : #PMPseudoRandomNumberGenerator,
#category : #'Math-Random'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Instance Variables

"
Class {
#name : #PMMarsagliaKissRandom,
#superclass : #PMRandomGenerator,
#name : #PMMarsagliaKissRandomGenerator,
#superclass : #PMPseudoRandomNumberGenerator,
#instVars : [
'kernelRand1',
'kernelRand2'
Expand All @@ -20,20 +20,20 @@ Class {
}

{ #category : #'instance creation' }
PMMarsagliaKissRandom class >> default [
PMMarsagliaKissRandomGenerator class >> default [
^self seed: #(
123456789 362436069 521288629 316191069
987654321 458629013 582859209 438195021)
]

{ #category : #'instance creation' }
PMMarsagliaKissRandom class >> new [
PMMarsagliaKissRandomGenerator class >> new [

^ self seed: (PMMarsagliaKissRandomKernel new next: 8)
]

{ #category : #'stream access' }
PMMarsagliaKissRandom >> next [
PMMarsagliaKissRandomGenerator >> next [
"Answer a Float in interval [0.0,1.0) with uniform distribution.
Note that constant 16rFFFFF800 is computed so as to truncate the 64 bits to Float precision.
It is thus ((1 << 32 - 1 << (64 - Float precision) bitAnd: 1 << 32 - 1)) hex"
Expand All @@ -42,23 +42,23 @@ PMMarsagliaKissRandom >> next [
]

{ #category : #'stream access' }
PMMarsagliaKissRandom >> peek [
PMMarsagliaKissRandomGenerator >> peek [
^self copy next
]

{ #category : #copying }
PMMarsagliaKissRandom >> postCopy [
PMMarsagliaKissRandomGenerator >> postCopy [
kernelRand1 := kernelRand1 copy.
kernelRand2 := kernelRand2 copy
]

{ #category : #accessing }
PMMarsagliaKissRandom >> seed [
PMMarsagliaKissRandomGenerator >> seed [
^ kernelRand1 seed , kernelRand2 seed
]

{ #category : #accessing }
PMMarsagliaKissRandom >> seed: aWordArray [
PMMarsagliaKissRandomGenerator >> seed: aWordArray [
"Initialize with an Array of eight 32-bits Integer"
kernelRand1 := PMMarsagliaKissRandomKernel seed: (aWordArray first: 4).
kernelRand2 := PMMarsagliaKissRandomKernel seed: (aWordArray last: 4)
Expand Down
4 changes: 2 additions & 2 deletions src/Math-Random/PMMersenneTwisterRandomGenerator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ MersenneTwisterRandom new nextInteger.
"
Class {
#name : #PMMersenneTwisterRandomGenerator,
#superclass : #PMRandomGenerator,
#superclass : #PMPseudoRandomNumberGenerator,
#instVars : [
'states',
'mti'
Expand All @@ -26,7 +26,7 @@ Class {
'TemperingMaskB',
'TemperingMaskC'
],
#category : 'Math-Random'
#category : #'Math-Random'
}

{ #category : #'class initialization' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PMParkMillerMinimumRandomGenerator new next
"
Class {
#name : #PMParkMillerMinimumRandomGenerator,
#superclass : #PMRandomGenerator,
#superclass : #PMPseudoRandomNumberGenerator,
#category : #'Math-Random'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ And finally, there's a taxonomy more related with the internal implementation, b

"
Class {
#name : #PMRandomGenerator,
#name : #PMPseudoRandomNumberGenerator,
#superclass : #Object,
#instVars : [
'seed'
],
#category : 'Math-Random'
#category : #'Math-Random'
}

{ #category : #testing }
PMRandomGenerator class >> chiSquare: range repeating: anInteger [
PMPseudoRandomNumberGenerator class >> chiSquare: range repeating: anInteger [
"Run a 'chi-square' test on a random number generator, over a range of integers given by range, repeating anInteger times.
Answer an Array containing the result of the chi-square test (which should be the same as the range), and the upper and lower bounds for 'good' randomness (assuming that anInteger is at least 10 * range)."

Expand All @@ -64,29 +64,29 @@ PMRandomGenerator class >> chiSquare: range repeating: anInteger [
]

{ #category : #'instance creation' }
PMRandomGenerator class >> new [
PMPseudoRandomNumberGenerator class >> new [
"Answer a new instance of the receiver"

^ (self seed: Time millisecondClockValue) initialize
]

{ #category : #'instance creation' }
PMRandomGenerator class >> seed: anInteger [
PMPseudoRandomNumberGenerator class >> seed: anInteger [
"Anwer a new Random stream with the initial seed anInteger."

^ self basicNew seed: anInteger; yourself
]

{ #category : #testing }
PMRandomGenerator >> atEnd [
PMPseudoRandomNumberGenerator >> atEnd [
"Answer whether the receiver is at its end - never true for a stream of Random numbers"

^ false

]

{ #category : #accessing }
PMRandomGenerator >> contents [
PMPseudoRandomNumberGenerator >> contents [
"Answer all of the objects in the collection accessed by the receiver.
Implementation Notes: Random streams are infinite, so there is no implementation possible."

Expand All @@ -95,52 +95,52 @@ PMRandomGenerator >> contents [
]

{ #category : #testing }
PMRandomGenerator >> isReadable [
PMPseudoRandomNumberGenerator >> isReadable [
"Answer whether the receiver can be read from (i.e. it implements the gettableStream protocol)."

^ true

]

{ #category : #testing }
PMRandomGenerator >> isWriteable [
PMPseudoRandomNumberGenerator >> isWriteable [
"Answer whether the receiver can be written to (i.e. it implements the puttableStream protocol)."

^ false

]

{ #category : #'stream access' }
PMRandomGenerator >> next [
PMPseudoRandomNumberGenerator >> next [
"Answer a pseudo-Random number"

^ self subclassResponsibility
]

{ #category : #'stream access' }
PMRandomGenerator >> next: anInteger [
PMPseudoRandomNumberGenerator >> next: anInteger [
"Answer a collection of size anInteger of pseudo-random Floats between 0 and 1. "

^ ( 1 to: anInteger ) collect: [ :i | self next ]

]

{ #category : #'stream access' }
PMRandomGenerator >> peek [
PMPseudoRandomNumberGenerator >> peek [
"Answer a pseudo-Random number generated from the next seed, but do not advance down the stream (i.e. self peek = self peek). "

^ self subclassResponsibility
]

{ #category : #accessing }
PMRandomGenerator >> seed [
PMPseudoRandomNumberGenerator >> seed [
"Returns the instance var of the receiver for external manipulation"

^ seed
]

{ #category : #accessing }
PMRandomGenerator >> seed: anObject [
PMPseudoRandomNumberGenerator >> seed: anObject [
"Sets the instance var of the receiver with anObject. (No checking is done)"

seed := anObject
Expand Down
11 changes: 0 additions & 11 deletions src/Math-Tests-Random/PMBernoulliGeneratorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ Class {
#category : #'Math-Tests-Random'
}

{ #category : #tests }
PMBernoulliGeneratorTest >> testGenerator [
| g bern |
g := PMLinearCongruentialRandomGenerator new.
bern := PMBernoulliGenerator new.
self
assert: (bern generator isKindOf: PMBernoulliGenerator defaultGeneratorClass).
bern generator: g.
self assert: (bern generator isKindOf: PMLinearCongruentialRandomGenerator)
]

{ #category : #tests }
PMBernoulliGeneratorTest >> testNextYieldsOneOrZero [
| gen |
Expand Down
41 changes: 20 additions & 21 deletions src/Math-Tests-Random/PMBinomialGeneratorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,6 @@ Class {
#category : #'Math-Tests-Random'
}

{ #category : #tests }
PMBinomialGeneratorTest >> testBinomialGeneratorConvergesToMean [
"Its purpose is to verify correct convergence of the binomial distribution,
should be Normal (np, np(1-p))"

| gen nums mean probabilityOfSuccess numberOfTrials |
probabilityOfSuccess := (Random seed: 0.0) next sqrt.
numberOfTrials := 1000.
gen := PMBinomialGenerator
numberOfTrials: numberOfTrials
probabilityOfSuccess: probabilityOfSuccess.

nums := OrderedCollection new.
(1 to: numberOfTrials) do: [ :ea | nums add: gen next ].

mean := numberOfTrials * probabilityOfSuccess.
self assert: nums min > (mean * (1 - 0.2)).
self assert: nums max < (mean * (1 + 0.2)).
self assert: (nums average - mean) abs < 5
]

{ #category : #tests }
PMBinomialGeneratorTest >> testBinomialGeneratorWithSuccessProbabilityOfOneAlwaysReturnNumberOfTrials [
| g numberOfTrials |
Expand Down Expand Up @@ -56,3 +35,23 @@ PMBinomialGeneratorTest >> testBinomialGeneratorWithSuccessProbabilityOfZeroAlwa
g generator: PMMersenneTwisterRandomGenerator new.
self assert: g next equals: 0
]

{ #category : #tests }
PMBinomialGeneratorTest >> testSampleMeanConvergesToExpectedValue [
"Its purpose is to verify correct convergence of the binomial distribution,
should be Normal (np, np(1-p))"

| gen sample probabilityOfSuccess numberOfTrials |
probabilityOfSuccess := (Random seed: 0.0) next sqrt.
numberOfTrials := 1000.
gen := PMBinomialGenerator
numberOfTrials: numberOfTrials
probabilityOfSuccess: probabilityOfSuccess.

sample := OrderedCollection new.
numberOfTrials timesRepeat: [ sample add: gen next ].

self assert: sample min > (gen expectedValue * (1 - 0.2)).
self assert: sample max < (gen expectedValue * (1 + 0.2)).
self assert: (sample average - gen expectedValue) abs < 5
]
15 changes: 8 additions & 7 deletions src/Math-Tests-Random/PMExponentialGeneratorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ PMExponentialGeneratorTest >> testGenerator [
eg := PMExponentialGenerator new.
self
assert: (eg generator isKindOf: PMExponentialGenerator defaultGeneratorClass).
self assert: (eg generator isKindOf: PMRandomGenerator).
self assert: (eg generator isKindOf: PMPseudoRandomNumberGenerator).
eg generator: PMMersenneTwisterRandomGenerator new.
self assert: (eg generator isKindOf: PMMersenneTwisterRandomGenerator).
self assert: (eg next isKindOf: Number)
]

{ #category : #tests }
PMExponentialGeneratorTest >> testPeekAlwaysAnswersTheSame [
PMExponentialGeneratorTest >> testPeekIsIdempotent [
| eg |
eg := PMExponentialGenerator new.
self assert: eg peek equals: eg peek.
Expand All @@ -31,10 +31,11 @@ PMExponentialGeneratorTest >> testPeekAlwaysAnswersTheSame [
PMExponentialGeneratorTest >> testSampleMeanConvergesToDistributionMean [
"testing a random sample seems suspect. We use a 5% interval here"

| eg arr |
eg := PMExponentialGenerator mean: 10.
arr := Array new: 10000.
arr := arr collect: [ :each | eg next ].
| gen sample |
gen := PMExponentialGenerator mean: 10.
sample := Array new: 10000.
sample := sample collect: [ :each | gen next ].

self
assert: (arr average between: eg mean * 0.95 and: eg mean * 1.05)
assert: (sample average between: gen mean * 0.95 and: gen mean * 1.05)
]
11 changes: 3 additions & 8 deletions src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ Class {
#category : #'Math-Tests-Random'
}

{ #category : #'as yet unclassified' }
PMLaplaceGeneratorTest >> classToTest [
^ PMLaplaceGenerator
]

{ #category : #accessing }
PMLaplaceGeneratorTest >> testPeekAlwaysReplyTheSameValue [
{ #category : #tests }
PMLaplaceGeneratorTest >> testPeekIsIdempotent [
| g |
g := self classToTest shape: 0.5 scale: 0.3.
g := PMLaplaceGenerator shape: 0.5 scale: 0.3.
self assert: g peek equals: g peek
]
8 changes: 4 additions & 4 deletions src/Math-Tests-Random/PMLehmerRandomTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ PMLehmerRandomTest >> testNextBetweenZeroAndOne [
]

{ #category : #tests }
PMLehmerRandomTest >> testPeekAlwaysReplyTheSameValue [
PMLehmerRandomTest >> testPeekAnswersSameAsNext [
| g |
g := PMLehmerRandomGenerator new.
self assert: g peek equals: g peek
self assert: g peek equals: g next
]

{ #category : #tests }
PMLehmerRandomTest >> testPeekAnswersSameAsNext [
PMLehmerRandomTest >> testPeekIsIdempotent [
| g |
g := PMLehmerRandomGenerator new.
self assert: g peek equals: g next
self assert: g peek equals: g peek
]
8 changes: 4 additions & 4 deletions src/Math-Tests-Random/PMLinearCongruentialRandomTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ PMLinearCongruentialRandomTest >> testNextBetweenZeroAndOne [
]

{ #category : #tests }
PMLinearCongruentialRandomTest >> testPeekAlwaysReplyTheSameValue [
PMLinearCongruentialRandomTest >> testPeekAnswersSameAsNext [
| g |
g := PMLinearCongruentialRandomGenerator new.
self assert: g peek equals: g peek
self assert: g peek equals: g next
]

{ #category : #tests }
PMLinearCongruentialRandomTest >> testPeekAnswersSameAsNext [
PMLinearCongruentialRandomTest >> testPeekIsIdempotent [
| g |
g := PMLinearCongruentialRandomGenerator new.
self assert: g peek equals: g next
self assert: g peek equals: g peek
]
Loading