require 'spec_helper' describe StatisticalSpread do describe '#sum' do it 'to be the sum of array values' do expect(StatisticalSpread.new([1,2,6]).sum).to eq(9) end it 'to be a block passed' do expect(StatisticalSpread.new([1,2,6]).sum{ |x| x * 2 }).to eq(18) end it 'to be 0 if empty' do expect(StatisticalSpread.new([]).sum).to eq(0) end end describe "#mean" do it "to be the sum of array values divided by number of array values" do expect(StatisticalSpread.new([1,2,6]).mean).to eq(3) end it "to be nil if empty" do expect(StatisticalSpread.new([]).mean).to be_nil end end describe '#median' do it 'to be the value lying at the midpoint of the array' do expect(StatisticalSpread.new([1,2,6]).median).to eq(2) end it 'to be the mean of the two midpoint values if the array length is even' do expect(StatisticalSpread.new([1,2,3,6]).median).to eq(2.5) end it 'to be nil if empty' do expect(StatisticalSpread.new([]).median).to be_nil end end describe '#mode' do it 'to be the most frequent value' do expect(StatisticalSpread.new([1,1,2,46]).mode).to eq(1) end it 'to be the first value if there is only one' do expect(StatisticalSpread.new([3.5]).mode).to eq(3.5) end it 'to be nil if nil if there is no most frequent value' do expect(StatisticalSpread.new([1,2,3]).mode).to be_nil end it 'to be nil if empty' do expect(StatisticalSpread.new([]).mode).to be_nil end end describe '#range' do it 'to be the mean squared deviation of the sample (n - 1 denominator)' do expect(StatisticalSpread.new([1,2,6]).range).to eq(5) end it 'to be nil if empty' do expect(StatisticalSpread.new([]).range).to be_nil end end describe '#max' do it 'to be the highest value' do expect(StatisticalSpread.new([1,2,6]).max).to eq(6) end it 'to be nil if empty' do expect(StatisticalSpread.new([]).max).to be_nil end end describe '#min' do it 'to be the lowest value' do expect(StatisticalSpread.new([1,2,6]).min).to eq(1) end it 'to be nil if empty' do expect(StatisticalSpread.new([]).min).to be_nil end end describe '#percentile_from_value' do it 'to be the precise percentile of each value' do data = [95.1772, 95.1567, 95.1937, 95.1959, 95.1442, 95.061, 95.1591, 95.1195,95.1065, 95.0925, 95.199, 95.1682] percentiles = [0,9,17,25,34,42,50,59,67,75,84,92] stats = StatisticalSpread.new(data) data.sort.each_with_index do |datum, i| expect(stats.percentile_from_value(datum)).to eq(percentiles[i]) end end it 'to be nil if empty' do expect(StatisticalSpread.new([]).percentile_from_value(1)).to be_nil end end describe '#value_from_percentile' do it 'to be the precise percentile of each value' do data = [95.1772, 95.1567, 95.1937, 95.1959, 95.1442, 95.061, 95.1591, 95.1195,95.1065, 95.0925, 95.199, 95.1682] percentiles = [0,10,20,30,40,50,60,70,80,90] values = [95.061,95.1065,95.1195,95.1442,95.1567,95.1591,95.1772,95.1937,95.1959,95.199] stats = StatisticalSpread.new(data) percentiles.sort.each_with_index do |percentile, i| expect(stats.value_from_percentile(percentile)).to eq(values[i]) end end it 'to be nil if empty' do expect(StatisticalSpread.new([]).value_from_percentile(1)).to be_nil end end describe '#variance' do it 'to be the mean squared deviation of the sample (n - 1 denominator)' do expect(StatisticalSpread.new([1,2,6]).variance).to eq(7) end it 'to be nil if empty' do expect(StatisticalSpread.new([]).variance).to be_nil end end describe '#population_variance' do it 'to be the mean squared deviation of the sample' do expect(StatisticalSpread.new([1,2,3,4]).population_variance).to eq(1.25) end it 'to be nil if empty' do expect(StatisticalSpread.new([]).population_variance).to be_nil end end describe '#standard_deviation' do it 'to be the square root of the #variance' do expect(StatisticalSpread.new([1,2,6]).standard_deviation).to eq(2.6457513110645907) end it 'to be nil for single element arrays' do expect(StatisticalSpread.new([1]).standard_deviation).to be_nil end it 'to be nil if empty' do expect(StatisticalSpread.new([]).standard_deviation).to be_nil end end describe '#relative_standard_deviation' do it 'to be 0 for constant values' do expect(StatisticalSpread.new([100,100,100]).relative_standard_deviation).to eq(0) end it 'to be the #population_standard_deviation divided by the mean * 100' do expect(StatisticalSpread.new([90,100,110]).relative_standard_deviation).to be_within(0.01).of(8.16) expect(StatisticalSpread.new([1,5,6,8,10,40,65,88]).relative_standard_deviation).to be_within(0.01).of(110.41) end it 'to be nil if empty' do expect(StatisticalSpread.new([]).relative_standard_deviation).to be_nil end end describe '#population_standard_deviation' do it 'to be the square root of the #population_variance' do expect(StatisticalSpread.new([1,2,6]).population_standard_deviation).to eq(2.160246899469287) end it 'to be 0 for single element arrays' do expect(StatisticalSpread.new([1]).population_standard_deviation).to eq(0) end it 'to be nil if empty' do expect(StatisticalSpread.new([]).population_standard_deviation).to be_nil end end describe '#skewness' do it 'to be the measure of skewness of the data as close to 0 when not skewed' do expect(StatisticalSpread.new([1,3,5,3,1]).skewness).to eq(0.30734449954312965) end it 'to be the measure of skewness of the data as high when skewed' do expect(StatisticalSpread.new([50,10,1,1,1]).skewness).to eq(1.2374536958450908) end it 'to be 0 if only one element' do expect(StatisticalSpread.new([1]).skewness).to eq(0) end it 'to be nil if empty' do expect(StatisticalSpread.new([]).skewness).to be_nil end end describe '#kurtosis' do it 'to be the measure of kurtosis of the data as close to 0 when not skewed' do expect(StatisticalSpread.new([1,3,5,3,1]).kurtosis).to eq(1.477551020408163) end it 'to be the measure of skewness of the data as high when skewed' do expect(StatisticalSpread.new([99,10,1,1,1]).kurtosis).to eq(2.56586117539186) end it 'to be 0 if only one element' do expect(StatisticalSpread.new([1]).kurtosis).to eq(0) end it 'to be nil if empty' do expect(StatisticalSpread.new([]).kurtosis).to be_nil end end end