class Oddsmaker::Odd::American

American Odds class, handling both positive and negative odds. A negative value expresses the dollar amount that would need to be wagered in order to win $100. A positive number expresses the dollar amount that would be won from a $100 wager, excluding the original wager.

Public Class Methods

new(value, id = nil) click to toggle source
# File lib/oddsmaker/odd/american.rb, line 8
def initialize(value, id = nil)
  @id    = id || value
  @value = value.to_i
end

Public Instance Methods

american() click to toggle source

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

@return [self]

# File lib/oddsmaker/odd/american.rb, line 23
def american
  self
end
decimal() click to toggle source

Convert to decimal odds, returning a new object.

@return [Decimal]

# File lib/oddsmaker/odd/american.rb, line 30
def decimal
  @decimal ||= implied_probability.decimal
end
fractional() click to toggle source

Convert to fractional odds, returning a new object.

@return [Fractional]

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

Properly display both positive and negative odds.

@return [String]

# File lib/oddsmaker/odd/american.rb, line 16
def to_s
  @value.positive? ? "+#{@value}" : @value.to_s
end

Private Instance Methods

calculate_fractional() click to toggle source

Calculate fractional representation of an odd.

@return [Float]

# File lib/oddsmaker/odd/american.rb, line 62
def calculate_fractional
  calculate_fractional ||= if @value.positive?
    @value / 100.rationalize
  else
    -100 / @value.rationalize
  end
end
calculate_probability() click to toggle source

Calculate implied probability of an odd.

@return [Float]

# File lib/oddsmaker/odd/american.rb, line 51
def calculate_probability
  calculate_probability ||= if @value.positive?
    100.0 / (@value + 100)
  else
    -@value.to_f / (100 - @value)
  end
end
calculate_profit(amount) click to toggle source

Calculate profit for a wager.

@return [Float, Integer]

# File lib/oddsmaker/odd/american.rb, line 73
def calculate_profit(amount)
  if @value.positive?
    (amount/100.rationalize) * @value
  else
    -100/@value.rationalize * amount
  end
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/american.rb, line 44
def type
  :american
end