1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
# This test create a database to adjust the best algorithm
# to use on correlation matrix
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../../lib"))
require 'distribution'
require 'statsample'
require 'benchmark'
if !File.exists?("binomial_coefficient.ds") or File.mtime(__FILE__) > File.mtime("binomial_coefficient.ds")
reps=100 #number of repetitions
ns={
5=> [1,3],
10=> [1,3,5],
50=> [1,3,5,10,25],
100=> [1,3,5,10,25,50],
500=> [1,3,5,10,25,50,100,250],
1000=> [1,3,5,10,25,50,100,250,500],
5000=> [1,3,5,10,25,50,100,250,500,1000,2500],
10000=>[1,3,5,10,25,50,100,250,500,1000,2500,5000]
}
rs=Statsample::Dataset.new(%w{n k mixed_factorial multiplicative})
ns.each do |n,ks|
ks.each do |k|
time_factorial= Benchmark.realtime do
reps.times {
(((n-k+1)..n).inject(1) {|ac,v| ac * v}).quo(Math.factorial(k))
}
end
time_multiplicative= Benchmark.realtime do
reps.times {
(1..k).inject(1) {|ac, i| (ac*(n-k+i).quo(i))}
}
end
puts "n:#{n}, k:#{k} -> factorial:%0.3f | multiplicative: %0.3f " % [time_factorial, time_multiplicative]
rs.add_case({'n'=>n,'k'=>k,'mixed_factorial'=>time_factorial, 'multiplicative'=>time_multiplicative})
end
end
else
rs=Statsample.load("binomial_coefficient.ds")
end
rs.fields.each {|f| rs[f].type=:scale}
rs.update_valid_data
rs.save("binomial_coefficient.ds")
Statsample::Excel.write(rs,"binomial_coefficient.xls")
|