Skip to content

Commit

Permalink
Rubocopped lib, spec, benchmark and bin
Browse files Browse the repository at this point in the history
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
agarie committed May 28, 2015
1 parent 3bde538 commit 79e04f7
Show file tree
Hide file tree
Showing 81 changed files with 2,206 additions and 2,345 deletions.
42 changes: 19 additions & 23 deletions benchmark/binomial_coefficient.rb
@@ -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
69 changes: 33 additions & 36 deletions benchmark/binomial_coefficient/experiment.rb
@@ -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')
15 changes: 7 additions & 8 deletions benchmark/factorial_hash.rb
@@ -1,4 +1,4 @@
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../lib"))
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
require 'bench_press'
require 'distribution'
extend BenchPress
Expand All @@ -8,17 +8,16 @@
date '2011-01-31'
summary "
Is better create a lookup table for factorial or just calculate it?
Distribution::MathExtension::SwingFactorial has a lookup table
Distribution::MathExtension::SwingFactorial has a lookup table
for factorials n<20
"

reps 1000 #number of repetitions
reps 1000 # number of repetitions

measure "Lookup" do
Math.factorial(19)
measure 'Lookup' do
Math.factorial(19)
end

measure "calculate" do
Distribution::MathExtension::SwingFactorial.naive_factorial(19)
measure 'calculate' do
Distribution::MathExtension::SwingFactorial.naive_factorial(19)
end

10 changes: 4 additions & 6 deletions benchmark/factorial_method.rb
@@ -1,4 +1,4 @@
$:.unshift(File.dirname(__FILE__)+"/../lib")
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
require 'distribution'
require 'bench_press'

Expand All @@ -10,19 +10,17 @@
summary "
Factorization requires a lot of processing, so approximation method could be required. But for greats value, bigdecimal are required and things start to get harder.
* Approximation (fast_factorial): Luschny f.3
* Exact (factorial): Luschny Swing Prime
* Exact (factorial): Luschny Swing Prime
"

reps 10 #number of repetitions
reps 10 # number of repetitions

x=200
x = 200

measure "Math.factorial(#{x})" do
Math.factorial(x)
end

measure "Math.fast_factorial(#{x})" do

Math.fast_factorial(x)
end

13 changes: 6 additions & 7 deletions benchmark/odd.rb
Expand Up @@ -10,13 +10,12 @@
Which is faster, n%1==1 or n%2==1
"

reps 10_000 #number of repetitions
n=100000
measure "Using &" do
n%1==1
reps 10_000 # number of repetitions
n = 100_000
measure 'Using &' do
n % 1 == 1
end

measure "Using %" do
n%2==1
measure 'Using %' do
n.odd?
end

22 changes: 11 additions & 11 deletions benchmark/power.rb
Expand Up @@ -6,22 +6,22 @@
author 'Claudio Bustos'
date '2011-02-02'
summary "
On ruby, the maximum size of a float is #{Float::MAX}.
On ruby, the maximum size of a float is #{Float::MAX}.
With Rational, we can raise to integer numbers and surpass Float maximum.
What is the speed reduction using Rational?"

reps 1000 #number of repetitions
int=10
rat=10.quo(1)
bd=BigDecimal("10")
measure "Using float pow" do
int**307
reps 1000 # number of repetitions
int = 10
rat = 10.quo(1)
bd = BigDecimal('10')
measure 'Using float pow' do
int**307
end

measure "Using rational" do
rat**307
measure 'Using rational' do
rat**307
end

measure "Using big decimal pow" do
bd**307
measure 'Using big decimal pow' do
bd**307
end
52 changes: 26 additions & 26 deletions bin/distribution
Expand Up @@ -3,27 +3,27 @@
require 'optparse'
require 'fileutils'
require 'erb'
gem_base=File.expand_path(File.dirname(__FILE__)+"/..")
require gem_base+"/lib/distribution"
gem_base = File.expand_path(File.dirname(__FILE__) + '/..')
require gem_base + '/lib/distribution'

new=false
parameters=""
new = false
parameters = ''
OptionParser.new do |opts|
opts.banner="Usage: distribution [--new] [--params parameters] distribution"
opts.on("-n", "--new", "Create a new template for distribution") do
new=true
opts.banner = 'Usage: distribution [--new] [--params parameters] distribution'
opts.on('-n', '--new', 'Create a new template for distribution') do
new = true
end
opts.on("-PMANDATORY", "--params MANDATORY", String, "Parameters for distribution") do |n_param|
parameters=", #{n_param}"
opts.on('-PMANDATORY', '--params MANDATORY', String, 'Parameters for distribution') do |n_param|
parameters = ", #{n_param}"
end
opts.on("-h", "--help", "Show this message") do

opts.on('-h', '--help', 'Show this message') do
puts opts
exit
end

begin
ARGV << "-h" if ARGV.empty?
ARGV << '-h' if ARGV.empty?
opts.parse!(ARGV)
rescue OptionParser::ParseError => e
STDERR.puts e.message, "\n", opts
Expand All @@ -33,19 +33,19 @@ end

ARGV.each do |distribution|
if new
basename=distribution.downcase
raise "You should be inside distribution lib directory" unless File.exists? "../distribution.rb"
raise "Distribution already created" if File.exists? basename+".rb"
main=ERB.new(File.read(gem_base+"/data/template/distribution.erb"))
ruby=ERB.new(File.read(gem_base+"/data/template/distribution/ruby.erb"))
gsl=ERB.new(File.read(gem_base+"/data/template/distribution/gsl.erb"))
spec=ERB.new(File.read(gem_base+"/data/template/spec.erb"))
FileUtils.mkdir(basename) unless File.exists? basename
File.open(basename+".rb","w") {|fp| fp.write(main.result(binding))}
File.open(basename+"/ruby.rb","w") {|fp| fp.write(ruby.result(binding))}
File.open(basename+"/gsl.rb","w") {|fp| fp.write(gsl.result(binding))}
File.open("../../spec/#{basename}_spec.rb","w") {|fp| fp.write(spec.result(binding))}
basename = distribution.downcase
fail 'You should be inside distribution lib directory' unless File.exist? '../distribution.rb'
fail 'Distribution already created' if File.exist? basename + '.rb'
main = ERB.new(File.read(gem_base + '/data/template/distribution.erb'))
ruby = ERB.new(File.read(gem_base + '/data/template/distribution/ruby.erb'))
gsl = ERB.new(File.read(gem_base + '/data/template/distribution/gsl.erb'))
spec = ERB.new(File.read(gem_base + '/data/template/spec.erb'))

FileUtils.mkdir(basename) unless File.exist? basename
File.open(basename + '.rb', 'w') { |fp| fp.write(main.result(binding)) }
File.open(basename + '/ruby.rb', 'w') { |fp| fp.write(ruby.result(binding)) }
File.open(basename + '/gsl.rb', 'w') { |fp| fp.write(gsl.result(binding)) }
File.open("../../spec/#{basename}_spec.rb", 'w') { |fp| fp.write(spec.result(binding)) }

end
end
9 changes: 4 additions & 5 deletions lib/distribution/bivariatenormal.rb
Expand Up @@ -7,21 +7,20 @@ module Distribution
# Pdf if easy to calculate, but CDF is not trivial. Several papers
# describe methods to calculate the integral.
module BivariateNormal
SHORTHAND='bnor'
SHORTHAND = 'bnor'

extend Distributable
create_distribution_methods

##
# :singleton-method: pdf(k,n,prob)
# Probability density function for exactly +k+ successes in +n+ trials
# with success probability +prob+
#
#

##
# :singleton-method: cdf(k,n,prob)
# Cumulative density function for +k+ or less successes in +n+ trials
# with success probability +prob+

end
end
4 changes: 2 additions & 2 deletions lib/distribution/bivariatenormal/gsl.rb
Expand Up @@ -2,8 +2,8 @@ module Distribution
module BivariateNormal
module GSL_
class <<self
def pdf(x,y,rho,s1=1.0,s2=1.0)
GSL::Ran::bivariate_gaussian_pdf(x, y, s1,s2,rho)
def pdf(x, y, rho, s1 = 1.0, s2 = 1.0)
GSL::Ran.bivariate_gaussian_pdf(x, y, s1, s2, rho)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/distribution/bivariatenormal/java.rb
Expand Up @@ -6,4 +6,4 @@ class << self
end
end
end
end
end

0 comments on commit 79e04f7

Please sign in to comment.