Skip to content

Instantly share code, notes, and snippets.

@foxor
Created January 2, 2022 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foxor/fb316b503855124e626440486549ceeb to your computer and use it in GitHub Desktop.
Save foxor/fb316b503855124e626440486549ceeb to your computer and use it in GitHub Desktop.
Markov chain win rate model
#!/usr/bin/env python3
class markov_node:
def __init__(self, name, outcomes):
self.name = name
self.probability = 0.0
self.incoming_probability = 0.0
self.outcomes = outcomes
def push(self):
for (probability, node) in self.outcomes:
node.incoming_probability += self.probability * probability
def collect(self):
self.probability = self.incoming_probability
self.incoming_probability = 0.0
def __str__(self):
return "%s %.2f" % (self.name, self.probability)
def games_until_streak_expected(win_rate, streak_length):
loss_node = markov_node("Loss", [])
success_node = markov_node("%d wins" % streak_length, [])
success_node.outcomes = [(1.0, success_node)]
streak_nodes = [success_node]
for i in range(1, streak_length):
name = "%d wins" % (streak_length - i)
new_node = markov_node(name, [(win_rate, streak_nodes[-1]), (1 - win_rate, loss_node)])
streak_nodes.append(new_node)
loss_node.probability = 1.0
loss_node.outcomes = [(win_rate, streak_nodes[-1]), (1 - win_rate, loss_node)]
streak_nodes.append(loss_node)
games = 0
while success_node.probability < 0.5:
games += 1
for node in streak_nodes:
node.push()
for node in streak_nodes:
node.collect()
return games
def run_trial(win_rate, streak_length):
games = games_until_streak_expected(win_rate, streak_length)
print("It takes %d games at %s win rate to get a %d game win streak" % (games, "{:.0%}".format(win_rate), streak_length))
if __name__ == '__main__':
"""
It takes 22715 games at 50% win rate to get a 14 game win streak
It takes 14637 games at 52% win rate to get a 14 game win streak
"""
run_trial(.5, 14)
run_trial(45.0/87, 14)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment