Home

Awesome

Ruby Native Statistics

Test status

This is a native extension to Ruby that adds native (C) statistical functions to the Array class. At present the following functions are provided:

Check the Github Actions build to see the currently supported versions of Ruby. This list will match whatever stable versions are specified at https://www.ruby-lang.org/en/downloads/.

It is generally more performant than calculating these values with pure Ruby. For a comparison, run the benchmarks with rake benchmark.

Test (Ruby 3.3.0)Run 1Run 2Run 3Run 4Run 5
bench_native_stdev0.0000690.0000740.0000640.0000650.000065
bench_ruby_stdev0.0009470.0009320.0009270.0009480.000909
Test (Ruby 3.3.0)Run 1Run 2Run 3Run 4Run 5
bench_native_median0.0007190.000670.0006590.0006380.000668
bench_ruby_median0.0007740.0007430.0007240.0006970.000683
Test (Ruby 3.3.0)Run 1Run 2Run 3Run 4Run 5
bench_native_mean0.0000350.0000350.0000340.0000320.000033
bench_ruby_mean0.0002910.0002870.0002910.0002990.000281

Found a bug? Need a function?

If you found a bug or need a particular function, please let me know! I work on this gem in my spare time, mainly for learning purposes. Feel free to open a PR or a Github issue and I'll take a look as soon as possible.

Usage

require 'ruby_native_statistics'
r = [1,3,21,32,42]

# calculate sample standard deviation, you can also use "stdevs"
p r.stdev

# calculate population standard deviation
p r.stdevp

# calculate mean
p r.mean

# calculate median
p r.median

# calculate percentile
p r.percentile(0.3333)

Implementation notes

Percentile

Percentile uses the same rounding method as Excel, sometimes called R7.

Links

This is the third version of this gem, and it is a total rewrite of a SWIG-based design. Lots of thanks to the following resources: