Enumerable::Statistics provides some methods to calculate statistical summary in arrays and enumerables.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install enumerable-statistics
You should load this library by the following line in your script at first.
The following methods are supplied by this library:
- Calculates a mean of values in an array or an enumerable
- Calculates a variance of values in an array or an enumerable
- Calculates a standard deviation of values in an array or an enumerable
- Calculates a mean and a variance simultaneously
- Calculates a mean and a standard deviation simultaneously
- Calculates a median of values in an array
- Calculates a percentile or percentiles of values in an array
- Count how many items for each value in the container
- Calculate histogram of the values in the array
Moreover, for Ruby < 2.4,
Enumerable#sum are provided.
All methods scan a collection once to calculate statistics and preserve precision as possible.
$ bundle exec rake bench # sum Warming up -------------------------------------- inject 1.545k i/100ms while 2.342k i/100ms sum 11.009k i/100ms Calculating ------------------------------------- inject 15.016k (
After checking out the repo, run
bin/setup to install dependencies. You can also run
bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run
bundle exec rake install. To release a new version, update the version number in
version.rb, and then run
bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the
.gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/mrkn/enumerable-statistics.