Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby script to calculate a multiple regression function.(2d)
#! /usr/local/bin/ruby
#*********************************************
# Ruby script to calculate a multiple regression function(2D).
# * y = b0 + b1x1 + b2x2 + b3x1x2 + b4x1^2 + b5x2^2
# これは、
# y = b0 + b1x1 + b2x2 + b3x3 + b4x4 + b5x5
# (但し、 x3 = x1x2, x4 = x1^2, x5 = x2^2)
# と同じ。
#*********************************************
#
class Array
def reg_multi_2d(y)
# 元の数、変量内のサンプル数
e_size, s_size = self.size, y.size
# 以下の場合は例外スロー
# - 引数の配列が Array クラスでない
# - 自身配列が空
# - 配列サイズが異なれば例外
raise "Argument is not a Array class!" unless y.class == Array
raise "Self array is nil!" if e_size == 0
raise "Argument array size is invalid!" unless self[0].size == s_size
1.upto(e_size - 1) do |i|
raise "Argument array size is invalid!" unless self[0].size == self[i].size
end
x1, x2 = self
x3 = x1.zip(x2).map { |a, b| a * b }
x4 = x1.map { |a| a * a }
x5 = x2.map { |a| a * a }
mtx = Array.new(6).map { Array.new(7, 0.0) }
# 左辺・対角成分
mtx[0][0] = s_size
mtx[1][1] = x1.map { |a| a * a }.sum
mtx[2][2] = x2.map { |a| a * a }.sum
mtx[3][3] = x3.map { |a| a * a }.sum
mtx[4][4] = x4.map { |a| a * a }.sum
mtx[5][5] = x5.map { |a| a * a }.sum
# 左辺・右上成分
mtx[0][1] = x1.sum
mtx[0][2] = x2.sum
mtx[0][3] = x3.sum
mtx[0][4] = x4.sum
mtx[0][5] = x5.sum
mtx[1][2] = x1.zip(x2).map { |a, b| a * b }.sum
mtx[1][3] = x1.zip(x3).map { |a, b| a * b }.sum
mtx[1][4] = x1.zip(x4).map { |a, b| a * b }.sum
mtx[1][5] = x1.zip(x5).map { |a, b| a * b }.sum
mtx[2][3] = x2.zip(x3).map { |a, b| a * b }.sum
mtx[2][4] = x2.zip(x4).map { |a, b| a * b }.sum
mtx[2][5] = x2.zip(x5).map { |a, b| a * b }.sum
mtx[3][4] = x3.zip(x4).map { |a, b| a * b }.sum
mtx[3][5] = x3.zip(x5).map { |a, b| a * b }.sum
mtx[4][5] = x4.zip(x5).map { |a, b| a * b }.sum
# 左辺・左下成分
mtx[1][0] = mtx[0][1]
mtx[2][0] = mtx[0][2]
mtx[2][1] = mtx[1][2]
mtx[3][0] = mtx[0][3]
mtx[3][1] = mtx[1][3]
mtx[3][2] = mtx[2][3]
mtx[4][0] = mtx[0][4]
mtx[4][1] = mtx[1][4]
mtx[4][2] = mtx[2][4]
mtx[4][3] = mtx[3][4]
mtx[5][0] = mtx[0][5]
mtx[5][1] = mtx[1][5]
mtx[5][2] = mtx[2][5]
mtx[5][3] = mtx[3][5]
mtx[5][4] = mtx[4][5]
# 右辺
mtx[0][6] = y.sum
mtx[1][6] = x1.zip(y).map { |a, b| a * b }.sum
mtx[2][6] = x2.zip(y).map { |a, b| a * b }.sum
mtx[3][6] = x3.zip(y).map { |a, b| a * b }.sum
mtx[4][6] = x4.zip(y).map { |a, b| a * b }.sum
mtx[5][6] = x5.zip(y).map { |a, b| a * b }.sum
# 連立方程式を解く (ガウスの消去法)
return gauss_e(mtx)
end
private
# ガウスの消去法
def gauss_e(ary)
# 行数
n = ary.size
# 前進消去
0.upto(n - 2) do |k|
(k + 1).upto(n - 1) do |i|
if ary[k][k] == 0
puts "解けない!"
exit 1
end
d = ary[i][k] / ary[k][k].to_f
(k + 1).upto(n) do |j|
ary[i][j] -= ary[k][j] * d
end
end
end
# 後退代入
(n - 1).downto(0) do |i|
if ary[i][i] == 0
puts "解けない!"
exit 1
end
d = ary[i][n]
(i + 1).upto(n - 1) do |j|
d -= ary[i][j] * ary[j][n]
end
ary[i][n] = d / ary[i][i].to_f
end
return ary.map { |a| a[-1] }
end
end
# 説明(独立)変数と目的(従属)変数
ary_x = [
[17.5, 17.0, 18.5, 16.0, 19.0, 19.5, 16.0, 18.0, 19.0, 19.5],
[30, 25, 20, 30, 45, 35, 25, 35, 35, 40]
]
ary_y = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
# 重回帰式算出(b0, b1, b2, ...)
reg_multi = ary_x.reg_multi_2d(ary_y)
# 結果出力
ary_x.each_with_index do |x, i|
puts "説明変数 X#{i + 1} = {#{ary_x[i].join(', ')}}"
end
puts "目的変数 Y = {#{ary_y.join(', ')}}"
puts "---"
p reg_multi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.