distribution 0.6.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/.travis.yml +13 -0
- data/.yardopts +5 -0
- data/Gemfile +5 -0
- data/History.txt +24 -8
- data/LICENCE.md +26 -0
- data/README.md +155 -0
- data/Rakefile +15 -19
- data/benchmark/binomial_coefficient.rb +19 -23
- data/benchmark/binomial_coefficient/experiment.rb +33 -36
- data/benchmark/factorial_hash.rb +7 -8
- data/benchmark/factorial_method.rb +4 -6
- data/benchmark/odd.rb +6 -7
- data/benchmark/power.rb +11 -11
- data/bin/distribution +26 -26
- data/data/template/spec.erb +7 -6
- data/distribution.gemspec +25 -0
- data/lib/distribution.rb +79 -124
- data/lib/distribution/beta.rb +6 -8
- data/lib/distribution/beta/gsl.rb +14 -9
- data/lib/distribution/beta/java.rb +1 -1
- data/lib/distribution/beta/ruby.rb +41 -7
- data/lib/distribution/binomial.rb +10 -11
- data/lib/distribution/binomial/gsl.rb +6 -5
- data/lib/distribution/binomial/java.rb +1 -1
- data/lib/distribution/binomial/ruby.rb +22 -15
- data/lib/distribution/bivariatenormal.rb +4 -5
- data/lib/distribution/bivariatenormal/gsl.rb +2 -2
- data/lib/distribution/bivariatenormal/java.rb +1 -1
- data/lib/distribution/bivariatenormal/ruby.rb +245 -254
- data/lib/distribution/chisquare.rb +8 -10
- data/lib/distribution/chisquare/gsl.rb +24 -19
- data/lib/distribution/chisquare/java.rb +1 -1
- data/lib/distribution/chisquare/ruby.rb +60 -55
- data/lib/distribution/chisquare/statistics2.rb +16 -13
- data/lib/distribution/distributable.rb +40 -0
- data/lib/distribution/exponential.rb +4 -5
- data/lib/distribution/exponential/gsl.rb +13 -9
- data/lib/distribution/exponential/ruby.rb +17 -11
- data/lib/distribution/f.rb +10 -11
- data/lib/distribution/f/gsl.rb +26 -22
- data/lib/distribution/f/java.rb +1 -1
- data/lib/distribution/f/ruby.rb +104 -105
- data/lib/distribution/f/statistics2.rb +22 -19
- data/lib/distribution/gamma.rb +5 -7
- data/lib/distribution/gamma/gsl.rb +13 -9
- data/lib/distribution/gamma/java.rb +1 -1
- data/lib/distribution/gamma/ruby.rb +5 -11
- data/lib/distribution/hypergeometric.rb +5 -8
- data/lib/distribution/hypergeometric/gsl.rb +5 -6
- data/lib/distribution/hypergeometric/java.rb +1 -1
- data/lib/distribution/hypergeometric/ruby.rb +34 -35
- data/lib/distribution/logistic.rb +6 -9
- data/lib/distribution/logistic/ruby.rb +14 -9
- data/lib/distribution/lognormal.rb +37 -0
- data/lib/distribution/lognormal/gsl.rb +21 -0
- data/lib/distribution/lognormal/ruby.rb +16 -0
- data/lib/distribution/math_extension.rb +187 -231
- data/lib/distribution/math_extension/chebyshev_series.rb +281 -272
- data/lib/distribution/math_extension/erfc.rb +28 -31
- data/lib/distribution/math_extension/exponential_integral.rb +17 -17
- data/lib/distribution/math_extension/gammastar.rb +19 -20
- data/lib/distribution/math_extension/gsl_utilities.rb +12 -12
- data/lib/distribution/math_extension/incomplete_beta.rb +52 -61
- data/lib/distribution/math_extension/incomplete_gamma.rb +166 -168
- data/lib/distribution/math_extension/log_utilities.rb +20 -22
- data/lib/distribution/normal.rb +11 -13
- data/lib/distribution/normal/gsl.rb +13 -10
- data/lib/distribution/normal/java.rb +30 -1
- data/lib/distribution/normal/ruby.rb +69 -59
- data/lib/distribution/normal/statistics2.rb +5 -2
- data/lib/distribution/normalmultivariate.rb +64 -64
- data/lib/distribution/poisson.rb +12 -14
- data/lib/distribution/poisson/gsl.rb +7 -7
- data/lib/distribution/poisson/java.rb +26 -0
- data/lib/distribution/poisson/ruby.rb +38 -9
- data/lib/distribution/shorthand.rb +17 -0
- data/lib/distribution/t.rb +16 -16
- data/lib/distribution/t/gsl.rb +27 -24
- data/lib/distribution/t/java.rb +1 -1
- data/lib/distribution/t/ruby.rb +99 -100
- data/lib/distribution/t/statistics2.rb +19 -19
- data/lib/distribution/uniform.rb +26 -0
- data/lib/distribution/uniform/gsl.rb +36 -0
- data/lib/distribution/uniform/ruby.rb +91 -0
- data/lib/distribution/version.rb +3 -0
- data/lib/distribution/weibull.rb +10 -0
- data/lib/distribution/weibull/gsl.rb +21 -0
- data/lib/distribution/weibull/ruby.rb +34 -0
- data/spec/beta_spec.rb +48 -50
- data/spec/binomial_spec.rb +80 -84
- data/spec/bivariatenormal_spec.rb +28 -35
- data/spec/chisquare_spec.rb +49 -52
- data/spec/distribution_spec.rb +11 -11
- data/spec/exponential_spec.rb +48 -39
- data/spec/f_spec.rb +73 -71
- data/spec/gamma_spec.rb +50 -53
- data/spec/hypergeometric_spec.rb +63 -69
- data/spec/logistic_spec.rb +31 -37
- data/spec/lognormal_spec.rb +54 -0
- data/spec/math_extension_spec.rb +192 -209
- data/spec/normal_spec.rb +80 -73
- data/spec/poisson_spec.rb +78 -36
- data/spec/shorthand_spec.rb +19 -22
- data/spec/spec_helper.rb +31 -6
- data/spec/t_spec.rb +63 -77
- data/spec/uniform_spec.rb +154 -0
- data/spec/weibull_spec.rb +17 -0
- data/vendor/java/commons-math-2.2.jar +0 -0
- metadata +91 -111
- data.tar.gz.sig +0 -0
- data/.autotest +0 -23
- data/.gemtest +0 -0
- data/Manifest.txt +0 -95
- data/README.txt +0 -100
- metadata.gz.sig +0 -0
data/benchmark/factorial_hash.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
|
2
2
|
require 'bench_press'
|
3
3
|
require 'distribution'
|
4
4
|
extend BenchPress
|
@@ -8,17 +8,16 @@ author 'Claudio Bustos'
|
|
8
8
|
date '2011-01-31'
|
9
9
|
summary "
|
10
10
|
Is better create a lookup table for factorial or just calculate it?
|
11
|
-
Distribution::MathExtension::SwingFactorial has a lookup table
|
11
|
+
Distribution::MathExtension::SwingFactorial has a lookup table
|
12
12
|
for factorials n<20
|
13
13
|
"
|
14
14
|
|
15
|
-
reps 1000 #number of repetitions
|
15
|
+
reps 1000 # number of repetitions
|
16
16
|
|
17
|
-
measure
|
18
|
-
|
17
|
+
measure 'Lookup' do
|
18
|
+
Math.factorial(19)
|
19
19
|
end
|
20
20
|
|
21
|
-
measure
|
22
|
-
|
21
|
+
measure 'calculate' do
|
22
|
+
Distribution::MathExtension::SwingFactorial.naive_factorial(19)
|
23
23
|
end
|
24
|
-
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
2
2
|
require 'distribution'
|
3
3
|
require 'bench_press'
|
4
4
|
|
@@ -10,19 +10,17 @@ date '2011-01-27'
|
|
10
10
|
summary "
|
11
11
|
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.
|
12
12
|
* Approximation (fast_factorial): Luschny f.3
|
13
|
-
* Exact (factorial): Luschny Swing Prime
|
13
|
+
* Exact (factorial): Luschny Swing Prime
|
14
14
|
"
|
15
15
|
|
16
|
-
reps 10 #number of repetitions
|
16
|
+
reps 10 # number of repetitions
|
17
17
|
|
18
|
-
x=200
|
18
|
+
x = 200
|
19
19
|
|
20
20
|
measure "Math.factorial(#{x})" do
|
21
21
|
Math.factorial(x)
|
22
22
|
end
|
23
23
|
|
24
24
|
measure "Math.fast_factorial(#{x})" do
|
25
|
-
|
26
25
|
Math.fast_factorial(x)
|
27
26
|
end
|
28
|
-
|
data/benchmark/odd.rb
CHANGED
@@ -10,13 +10,12 @@ summary "
|
|
10
10
|
Which is faster, n%1==1 or n%2==1
|
11
11
|
"
|
12
12
|
|
13
|
-
reps 10_000 #number of repetitions
|
14
|
-
n=
|
15
|
-
measure
|
16
|
-
n%1==1
|
13
|
+
reps 10_000 # number of repetitions
|
14
|
+
n = 100_000
|
15
|
+
measure 'Using &' do
|
16
|
+
n % 1 == 1
|
17
17
|
end
|
18
18
|
|
19
|
-
measure
|
20
|
-
n
|
19
|
+
measure 'Using %' do
|
20
|
+
n.odd?
|
21
21
|
end
|
22
|
-
|
data/benchmark/power.rb
CHANGED
@@ -6,22 +6,22 @@ name 'Float vs Rational power'
|
|
6
6
|
author 'Claudio Bustos'
|
7
7
|
date '2011-02-02'
|
8
8
|
summary "
|
9
|
-
On ruby, the maximum size of a float is #{Float::MAX}.
|
9
|
+
On ruby, the maximum size of a float is #{Float::MAX}.
|
10
10
|
With Rational, we can raise to integer numbers and surpass Float maximum.
|
11
11
|
What is the speed reduction using Rational?"
|
12
12
|
|
13
|
-
reps 1000 #number of repetitions
|
14
|
-
int=10
|
15
|
-
rat=10.quo(1)
|
16
|
-
bd=BigDecimal(
|
17
|
-
measure
|
18
|
-
|
13
|
+
reps 1000 # number of repetitions
|
14
|
+
int = 10
|
15
|
+
rat = 10.quo(1)
|
16
|
+
bd = BigDecimal('10')
|
17
|
+
measure 'Using float pow' do
|
18
|
+
int**307
|
19
19
|
end
|
20
20
|
|
21
|
-
measure
|
22
|
-
|
21
|
+
measure 'Using rational' do
|
22
|
+
rat**307
|
23
23
|
end
|
24
24
|
|
25
|
-
measure
|
26
|
-
|
25
|
+
measure 'Using big decimal pow' do
|
26
|
+
bd**307
|
27
27
|
end
|
data/bin/distribution
CHANGED
@@ -3,27 +3,27 @@
|
|
3
3
|
require 'optparse'
|
4
4
|
require 'fileutils'
|
5
5
|
require 'erb'
|
6
|
-
gem_base=File.expand_path(File.dirname(__FILE__)+
|
7
|
-
require gem_base+
|
6
|
+
gem_base = File.expand_path(File.dirname(__FILE__) + '/..')
|
7
|
+
require gem_base + '/lib/distribution'
|
8
8
|
|
9
|
-
new=false
|
10
|
-
parameters=
|
9
|
+
new = false
|
10
|
+
parameters = ''
|
11
11
|
OptionParser.new do |opts|
|
12
|
-
opts.banner=
|
13
|
-
opts.on(
|
14
|
-
new=true
|
12
|
+
opts.banner = 'Usage: distribution [--new] [--params parameters] distribution'
|
13
|
+
opts.on('-n', '--new', 'Create a new template for distribution') do
|
14
|
+
new = true
|
15
15
|
end
|
16
|
-
opts.on(
|
17
|
-
parameters=", #{n_param}"
|
16
|
+
opts.on('-PMANDATORY', '--params MANDATORY', String, 'Parameters for distribution') do |n_param|
|
17
|
+
parameters = ", #{n_param}"
|
18
18
|
end
|
19
|
-
|
20
|
-
opts.on(
|
19
|
+
|
20
|
+
opts.on('-h', '--help', 'Show this message') do
|
21
21
|
puts opts
|
22
22
|
exit
|
23
23
|
end
|
24
24
|
|
25
25
|
begin
|
26
|
-
ARGV <<
|
26
|
+
ARGV << '-h' if ARGV.empty?
|
27
27
|
opts.parse!(ARGV)
|
28
28
|
rescue OptionParser::ParseError => e
|
29
29
|
STDERR.puts e.message, "\n", opts
|
@@ -33,19 +33,19 @@ end
|
|
33
33
|
|
34
34
|
ARGV.each do |distribution|
|
35
35
|
if new
|
36
|
-
basename=distribution.downcase
|
37
|
-
|
38
|
-
|
39
|
-
main=ERB.new(File.read(gem_base+
|
40
|
-
ruby=ERB.new(File.read(gem_base+
|
41
|
-
gsl=ERB.new(File.read(gem_base+
|
42
|
-
spec=ERB.new(File.read(gem_base+
|
43
|
-
|
44
|
-
FileUtils.mkdir(basename) unless File.
|
45
|
-
File.open(basename+
|
46
|
-
File.open(basename+
|
47
|
-
File.open(basename+
|
48
|
-
File.open("../../spec/#{basename}_spec.rb",
|
49
|
-
|
36
|
+
basename = distribution.downcase
|
37
|
+
fail 'You should be inside distribution lib directory' unless File.exist? '../distribution.rb'
|
38
|
+
fail 'Distribution already created' if File.exist? basename + '.rb'
|
39
|
+
main = ERB.new(File.read(gem_base + '/data/template/distribution.erb'))
|
40
|
+
ruby = ERB.new(File.read(gem_base + '/data/template/distribution/ruby.erb'))
|
41
|
+
gsl = ERB.new(File.read(gem_base + '/data/template/distribution/gsl.erb'))
|
42
|
+
spec = ERB.new(File.read(gem_base + '/data/template/spec.erb'))
|
43
|
+
|
44
|
+
FileUtils.mkdir(basename) unless File.exist? basename
|
45
|
+
File.open(basename + '.rb', 'w') { |fp| fp.write(main.result(binding)) }
|
46
|
+
File.open(basename + '/ruby.rb', 'w') { |fp| fp.write(ruby.result(binding)) }
|
47
|
+
File.open(basename + '/gsl.rb', 'w') { |fp| fp.write(gsl.result(binding)) }
|
48
|
+
File.open("../../spec/#{basename}_spec.rb", 'w') { |fp| fp.write(spec.result(binding)) }
|
49
|
+
|
50
50
|
end
|
51
51
|
end
|
data/data/template/spec.erb
CHANGED
@@ -41,13 +41,14 @@ describe Distribution::<%= distribution.capitalize %> do
|
|
41
41
|
it_should_behave_like "<%= distribution %> engine"
|
42
42
|
|
43
43
|
end
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
|
45
|
+
if Distribution.has_gsl?
|
46
|
+
describe Distribution::<%= distribution.capitalize %>::GSL_ do
|
47
|
+
before do
|
48
|
+
@engine=Distribution::<%= distribution.capitalize %>::GSL_
|
49
|
+
end
|
50
|
+
it_should_behave_like "<%= distribution %> engine"
|
48
51
|
end
|
49
|
-
it_should_behave_like "<%= distribution %> engine"
|
50
|
-
|
51
52
|
end
|
52
53
|
|
53
54
|
|
@@ -0,0 +1,25 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'distribution/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "distribution"
|
8
|
+
s.version = Distribution::VERSION
|
9
|
+
s.date = Date.today.to_s
|
10
|
+
s.homepage = "https://github.com/sciruby/distribution"
|
11
|
+
|
12
|
+
s.authors = ['Claudio Bustos', 'Carlos Agarie']
|
13
|
+
s.email = ['clbustos@gmail.com', 'carlos.agarie@gmail.com']
|
14
|
+
s.platform = Gem::Platform::RUBY
|
15
|
+
s.summary = "Distribution"
|
16
|
+
s.description = "Distribution is a gem with several probabilistic distributions. Pure Ruby is used by default, C (GSL) or Java extensions are used if available"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
20
|
+
s.require_path = 'lib'
|
21
|
+
|
22
|
+
s.add_development_dependency 'bundler'
|
23
|
+
s.add_development_dependency 'rake'
|
24
|
+
s.add_development_dependency "rspec", '~> 3.2'
|
25
|
+
end
|
data/lib/distribution.rb
CHANGED
@@ -1,157 +1,112 @@
|
|
1
|
-
# = distribution.rb -
|
1
|
+
# = distribution.rb -
|
2
2
|
# Distribution - Statistical Distributions package for Ruby
|
3
|
-
#
|
4
|
-
# Copyright (C) 2011 Claudio Bustos
|
5
3
|
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# as published by the Free Software Foundation; either version 2
|
9
|
-
# of the License, or (at your option) any later version.
|
4
|
+
# Copyright (c) 2011-2012, Claudio Bustos
|
5
|
+
# All rights reserved.
|
10
6
|
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions are met:
|
9
|
+
# * Redistributions of source code must retain the above copyright
|
10
|
+
# notice, this list of conditions and the following disclaimer.
|
11
|
+
# * Redistributions in binary form must reproduce the above copyright
|
12
|
+
# notice, this list of conditions and the following disclaimer in the
|
13
|
+
# documentation and/or other materials provided with the distribution.
|
14
|
+
# * Neither the name of the copyright holder nor the
|
15
|
+
# names of its contributors may be used to endorse or promote products
|
16
|
+
# derived from this software without specific prior written permission.
|
17
|
+
|
18
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
19
|
+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21
|
+
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
22
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
23
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
24
|
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
25
|
+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
26
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
27
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
19
28
|
#
|
20
29
|
# == Other Sources
|
21
|
-
#
|
22
|
-
# * Code of several Ruby engines came from statistics2.rb,
|
30
|
+
#
|
31
|
+
# * Code of several Ruby engines came from statistics2.rb,
|
23
32
|
# created by Shin-ichiro HARA(sinara@blade.nagaokaut.ac.jp).
|
24
33
|
# Retrieve from http://blade.nagaokaut.ac.jp/~sinara/ruby/math/statistics2/
|
25
34
|
# * Code of Beta and Gamma distribution came from GSL project.
|
26
35
|
# Ported by John O. Woods
|
27
36
|
# Specific notices will be placed where there are appropiate
|
28
|
-
#
|
29
|
-
if !respond_to? :define_singleton_method
|
30
|
-
class Module
|
31
|
-
public :define_method
|
32
|
-
end
|
37
|
+
#
|
33
38
|
|
34
|
-
class Object
|
35
|
-
def define_singleton_method(name,&block)
|
36
|
-
sc=class <<self;self;end
|
37
|
-
sc.define_method(name,&block)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
39
|
require 'distribution/math_extension'
|
42
|
-
|
40
|
+
require 'distribution/shorthand'
|
41
|
+
require 'distribution/distributable'
|
42
|
+
require 'distribution/version'
|
43
43
|
|
44
44
|
# Several distributions modules to calculate pdf, cdf, inverse cdf and generate
|
45
45
|
# pseudo-random numbers for several statistical distributions
|
46
|
-
#
|
46
|
+
#
|
47
47
|
# == Usage:
|
48
48
|
# Distribution::Normal.cdf(1.96)
|
49
49
|
# => 0.97500210485178
|
50
50
|
# Distribution::Normal.p_value(0.95)
|
51
51
|
# => 1.64485364660836
|
52
52
|
module Distribution
|
53
|
-
VERSION="0.6.0"
|
54
|
-
|
55
|
-
module Shorthand
|
56
|
-
EQUIVALENCES={:p_value=>:p, :cdf=>:cdf, :pdf=>:pdf, :rng=>:r, :exact_pdf=>:epdf, :exact_cdf=>:ecdf, :exact_p_value=>:ep}
|
57
|
-
def self.add_shortcut(sh,m, &block)
|
58
|
-
if EQUIVALENCES.include? m.to_sym
|
59
|
-
sh_name=sh+"_#{m}"
|
60
|
-
define_method(sh_name,&block)
|
61
|
-
sh_name=sh+"_#{EQUIVALENCES[m.to_sym]}"
|
62
|
-
define_method(sh_name,&block)
|
63
|
-
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
|
69
53
|
SQ2PI = Math.sqrt(2 * Math::PI)
|
70
54
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
55
|
+
class << self
|
56
|
+
# Create a method 'has_<library>' on Module
|
57
|
+
# which require a library and return true or false
|
58
|
+
# according to success of failure
|
59
|
+
def create_has_library(library) #:nodoc:
|
60
|
+
define_singleton_method("has_#{library}?") do
|
61
|
+
cv = "@@#{library}"
|
62
|
+
unless class_variable_defined? cv
|
63
|
+
begin
|
64
|
+
require library.to_s
|
65
|
+
class_variable_set(cv, true)
|
66
|
+
rescue LoadError
|
67
|
+
class_variable_set(cv, false)
|
68
|
+
end
|
83
69
|
end
|
70
|
+
class_variable_get(cv)
|
84
71
|
end
|
85
|
-
|
72
|
+
end
|
73
|
+
|
74
|
+
# Retrieves the libraries used to calculate
|
75
|
+
# distributions
|
76
|
+
def libraries_order
|
77
|
+
order = [:Ruby_]
|
78
|
+
order.unshift(:Statistics2_) if has_statistics2?
|
79
|
+
order.unshift(:GSL_) if has_gsl?
|
80
|
+
order.unshift(:Java_) if has_java?
|
81
|
+
order
|
82
|
+
end
|
83
|
+
|
84
|
+
def init_java
|
85
|
+
$LOAD_PATH.unshift File.expand_path('../../vendor/java', __FILE__)
|
86
|
+
require 'commons-math-2.2.jar'
|
87
|
+
java_import 'org.apache.commons.math.distribution.NormalDistributionImpl'
|
88
|
+
java_import 'org.apache.commons.math.distribution.PoissonDistributionImpl'
|
86
89
|
end
|
87
90
|
end
|
88
|
-
|
89
|
-
# distributions
|
90
|
-
def self.libraries_order
|
91
|
-
order=[:Ruby_]
|
92
|
-
order.unshift(:Statistics2_) if has_statistics2?
|
93
|
-
order.unshift(:GSL_) if has_gsl?
|
94
|
-
order.unshift(:Java_) if has_java?
|
95
|
-
order
|
96
|
-
end
|
91
|
+
|
97
92
|
create_has_library :gsl
|
98
93
|
create_has_library :statistics2
|
99
94
|
create_has_library :java
|
100
|
-
|
101
|
-
# Magic module
|
102
|
-
module Distributable #:nodoc:
|
103
|
-
# Create methods for each module and add methods to
|
104
|
-
# Distribution::Shorthand.
|
105
|
-
#
|
106
|
-
# Traverse Distribution.libraries_order adding
|
107
|
-
# methods availables for each engine module on
|
108
|
-
# the current library
|
109
|
-
#
|
110
|
-
# Kids: Metaprogramming trickery! Don't do at work.
|
111
|
-
# This section was created between a very long reunion
|
112
|
-
# and a 456 Km. travel
|
113
|
-
def create_distribution_methods()
|
114
|
-
|
115
|
-
Distribution.libraries_order.each do |l_name|
|
116
|
-
if const_defined? l_name
|
117
|
-
l =const_get(l_name)
|
118
|
-
# Add methods from engine to base base, if not yet included
|
119
|
-
l.singleton_methods.each do |m|
|
120
|
-
if !singleton_methods.include? m
|
121
|
-
define_method(m) do |*args|
|
122
|
-
l.send(m,*args)
|
123
|
-
end
|
124
|
-
# Add method to Distribution::Shorthand
|
125
|
-
sh=const_get(:SHORTHAND)
|
126
|
-
Distribution::Shorthand.add_shortcut(sh,m) do |*args|
|
127
|
-
l.send(m,*args)
|
128
|
-
end
|
129
|
-
|
130
|
-
module_function m
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
end
|
136
|
-
# create alias for common methods
|
137
|
-
alias_method :inverse_cdf, :p_value if singleton_methods.include? :p_value
|
138
|
-
end
|
139
95
|
|
140
|
-
|
96
|
+
require 'distribution/normal'
|
97
|
+
require 'distribution/chisquare'
|
98
|
+
require 'distribution/gamma'
|
99
|
+
require 'distribution/beta'
|
100
|
+
require 'distribution/t'
|
101
|
+
require 'distribution/f'
|
102
|
+
require 'distribution/bivariatenormal'
|
103
|
+
require 'distribution/binomial'
|
104
|
+
require 'distribution/hypergeometric'
|
105
|
+
require 'distribution/exponential'
|
106
|
+
require 'distribution/poisson'
|
107
|
+
require 'distribution/logistic'
|
108
|
+
require 'distribution/lognormal'
|
109
|
+
require 'distribution/weibull'
|
141
110
|
|
142
|
-
|
143
|
-
autoload(:ChiSquare, 'distribution/chisquare')
|
144
|
-
autoload(:Gamma, 'distribution/gamma')
|
145
|
-
autoload(:Beta, 'distribution/beta')
|
146
|
-
autoload(:T, 'distribution/t')
|
147
|
-
autoload(:F, 'distribution/f')
|
148
|
-
autoload(:BivariateNormal, 'distribution/bivariatenormal')
|
149
|
-
autoload(:Binomial, 'distribution/binomial')
|
150
|
-
autoload(:Hypergeometric, 'distribution/hypergeometric')
|
151
|
-
autoload(:Exponential, 'distribution/exponential')
|
152
|
-
autoload(:Poisson, 'distribution/poisson')
|
153
|
-
autoload(:Logistic, 'distribution/logistic')
|
111
|
+
init_java if has_java?
|
154
112
|
end
|
155
|
-
|
156
|
-
|
157
|
-
|