Sha256: a86eb6db59f94cca7b82630cdb3464f68f465b38743895f2b338f215ac730a91

Contents?: true

Size: 578 Bytes

Versions: 1

Compression:

Stored size: 578 Bytes

Contents

class Array
  # For reference:
  # https://www.mathsisfun.com/data/standard-deviation-formulas.html

  # Population standard deviation
  def stdevp
    return false unless self.length > 0

    arr = self
    # Step 1: Get the mean
    mean = arr.inject(:+).to_f / arr.length.to_f

    # Step 2: Subtract the mean and square the result
    step_2 = arr.reduce([]) do |i, n|
      i << (n.to_f - mean) ** 2
    end

    # Step 3: Get mean of step 2
    step_3 = step_2.inject(:+).to_f / step_2.length.to_f

    # Step 4: Square root of step 3
    Math.sqrt(step_3).to_f
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
quick_codes-0.1.0 lib/overloads/array.rb