############################ # 主成分分析 # coded by Y.Suganuma ############################ #************************************************************/ # 実対称行列の固有値・固有ベクトル(ヤコビ法) */ # n : 次数 */ # ct : 最大繰り返し回数 */ # eps : 収束判定条件 */ # a : 対象とする行列 */ # a1, a2 : 作業域(nxnの行列),a1の対角要素が固有値 */ # x1, x2 : 作業域(nxnの行列),x1の各列が固有ベクトル */ # return : =0 : 正常 */ # =1 : 収束せず */ # coded by Y.Suganuma */ #************************************************************/ def Jacobi(n, ct, eps, a, a1, a2, x1, x2) # 初期設定 k = 0 ind = 1 p = 0 q = 0 for i1 in 0 ... n for i2 in 0 ... n a1[i1][i2] = a[i1][i2] x1[i1][i2] = 0.0 end x1[i1][i1] = 1.0 end # 計算 while ind > 0 && k < ct # 最大要素の探索 max = 0.0 for i1 in 0 ... n for i2 in 0 ... n if i2 != i1 if a1[i1][i2].abs() > max max = a1[i1][i2].abs() p = i1 q = i2 end end end end # 収束判定 # 収束した if max < eps ind = 0 # 収束しない else # 準備 s = -a1[p][q] t = 0.5 * (a1[p][p] - a1[q][q]) v = t.abs() / Math.sqrt(s * s + t * t) sn = Math.sqrt(0.5 * (1.0 - v)) if s*t < 0.0 sn = -sn end cs = Math.sqrt(1.0 - sn * sn) # akの計算 for i1 in 0 ... n if i1 == p for i2 in 0 ... n if i2 == p a2[p][p] = a1[p][p] * cs * cs + a1[q][q] * sn * sn - 2.0 * a1[p][q] * sn * cs elsif i2 == q a2[p][q] = 0.0 else a2[p][i2] = a1[p][i2] * cs - a1[q][i2] * sn end end elsif i1 == q for i2 in 0 ... n if (i2 == q) a2[q][q] = a1[p][p] * sn * sn + a1[q][q] * cs * cs + 2.0 * a1[p][q] * sn * cs elsif i2 == p a2[q][p] = 0.0 else a2[q][i2] = a1[q][i2] * cs + a1[p][i2] * sn end end else for i2 in 0 ... n if i2 == p a2[i1][p] = a1[i1][p] * cs - a1[i1][q] * sn elsif i2 == q a2[i1][q] = a1[i1][q] * cs + a1[i1][p] * sn else a2[i1][i2] = a1[i1][i2] end end end end # xkの計算 for i1 in 0 ... n for i2 in 0 ... n if i2 == p x2[i1][p] = x1[i1][p] * cs - x1[i1][q] * sn elsif i2 == q x2[i1][q] = x1[i1][q] * cs + x1[i1][p] * sn else x2[i1][i2] = x1[i1][i2] end end end # 次のステップへ k += 1 for i1 in 0 ... n for i2 in 0 ... n a1[i1][i2] = a2[i1][i2] x1[i1][i2] = x2[i1][i2] end end end end return ind end ################################### # 主成分分析 # p : 変数の数 # n : データの数 # x : データ # r : 分散(主成分) # a : 係数 # eps : 収束性を判定する規準 # ct : 最大繰り返し回数 # return : =0 : 正常 # =1 : エラー # coded by Y.Suganuma ################################### def principal(p, n, x, r, a, eps, ct) sw = 0 # 領域の確保 c = Array.new(p) a1 = Array.new(p) a2 = Array.new(p) x1 = Array.new(p) x2 = Array.new(p) for i1 in 0 ... p c[i1] = Array.new(p) a1[i1] = Array.new(p) a2[i1] = Array.new(p) x1[i1] = Array.new(p) x2[i1] = Array.new(p) end # データの基準化 for i1 in 0 ... p mean = 0.0 s2 = 0.0 for i2 in 0 ... n mean += x[i1][i2] s2 += x[i1][i2] * x[i1][i2] end mean /= n s2 /= n s2 = n * (s2 - mean * mean) / (n - 1) s2 = Math.sqrt(s2) for i2 in 0 ... n x[i1][i2] = (x[i1][i2] - mean) / s2 end end # 分散強分散行列の計算 for i1 in 0 ... p for i2 in 0 ... p s2 = 0.0 for i3 in 0 ... n s2 += x[i1][i3] * x[i2][i3] end s2 /= (n - 1) c[i1][i2] = s2 if i1 != i2 c[i2][i1] = s2 end end end # 固有値と固有ベクトルの計算(ヤコビ法) sw = Jacobi(p, ct, eps, c, a1, a2, x1, x2) if sw == 0 for i1 in 0 ... p r[i1] = a1[i1][i1] for i2 in 0 ... p a[i1][i2] = x1[i2][i1] end end end return sw end ss = gets().split(" ") p = Integer(ss[0]) # 変数の数 n = Integer(ss[1]) # データの数 r = Array.new(p) x = Array.new(p) a = Array.new(p) for i1 in 0 ... p x[i1] = Array.new(n) a[i1] = Array.new(p) end for i1 in 0 ... n # データ ss = gets().split(" ") for i2 in 0 ... p x[i2][i1] = Float(ss[i2]) end end sw = principal(p, n, x, r, a, 1.0e-10, 200) if sw == 0 for i1 in 0 ... p print("主成分 " + String(r[i1])) print(" 係数") for i2 in 0 ... p print(" " + String(a[i1][i2])) end print("\n") end else print("***error 解を求めることができませんでした") end =begin ---------データ例(コメント部分を除いて下さい)--------- 4 100 # 変数の数(p)とデータの数(n) 66 22 44 31 # x1, x2, x3, x4 25 74 17 81 50 23 53 71 25 57 19 81 74 47 64 47 39 33 48 46 14 22 9 69 67 60 49 26 42 40 77 65 11 80 0 86 32 0 43 74 68 69 44 68 24 49 9 71 42 74 28 46 60 58 73 28 36 37 33 68 24 44 19 83 30 40 31 50 55 40 60 49 63 47 94 41 72 30 100 45 19 22 13 75 43 39 43 34 90 83 92 31 51 77 52 82 53 70 34 31 28 51 53 44 40 62 42 79 31 48 22 68 57 29 51 30 64 89 57 42 49 82 72 29 53 31 55 43 79 52 70 10 45 19 43 57 35 34 34 89 4 69 0 100 49 49 66 66 92 82 97 6 5 89 0 100 65 26 83 28 56 36 64 38 48 50 25 22 30 30 15 55 40 65 38 42 14 67 9 67 84 96 90 8 53 64 51 54 50 89 60 52 76 41 68 9 49 40 53 49 78 66 66 17 76 58 90 29 41 15 40 49 63 60 55 33 40 36 49 67 78 54 71 18 62 72 69 12 64 47 42 53 56 64 9 15 77 35 56 25 44 12 46 87 80 9 56 19 36 21 52 78 48 63 64 48 43 61 50 47 58 23 28 50 90 12 100 0 13 33 11 77 67 44 48 28 75 45 68 17 81 22 89 9 46 45 59 55 56 49 64 55 65 62 72 27 34 49 29 77 45 33 60 63 20 45 14 99 33 38 26 87 44 51 69 52 64 57 64 48 44 64 51 28 63 48 56 11 29 39 33 84 40 48 51 54 40 38 26 62 68 46 61 26 58 45 68 48 64 44 77 63 59 62 44 66 81 53 93 19 23 34 12 68 51 35 55 46 74 70 84 17 42 33 56 44 46 31 46 53 33 57 38 63 40 24 20 42 53 36 60 31 0 34 0 100 =end