class Oddsmaker::Odd::ImpliedProbability

Implied probability is the probability of an event, based on the odds for that event. Implied probability is what translates between various types of odds.

Public Class Methods

new(value, id = nil) click to toggle source
# File lib/oddsmaker/odd/implied_probability.rb, line 7
def initialize(value, id = nil)
  @id    = id || value
  @value = value >= 1 ? value.fdiv(100) : value
end

Public Instance Methods

<=>(other) click to toggle source

Compare two odds against each other.

@param other [ImpliedProbability] @return [-1,0,1]

# File lib/oddsmaker/odd/implied_probability.rb, line 58
def <=>(other)
  value <=> other.value
end
==(other) click to toggle source

Check two odds for equality.

@param other [ImpliedProbability] @return [Boolean]

# File lib/oddsmaker/odd/implied_probability.rb, line 46
def ==(other)
  if other.is_a?(self.class)
    @value == other.value
  else
    @value == other.implied_probability.value
  end
end
american() click to toggle source

Convert to American odds, returning a new object.

@return [American]

# File lib/oddsmaker/odd/implied_probability.rb, line 72
def american
  @american ||= American.new(calculate_american, id)
end
decimal() click to toggle source

Convert to decimal odds, returning a new object.

@return [Decimal]

# File lib/oddsmaker/odd/implied_probability.rb, line 79
def decimal
  @decimal ||= Decimal.new(calculate_decimal, id)
end
fractional() click to toggle source

Convert to fractional odds, returning a new object.

@return [Fractional]

# File lib/oddsmaker/odd/implied_probability.rb, line 86
def fractional
  @fractional ||= Fractional.new(calculate_fractional, id)
end
implied_probability() click to toggle source

Returns self. This creates a consistent API for all odds.

@return [self]

# File lib/oddsmaker/odd/implied_probability.rb, line 65
def implied_probability
  self
end
overround!(v) click to toggle source

Calculates a new odd, with the given overround percentage.

@param v [Integer] Overround percentage (as whole number) @return [ImpliedProbability]

# File lib/oddsmaker/odd/implied_probability.rb, line 23
def overround!(v)
  self.class.new(@value * ((100 + v)/100.rationalize), id)
end
round(n = 2) click to toggle source

Round decimal value.

@param n [Integer] Number of decimal places for rounding.

# File lib/oddsmaker/odd/implied_probability.rb, line 38
def round(n = 2)
  @value.round(n)
end
to_percent(n = nil) click to toggle source

Convert probability to percent. Optionally round the resulting decimal.

@param n [Integer] Number of decimal places for rounding.

# File lib/oddsmaker/odd/implied_probability.rb, line 31
def to_percent(n = nil)
  n ? (@value * 100).round(n) : @value * 100
end
without_vig(total_probability) click to toggle source

Calculates a new odd, removing the vig.

@return [ImpliedProbability]

# File lib/oddsmaker/odd/implied_probability.rb, line 15
def without_vig(total_probability)
  self.class.new(@value / total_probability, id)
end

Private Instance Methods

calculate_american() click to toggle source

Calculate American odds value.

@return [Integer, Float]

# File lib/oddsmaker/odd/implied_probability.rb, line 100
def calculate_american
  value = @value.rationalize
  if value >= 0.5
    - ( value / (1 - value)) * 100
  else
    ((1 - value) / value ) * 100
  end
end
calculate_decimal() click to toggle source

Calculate decimal odds value.

@return [Float]

# File lib/oddsmaker/odd/implied_probability.rb, line 112
def calculate_decimal
  1.0 / @value
end
calculate_fractional() click to toggle source

Calculate fractional odds value.

@return [Rational]

# File lib/oddsmaker/odd/implied_probability.rb, line 119
def calculate_fractional
  (1.0 / @value - 1).rationalize
end
type() click to toggle source

Allows for implied probability to create a vigged/unvigged odd and convert it back to source format.

# File lib/oddsmaker/odd/implied_probability.rb, line 93
def type
  :implied_probability
end