superbowl 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6f8c655b547e0f5ba1f877514ca8e5f982379e4b
4
+ data.tar.gz: 02bc940de40a30c5cf626bd480490c121e8d3f75
5
+ SHA512:
6
+ metadata.gz: 935fa549fb0eed317684d72d28147b3edb1076f5ee79d78ba721aa28ee7edd626c0b4b9a004345cb946a47161ecb4614d1c9c93f2ce3ed6984432a0f26e78b99
7
+ data.tar.gz: d51a7573a9b1891f961d42b85b51f7eca71c7c2343b5481b44ce1e10943e01f1a99356204cdff28cc71083957684e496b6bb7f9e01470718250d6b51256db2ba
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ Gemfile.lock
2
+ .*.swp
3
+ .drake/
4
+ data/
5
+ drake.log
6
+ squares.tsv
data/Drakefile ADDED
@@ -0,0 +1,25 @@
1
+ ;
2
+ ; Compute probabilities of winning Super Bowl squares pool
3
+ ;
4
+
5
+ BASE=data/
6
+
7
+ historic_box_scores_html/ <- [-timecheck]
8
+ mkdir -p $OUTPUT
9
+ cd $OUTPUT
10
+ wget http://www.printyourbrackets.com/best-super-bowl-squares-odds-probabilities.html
11
+ wget http://www.printyourbrackets.com/box-scores-for-super-bowl-1-through-10.html
12
+ wget http://www.printyourbrackets.com/box-scores-for-super-bowl-11-through-20.html
13
+ wget http://www.printyourbrackets.com/box-scores-for-super-bowl-21-through-30.html
14
+ wget http://www.printyourbrackets.com/box-scores-for-super-bowl-31-through-40.html
15
+
16
+ historic_box_scores <- historic_box_scores_html/
17
+ find $INPUTS -type f -print0 | parallel -0 bundle exec ruby ./bin/parse_box_scores.rb > $OUTPUT
18
+
19
+
20
+ ;
21
+ ; squares.tsv is a tab-delimited file that you must supply, containing the
22
+ ; names of the people who have purchased each square.
23
+ ;
24
+ squares.html <- !squares.tsv, historic_box_scores
25
+ bundle exec ruby ./bin/build_squares_html.rb --squares=$INPUT0 --box-scores=$INPUT1 > $OUTPUT
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in superbowl.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Matt Gillooly
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Superbowl Box Scores
2
+
3
+ Use the magic of data to predict how much money you're going to win in your Super Bowl squares pool.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'superbowl'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install superbowl
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/superbowl/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'erb'
4
+ require 'optparse'
5
+ require 'superbowl'
6
+
7
+ squares_file = nil
8
+ box_scores_file = nil
9
+
10
+ opt_parser = OptionParser.new do |opts|
11
+ opts.banner = "Usage: build_squares_html.rb [options]"
12
+
13
+ opts.on("-sFILE", "--squares=FILE", "File containing square owners") do |f|
14
+ squares_file = f
15
+ end
16
+
17
+ opts.on("-bFILE", "--box-scores=FILE", "File containing historic box scores") do |f|
18
+ box_scores_file = f
19
+ end
20
+ end
21
+
22
+ opt_parser.parse!
23
+
24
+ fail "missing squares file" unless squares_file
25
+ fail "missing box-scores file" unless box_scores_file
26
+
27
+ grid = Superbowl::Grid.parse(squares_file)
28
+ box_score_history = Superbowl::BoxScoreHistory.parse(box_scores_file)
29
+
30
+ renderer = ERB.new(File.read('squares.html.erb'))
31
+ puts renderer.result()
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'nokogiri'
4
+
5
+ doc = Nokogiri::HTML(ARGF.read)
6
+
7
+ tables = doc.xpath('//*[@id="content"]/div/div/table/tbody/tr/td/table')
8
+ winning_squares_tables = tables.select{|t| t.to_s.include? 'Winner'}
9
+
10
+ winning_squares_tables.each do |table|
11
+ labels, digit_pairs = table.xpath('.//td').map(&:text).each_slice(4).to_a
12
+
13
+ labels.zip(digit_pairs).each do |label, digit_pair|
14
+ unless label.match(/Quarter Winner/)
15
+ fail "unexpected label: #{label}"
16
+ end
17
+
18
+ puts "Q#{label[0]},#{digit_pair}"
19
+ end
20
+ end
data/lib/superbowl.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "superbowl/version"
2
+
3
+ require "superbowl/box_score_history"
4
+ require "superbowl/contestant"
5
+ require "superbowl/grid"
6
+
7
+ module Superbowl
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,36 @@
1
+ require 'csv'
2
+
3
+ module Superbowl
4
+
5
+ class BoxScoreHistory
6
+ def self.parse(filename)
7
+ new(CSV.open(filename, headers: %w(quarter x y)).to_a)
8
+ end
9
+
10
+ def initialize(box_scores)
11
+ @counts = Array.new(10) { Array.new(10) { 0 } }
12
+
13
+ box_scores.each do |box_score|
14
+ x, y = box_score.values_at('x', 'y').map(&:to_i)
15
+ @counts[x][y] += 1
16
+ end
17
+
18
+ @num_box_scores = box_scores.count
19
+ end
20
+
21
+ def ratio(row_index, col_index)
22
+ # TODO: force symmetry?
23
+ @counts[row_index][col_index].to_f / @num_box_scores
24
+ end
25
+
26
+ def probability(row_index, col_index)
27
+ "%.2f%" % (ratio(row_index, col_index) * 100)
28
+ end
29
+
30
+ def grid_color(row_index, col_index)
31
+ hex_val = 15 - (ratio(row_index, col_index) * 100).to_i
32
+ hex_val.to_s(16) * 6
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,13 @@
1
+ module Superbowl
2
+
3
+ class Contestant < Struct.new(:name, :expected_payout, :num_squares)
4
+ def total_investment
5
+ 10.0 * num_squares
6
+ end
7
+
8
+ def expected_roi
9
+ 100 * (expected_payout - total_investment) / total_investment
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,31 @@
1
+ require 'csv'
2
+
3
+ module Superbowl
4
+
5
+ class Grid
6
+ attr_reader :squares
7
+
8
+ def self.parse(filename)
9
+ new(CSV.open(filename, col_sep: "\t").to_a)
10
+ end
11
+
12
+ def initialize(squares)
13
+ @squares = squares
14
+ end
15
+
16
+ def contestants(box_score_history)
17
+ contestants_by_name = {}
18
+
19
+ @squares.each_with_index do |row, row_index|
20
+ row.each_with_index do |owner, col_index|
21
+ c = contestants_by_name[owner] ||= Contestant.new(owner, 0, 0)
22
+ c.expected_payout += box_score_history.ratio(row_index, col_index) * 1000
23
+ c.num_squares += 1
24
+ end
25
+ end
26
+
27
+ contestants_by_name.values.sort_by(&:expected_payout).reverse
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,3 @@
1
+ module Superbowl
2
+ VERSION = "0.0.1"
3
+ end
data/squares.html.erb ADDED
@@ -0,0 +1,55 @@
1
+ <html>
2
+ <body>
3
+
4
+ <table>
5
+ <CAPTION>Seahawks across, Patriots down</CAPTION>
6
+
7
+ <tr>
8
+ <th></th>
9
+ <th>0</th>
10
+ <th>1</th>
11
+ <th>2</th>
12
+ <th>3</th>
13
+ <th>4</th>
14
+ <th>5</th>
15
+ <th>6</th>
16
+ <th>7</th>
17
+ <th>8</th>
18
+ <th>9</th>
19
+ </tr>
20
+
21
+ <% grid.squares.each_with_index do |row, row_index| %>
22
+ <tr>
23
+ <th><%= row_index %></th>
24
+ <% row.each_with_index do |owner, col_index| %>
25
+ <td style="background-color:#<%= box_score_history.grid_color(row_index, col_index) %>;">
26
+ <h3><%= owner %></h3>
27
+ <%= box_score_history.probability(row_index, col_index) %>
28
+ </td>
29
+ <% end %>
30
+ </tr>
31
+ <% end %>
32
+ </table>
33
+
34
+ <p>Odds based on <a href="http://www.printyourbrackets.com/best-super-bowl-squares-odds-probabilities.html">past Super Bowl results</a>.</p>
35
+
36
+ <table border=1 cellpadding="5">
37
+ <tr>
38
+ <th>Contestant</th>
39
+ <th>Expected Payout</th>
40
+ <th># Squares</th>
41
+ <th>Expected ROI</th>
42
+ <th>Actual Payout</th>
43
+ </tr>
44
+ <% grid.contestants(box_score_history).each do |contestant| %>
45
+ <tr>
46
+ <th><%= contestant.name %></th>
47
+ <td><%= '$%.2f' % contestant.expected_payout %></td>
48
+ <td><%= contestant.num_squares %></td>
49
+ <td><%= '%.2f%' % contestant.expected_roi %></td>
50
+ </tr>
51
+ <% end %>
52
+ </table>
53
+
54
+ </body>
55
+ </html>
data/superbowl.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'superbowl/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "superbowl"
8
+ spec.version = Superbowl::VERSION
9
+ spec.authors = ["Matt Gillooly"]
10
+ spec.email = ["matt@swipely.com"]
11
+ spec.summary = %q{Use the magic of data to predict how much money you're going to win in your Super Bowl squares pool.}
12
+ spec.homepage = "http://github.com/mattgillooly/superbowl"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+
23
+ spec.add_dependency 'nokogiri'
24
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: superbowl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Gillooly
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - matt@swipely.com
58
+ executables:
59
+ - build_squares_html.rb
60
+ - parse_box_scores.rb
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Drakefile
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/build_squares_html.rb
71
+ - bin/parse_box_scores.rb
72
+ - lib/superbowl.rb
73
+ - lib/superbowl/box_score_history.rb
74
+ - lib/superbowl/contestant.rb
75
+ - lib/superbowl/grid.rb
76
+ - lib/superbowl/version.rb
77
+ - squares.html.erb
78
+ - superbowl.gemspec
79
+ homepage: http://github.com/mattgillooly/superbowl
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.2.2
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Use the magic of data to predict how much money you're going to win in your
103
+ Super Bowl squares pool.
104
+ test_files: []