spec/math_extension_spec.rb in distribution-0.3.0 vs spec/math_extension_spec.rb in distribution-0.4.0

- old
+ new

@@ -6,27 +6,32 @@ n.times do |k| Math.binomial_coefficient(n,k).should eq(Math.factorial(n).quo(Math.factorial(k)*Math.factorial(n-k))) end end - it "binomial coefficient(gamma) with n<=48 should be correct " do - - [1,5,10,25,48].each {|n| - k=(n/2).to_i - Math.binomial_coefficient_gamma(n,k).round.should eq(Math.binomial_coefficient(n,k)) - } - end + it "rising_factorial should return correct values" do x=rand(10)+1 Math.rising_factorial(x,0).should eq 1 Math.rising_factorial(x,1).should eq x Math.rising_factorial(x,2).should eq x**2+x Math.rising_factorial(x,3).should eq x**3+3*x**2+2*x Math.rising_factorial(x,4).should eq x**4+6*x**3+11*x**2+6*x end + + it "permutations should return correct values" do + n=rand(50)+50 + 10.times { |k| + Math.permutations(n,k).should eq(Math.factorial(n) / Math.factorial(n-k)) + } + + + Math.permutations(n,n).should eq(Math.factorial(n) / Math.factorial(n-n)) + end + it "incomplete beta function should return similar results to R" do pending("Not working yet") Math.incomplete_beta(0.5,5,6).should be_within(1e-6).of(Math.beta(5,6)*0.6230469) Math.incomplete_beta(0.6,5,6).should be_within(1e-6).of(Math.beta(5,6)*0.0006617154) end @@ -44,15 +49,25 @@ Math.regularized_beta_function(x,a,b).should be_within(1e-6). of(1-Math.regularized_beta_function(1-x,b,a)) end - it "binomial coefficient(gamma) with 48<n<1000 should have 12 correct digits" do + + it "binomial coefficient(gamma) with n<=48 should be correct " do - [50,100,1000].each {|n| - k=n/2.to_i - obs=Math.binomial_coefficient_gamma(n,k).to_i.to_s[0,12] - exp=Math.binomial_coefficient(n,k).to_i.to_s[0,12] + [1,5,10,25,48].each {|n| + k=(n/2).to_i + Math.binomial_coefficient_gamma(n,k).round.should eq(Math.binomial_coefficient(n,k)) + } + end + + it "binomial coefficient(gamma) with 48<n<1000 should have 11 correct digits" do + + [50,100,200,1000].each {|n| + k=(n/2).to_i + obs=Math.binomial_coefficient_gamma(n, k).to_i.to_s[0,11] + exp=Math.binomial_coefficient(n, k).to_i.to_s[0,11] + obs.should eq(exp) } end describe Distribution::MathExtension::SwingFactorial do