Class: Oddsmaker::Market
- Inherits:
-
Object
- Object
- Oddsmaker::Market
- Defined in:
- lib/oddsmaker/market.rb
Overview
Market is a container of multiple odds. This is useful to represent the full data of sports betting. A generic moneyline of +300/-400 can be represented by a market, containing both of the individual odds. This also allows for calculating the total probability, which will be over 100% if set by a sportsbook. Additional functions include the ability to determine the odds without vig (the book’s profit), and the ability to calculate odds for a given overround (the total probability above 100%).
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#odds ⇒ Object
readonly
Returns the value of attribute odds.
-
#vigged ⇒ Object
readonly
Returns the value of attribute vigged.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compare equality to another market.
-
#initialize(*odds, name: nil) ⇒ Market
constructor
A new instance of Market.
-
#odd(id) ⇒ Odd
Retrieve an odd by identifier.
-
#overround!(v = 5) ⇒ Market
Create market with an added overround amount.
-
#overround_odds!(v = 5) ⇒ Array<Odd>
Calculate the odds with given overround.
-
#to_h ⇒ Hash
Hash representation of a market.
-
#to_json ⇒ String
JSON representation of a market.
-
#total_probability ⇒ Float
Total probability for a set of odds.
-
#vig ⇒ Float
Total vig (maximum vig under balanced book scenario).
-
#without_vig ⇒ Market
Calculate equivalent market without any overround (vig).
-
#without_vig_odds ⇒ <Odd>
Calculate the odds without any overround (vig).
Constructor Details
#initialize(*odds, name: nil) ⇒ Market
Returns a new instance of Market.
10 11 12 13 |
# File 'lib/oddsmaker/market.rb', line 10 def initialize(*odds, name: nil) @name = name @odds = odds.flatten end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/oddsmaker/market.rb', line 8 def name @name end |
#odds ⇒ Object (readonly)
Returns the value of attribute odds.
8 9 10 |
# File 'lib/oddsmaker/market.rb', line 8 def odds @odds end |
#vigged ⇒ Object (readonly)
Returns the value of attribute vigged.
8 9 10 |
# File 'lib/oddsmaker/market.rb', line 8 def vigged @vigged end |
Instance Method Details
#==(other) ⇒ Boolean
Compare equality to another market
76 77 78 |
# File 'lib/oddsmaker/market.rb', line 76 def ==(other) odds.sort == other.odds.sort end |
#odd(id) ⇒ Odd
Retrieve an odd by identifier.
19 20 21 |
# File 'lib/oddsmaker/market.rb', line 19 def odd(id) odds_hash[id] end |
#overround!(v = 5) ⇒ Market
Create market with an added overround amount
60 61 62 |
# File 'lib/oddsmaker/market.rb', line 60 def overround!(v = 5) @vigged = self.class.new(overround_odds!(v)) end |
#overround_odds!(v = 5) ⇒ Array<Odd>
Calculate the odds with given overround
68 69 70 |
# File 'lib/oddsmaker/market.rb', line 68 def overround_odds!(v = 5) without_vig_odds.map { |odd| odd.overround!(v) } end |
#to_h ⇒ Hash
Hash representation of a market.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/oddsmaker/market.rb', line 82 def to_h full_odds = if self.total_probability != 1 && self.odds.size > 1 no_vig = self.without_vig_odds odds.map.with_index { |odd, index| odd.to_h.merge(actual: no_vig[index].implied_probability.value, without_vig: no_vig[index].value) } else odds.map(&:to_h) end { name: self.name, total_probability: self.total_probability, vig: self.vig, odds: full_odds, } end |
#to_json ⇒ String
JSON representation of a market.
99 100 101 |
# File 'lib/oddsmaker/market.rb', line 99 def to_json to_h.to_json end |
#total_probability ⇒ Float
Total probability for a set of odds. This will be over 1 if set by a sportsbook.
27 28 29 |
# File 'lib/oddsmaker/market.rb', line 27 def total_probability @total_probability ||= @odds.inject(0) { |total, odd| total + odd.implied_probability.value } # Change to #sum when 2.3 support can be dropped end |
#vig ⇒ Float
Total vig (maximum vig under balanced book scenario)
52 53 54 |
# File 'lib/oddsmaker/market.rb', line 52 def vig 1 - total_probability**-1 end |
#without_vig ⇒ Market
Calculate equivalent market without any overround (vig).
34 35 36 |
# File 'lib/oddsmaker/market.rb', line 34 def without_vig self.class.new(without_vig_odds) end |
#without_vig_odds ⇒ <Odd>
Calculate the odds without any overround (vig).
41 42 43 44 45 46 47 |
# File 'lib/oddsmaker/market.rb', line 41 def without_vig_odds @without_vig_odds = if total_probability != 1.0 @odds.map { |odd| odd.without_vig(total_probability) } else @odds end end |