Sha256: 1b018681f7c78a772c25c6afcd54853799857711b0a59c62752717704a54b4b0

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# Native Standard Deviation

An implementation of the standard deviation calculation in C, with much better performance (50x-100x) than using pure ruby.

## Installation

Add this line to your application's Gemfile:

    gem 'standard_deviation'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install standard_deviation

## Usage

Just call `standard_deviation` or `stdev` on a array with numbers.

``` ruby
[1, 2, 3, 4, 5].stdev => 1.5811388300841898

[521.0, BigDecimal("1234.45"), 1_120].standard_deviation => 383.168958598336
```

## Benchmarks

``` ruby
# Basic ruby implementation
class Array
  def stdev_ruby
    total = inject :+
    mean  = total.to_f / size
    variance = inject(0) { |variance, value| variance + (value - mean) ** 2 }

    Math.sqrt(variance / (size - 1))
  end
end

# Running on a 2.4 GHz MacBook Pro
n = 10000
values = (1..10_000).map(&:to_f)

Benchmark.bm do |b|
  b.report do
    n.times { values.stdev }
  end

  b.report do
    n.times { values.stdev_ruby }
  end
end

#       user     system      total        real
#   1.070000   0.000000   1.070000 (  1.065246)
#  88.180000   0.550000  88.730000 ( 93.835144)
```

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
standard_deviation-0.0.1 README.md