Algoritma dan Pemrograman: Implementasi Metode Fitting (Regresi Linear Sederhana)
Table of Contents
Implementasi dengan pemrograman java :
/** * * @author ABD. CHARIS FAUZAN */ public class FittingRegresi { double matrik[][] = { {3.5, 1000000,4, 80,90}, {3, 500000,3, 70,65}, {2.7, 500000,1, 70,65}, {3.4, 600000,2, 80,90}, {3.2, 700000,1, 75,85}, {3.1, 800000,2, 80,75}, {3, 450000,3, 75,75}, {3.3, 750000,4, 80,80}, {2.9, 600000,2, 75,85}, {3.7, 1000000,4, 85,90} }; int row = matrik.length; int col = matrik[0].length; double Y[] = new double[row]; double uangSaku[] = new double[row]; double lamaBelajar[] = new double[row]; double fasilitas[] = new double[row]; double caraNgajar[] = new double[row]; private void init() { double X[][] = new double[row][col]; double XTrans[][] = new double[col][row]; for (int i = 0; i < row; i++) { X[i][0] = 1; Y[i] = matrik[i][0]; lamaBelajar[i] = matrik[i][1]; uangSaku[i] = matrik[i][2]; fasilitas[i] = matrik[i][3]; caraNgajar[i] = matrik[i][4]; for (int j = 1; j < col; j++) { Y[i] = matrik[i][0]; } for (int j = 1; j < col; j++) { X[i][j] = matrik[i][j]; } } printMatrix(Y, "Matriks Y :"); printMatrix(X, "Matriks X :"); XTrans = Transpose(X); printMatrix(XTrans, "X Transpose :"); double XTrans_mul_X[][] = MMULT(XTrans, X); printMatrix(XTrans_mul_X, "X'X :"); double InversXTrans_mul_X[][] = invers(XTrans_mul_X); printMatrix(InversXTrans_mul_X,"[X'X]-1 :"); double XTransMY[] = MMULT(XTrans, Y); printMatrix(XTransMY,"(X'Y) :"); double BMult[] = MMULT(InversXTrans_mul_X, XTransMY); printMatrix(BMult,"B^=[X'X]-1 X'Y :"); double IPK[] = new double[Y.length]; double E[]=new double[Y.length]; for (int i = 0; i < IPK.length; i++) { IPK[i] = BMult[0] + (BMult[1] * lamaBelajar[i]) + (BMult[2] * uangSaku[i]) + (BMult[3] * fasilitas[i]) + (BMult[4] * caraNgajar[i]); E[i]=Y[i]-IPK[i]; } printMatrix(IPK,"Y^ :"); printMatrix(E, "E=Y-Y^ :"); } private void printMatrix(double[][] A, String Title) { System.out.println(Title ); for (int i = 0; i < A.length; i++) { for (int j = 0; j < A[0].length; j++) { System.out.print(A[i][j] + ", "); } System.out.println(""); } System.out.println(""); } private void printMatrix(double[] A, String Title) { System.out.println(Title ); for (int i = 0; i < A.length; i++) { System.out.println(A[i]); } System.out.println(""); } double[][] Transpose(double A[][]) { int row = A.length; int col = A[0].length; double ATrans[][] = new double[col][row]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { ATrans[j][i] = A[i][j]; } } return ATrans; } private double[][] MMULT(double A[][], double x[][]) { int m, n, p; m = A.length; n = A[0].length; p = x[0].length; double[][] result = new double[m][p]; for (int i = 0; i < m; i++) { for (int j = 0; j < p; j++) { result[i][j] = 0; for (int k = 0; k < n; k++) { result[i][j] += A[i][k] * x[k][j]; } } } return result; } private double[] MMULT(double A[][], double x[]) { int m = A.length; double[] result = new double[m]; for (int i = 0; i < m; i++) { result[i] = 0; for (int k = 0; k < x.length; k++) { result[i] += A[i][k] * x[k]; } } return result; } private double[][] invers(double[][] A) { int n = A.length; double I[][] = { {1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 0, 1, 0}, {0, 0, 0, 0, 1} }; for (int row = 0; row < n; row++) { double tampung = A[row][row]; for (int col = 0; col < n; col++) { A[row][col] /= tampung; I[row][col] /= tampung; } for (int i = 0; i < n; i++) { if (i != row) { double m = A[i][row]; for (int col = 0; col < n; col++) { A[i][col] -= m * A[row][col]; I[i][col] -= m * I[row][col]; } } } } return I; } public static void main(String[] args) { FittingRegresi Reg = new FittingRegresi(); Reg.init(); } }
Tampilan ketika dijalankan :
lanjutan
lanjutan
Post a Comment