Compare commits

...

12 Commits

Author SHA1 Message Date
Arthur Poulet 924893d02d
migrate 2019-02-18 12:09:58 +01:00
Arthur POULET 2c76ea54fc
Update readme 2018-11-30 00:56:32 +01:00
Arthur POULET 7c8a0e01ca
Merge branch 'fix-big' 2018-11-30 00:55:54 +01:00
Arthur POULET 0d9304d183
Bump v0.3.0 2018-11-30 00:55:40 +01:00
Arthur POULET 15aad6fd73
Fix some big test and functions 2018-11-30 00:55:08 +01:00
Arthur POULET 9739e3d42c
Fix coding style 2018-11-29 00:17:23 +01:00
Arthur POULET 7ba3ee31ee
Update README freq exemple 2018-11-29 00:08:19 +01:00
Arthur POULET 59fa29a95d
Update readme development section 2018-11-29 00:05:17 +01:00
Arthur POULET 2f0a0ad399
Dump version v0.2.6 2018-11-29 00:04:03 +01:00
Arthur POULET e82eac24e1
Add all_frequencies 2018-11-29 00:03:38 +01:00
Arthur POULET 6af7fa3a3e
Bump v0.2.5 2018-11-28 19:48:20 +01:00
Arthur POULET 9a3b930177
Add frequency of 2018-11-28 19:47:52 +01:00
16 changed files with 133 additions and 17 deletions

View File

@ -1,3 +1,5 @@
**Migrated to <https://git.sceptique.eu/Sceptique/stats>**
# stats
An expressive implementation of statistical distributions.
@ -10,7 +12,7 @@ Add this to your application's `shard.yml`:
```yaml
dependencies:
stats:
github: Nephos/stats
git: https://git.sceptique.eu/Sceptique/stats
```
@ -92,6 +94,8 @@ Math.factorial(4) # => 24
### Quartiles & Boxplot
*Note: not big compatible yet*
```crystal
[1, 3, 5].first_quartile # => 2.0 (alias of lower_quartile)
[1, 3, 5].second_quartile # => 3.0 (alias of median)
@ -118,9 +122,16 @@ arr.upper_fence(3) # => 47.5 (Q3 + 3 * IQR)
arr.upper_outliers(3) # => [1337]
```
### Frequency
```crystal
[0, 1, 2, 3].frequency_of(0) # => 0.25 (amount of X in the population, by the size of the population)
[0, 0, 1, 2, 3].all_frequencies # => { 0 => 0.4, 1 => 0.2, 2 => 0.2, 3 => 0.2}
```
## Development
- The lib is adapted to be usable with BigInt and BigFloat values
- The lib should take care of "big" numbers
## Contributing

View File

@ -1,5 +1,5 @@
name: stats
version: 0.2.4
version: 0.3.0
authors:
- Arthur Poulet <arthur.poulet@mailoo.org>

View File

@ -1,3 +1,5 @@
require "big"
describe Math::Correlation do
it "test basic confidence interval" do
arr1 = [1, 2, 2.5, 3, 3.5, 3.8, 4, 4.2, 4.4, 4.5]
@ -9,4 +11,11 @@ describe Math::Correlation do
arr1.covariance(arr2).round(4).should eq 4.2215
arr1.correlation_coef(arr2).round(4).should eq 0.8906
end
it "test big" do
[BigInt.new(1), 2].correlation_coef([2, 3])
[1, 2].correlation_coef([BigInt.new(2), 3])
[BigInt.new(1), 2].correlation_coef([BigInt.new(2), 3])
[1, 2].correlation_coef([BigInt.new(2), BigInt.new(3)])
end
end

View File

@ -10,8 +10,8 @@ describe Math do
Math.factorial(6).should eq 720
end
it "factorial bigint" do
res = (1..20).to_a.reduce(BigInt.new 1) { |e, i| e * BigInt.new(i) }
Math.factorial(BigInt.new 20).should eq res
it "factorial big" do
res = (1..20).to_a.reduce(BigInt.new(1)) { |e, i| e * BigInt.new(i) }
Math.factorial(BigInt.new(20)).should eq res
end
end

34
spec/math/frequency.cr Normal file
View File

@ -0,0 +1,34 @@
FREQ_LIMIT = 100
describe Math::Frequency do
it "test trivia" do
([] of Int32).frequency_of(0).should eq 0.0
[0, 1, 2, 3].frequency_of(0).should eq 0.25
[0, 0, 1, 2, 3].frequency_of(0).should eq 0.40
[0, 0, 1, 2, 3].all_frequencies.should eq({0 => 0.4, 1 => 0.2, 2 => 0.2, 3 => 0.2})
# allfreq1 = [0, 0, 1, 2, 3].all_frequencies(2)
# allfreq1[1].should eq 0.4
# allfreq1.size.should eq 2
# expect_raises(Error) { [0, 0, 1, 2, 3].all_frequencies(2, true) }
end
it "test basic" do
FREQ_LIMIT.times do |modi|
modulo = modi + 1
# we should have the same or more because we don't / modulo in the freq
arr = FREQ_LIMIT.times.to_a.map { |e| e % modulo }
(arr.frequency_of(0) >= (1.0f64 / modulo)).should be_true
next if modulo <= 20
# make an array with less or equal amount of iterations
arr_less = FREQ_LIMIT.times.to_a.map { |e| e % (modulo + 5) }
(arr.frequency_of(0) >= arr_less.frequency_of(0)).should be_true
# make an array with more or equal amount of iterations
arr_more = FREQ_LIMIT.times.to_a.map { |e| e % (modulo - 5) }
(arr.frequency_of(0) <= arr_more.frequency_of(0)).should be_true
end
[0, 0, 0, 0, 1, 1, 1, 2, 2, 3].all_frequencies.should eq({0 => 0.4, 1 => 0.3, 2 => 0.2, 3 => 0.1})
end
end

View File

@ -1,5 +1,11 @@
require "big"
describe Math::MACD do
it "test basic macd" do
[1, 2, 3, 2, 1].macd(3).map { |e| e.round(3) }.should eq [2, 2.333, 2]
end
it "test big basic macd" do
puts [BigInt.new(1), BigInt.new(2), BigInt.new(3), BigInt.new(2), 1].macd(3).map { |e| e.round(3) }.should eq [2, 2.333, 2]
end
end

View File

@ -1,3 +1,5 @@
require "big"
describe Math::Mean do
it "test several basic mean special case" do
arr = ([] of Int32)
@ -17,6 +19,11 @@ describe Math::Mean do
[1, 2, -3].mean.should eq 0.0
end
it "test mean on big" do
[BigInt.new(1), 2, 3].mean.should eq 2.0
[BigInt.new(1), BigInt.new(2), BigFloat.new(-3)].mean.should eq 0.0
end
it "test quadratic mean" do
[1, 2, 3, 2].quadratic_mean.round(4).should eq(2.1213)
[1, 2, 1, 5, 10, 9, 1, -13, 2].quadratic_mean.round(4).should eq(6.549)

View File

@ -1,6 +1,8 @@
require "big"
describe Math::Median do
it "test trivia" do
arr = ([]of Int32)
arr = ([] of Int32)
arr.median.should eq 0.0
end
@ -10,7 +12,12 @@ describe Math::Median do
[1, 2, 5].median.should eq 2.0
[2, 5, 1].median.should eq 2.0
[4, 1, 1, 1, 2].median.should eq 1.0
end
it "test big" do
[BigInt.new(1.0), 2.0].median.should eq 1.5
[BigInt.new(1.0), BigFloat.new(2.0)].median.should eq 1.5
end
end

View File

@ -1,3 +1,5 @@
require "big"
module Math::Quartile
it "trivial" do
arr = [1, 3, 5]
@ -11,6 +13,19 @@ module Math::Quartile
arr.iqr.should eq 2.0
end
# TODO
# it "big" do
# arr = [BigInt.new(1), BigFloat.new(3), 5]
#
# arr.first_quartile.should eq 2.0
# arr.second_quartile.should eq 3.0
# arr.third_quartile.should eq 4.0
#
# arr.quartiles.should eq [2.0, 3.0, 4.0]
#
# arr.iqr.should eq 2.0
# end
it "odd size input" do
arr = [6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49]
@ -79,6 +94,5 @@ module Math::Quartile
arr.upper_fence(3).should eq 47.5
arr.upper_outliers(3).should eq [1337]
end
end

View File

@ -9,6 +9,16 @@ describe Math::StandardDeviation do
arr.standard_deviation.should eq(standard_deviation)
end
it "test big" do
arr = [BigInt.new(1), BigFloat.new(2), 3, 4.0, 4]
mean = (4 + 4 + 3 + 2 + 1) / 5.0
variance = ((4 - mean)**2 + (4 - mean)**2 + (3 - mean)**2 + (2 - mean)**2 + (1 - mean)**2) / 5.0
standard_deviation = Math.sqrt(variance)
arr.mean.should eq(mean)
arr.variance.should eq(variance)
arr.standard_deviation.should eq(standard_deviation)
end
it "test standard deviation without explanations" do
arr = [1, 5, 23, 2, 0, 0, 1]
arr.mean.round(2).should eq 4.57

18
src/lib/math/frequency.cr Normal file
View File

@ -0,0 +1,18 @@
module Math::Frequency(T)
def frequency_of(value : T) : Float64
return 0.0f64 if empty?
count { |curr| curr == value }.to_f64 / size.to_f64
end
def all_frequencies : Hash(T, Float64)
values = to_set
frequencies = Hash(T, Float64).new(0.0f64, values.size)
each { |value| frequencies[value] += 1 }
frequencies.each { |k, _| frequencies[k] = frequencies[k] / size }
frequencies
end
end
module Enumerable(T)
include Math::Frequency(T)
end

View File

@ -1,14 +1,14 @@
module Math::Mean
# Standard arithmetic mean
# TODO: Handle big Float/Int
def mean : Float64
def mean
return 0.0_f64 if empty?
sum.to_f64 / size.to_f64
end
# The root square mean of the list.
# TODO: Handle big Float/Int
def quadratic_mean : Float64
def quadratic_mean
return 0.0_f64 if empty?
Math.sqrt map { |e| e ** 2 }.mean
end
@ -16,14 +16,14 @@ module Math::Mean
# The geometric mean of the list.
# For [a, b], a/c = c/b; c**2 = a*b
# TODO: Handle big Float/Int
def geometric_mean : Float64
def geometric_mean
return 0.0_f64 if empty?
reduce { |l, r| l * r } ** (1.0 / size.to_f64)
end
# The harmonic mean of the list.
# TODO: Handle big Float/Int
def harmonic_mean : Float64
def harmonic_mean
return 0.0_f64 if empty?
size.to_f64 / map { |e| 1.0 / e }.sum
end

View File

@ -1,9 +1,9 @@
module Math::Median
def median : Float64
def median
return 0.0_f64 if empty?
sorted = sort
size = size()
return sorted[(size - 1) / 2].to_f64 if size.odd?
return sorted[(size - 1) / 2] / 1.0 if size.odd?
(sorted[(size / 2) - 1] + sorted[size / 2]) / 2.0
end
end

View File

@ -1,5 +1,5 @@
# There are several methods for computing the quartiles of an array.
#
#
# This library utilizes the method proposed by John Tukey
# https://en.wikipedia.org/wiki/Quartile#Method_2
#

View File

@ -1,3 +1,3 @@
module Stats
VERSION = "0.2"
VERSION = "0.3"
end