Browse Source

Improve type inference

pull/2/head
Arthur POULET 4 years ago
parent
commit
5fd9f504c4
No known key found for this signature in database GPG Key ID: FC154EA63FF5D8BC
  1. 18
      spec/binomial_distribution.cr
  2. 6
      spec/normale_distribution.cr
  3. 6
      src/lib/binomial_distribution.cr
  4. 10
      src/lib/normale_distribution.cr
  5. 2
      src/lib/normale_distribution/persistant.cr

18
spec/binomial_distribution.cr

@ -16,18 +16,18 @@ describe BinomialDistribution do
BinomialDistribution(Int32, Float64).new(1).should be_a(BinomialDistribution(Int32, Float64))
BinomialDistribution(Int32, Float64).new(100).should be_a(BinomialDistribution(Int32, Float64))
BinomialDistribution(Int32, Int32).new(1, 0).should be_a(BinomialDistribution(Int32, Int32))
BinomialDistribution(Int32, Float64).new(1, 0.5).should be_a(BinomialDistribution(Int32, Float64))
BinomialDistribution(Int32, Int32).new(1, 1).should be_a(BinomialDistribution(Int32, Int32))
BinomialDistribution.new(1, 0.5).should be_a(BinomialDistribution(Int32, Float64))
BinomialDistribution.new(1, 1).should be_a(BinomialDistribution(Int32, Int32))
end
it "initialize errors" do
expect_raises { BinomialDistribution(Int32, Float64).new(-1) }
expect_raises { BinomialDistribution(Int32, Int32).new(0, -1) }
expect_raises { BinomialDistribution(Int32, Float64).new(0, 1.5) }
expect_raises { BinomialDistribution.new(0, -1) }
expect_raises { BinomialDistribution.new(0, 1.5) }
end
it "distribute" do
b = BinomialDistribution(Int32, Float64).new(2, 0.5)
b = BinomialDistribution.new(2, 0.5)
b.should be_a(BinomialDistribution(Int32, Float64))
b.distribute(0).should eq 0.25
b.distribute(1).should eq 0.5
@ -41,10 +41,10 @@ describe BinomialDistribution do
end
it "with BitNumber" do
BinomialDistribution(BigInt, BigFloat).new(BigInt.new(1), BigFloat.new(1)).should be_a(BinomialDistribution(BigInt, BigFloat))
((a = BinomialDistribution(BigInt, BigFloat).new(BigInt.new(10), BigFloat.new(0.02)).distribute(BigInt.new(1)..BigInt.new(10))) > 0.02).should be_true
((b = BinomialDistribution(BigInt, BigFloat).new(BigInt.new(20), BigFloat.new(0.02)).distribute(BigInt.new(1)..BigInt.new(20))) > 0.02).should be_true
((c = BinomialDistribution(BigInt, BigFloat).new(BigInt.new(50), BigFloat.new(0.02)).distribute(BigInt.new(1)..BigInt.new(50))) > 0.02).should be_true
BinomialDistribution.new(BigInt.new(1), BigFloat.new(1)).should be_a(BinomialDistribution(BigInt, BigFloat))
((a = BinomialDistribution.new(BigInt.new(10), BigFloat.new(0.02)).distribute(BigInt.new(1)..BigInt.new(10))) > 0.02).should be_true
((b = BinomialDistribution.new(BigInt.new(20), BigFloat.new(0.02)).distribute(BigInt.new(1)..BigInt.new(20))) > 0.02).should be_true
((c = BinomialDistribution.new(BigInt.new(50), BigFloat.new(0.02)).distribute(BigInt.new(1)..BigInt.new(50))) > 0.02).should be_true
(b > a).should be_true
(c > b).should be_true
# p a, b, c

6
spec/normale_distribution.cr

@ -17,7 +17,7 @@ describe NormaleDistribution::Persistant do
end
it "instances with BigNumber" do
n = NormaleDistribution::Persistant(BigInt, BigFloat).new(
n = NormaleDistribution::Persistant.new(
standard_deviation: BigInt.new(1),
esperance: BigFloat.new(1))
n.standard_deviation.should eq(1)
@ -25,14 +25,14 @@ describe NormaleDistribution::Persistant do
end
it "QI" do
rule = NormaleDistribution::Persistant(Int32, Int32).new standard_deviation: 15, esperance: 100
rule = NormaleDistribution::Persistant.new standard_deviation: 15, esperance: 100
rule.between(85, 115).round(2).should eq(0.68)
end
it "centroid" do
[0.1, 1.0, 2.0, 4.1324].each do |space|
[0.0, 1.0, -1.0, 12.0, 41.0, 0.2, 0.233].each do |center|
rule = NormaleDistribution::Persistant(Float64, Float64).new standard_deviation: space, esperance: center
rule = NormaleDistribution::Persistant.new standard_deviation: space, esperance: center
[0.2, 0.4, 0.45, 0.55, 0.94, 1.1].each do |diff|
(-rule.between(diff, center)).should eq(rule.between(center, diff))
end

6
src/lib/binomial_distribution.cr

@ -1,12 +1,12 @@
class Stats::BinomialDistribution(N, P)
@n : N
@p : P
getter n : N
getter p : P
# @param n [Fixnum] number of tries
# @param p [Float] probability of success
#
# NOTE if no probability is defined, the default value will be 0.5
def initialize(@n, @p = 0.5)
def initialize(@n : N, @p : P = 0.5)
raise Math::DomainError.new "The argument `p` `#{@p}` is not in greater or equal to 0" if @p < 0.0
raise Math::DomainError.new "The argument `p` `#{@p}` is not in lesser or equal to 1" if @p > 1.0
raise Math::DomainError.new "The argument `n` `#{@n}` is not in greater or equal to 0" if @n < 0.0

10
src/lib/normale_distribution.cr

@ -1,20 +1,20 @@
require "./normale_distribution/persistant"
module Stats::NormaleDistribution
extend self
# Gives the probability of [ min; max ] for a normal distribution (*standard_deviation*)
def between(standard_deviation : Number, esperance : Number, min : Number, max : Number)
Stats::NormaleDistribution::Persistant(typeof(standard_deviation), typeof(esperance)).new(standard_deviation: standard_deviation, esperance: esperance).between min, max
Stats::NormaleDistribution::Persistant.new(standard_deviation: standard_deviation, esperance: esperance).between min, max
end
# Gives the probability of [ min; +inf [ for a normal distribution (*standard_deviation*)
def greater_than(standard_deviation : Number, esperance : Number, min : Number)
Stats::NormaleDistribution::Persistant(typeof(standard_deviation), typeof(esperance)).new(standard_deviation: standard_deviation, esperance: esperance).greater_than min
Stats::NormaleDistribution::Persistant.new(standard_deviation: standard_deviation, esperance: esperance).greater_than min
end
# Gives the probability of ] -inf; max ] for a normal distribution (*standard_deviation*)
def less_than(standard_deviation : Number, esperance : Number, max : Number)
Stats::NormaleDistribution::Persistant(typeof(standard_deviation), typeof(esperance)).new(standard_deviation: standard_deviation, esperance: esperance).less_than max
Stats::NormaleDistribution::Persistant.new(standard_deviation: standard_deviation, esperance: esperance).less_than max
end
extend self
end

2
src/lib/normale_distribution/persistant.cr

@ -3,7 +3,7 @@ class Stats::NormaleDistribution::Persistant(D, E)
property esperance : E
# Create a new normale distribution
def initialize(@standard_deviation = 1.0, @esperance = 0.0)
def initialize(@standard_deviation : D = 1.0, @esperance : E = 0.0)
raise ArgumentError.new "standard_deviation must be > 0" unless @standard_deviation > 0.0
end

Loading…
Cancel
Save