Browse Source

fix format (crystal tool format)

pull/2/head
Arthur POULET 5 years ago
parent
commit
e841b1f93c
No known key found for this signature in database GPG Key ID: 7C42395DCA99B923
  1. 4
      spec/binomial_distribution.cr
  2. 5
      spec/math/coef_binomial.cr
  3. 4
      spec/math/factorial.cr
  4. 2
      spec/normale_distribution.cr
  5. 4
      src/stats.cr
  6. 6
      src/stats/binomial_distribution.cr
  7. 1
      src/stats/math/coef_binomial.cr
  8. 1
      src/stats/math/exceptions.cr
  9. 4
      src/stats/math/factorial.cr
  10. 3
      src/stats/math/numeric_value.cr
  11. 2
      src/stats/normale_distribution.cr
  12. 6
      src/stats/normale_distribution/persistant.cr

4
spec/binomial_distribution.cr

@ -1,5 +1,4 @@
describe Math do
it "binomial_distribution" do
0.0.step(by: 0.1, limit: 1.0) do |proba|
Math.binomial_distribution(tries: 1, probability: proba, success: 1).should eq(proba)
@ -7,11 +6,9 @@ describe Math do
Math.binomial_distribution(tries: 10, probability: 0.2, success: 3).round(3).should eq(0.201)
Math.binomial_distribution(tries: 10, probability: 0.2, success: 6).round(4).should eq(0.0055)
end
end
describe BinomialDistribution do
it "initialize" do
BinomialDistribution.new(0).should be_a(BinomialDistribution)
BinomialDistribution.new(1).should be_a(BinomialDistribution)
@ -42,5 +39,4 @@ describe BinomialDistribution do
BinomialDistribution.new(
BigInt.new(1), BigFloat.new(1)).should be_a(BinomialDistribution)
end
end

5
spec/math/coef_binomial.cr

@ -12,11 +12,11 @@ describe Math do
Math.coef_binomial(261, 262).should eq 0
Math.coef_binomial(261, 263).should eq 0
Math.coef_binomial(261, 26372).should eq 0
20.times {|i| Math.coef_binomial(i + 1, i + rand(1..5) + 1).should eq 0}
20.times { |i| Math.coef_binomial(i + 1, i + rand(1..5) + 1).should eq 0 }
end
it "coef binomial tests a = b" do
20.times {|i| Math.coef_binomial(i + 1, i + 1).should eq 1}
20.times { |i| Math.coef_binomial(i + 1, i + 1).should eq 1 }
end
it "coef_binomial with BigInt" do
@ -25,5 +25,4 @@ describe Math do
BigInt.new("fffffffffffffffffffffffffffffffe", 16),
BigInt.new("ffffffffffffffffffffffffffffffff", 16)).should eq 0
end
end

4
spec/math/factorial.cr

@ -1,5 +1,4 @@
describe Math do
it "factorial" do
Math.factorial(0).should eq 1
Math.factorial(1).should eq 1
@ -12,8 +11,7 @@ describe Math do
end
it "factorial bigint" do
res = (1..20).to_a.reduce(BigInt.new 1) {|e, i| e * BigInt.new(i) }
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

2
spec/normale_distribution.cr

@ -8,7 +8,6 @@ describe NormaleDistribution do
end
describe NormaleDistribution::Persistant do
it "instances" do
NormaleDistribution::Persistant.new
NormaleDistribution::Persistant.new(standard_deviation: 12).standard_deviation.should eq(12)
@ -43,5 +42,4 @@ describe NormaleDistribution::Persistant do
expect_raises { NormaleDistribution::Persistant.new standard_deviation: -1 }
expect_raises { NormaleDistribution::Persistant.new standard_deviation: 0 }
end
end

4
src/stats.cr

@ -1,5 +1,5 @@
#require "big_int"
#require "big_float"
# require "big_int"
# require "big_float"
require "./stats/*"
require "./stats/math/*"

6
src/stats/binomial_distribution.cr

@ -1,5 +1,4 @@
class BinomialDistribution
@n : Int::All
@p : Number::All
@ -18,9 +17,9 @@ class BinomialDistribution
# @param k [Fixnum] number of test successful.
# @return [Float] probability
# TODO : Enumerable of Int
#  TODO : Enumerable of Int
def distribute(k : Enumerable)
k.map{|p| distribute(p) }.reduce{|a, b| a + b}
k.map { |p| distribute(p) }.reduce { |a, b| a + b }
end
# @param k [Enumerable] list of number of test successful.
@ -29,5 +28,4 @@ class BinomialDistribution
raise Math::SuperiorityError.new "the number of success must be lesser or equal to the number of tries (#{@n})" if k > @n
Math.coef_binomial(@n, k) * (@p**k) * ((1 - @p) ** (@n - k))
end
end

1
src/stats/math/coef_binomial.cr

@ -17,5 +17,4 @@ module Math
def binomial_distribution(tries : Int::All, success : Int::All, probability : Number::All)
BinomialDistribution.new(n: tries, p: probability).distribute(success)
end
end

1
src/stats/math/exceptions.cr

@ -1,4 +1,5 @@
module Math
class SuperiorityError < Exception; end
class DomainError < Exception; end
end

4
src/stats/math/factorial.cr

@ -1,11 +1,9 @@
module Math
module Factorial(U)
def factorial(n : U)
raise Math::DomainError.new "The argument must be a natural (out of domain -- factorial)" if n < 0
return U.new(1) if n == 0
return (1..n).to_a.reduce(U.new(1)){|a, b| a * b}
return (1..n).to_a.reduce(U.new(1)) { |a, b| a * b }
end
end
end

3
src/stats/math/numeric_value.cr

@ -3,8 +3,9 @@ abstract struct Int
end
require "big_float"
abstract struct Float
alias All = Float::Primitive | BigFloat #| BigRational
alias All = Float::Primitive | BigFloat # | BigRational
end
abstract struct Number

2
src/stats/normale_distribution.cr

@ -1,7 +1,6 @@
require "./normale_distribution/persistant"
module NormaleDistribution
def between(standard_deviation : Number::All, esperance : Number::All, min : Number::All, max : Number::All)
NormaleDistribution::Persistant.new(standard_deviation: standard_deviation, esperance: esperance).between min, max
end
@ -15,5 +14,4 @@ module NormaleDistribution
end
extend self
end

6
src/stats/normale_distribution/persistant.cr

@ -1,11 +1,10 @@
module NormaleDistribution
class Persistant
getter standard_deviation, esperance
setter standard_deviation, esperance
@standard_deviation : Number::All
@esperance : Number::All
@esperance : Number::All
def initialize(@standard_deviation = 1.0, @esperance = 0.0)
raise ArgumentError.new "standard_deviation must be > 0" unless @standard_deviation > 0.0
@ -24,13 +23,12 @@ module NormaleDistribution
end
private def density(t : Number::All)
#1.0 / (standard_deviation * (2 * Math::PI) ** 0.5) * Math::exp((t - esperance) / (2 * standard_deviation)))
# 1.0 / (standard_deviation * (2 * Math::PI) ** 0.5) * Math::exp((t - esperance) / (2 * standard_deviation)))
end
private def repartition(t : Number::All)
erf = (t - esperance) / (standard_deviation * 2.0**0.5)
0.5 * (1.0 + Math.erf(erf))
end
end
end

Loading…
Cancel
Save