testing variance on random sampling method

This commit is contained in:
Joseph Montanaro 2021-03-25 23:08:06 -07:00
parent afacd2b52b
commit b4c31a7a68

View File

@ -5,6 +5,10 @@ import combinators, game, fixedseq
type type
ScoreSet* = array[Color, int] ScoreSet* = array[Color, int]
ScoreSpread = object
lo: array[Color, float]
hi: array[Color, float]
LegResults* = tuple[scores: ScoreSet, endStates: HashSet[Board]] LegResults* = tuple[scores: ScoreSet, endStates: HashSet[Board]]
@ -69,9 +73,24 @@ proc randomGames(b: Board, count: SomeInteger): ScoreSet =
for i in 1 .. count: for i in 1 .. count:
let winner = b.randomGame(r) let winner = b.randomGame(r)
inc result[winner] inc result[winner]
if i mod 100_000 == 0 or i == count - 1: # if i mod 100_000 == 0 or i == count - 1:
stdout.write("simulated: " & $i & "\r") # stdout.write("simulating " & count & "random games: " & $i & "\r")
echo "" # echo ""
proc randomSpread(b: Board, nTests: SomeInteger, nSamples: SomeInteger): ScoreSpread =
for s in result.lo.mitems:
s = 1
for i in 0 ..< nTests:
let scores = b.randomGames(nSamples)
let total = scores.sum
for color, score in scores:
let pct = score / total
if pct < result.lo[color]:
result.lo[color] = pct
if pct > result.hi[color]:
result.hi[color] = pct
var b: Board var b: Board
@ -85,6 +104,7 @@ for i, roll in randomFuture(b.diceRemaining, r):
rolls[i] = (roll[0], roll[1] + 1) rolls[i] = (roll[0], roll[1] + 1)
b.setState(rolls, @[]) b.setState(rolls, @[])
echo "Starting state:"
b.display(1, 5) b.display(1, 5)
# block outer: # block outer:
@ -99,7 +119,16 @@ b.display(1, 5)
# echo "winner: ", b.leader.get # echo "winner: ", b.leader.get
# b.display(min(b.camels) - 1, 16) # b.display(min(b.camels) - 1, 16)
let scores = b.randomGames(10_000_000) # let scores = b.randomGames(10_000_000)
let total = scores.sum # let total = scores.sum
for i, c in scores: # for i, c in scores:
echo Color(i), ": ", (100 * c / total).round(2), "% (", c, " / ", total, ")" # echo Color(i), ": ", (100 * c / total).round(2), "% (", c, " / ", total, ")"
for i in 0..3:
let samples = 10 ^ (i + 3)
echo "Simulating ", samples, " games 100 times"
let spread = b.randomSpread(100, samples)
for color, lo in spread.lo:
stdout.write($color & ": " & $lo.round(4) & "-" & $spread.hi[color].round(4) & ", ")
stdout.flushFile
echo "\n"