Skip to content

Commit

Permalink
Smalltalk version
Browse files Browse the repository at this point in the history
  • Loading branch information
dfings committed Mar 5, 2021
1 parent 3aac8cc commit fdc3f98
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions envelopes.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env gst -g
"$ brew install gnu-smalltalk"

Object subclass: Envelopes [
| numTrials priorLowerMax rand |
Envelopes class >> new [
^(super new) init
]
init [
numTrials := 10000.
priorLowerMax := 100.
rand := Random new.
]
"
Runs a single trial where an envelope is chosen.
If the chosen envelope has a value < cutoff, the function will switch
envelopes, otherwise it will keep the envelope it has chosen. Returns
the value of the envelope it ultimately selects.
"
singleTrial: cutoff [
| lowerValue higherValue |
lowerValue := rand next * priorLowerMax.
higherValue := 2 * lowerValue.
((rand next * 2) floor) == 0
ifTrue: [lowerValue >= cutoff
ifTrue: [^lowerValue]
ifFalse: [^higherValue]
]
ifFalse: [higherValue >= cutoff
ifTrue: [^higherValue]
ifFalse: [^lowerValue]
]
]
"Runs many trials at a given cutoff to approximate the expected value."
multiTrial: cutoff [
| total |
total := 0.0e.
1 to: numTrials do: [:i |
total := total + (self singleTrial: cutoff)
].
^(total / numTrials)
]
].

"Approximates the expected value for each integral cutoff value."
e := Envelopes new.
1 to: 200 do: [:i |
'cutoff=' display.
i display.
' expectedValue ' display.
(e multiTrial: i) asFloatE displayNl.
]

0 comments on commit fdc3f98

Please sign in to comment.