Module: TLib::Math
- Defined in:
- lib/t_lib/math.rb
Instance Method Summary collapse
-
#exp(ave = 0.5) ⇒ Object
指数分布に従った乱数を返す.
-
#gauusian(x, mu, sigma) ⇒ Object
gauusian distribution.
-
#gauusian_array(x, mu, sigma) ⇒ Object
gauusian distribution.
-
#gauusian_over_2dim(x, mu, conv) ⇒ Object
gauusian distribution over 2 dim version.
-
#normal_rand(mu = 0, sigma = 1.0) ⇒ Object
ボックス―ミューラー法をよる正規分布乱数発生.
-
#poisson_rand(mu = 0.0) ⇒ Object
ポアソン分布に従う乱数を発生する.
- #scale(x) ⇒ Object
Instance Method Details
#exp(ave = 0.5) ⇒ Object
指数分布に従った乱数を返す
60 61 62 63 64 65 66 67 68 |
# File 'lib/t_lib/math.rb', line 60 def exp(ave=0.5) x = rand() ; y = rand() ; while ((1.0/ave)*Math.exp(-(x/ave)) < y ) x = rand() ; y = rand() ; end return x ; end |
#gauusian(x, mu, sigma) ⇒ Object
TODO:
gauusian distribution
11 12 13 14 15 |
# File 'lib/t_lib/math.rb', line 11 def gauusian(x, mu, sigma) f1 = 1.0/(Math.sqrt(2.0*Math::PI)*Math.sqrt(sigma)) f2 = Math.exp(-(((x-mu)**2)/((2.0*sigma)))) return f1 * f2 end |
#gauusian_array(x, mu, sigma) ⇒ Object
TODO:
gauusian distribution
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/t_lib/math.rb', line 21 def gauusian_array(x, mu, sigma) if x[0].size <= 1 x = x[0] mu = mu[0] sigma = sigma[0][0] return gauusian(x, mu, sigma) else return gauusian_over_2dim(x, mu, sigma) end end |
#gauusian_over_2dim(x, mu, conv) ⇒ Object
gauusian distribution over 2 dim version
36 37 38 39 40 41 42 43 44 |
# File 'lib/t_lib/math.rb', line 36 def gauusian_over_2dim(x, mu, conv) x = Matrix[x] mu = Matrix[mu] conv = Matrix[*conv] f1 = 1.0/(((2.0 * Math::PI)**(@dim/2.0)) * ( conv.det**(0.5) )) f2 = Math.exp((-1.0/2.0)*((x-mu) * conv.inverse * (x-mu).transpose)[0, 0]) return (f1 * f2) end |
#normal_rand(mu = 0, sigma = 1.0) ⇒ Object
ボックス―ミューラー法をよる正規分布乱数発生
52 53 54 55 |
# File 'lib/t_lib/math.rb', line 52 def normal_rand(mu = 0,sigma = 1.0) a, b = rand(), rand() ; return (Math.sqrt(-2*Math.log(rand()))*Math.sin(2*Math::PI*rand()) * sigma) + mu end |
#poisson_rand(mu = 0.0) ⇒ Object
ポアソン分布に従う乱数を発生する
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/t_lib/math.rb', line 75 def poisson_rand(mu=0.0) lambda = Math.exp(-mu) k = 0 p = 1.0 while p >= lambda p *= rand() k += 1 end return k - 1 end |
#scale(x) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/t_lib/math.rb', line 85 def scale(x) sum_each_vec = [] ave_list = [] std_list = [] x.each{|vec| vec.each_with_index{|data, i| sum_each_vec[i] = (sum_each_vec[i] == nil) ? data : sum_each_vec[i]+data } } x[0].size.times{|i| ave_list.push(sum_each_vec[i]/x.size) } sum_each_vec = [] x.each{|vec| vec.each_with_index{|data, i| sum_each_vec[i] = (sum_each_vec[i] == nil) ? (ave_list[i]-data)**2 : (sum_each_vec[i]+(ave_list[i]-data)**2) } } x[0].size.times{|i| std_list.push(Math.sqrt(sum_each_vec[i]/x.size)) } scaled_x = [] x.each_with_index{|vec, i| scaled_x[i] ||= [] vec.each_with_index{|data, j| scaled_x[i][j] ||= (data-ave_list[j])/std_list[j] } } return scaled_x end |