lib/mutually_exclusive_collection.rb in rodders-3.0.1 vs lib/mutually_exclusive_collection.rb in rodders-3.1.0
- old
+ new
@@ -2,11 +2,11 @@
# Includes methods for sports betting arbitrage. Due to the discrete nature
# of real-world monetary amounts, there may be small rounding errors.
class MutuallyExclusiveCollection
# create a new collection with the given odds
# @param [Array<FixedOdds>] mutually_exclusive_outcome_odds the odds for all the mutually exclusive outcomes
- def initialize(mutually_exclusive_outcome_odds)
+ def initialize mutually_exclusive_outcome_odds
@mutually_exclusive_outcome_odds = mutually_exclusive_outcome_odds.sort
end
# the least likely of the odds to occur
# @return [FixedOdds] the least likely odd
@@ -41,35 +41,31 @@
# the bookmaker's return rate
# @return [Number] the bookmaker's return rate as a percentage
def bookmakers_return_rate
fs = fractions
- fs.any? ? 1 - fs.reduce(:*) / fs.reduce(:+) : 0
+ 1 - fs.reduce(:*) / fs.reduce(:+) if fs.any?
end
# hash of the odds and what percentage of the total stake should go on each
# @return [Hash<FixedOdds, Number>] hash of odds to percentages
def percentages
- hash = {}
- @mutually_exclusive_outcome_odds.each {|odds| hash[odds] = 1 / odds.fractional_odds / sum_inverse_outcome }
- hash
+ @mutually_exclusive_outcome_odds.each_with_object({}) {|odds, hash| hash[odds] = 1 / odds.fractional_odds / sum_inverse_outcome }
end
# hash of the odds and what stakes to put on each given a total stake
# @param [Money] total_stake the money to distribute on the outcomes
# @return [Hash<FixedOdds, Money>] hash of odds to stakes
def stakes_for_total_stake total_stake
- hash = {}
- @mutually_exclusive_outcome_odds.each {|odds| hash[odds] = total_stake / odds.fractional_odds / sum_inverse_outcome }
- hash
+ @mutually_exclusive_outcome_odds.each_with_object({}) {|odds, hash| hash[odds] = total_stake / odds.fractional_odds / sum_inverse_outcome }
end
# hash of the odds and the stakes needed to make the specified profit
# @param (see #stakes_for_total_stake)
# @return (see #stakes_for_total_stake)
def stakes_for_profit desired_profit
- stakes_for_total_stake(stake_to_profit(desired_profit))
+ stakes_for_total_stake stake_to_profit desired_profit
end
# the stake needed to win the desired profit
# @param [Money] desired_profit the profit to gain from arbitrage
# @return [Money] the stake necessary to realise the desired profit
@@ -95,8 +91,8 @@
def sum_inverse_outcome
fractions.reduce(0) {|sum, n| sum + Rational(1, n) }
end
def fractions
- @mutually_exclusive_outcome_odds.collect {|o| o.fractional_odds }
+ @mutually_exclusive_outcome_odds.collect &:fractional_odds
end
end
\ No newline at end of file