forked from clbustos/distribution
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rubocopped lib, spec, benchmark and bin
Don't worry about the bizarre number of files changed, I simply ran `rubocop --auto-correct lib spec benchmark bin` and waited.
- Loading branch information
Showing
81 changed files
with
2,206 additions
and
2,345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,52 @@ | ||
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../lib")) | ||
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib')) | ||
require 'distribution' | ||
require 'bench_press' | ||
|
||
extend BenchPress | ||
|
||
|
||
|
||
samples=10.times.map {|i| 2**(i+1)} | ||
samples = 10.times.map { |i| 2**(i + 1) } | ||
|
||
name 'binomial coefficient: multiplicative, factorial and optimized factorial methods' | ||
author 'Claudio Bustos' | ||
date '2011-01-27' | ||
summary "Exact calculation of Binomial Coefficient could be obtained using multiplicative, pure factorial or optimized factorial algorithm (failing + factorial). | ||
summary "Exact calculation of Binomial Coefficient could be obtained using multiplicative, pure factorial or optimized factorial algorithm (failing + factorial). | ||
Which one is faster? | ||
Lower k is the best for all | ||
Lower k is the best for all | ||
k=n/2 is the worst case. | ||
The factorial method uses the fastest Swing Prime Algorithm." | ||
|
||
reps 10 #number of repetitions | ||
|
||
x=100 | ||
|
||
n=100 | ||
k=50 | ||
reps 10 # number of repetitions | ||
|
||
x = 100 | ||
|
||
n = 100 | ||
k = 50 | ||
|
||
measure "Multiplicative" do | ||
measure 'Multiplicative' do | ||
samples.each do |n| | ||
[5,n/2].each do |k| | ||
k=[k,n-k].min | ||
(1..k).inject(1) {|ac, i| (ac*(n-k+i).quo(i))} | ||
[5, n / 2].each do |k| | ||
k = [k, n - k].min | ||
(1..k).inject(1) { |ac, i| (ac * (n - k + i).quo(i)) } | ||
end | ||
end | ||
end | ||
|
||
measure "Pure Factorial" do | ||
measure 'Pure Factorial' do | ||
samples.each do |n| | ||
[5,n/2].each do |k| | ||
k=[k,n-k].min | ||
[5, n / 2].each do |k| | ||
k = [k, n - k].min | ||
Math.factorial(n).quo(Math.factorial(k) * Math.factorial(n - k)) | ||
end | ||
end | ||
end | ||
|
||
measure "Failing factorial + factorial" do | ||
measure 'Failing factorial + factorial' do | ||
samples.each do |n| | ||
[5,n/2].each do |k| | ||
k=[k,n-k].min | ||
(((n-k+1)..n).inject(1) {|ac,v| ac * v}).quo(Math.factorial(k)) | ||
[5, n / 2].each do |k| | ||
k = [k, n - k].min | ||
(((n - k + 1)..n).inject(1) { |ac, v| ac * v }).quo(Math.factorial(k)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,51 @@ | ||
# This test create a database to adjust the best algorithm | ||
# to use on correlation matrix | ||
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../../lib")) | ||
$LOAD_PATH.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)) | ||
if !File.exist?('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], | ||
10_000 => [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))} | ||
|
||
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}) | ||
|
||
rs.add_case('n' => n, 'k' => k, 'mixed_factorial' => time_factorial, 'multiplicative' => time_multiplicative) | ||
end | ||
end | ||
|
||
else | ||
rs=Statsample.load("binomial_coefficient.ds") | ||
rs = Statsample.load('binomial_coefficient.ds') | ||
end | ||
|
||
|
||
rs.fields.each {|f| rs[f].type=:scale} | ||
|
||
rs.fields.each { |f| rs[f].type = :scale } | ||
|
||
rs.update_valid_data | ||
rs.save("binomial_coefficient.ds") | ||
Statsample::Excel.write(rs,"binomial_coefficient.xls") | ||
rs.save('binomial_coefficient.ds') | ||
Statsample::Excel.write(rs, 'binomial_coefficient.xls') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ class << self | |
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.