Last active
March 4, 2023 18:26
-
-
Save JEG2/13fc565c848b369de8afd352e3e52d09 to your computer and use it in GitHub Desktop.
Experimenting with cumulative probability
This file contains 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
trials = 10_000 | |
simulation = | |
4 | |
|> Stream.iterate(fn dollars -> | |
case :rand.uniform(4) do | |
1 -> dollars - 1 | |
2 -> dollars + 1 | |
_n -> dollars | |
end | |
end) | |
|> Stream.with_index() | |
|> Stream.map(fn | |
{-1, i} -> i | |
state -> state | |
end) | |
Stream.repeatedly(fn -> Enum.find(simulation, &is_integer/1) end) | |
|> Stream.take(trials) | |
|> Enum.sum() | |
|> then(&(&1 / trials)) | |
|> IO.inspect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment