Last active
September 3, 2015 17:21
-
-
Save dannypage/ec56819357748daa63c5 to your computer and use it in GitHub Desktop.
Team 1 has 3 good shots. Team 2 has 20 bad shots. Both total an xG of 1. Who wins? - Sample result: {:win=>35710, :tie=>31614, :lose=>32676}
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
#!/bin/ruby | |
def generate_game(team_1_shots, team_2_shots) | |
score1 = (team_1_shots.map { |x| if rand < x then 1 else 0 end }).inject(:+) | |
score2 = (team_2_shots.map { |x| if rand < x then 1 else 0 end }).inject(:+) | |
return [score1,score2] | |
end | |
def evaluate(game) | |
if game[0] > game[1] | |
:win | |
elsif game[0] == game[1] | |
:tie | |
else | |
:lose | |
end | |
end | |
results = {:win => 0, :tie => 0, :lose => 0} | |
games = [] | |
sims = 10000 | |
scores = {} | |
team_1_shots = [0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.20,0.20,0.20] | |
team_2_shots = [0.6,0.6] | |
sims.times.each do |game| | |
game = generate_game(team_1_shots, team_2_shots) | |
results[evaluate(game)] += 1 | |
if scores["#{game[0]}-#{game[1]}"] | |
scores["#{game[0]}-#{game[1]}"] = scores["#{game[0]}-#{game[1]}"] += 1 | |
else | |
scores["#{game[0]}-#{game[1]}"] = 1 | |
end | |
end | |
puts "Team 1 xG: #{team_1_shots.inject(:+)}" | |
puts "Team 2 xG: #{team_2_shots.inject(:+)}" | |
puts "Team 1 Wins: #{results[:win]}, Ties: #{results[:tie]}, Team 2 Wins: #{results[:lose]}" | |
#Reenable this to show each score-line. | |
#puts Hash[scores.sort_by{|k, v| v}.reverse] | |
puts "Team 1 PPG: #{(results[:win]*3.0+results[:lose])/sims}" | |
puts "Team 2 PPG: #{(results[:lose]*3.0+results[:lose])/sims}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment