Skip to content

Commit

Permalink
support collections with a single value
Browse files Browse the repository at this point in the history
For collections of a single value, percentile was raising an exception.
This was resolved by simply returning the single value (converted to
a Float) in this case.
  • Loading branch information
thirtysixthspan committed Sep 2, 2014
1 parent be67232 commit 23c656e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 3 deletions.
7 changes: 5 additions & 2 deletions Rakefile
@@ -1,10 +1,13 @@
require 'bundler/setup'
require 'bundler/gem_tasks'
require 'rake/testtask'
require 'rspec/core/rake_task'

Rake::TestTask.new do |t|
t.test_files = FileList['spec/*.rb','test/*.rb']
t.test_files = FileList['test/*.rb']
t.verbose = true
end

task :default => :test
RSpec::Core::RakeTask.new(:spec)

task :default => [ :test, :spec ]
2 changes: 1 addition & 1 deletion descriptive_statistics.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'descriptive_statistics'
s.version = '2.0.0'
s.version = '2.0.1'
s.homepage = 'https://github.com/thirtysixthspan/descriptive_statistics'
s.summary = 'Descriptive Statistics'
s.description = 'Adds descriptive statistics methods to Enumerable module for use on collections or Numeric data'
Expand Down
2 changes: 2 additions & 0 deletions lib/descriptive_statistics/percentile.rb
Expand Up @@ -3,6 +3,8 @@ def percentile(p)
values = Support::convert(self)
return unless values.size > 0

return values.first unless values.size > 1

sorted = values.sort
return sorted[-1] if p == 100
rank = p / 100.0 * (values.number - 1)
Expand Down
Binary file removed pkg/descriptive_statistics-2.0.0.gem
Binary file not shown.
Binary file added pkg/descriptive_statistics-2.0.1.gem
Binary file not shown.
58 changes: 58 additions & 0 deletions spec/single_value_spec.rb
@@ -0,0 +1,58 @@
require 'rspec'

describe "DescriptiveStatistics" do
require 'descriptive_statistics'

subject { [2] }

context "with a single value array" do

it "calculates the number" do
expect(subject.number).to eql(1.0)
end

it "calculates the sum" do
expect(subject.sum).to eql(2.0)
end

it "calculates the mean" do
expect(subject.mean).to eql(2.0)
end

it "calculates the median" do
expect(subject.median).to eql(2.0)
end

it "calculates the variance" do
expect(subject.variance).to eql(0.0)
end

it "calculates the standard_deviation" do
expect(subject.standard_deviation).to eql(0.0)
end

it "calculates the percentile" do
expect(subject.percentile(30)).to eql(2.0)
expect(subject.percentile(50)).to eql(2.0)
expect(subject.percentile(70)).to eql(2.0)
end

it "calculates the same value for the 50th percentile and median" do
expect(subject.percentile(50)).to eql(subject.median)
end

it "calculates the mode" do
expect(subject.mode).to eql(2)
end

it "calculates the range" do
expect(subject.range).to eql(0.0)
end

it "calculates the percentile rank" do
expect(subject.percentile_rank(2)).to eql(100.0)
end

end

end

0 comments on commit 23c656e

Please sign in to comment.