File: experiment.rb

package info (click to toggle)
ruby-distribution 0.8.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 624 kB
  • sloc: ruby: 4,535; makefile: 10
file content (51 lines) | stat: -rw-r--r-- 1,612 bytes parent folder | download
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
# This test create a database to adjust the best algorithm
# to use on correlation matrix
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../../lib'))
require 'distribution'
require 'statsample'
require 'benchmark'

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)) }
        }
      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')