Created
February 6, 2023 03:36
-
-
Save mculp/939cad98373d5ff98f24b004b8eea4ea to your computer and use it in GitHub Desktop.
GPT-3 calculation of probability of two scenarios in super bowl squares game with arbitrarily weighted numbers
This file contains hidden or 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
# This was generated by GPT-3, so fact check it before using it | |
class SquareSimulator | |
attr_reader :board_size, :player_squares, :column_weights, :row_weights | |
def initialize(board_size:, player_squares:, column_weights:, row_weights:) | |
@board_size = board_size | |
@player_squares = player_squares | |
@column_weights = column_weights | |
@row_weights = row_weights | |
end | |
def run_simulation | |
board = Array.new(board_size) { Array.new(board_size) } | |
# Assign weights to columns and rows | |
board.each_with_index do |row, row_index| | |
row.each_with_index do |_, column_index| | |
board[row_index][column_index] = { | |
column_weight: column_weights[column_index], | |
row_weight: row_weights[row_index] | |
} | |
end | |
end | |
# Assign player squares | |
player_squares.each do |square| | |
board[square[:row]][square[:column]][:player_square] = true | |
end | |
# Calculate the probability of a player square being chosen | |
total_weight = board.flatten.map { |square| square[:column_weight] * square[:row_weight] }.sum | |
player_square_weight = board.flatten.map { |square| square[:column_weight] * square[:row_weight] if square[:player_square] }.compact.sum | |
player_square_probability = player_square_weight / total_weight | |
player_square_probability | |
end | |
end | |
RSpec.describe SquareSimulator do | |
describe '#run_simulation' do | |
context 'when the player squares are all clustered together' do | |
it 'returns a probability of 0.1' do | |
simulator = SquareSimulator.new( | |
board_size: 10, | |
player_squares: [ | |
{ row: 0, column: 0 }, | |
{ row: 0, column: 1 }, | |
{ row: 0, column: 2 }, | |
{ row: 0, column: 3 }, | |
{ row: 0, column: 4 }, | |
{ row: 0, column: 5 }, | |
{ row: 0, column: 6 }, | |
{ row: 0, column: 7 }, | |
{ row: 0, column: 8 }, | |
{ row: 0, column: 9 } | |
], | |
column_weights: [2, 1, 1.5, 2, 1, 1, 1, 2, 1.5, 1], | |
row_weights: [2] * 10 | |
) | |
probability = simulator.run_simulation | |
expect(probability).to eq(0.1) | |
puts "Probability of winning on a diagonal: #{probability}" | |
end | |
end | |
context 'when the player squares are spread out' do | |
it 'returns a probability greater than 0.1' do | |
simulator = SquareSimulator.new( | |
board_size: 10, | |
player_squares: [ | |
{ row: 0, column: 0 }, | |
{ row: 1, column: 1 }, | |
{ row: 2, column: 2 }, | |
{ row: 3, column: 3 }, | |
{ row: 4, column: 4 }, | |
{ row: 5, column: 5 }, | |
{ row: 6, column: 6 }, | |
{ row: 7, column: 7 }, | |
{ row: 8, column: 8 }, | |
{ row: 9, column: 9 } | |
], | |
column_weights: [2, 1.5, 1, 2, 1.5, 1, 2, 1.5, 1, 1], | |
row_weights: [2, 1.5, 1, 2, 1.5, 1, 2, 1.5, 1, 1] | |
) | |
probability = simulator.run_simulation | |
expect(probability).to be > 0.1 | |
puts "Probability of winning on a diagonal: #{probability}" | |
end | |
end | |
end | |
end | |
# output: | |
# Probability of winning on a straight line: 0.1 | |
# Probability of winning on a diagonal: 0.10820451843043995 | |
Finished in 0.00224 seconds (files took 0.05369 seconds to load) | |
2 examples, 0 failures |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment