Class: MathProbability::Probability
- Inherits:
-
Object
- Object
- MathProbability::Probability
- Defined in:
- lib/math_probability.rb
Overview
This class encapsulates mathimatical probability methods.
Class Method Summary collapse
- .combinations(objects, at_a_time) ⇒ Object
- .permutations_no_repeat(objects, at_a_time) ⇒ Object
- .permutations_with_repeat(objects, at_a_time) ⇒ Object
- .probability(choices, outcomes, reduce = true) ⇒ Object
- .sigma(start_seq, end_seq, seq_step) ⇒ Object
Class Method Details
.combinations(objects, at_a_time) ⇒ Object
26 27 28 |
# File 'lib/math_probability.rb', line 26 def self.combinations(objects, at_a_time) (((objects.factoral)/((objects-at_a_time).factoral))*(1.0/(at_a_time.factoral))).to_i end |
.permutations_no_repeat(objects, at_a_time) ⇒ Object
18 19 20 |
# File 'lib/math_probability.rb', line 18 def self.permutations_no_repeat(objects, at_a_time) (objects.factoral)/(objects - at_a_time).factoral end |
.permutations_with_repeat(objects, at_a_time) ⇒ Object
22 23 24 |
# File 'lib/math_probability.rb', line 22 def self.permutations_with_repeat(objects, at_a_time) objects**at_a_time end |
.probability(choices, outcomes, reduce = true) ⇒ Object
30 31 32 33 |
# File 'lib/math_probability.rb', line 30 def self.probability(choices, outcomes, reduce=true) a = choices.to_f / outcomes answer = {decimal: a, percentage: "#{a*100}%", fraction: (reduce ? reduced_fraction(choices, outcomes) : "#{choices}/#{outcomes}")} end |
.sigma(start_seq, end_seq, seq_step) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/math_probability.rb', line 8 def self.sigma(start_seq, end_seq, seq_step) summnation ||= 0 seq_step.gsub!(/((?![x-z0-9\s\.\-\+\*\/\(\)]).)*/,'').upcase! raise ArgumentError , "Only use x,y,z as variables" if (seq_step =~ /[A-Z]/).nil? (start_seq..end_seq).each do |value| summnation += eval(seq_step.gsub(/[X-Z]/,value.to_s)) end summnation end |