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
# File lib/oddsmaker/odd/american.rb, line 8 def initialize(value, id = nil) @id = id || value @value = value.to_i end
Public Instance Methods
Returns self. This creates a consistent API for all odds.
@return [self]
# File lib/oddsmaker/odd/american.rb, line 23 def american self end
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
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
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 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 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 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
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