Class: Oddsmaker::Market

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/oddsmaker/market.rb', line 8

def name
  @name
end

#oddsObject (readonly)

Returns the value of attribute odds.



8
9
10
# File 'lib/oddsmaker/market.rb', line 8

def odds
  @odds
end

#viggedObject (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

Parameters:

Returns:

  • (Boolean)


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.

Parameters:

  • id (String, Integer)

    Odd identifier. Defaults to provided value.

Returns:



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

Parameters:

  • v (Integer) (defaults to: 5)

    Overround percent

Returns:

  • (Market)

    New market with overrounded odds



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

Parameters:

  • v (Integer) (defaults to: 5)

    Overround percent

Returns:

  • (Array<Odd>)

    Overrounded odds



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_hHash

Hash representation of a market.

Returns:

  • (Hash)


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_jsonString

JSON representation of a market.

Returns:

  • (String)


99
100
101
# File 'lib/oddsmaker/market.rb', line 99

def to_json
  to_h.to_json
end

#total_probabilityFloat

Total probability for a set of odds. This will be over 1 if set by a sportsbook.

Returns:

  • (Float)

    Total probability, with 1.0 representing 100%



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

#vigFloat

Total vig (maximum vig under balanced book scenario)

Returns:

  • (Float)


52
53
54
# File 'lib/oddsmaker/market.rb', line 52

def vig
  1 - total_probability**-1
end

#without_vigMarket

Calculate equivalent market without any overround (vig).

Returns:

  • (Market)

    New market with total probability == 1.0



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).

Returns:

  • (<Odd>)

    Array of odds with the vig removed



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