ruby-em_algorithm 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. data/Gemfile +6 -0
  2. data/Gemfile.lock +30 -0
  3. data/README.md +44 -0
  4. data/Rakefile +7 -0
  5. data/example/.ex1.rb.swp +0 -0
  6. data/example/.ex2.rb.swp +0 -0
  7. data/example/.ex3-tmp.rb.swp +0 -0
  8. data/example/.ex3.rb.swp +0 -0
  9. data/example/data/2dim-gmm-new.txt +1267 -0
  10. data/example/data/2dim-gmm-simple.txt +676 -0
  11. data/example/data/2dim-gmm-test.txt +6565 -0
  12. data/example/data/2dim-gmm-test2.txt +2782 -0
  13. data/example/data/2dim-gmm-test3.csv +1641 -0
  14. data/example/data/2dim-gmm-test3.txt +2782 -0
  15. data/example/data/2dim-gmm-test4.csv +868 -0
  16. data/example/data/2dim-gmm-test4.txt +4924 -0
  17. data/example/data/2dim-gmm-without_weight-small.txt +2401 -0
  18. data/example/data/2dim-gmm-without_weight.txt +18001 -0
  19. data/example/data/2dim-gmm.txt +1267 -0
  20. data/example/data/gmm-new.txt +10001 -0
  21. data/example/data/gmm-simple.txt +676 -0
  22. data/example/data/gmm.txt +10001 -0
  23. data/example/data/old-gmm.txt +10000 -0
  24. data/example/ex1.rb +20 -0
  25. data/example/ex1.rb~ +20 -0
  26. data/example/ex2.rb +33 -0
  27. data/example/ex2.rb~ +33 -0
  28. data/example/ex3-tmp.rb +23 -0
  29. data/example/ex3-tmp.rb~ +25 -0
  30. data/example/ex3.rb +43 -0
  31. data/example/ex3.rb~ +43 -0
  32. data/example/tools/.2dim.rb.swp +0 -0
  33. data/example/tools/2dim.rb +69 -0
  34. data/example/tools/2dim.rb~ +69 -0
  35. data/example/tools/boxmuller.rb +28 -0
  36. data/example/tools/boxmuller.rb~ +28 -0
  37. data/example/tools/conv_from_yaml.rb +8 -0
  38. data/example/tools/conv_from_yaml_to_csv.rb +8 -0
  39. data/example/tools/conv_to_yaml.rb +17 -0
  40. data/example/tools/ellipsoid.gnuplot +63 -0
  41. data/example/tools/ellipsoid.gnuplot~ +64 -0
  42. data/example/tools/histogram.rb +19 -0
  43. data/example/tools/histogram2d.rb +20 -0
  44. data/example/tools/histogram2d.rb~ +18 -0
  45. data/example/tools/kmeans.rb +34 -0
  46. data/example/tools/mean.rb +19 -0
  47. data/example/tools/table.data +4618 -0
  48. data/example/tools/tmp.txt +69632 -0
  49. data/example/tools/xmeans.R +608 -0
  50. data/example/tools/xmeans.rb +35 -0
  51. data/lib/em_algorithm/.base.rb.swp +0 -0
  52. data/lib/em_algorithm/base.rb +116 -0
  53. data/lib/em_algorithm/base.rb~ +116 -0
  54. data/lib/em_algorithm/convergence/.chi_square.rb.swp +0 -0
  55. data/lib/em_algorithm/convergence/.likelihood.rb.swp +0 -0
  56. data/lib/em_algorithm/convergence/check_method.rb +4 -0
  57. data/lib/em_algorithm/convergence/check_method.rb~ +0 -0
  58. data/lib/em_algorithm/convergence/chi_square.rb +40 -0
  59. data/lib/em_algorithm/convergence/chi_square.rb~ +40 -0
  60. data/lib/em_algorithm/convergence/likelihood.rb +35 -0
  61. data/lib/em_algorithm/convergence/likelihood.rb~ +35 -0
  62. data/lib/em_algorithm/models/.gaussian.rb.swp +0 -0
  63. data/lib/em_algorithm/models/.md_gaussian.rb.swp +0 -0
  64. data/lib/em_algorithm/models/.mixture.rb.swp +0 -0
  65. data/lib/em_algorithm/models/.model.rb.swp +0 -0
  66. data/lib/em_algorithm/models/gaussian.rb +47 -0
  67. data/lib/em_algorithm/models/gaussian.rb~ +47 -0
  68. data/lib/em_algorithm/models/md_gaussian.rb +67 -0
  69. data/lib/em_algorithm/models/md_gaussian.rb~ +67 -0
  70. data/lib/em_algorithm/models/mixture.rb +122 -0
  71. data/lib/em_algorithm/models/mixture.rb~ +122 -0
  72. data/lib/em_algorithm/models/model.rb +19 -0
  73. data/lib/em_algorithm/models/model.rb~ +19 -0
  74. data/lib/ruby-em_algorithm.rb +3 -0
  75. data/lib/ruby-em_algorithm/version.rb +3 -0
  76. data/ruby-em_algorithm.gemspec +21 -0
  77. data/spec/spec_helper.rb +9 -0
  78. metadata +178 -0
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'ruby-em_algorithm'
5
+ include EMAlgorithm
6
+
7
+ data_array = YAML.load_file(ARGV[0])
8
+
9
+ #model = Mixture.new(
10
+ # :models => [Gaussian.new(0.0, 9.0)],
11
+ # :weights => [1.0]
12
+ #)
13
+ model = Mixture.new(
14
+ :models => [Gaussian.new(0.0, 9.0), Gaussian.new(10.0, 9.0)],
15
+ :weights => [0.5, 0.5]
16
+ )
17
+
18
+ em = EMAlgorithm::Base.new(:model => model, :data_array => data_array)
19
+ em.run!
20
+ puts em.model.to_gnuplot
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'ruby-em_algorithm'
5
+ include EMAlgorithm
6
+
7
+ data_array = YAML.load_file(ARGV[0])
8
+
9
+ #model = Mixture.new(
10
+ # :models => [Gaussian.new(0.0, 9.0)],
11
+ # :weights => [1.0]
12
+ #)
13
+ model = Mixture.new(
14
+ :models => [Gaussian.new(0.0, 9.0), Gaussian.new(10.0, 9.0)],
15
+ :weights => [0.5, 0.5]
16
+ )
17
+
18
+ em = EMAlgorithm::Base.new(:model => model, :data_array => data_array)
19
+ em.run!
20
+ em.model.to_gnuplot
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'ruby-em_algorithm'
5
+ include EMAlgorithm
6
+ include GSL
7
+
8
+ data_array = YAML.load_file(ARGV[0]).map {|v| GSL::Vector[v]}
9
+
10
+ #model = Mixture.new(
11
+ # :models => [Gaussian.new(0.0, 3.0)],
12
+ # :weights => [1.0]
13
+ #)
14
+ #model = Mixture.new(
15
+ # :models => [MdGaussian.new(Vector[0.0, 0.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
16
+ # :weights => [1.0]
17
+ #)
18
+ model = Mixture.new(
19
+ :models => [MdGaussian.new(Vector[10.0, 10.0], Matrix[[9.0, 0.0], [0.0, 9.0]]), MdGaussian.new(Vector[-10.0, -10.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
20
+ :weights => [0.5, 0.5]
21
+ )
22
+ #model = Mixture.new(
23
+ # :models => [MdGaussian.new(Vector[18.0, 18.0], Matrix[[1.0, 0.0], [0.0, 1.0]])],
24
+ # :weights => [1.0]
25
+ #)
26
+ #model = Mixture.new(
27
+ # :models => [MdGaussian.new(Vector[-2.0, -2.0], Matrix[[1.0, 0.0], [0.0, 1.0]], 1.0), MdGaussian.new(Vector[2.0, 2.0], Matrix[[1.0, 0.0], [0.0, 1.0]], 1.0)],
28
+ # :weights => [0.5, 0.5]
29
+ #)
30
+
31
+ em = EMAlgorithm::Base.new(:model => model, :data_array => data_array)
32
+ em.run!
33
+ puts em.model.to_gnuplot
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'ruby-em_algorithm'
5
+ include EMAlgorithm
6
+ include GSL
7
+
8
+ data_array = YAML.load_file(ARGV[0]).map {|v| GSL::Vector[v]}
9
+
10
+ #model = Mixture.new(
11
+ # :models => [Gaussian.new(0.0, 3.0)],
12
+ # :weights => [1.0]
13
+ #)
14
+ #model = Mixture.new(
15
+ # :models => [MdGaussian.new(Vector[0.0, 0.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
16
+ # :weights => [1.0]
17
+ #)
18
+ model = Mixture.new(
19
+ :models => [MdGaussian.new(Vector[10.0, 10.0], Matrix[[9.0, 0.0], [0.0, 9.0]]), MdGaussian.new(Vector[-10.0, -10.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
20
+ :weights => [0.5, 0.5]
21
+ )
22
+ #model = Mixture.new(
23
+ # :models => [MdGaussian.new(Vector[18.0, 18.0], Matrix[[1.0, 0.0], [0.0, 1.0]])],
24
+ # :weights => [1.0]
25
+ #)
26
+ #model = Mixture.new(
27
+ # :models => [MdGaussian.new(Vector[-2.0, -2.0], Matrix[[1.0, 0.0], [0.0, 1.0]], 1.0), MdGaussian.new(Vector[2.0, 2.0], Matrix[[1.0, 0.0], [0.0, 1.0]], 1.0)],
28
+ # :weights => [0.5, 0.5]
29
+ #)
30
+
31
+ em = EMAlgorithm::Base.new(:model => model, :data_array => data_array)
32
+ em.run!
33
+ puts em.model.to_gnuplot
@@ -0,0 +1,23 @@
1
+ require 'yaml'
2
+ require 'ruby-em_algorithm'
3
+ include EMAlgorithm
4
+ include GSL
5
+
6
+ data_array = YAML.load_file("example/data/2dim-gmm-test.txt").map {|v| GSL::Vector[v]}
7
+
8
+ model = Mixture.new(
9
+ :models => [MdGaussian.new(Vector[-5.0, -5.0], Matrix[[9.0, 0.0], [0.0, 9.0]]), MdGaussian.new(Vector[5.0, 5.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
10
+ :weights => [0.5, 0.5]
11
+ )
12
+
13
+ em = EMAlgorithm::Base.new(:model => model, :data_array => data_array,
14
+ #:convergence_check => "ChiSquare",
15
+ :convergence_check => "Likelihood",
16
+ :estimate_as_frequency => 2,
17
+ :debug => false)
18
+ em.run!
19
+ puts em.model.to_gnuplot(:mixture_only)
20
+ p em.distribution_to_value_ratio
21
+ chi_square = ChiSquare.new(data_array)
22
+ chi_square.calculate(em.model, em.distribution_to_value_ratio[0])
23
+
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'ruby-em_algorithm'
5
+ include EMAlgorithm
6
+ include GSL
7
+
8
+ data_array = YAML.load_file("example/data/2dim-gmm-test.txt").map {|v| GSL::Vector[v]}
9
+
10
+ model = Mixture.new(
11
+ :models => [MdGaussian.new(Vector[-5.0, -5.0], Matrix[[9.0, 0.0], [0.0, 9.0]]), MdGaussian.new(Vector[5.0, 5.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
12
+ :weights => [0.5, 0.5]
13
+ )
14
+
15
+ em = EMAlgorithm::Base.new(:model => model, :data_array => data_array,
16
+ #:convergence_check => "ChiSquare",
17
+ :convergence_check => "Likelihood",
18
+ :estimate_as_frequency => 2,
19
+ :debug => false)
20
+ em.run!
21
+ puts em.model.to_gnuplot(:mixture_only)
22
+ p em.distribution_to_value_ratio
23
+ chi_square = ChiSquare.new(data_array)
24
+ chi_square.calculate(em.model, em.distribution_to_value_ratio[0])
25
+
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'ruby-em_algorithm'
5
+ include EMAlgorithm
6
+ include GSL
7
+
8
+ data_array = YAML.load_file(ARGV[0]).map {|v| GSL::Vector[v]}
9
+
10
+ #model = Mixture.new(
11
+ # :models => [Gaussian.new(0.0, 3.0)],
12
+ # :weights => [1.0]
13
+ #)
14
+ #model = Mixture.new(
15
+ # :models => [MdGaussian.new(Vector[10.0, 10.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
16
+ # :weights => [1.0]
17
+ #)
18
+ #model = Mixture.new(
19
+ # :models => [MdGaussian.new(Vector[1.0, 1.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
20
+ # :weights => [1.0]
21
+ #)
22
+ #model = Mixture.new(
23
+ # :models => [MdGaussian.new(Vector[1.24102564102564, 1.3843589743589746], Matrix[[4.114512820512822, 1.095717948717949], [1.095717948717949, 3.558512820512825]]), MdGaussian.new(Vector[-0.9518604651162781, -2.0293023255813956], Matrix[[3.4144651162790747, 1.764395348837209], [1.764395348837209, 6.793999999999991]])],
24
+ # :weights => [0.5, 0.5]
25
+ #)
26
+ #model = Mixture.new(
27
+ # :models => [MdGaussian.new(Vector[-3.0, -4.0], Matrix[[9.0, 0.0], [0.0, 9.0]]), MdGaussian.new(Vector[2.0, 2.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
28
+ # :weights => [0.5, 0.5]
29
+ #)
30
+ model = Mixture.new(
31
+ :models => [MdGaussian.new(Vector[-5.0, -5.0], Matrix[[9.0, 0.0], [0.0, 9.0]]), MdGaussian.new(Vector[5.0, 5.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
32
+ :weights => [0.5, 0.5]
33
+ )
34
+
35
+ em = EMAlgorithm::Base.new(:model => model, :data_array => data_array,
36
+ :value_distribution_estimation => true,
37
+ :debug => true
38
+ )
39
+ em.run!
40
+ #puts em.model.to_gnuplot(:mixture_only)
41
+ #puts em.model.to_gnuplot
42
+ puts em.model.value_distribution_to_gnuplot(em.const)
43
+
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'ruby-em_algorithm'
5
+ include EMAlgorithm
6
+ include GSL
7
+
8
+ data_array = YAML.load_file(ARGV[0]).map {|v| GSL::Vector[v]}
9
+
10
+ #model = Mixture.new(
11
+ # :models => [Gaussian.new(0.0, 3.0)],
12
+ # :weights => [1.0]
13
+ #)
14
+ #model = Mixture.new(
15
+ # :models => [MdGaussian.new(Vector[10.0, 10.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
16
+ # :weights => [1.0]
17
+ #)
18
+ #model = Mixture.new(
19
+ # :models => [MdGaussian.new(Vector[1.0, 1.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
20
+ # :weights => [1.0]
21
+ #)
22
+ #model = Mixture.new(
23
+ # :models => [MdGaussian.new(Vector[1.24102564102564, 1.3843589743589746], Matrix[[4.114512820512822, 1.095717948717949], [1.095717948717949, 3.558512820512825]]), MdGaussian.new(Vector[-0.9518604651162781, -2.0293023255813956], Matrix[[3.4144651162790747, 1.764395348837209], [1.764395348837209, 6.793999999999991]])],
24
+ # :weights => [0.5, 0.5]
25
+ #)
26
+ #model = Mixture.new(
27
+ # :models => [MdGaussian.new(Vector[-3.0, -4.0], Matrix[[9.0, 0.0], [0.0, 9.0]]), MdGaussian.new(Vector[2.0, 2.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
28
+ # :weights => [0.5, 0.5]
29
+ #)
30
+ model = Mixture.new(
31
+ :models => [MdGaussian.new(Vector[-5.0, -5.0], Matrix[[9.0, 0.0], [0.0, 9.0]]), MdGaussian.new(Vector[5.0, 5.0], Matrix[[9.0, 0.0], [0.0, 9.0]])],
32
+ :weights => [0.5, 0.5]
33
+ )
34
+
35
+ em = EMAlgorithm::Base.new(:model => model, :data_array => data_array,
36
+ :estimate_as_frequency => true,
37
+ :debug => true
38
+ )
39
+ em.run!
40
+ #puts em.model.to_gnuplot(:mixture_only)
41
+ #puts em.model.to_gnuplot
42
+ puts em.model.value_distribution_to_gnuplot(em.const)
43
+
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'gsl'
5
+ include GSL
6
+
7
+ def bivariate_gaussian(number, mu_x, mu_y, sigma_x, sigma_y, rho, seed)
8
+ r = Rng.alloc(Rng::TAUS, seed)
9
+ array_x = []
10
+ array_y = []
11
+ array_xy = []
12
+ number.times do
13
+ x, y = r.bivariate_gaussian(sigma_x, sigma_y, rho)
14
+ array_x << x + mu_x
15
+ array_y << y + mu_y
16
+ array_xy << [x + mu_x, y + mu_y]
17
+ end
18
+
19
+ v_x = Vector.alloc(array_x)
20
+ v_y = Vector.alloc(array_y)
21
+
22
+ if ARGV[0] == "without_weight"
23
+ return array_xy
24
+ end
25
+
26
+ #h = Histogram2d.alloc(30, [-6, 6], 30, [-6, 6])
27
+ h = Histogram2d.alloc(100, [-10, 10], 100, [-10, 10])
28
+ #h = Histogram2d.alloc(256, [-6, 6], 256, [-6, 6])
29
+ h.fill(v_x, v_y)
30
+ data_array = []
31
+ (0..(h.nx-1)).each do |x|
32
+ (0..(h.ny-1)).each do |y|
33
+ data_array << [(h.xrange[x]+h.xrange[x+1])/2.0, (h.yrange[y]+h.yrange[y+1])/2.0, h[x,y].to_f, (h.xrange[x]-h.xrange[x+1]).abs*(h.yrange[y]-h.yrange[y+1]).abs]
34
+ #p [(h.xrange[x]+h.xrange[x+1])/2.0, (h.yrange[y]+h.yrange[y+1])/2.0, h[x,y].to_f, (h.xrange[x]-h.xrange[x+1]).abs*(h.yrange[y]-h.yrange[y+1]).abs]
35
+ end
36
+ end
37
+ data_array
38
+ end
39
+
40
+ #data_array = bivariate_gaussian(24000, 1.0, 1.0, 1.0, 1.0, 0, 1)
41
+
42
+ data_array = bivariate_gaussian(24000, 1, 1, 1, 1, 0, 1)
43
+ data_array += bivariate_gaussian(12000, -1, -2, 1, 1, 0, 1)
44
+
45
+ #data_array = bivariate_gaussian(2400, 1, 1, 1, 1, 0, 1)
46
+ #data_array += bivariate_gaussian(1200, -1, -2, 1, 1, 0, 1)
47
+
48
+ #data_array = bivariate_gaussian(6000, 1, 1, 1, 1, 0, 1)
49
+ #data_array += bivariate_gaussian(3000, -1, -2, 1, 1, 0, 1)
50
+ if ARGV[0] == "without_weight"
51
+ puts data_array.to_yaml
52
+ exit(0)
53
+ end
54
+ data_array = data_array.map do |datum|
55
+ if rand > 1.0
56
+ [datum[0], datum[1], 0.0, 0.0]
57
+ else
58
+ [datum[0], datum[1], datum[2], datum[3]]
59
+ end
60
+ end
61
+ if ARGV[0] == "gnuplot"
62
+ data_array.each do |datum|
63
+ next if datum[2] == 0.0
64
+ puts "#{datum[0]} #{datum[1]} #{datum[2]}"
65
+ end
66
+ elsif ARGV[0].nil?
67
+ data_array = data_array.reject {|datum| datum[2] == 0.0}
68
+ puts data_array.to_yaml
69
+ end
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'gsl'
5
+ include GSL
6
+
7
+ def bivariate_gaussian(number, mu_x, mu_y, sigma_x, sigma_y, rho, seed)
8
+ r = Rng.alloc(Rng::TAUS, seed)
9
+ array_x = []
10
+ array_y = []
11
+ array_xy = []
12
+ number.times do
13
+ x, y = r.bivariate_gaussian(sigma_x, sigma_y, rho)
14
+ array_x << x + mu_x
15
+ array_y << y + mu_y
16
+ array_xy << [x + mu_x, y + mu_y]
17
+ end
18
+
19
+ v_x = Vector.alloc(array_x)
20
+ v_y = Vector.alloc(array_y)
21
+
22
+ if ARGV[0] == "without_weight"
23
+ return array_xy
24
+ end
25
+
26
+ #h = Histogram2d.alloc(30, [-6, 6], 30, [-6, 6])
27
+ h = Histogram2d.alloc(100, [-10, 10], 100, [-10, 10])
28
+ #h = Histogram2d.alloc(256, [-6, 6], 256, [-6, 6])
29
+ h.fill(v_x, v_y)
30
+ data_array = []
31
+ (0..(h.nx-1)).each do |x|
32
+ (0..(h.ny-1)).each do |y|
33
+ data_array << [(h.xrange[x]+h.xrange[x+1])/2.0, (h.yrange[y]+h.yrange[y+1])/2.0, h[x,y].to_f, (h.xrange[x]-h.xrange[x+1]).abs*(h.yrange[y]-h.yrange[y+1]).abs]
34
+ #p [(h.xrange[x]+h.xrange[x+1])/2.0, (h.yrange[y]+h.yrange[y+1])/2.0, h[x,y].to_f, (h.xrange[x]-h.xrange[x+1]).abs*(h.yrange[y]-h.yrange[y+1]).abs]
35
+ end
36
+ end
37
+ data_array
38
+ end
39
+
40
+ #data_array = bivariate_gaussian(24000, 1.0, 1.0, 1.0, 1.0, 0, 1)
41
+
42
+ data_array = bivariate_gaussian(24000, 1, 1, 1, 1, 0, 1)
43
+ data_array += bivariate_gaussian(12000, -1, -2, 1, 1, 0, 1)
44
+
45
+ #data_array = bivariate_gaussian(2400, 1, 1, 1, 1, 0, 1)
46
+ #data_array += bivariate_gaussian(1200, -1, -2, 1, 1, 0, 1)
47
+
48
+ #data_array = bivariate_gaussian(6000, 1, 1, 1, 1, 0, 1)
49
+ #data_array += bivariate_gaussian(3000, -1, -2, 1, 1, 0, 1)
50
+ if ARGV[0] == "without_weight"
51
+ puts data_array.to_yaml
52
+ exit(0)
53
+ end
54
+ data_array = data_array.map do |datum|
55
+ if rand > 1.0
56
+ [datum[0], datum[1], 0.0, 0.0]
57
+ else
58
+ [datum[0], datum[1], datum[2], datum[3]]
59
+ end
60
+ end
61
+ if ARGV[0] == "gnuplot"
62
+ data_array.each do |datum|
63
+ next if datum[2] == 0.0
64
+ puts "#{datum[0]} #{datum[1]} #{datum[2]}"
65
+ end
66
+ elsif ARGV[0].nil?
67
+ data_array = data_array.reject {|datum| datum[2] == 0.0}
68
+ puts data_array.to_yaml
69
+ end
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ ##ボックスミュラー法によるガウス分布データ生成
4
+ ##偶数個しか作れない仕様であることに注意
5
+
6
+ require 'yaml'
7
+
8
+ include Math
9
+ def boxmuller(n,mu,sigma)
10
+ j=0
11
+ data_array = []
12
+ while j < n
13
+ a=rand()
14
+ b=rand()
15
+ r1=sqrt(-2*Math.log(a.to_f)).to_f*sin(2*Math::PI*b.to_f).to_f
16
+ r2=sqrt(-2*Math.log(a.to_f)).to_f*cos(2*Math::PI*b.to_f).to_f
17
+ r3=r1.to_f*sigma.to_f+mu.to_f
18
+ r4=r2.to_f*sigma.to_f+mu.to_f
19
+ data_array << r3
20
+ data_array << r4
21
+ j=j+2
22
+ end
23
+ data_array
24
+ end
25
+
26
+ data_array = boxmuller(7000, 5, 4.0)
27
+ data_array = data_array + boxmuller(3000, -5, 1.0)
28
+ puts data_array.to_yaml
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ ##ボックスミュラー法によるガウス分布データ生成
4
+ ##偶数個しか作れない仕様であることに注意
5
+
6
+ require 'yaml'
7
+
8
+ include Math
9
+ def boxmuller(n,mu,sigma)
10
+ j=0
11
+ data_array = []
12
+ while j < n
13
+ a=rand()
14
+ b=rand()
15
+ r1=sqrt(-2*Math.log(a.to_f)).to_f*sin(2*Math::PI*b.to_f).to_f
16
+ r2=sqrt(-2*Math.log(a.to_f)).to_f*cos(2*Math::PI*b.to_f).to_f
17
+ r3=r1.to_f*sigma.to_f+mu.to_f
18
+ r4=r2.to_f*sigma.to_f+mu.to_f
19
+ data_array << r3
20
+ data_array << r4
21
+ j=j+2
22
+ end
23
+ data_array
24
+ end
25
+
26
+ data_array = boxmuller(7000, 5, 4.0)
27
+ #data_array = data_array + boxmuller(3000, -5, 1.0)
28
+ puts data_array.to_yaml