Home

Awesome

DescriptiveStatistics

This gem calculates descriptive statistics including measures of central tendency (e.g. mean, median mode), dispersion (e.g. range, and quartiles), and spread (e.g variance and standard deviation).

Tested against 2.4.9, 2.5.7, 2.6.5, 2.7.0, ruby-head, jruby-9.1.9.0, jruby-head and rubinius-4.7.

Build Status Dependency Status Code Climate Gem Version Coverage Status

Installation

Add this line to your application's Gemfile:

gem 'descriptive-statistics'

And then execute:

$ bundle

Or install it yourself as:

$ gem install descriptive-statistics

Usage

Central Tendency:

stats = DescriptiveStatistics::Stats.new([1,1,2,3,10])
stats.mean #=> 3.4
stats.median #=> 2
stats.mode #=> 1

Dispersion:

stats = DescriptiveStatistics::Stats.new([1,1,2,3,10])
stats.range #=> 9
stats.min #=> 1
stats.max #=> 10
stats.percentile_from_value(10) #=> 80
stats.value_from_percentile(60) #=> 3

Spread:

stats = DescriptiveStatistics::Stats.new([1,1,2,3,10])
stats.variance #=> 14.299999999999999
stats.population_variance  #=> 11.44
stats.standard_deviation #=> 3.7815340802378072
stats.relative_standard_deviation #=> 99.47961485463391

Other Measures:

stats = DescriptiveStatistics::Stats.new([1,1,2,3,10])
stats.skewness #=> 1.188328915820243
stats.kurtosis #=> 2.405613966453127

Alternative Usage (Not suggested)

If you want to monkey patch descriptive statistics methods into Enumerable, you can use the following:

(e.g. config/initializers/descriptive_statistics_monkey_patch.rb)

require 'descriptive-statistics'

module Enumerable
  include DescriptiveStatistics

  # Warning: hacky evil meta programming. Required because classes that have already included
  # Enumerable will not otherwise inherit the statistics methods.
  DescriptiveStatistics.instance_methods.each do |m|
    define_method(m, DescriptiveStatistics.instance_method(m))
  end
end

Then you can use these methods directly on Arrays:

[1,1,2,3,10].mean #=> 3.4

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request