// Mengecek apakah dua vektor bertipe sama public static bool IsSameType(Vector a, Vector b) { return (a.Type == b.Type); }
// Melakukan perkalian matriks terhadap dua vektor public static Matrix MatrixMultiply(Vector a, Vector b) { Matrix result = new Matrix(a.Tuples, b.Tuples); if (CanMatrixMultiply(a, b)) { for (int i = 0; i < a.Tuples; i++) for (int j = 0; j < b.Tuples; j++) result[i, j] = a[i] * b[j]; } else { throw new WrongVectorSizeOrTypeException("Vector can't be matrix Multiplied"); } return result; }
// Operator overloading. Operasi perkalian skalar public static Vector operator *(double constant, Vector a) { Vector result = new Vector(a.Tuples, a.Type); for (int i = 0; i < a.Tuples; i++) result[i] = constant * a[i]; return result; }
// Mengecek apakah dua vektor berukuran dan bertipe sama public static bool IsSameSizeType(Vector a, Vector b) { return IsSameSize(a, b) && IsSameType(a, b); }
/// <summary> /// Memecahkan persamaan A * X = B /// dimana A adalah matriks ini (this) /// B adalah vektor input /// X adalah vektor yang dicari. /// Dilakukan dengan mendekomposisi A menjadi LU terlebih dahulu /// </summary> /// <param name="B">Vector. Vektor input</param> /// <returns>Vector. Vektor hasil</returns> public Vector Solve(Vector B) { Matrix inp = new Matrix(B.Tuples, 1); Matrix outp = new Matrix(B.Tuples, 1); Vector result = new Vector(B.Tuples, Vector.VectorType.ColumnVector); if (B.Type == Vector.VectorType.RowVector) throw new WrongVectorTypeException("Only ColumnVector valid"); for (int i = 0; i < B.Tuples; i++) inp[i, 0] = B[i]; outp = this.Solve(inp); for (int i = 0; i < outp.Rows; i++) result[i] = outp[i, 0]; return result; }
//Menghitung Nilai uji parameter secara Parsial private Vector ParsialTest(Vector a, Matrix b) { Vector result = new Vector(a.Tuples); for (int i = 0; i < result.Tuples; i++) { for (int j = 0; j < b.Cols; j++) if (i == j) result[i] = a[i] / Math.Sqrt(b[i, j]); } return result; }
// Mengecek apakah sebuah matriks dapat dikalikan dengan sebuah vektor public static bool CanMultiply(Matrix a, Vector b) { return (a.Cols == b.Tuples) && (b.Type == VectorType.ColumnVector); }
// Mendapatkan vector data kuadrat vektor public Vector GetDataSquare() { Vector result = new Vector(this.tuples); for (int i = 0; i < this.tuples; i++) { result[i] = this.data[i] * this.data[i]; ; } return result; }
// Mendapatkan sudut (dalam radian) antara dua vektor public static double Angle(Vector a, Vector b) { double result; if (a.Type == VectorType.ColumnVector && b.Type == VectorType.ColumnVector) { Vector aT = a.GetTranspose(); result = Vector.DoubleMultiply(aT, b); result /= a.Length * b.Length; result = Math.Acos(result); } else { throw new WrongVectorTypeException("Only Column Vector valid"); } return result; }
// Mengecek apakah dua vektor dapat dilakukan perkalian matriks. // Perkalian matriks adalah perkalian vektor kolom dengan vektor baris // dimana menghasilkan sebuah matriks persegi. public static bool CanMatrixMultiply(Vector a, Vector b) { return IsSameSize(a, b) && (a.Type == VectorType.ColumnVector) && (b.Type == VectorType.RowVector); }
// Operator overloading. Operasi pengurangan vektor public static Vector operator -(Vector a, Vector b) { Vector result = new Vector(a.Tuples, a.Type); if (IsSameSizeType(a, b)) { for (int i = 0; i < a.Tuples; i++) result[i] = a[i] - b[i]; } else { throw new NotSameSizeVectorException(); } return result; }
// Operator overloading. Operasi perkalian vektor dengan matriks public static Vector operator *(Vector a, Matrix b) { Vector result = new Vector(b.Cols, VectorType.RowVector); if (CanMultiply(a, b)) { for (int i = 0; i < b.Cols; i++) { result[i] = 0.0; for (int j = 0; j < b.Rows; j++) { result[i] += a[j] * b[j, i]; } } } else { throw new WrongVectorSizeOrTypeException("Can't be multiplied"); } return result; }
// Operator overloading. Operasi perkalian matriks dengan vektor public static Vector operator *(Matrix a, Vector b) { Vector result = new Vector(a.Rows, VectorType.ColumnVector); if (CanMultiply(a, b)) { for (int i = 0; i < a.Rows; i++) { result[i] = 0.0; for (int j = 0; j < a.Cols; j++) { result[i] += a[i, j] * b[j]; } } } else { throw new WrongVectorSizeOrTypeException("Can't be multiplied"); } return result; }
// Mendapatkan vektor proyeksi antara Vektor X dan Vektor Y public static Vector ProjectionXonY(Vector X, Vector Y) { Vector result; if (X.Type == VectorType.ColumnVector && Y.Type == VectorType.ColumnVector) { Vector XT = X.GetTranspose(); double proj = Vector.DoubleMultiply(XT, Y) / (Math.Pow(Y.Length, 2)); result = proj * Y; } else { throw new WrongVectorTypeException("Only Column Vector valid"); } return result; }
// Mengecek apakah sebuah vektor dapat dikalikan dengan sebuah matriks public static bool CanMultiply(Vector a, Matrix b) { return (a.Tuples == b.Rows) && (a.Type == VectorType.RowVector); }
// Mendapatkan copy dari vektor public Vector Copy() { Vector result = new Vector(this.tuples, this.type); for (int i = 0; i < this.tuples; i++) result[i] = this.data[i]; return result; }
// Melakukan perkalian double terhadap dua vektor public static double DoubleMultiply(Vector a, Vector b) { double result = 0.0; if (CanDoubleMultiply(a, b)) { for (int i = 0; i < a.Tuples; i++) result += a[i] * b[i]; } else { throw new WrongVectorSizeOrTypeException("Vector can't be double Multiplied"); } return result; }
// Mendapatkan vektor transpose public Vector GetTranspose() { VectorType newType; if (this.type == VectorType.ColumnVector) newType = VectorType.RowVector; else newType = VectorType.ColumnVector; Vector result = new Vector(this.tuples, newType); for (int i = 0; i < this.tuples; i++) result[i] = this.data[i]; return result; }
// Mengecek apakah dua buah vektor perpendicular. // Dua buah vektor perpendicular jika hasil perkalian doublenya 0 public static bool IsPerpendicular(Vector a, Vector b) { bool result; if (a.Type == VectorType.ColumnVector && b.Type == VectorType.ColumnVector) { Vector aT = a.GetTranspose(); result = Vector.DoubleMultiply(aT, b) == 0.0; } else { throw new WrongVectorTypeException("Only Column Vector valid"); } return result; }
//Menghitung estimator spatial regresi dengan OLS public LeastSquareEstimator(Matrix X, Matrix W, Vector Y) { //this.x = X; this.w = W; this.y = Y; int n = X.Rows; // this.p = X.Cols; Matrix vector1 = new Matrix(n, 1); vector1.InitializeAllValue(1); x = new Matrix(X.Rows, vector1.Cols + X.Cols); x = x.GroupingCols(vector1, X); //this.p = x.Cols; wStandard = new Matrix(w.Rows, w.Cols); Matrix wTemp = new Matrix(w.Rows, w.Cols); wStandard = wTemp.Standardized(w); xstar = wStandard * X; //Penggabungan matriks x dan x* sebagai matrik x observasi xObs = new Matrix(x.Rows, x.Cols + xstar.Cols); xObs = xObs.GroupingCols(x, xstar); this.p = xObs.Cols; xObsTx = xObs.GetTranspose() * xObs; xObsTy = xObs.GetTranspose() * y; xObsTxInverse = xObsTx.GetInverse(); estimator = xObsTxInverse * xObsTy; ycap = xObs * estimator; e = y - ycap; eSquare = e.GetDataSquare(); eVariance = Vector.DoubleMultiply(e.GetTranspose(), e) / (n - p); double seReg = Math.Sqrt(eVariance); covEstimator = eVariance * xObsTxInverse; Matrix J = new Matrix(n, n); J.InitializeAllValue(1.0); double yty = Vector.DoubleMultiply(Y.GetTranspose(), Y); Vector jy = J * y; ssto = yty - Vector.DoubleMultiply(Y.GetTranspose(), jy) / n; //sse = yty - Vector.DoubleMultiply(estimator.GetTranspose(), xty); sse = Vector.DoubleMultiply(e.GetTranspose(), e); ssr = ssto - sse; msr = ssr / (p - 1); mse = sse / (n - p); rSquare = ssr / ssto; r = Math.Sqrt(rSquare); // Mendapatkan Nilai F pada uji Simultan fValue = msr / mse; // Mendapatkan Nilai PValue pada uji F Simultan Distribution.F fDist = new Distribution.F(); fProb = fDist.PValue(fValue, p - 1, n - p); // Mendapatkan Nilai t untuk uji Parsial tTest = new Vector(estimator.Tuples); tTest = ParsialTest(estimator, covEstimator); //Mendapatkan Nilai pValue pada uji t Parsial tProb = new Vector(tTest.Tuples); tProb = PValueParsialTest(tTest, p); }
// Mengecek apakah dua vektor berukuran sama public static bool IsSameSize(Vector a, Vector b) { return (a.Tuples == b.Tuples); }
private Vector PValueParsialTest(Vector a, double k) { Vector result = new Vector(a.Tuples); Distribution.t pValue = new Distribution.t(); for (int i = 0; i < result.Tuples; i++) { //for (int j = 0; j < result.Cols; j++) result[i] = pValue.FullPValue(a[i], k); } return result; }
static void Main(string[] args) { Console.Out.Write("Jumlah Variabel Bebas : "); int p = Convert.ToInt32(Console.ReadLine()); Console.Out.Write("Jumlah Observasi : "); int n = Convert.ToInt32(Console.ReadLine()); Console.Out.WriteLine("\n"); Console.Out.WriteLine("Masukkan Nilai Observasi Variabel Bebas X"); //Console.Out.WriteLine("\n"); Console.Out.WriteLine("------------------------------------------"); Matrix X = new Matrix(n,p); for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { Console.Out.Write("X[" + i +","+ j+"]:"); X[i,j] = Convert.ToDouble(Console.ReadLine()); } } Console.Out.WriteLine("------------------------------------------"); Console.Out.WriteLine("\n"); Console.Out.WriteLine("Masukkan Nilai Observasi Variabel Terikat Y"); //Console.Out.WriteLine("\n"); Console.Out.WriteLine("------------------------------------------"); Vector Y = new Vector(n,Vector.VectorType.ColumnVector); for (int i = 0; i < n; i++) { Console.Write("Y[" + i + "]: "); Y[i] = Convert.ToDouble(Console.ReadLine()); } Console.Out.WriteLine("------------------------------------------"); Console.Out.WriteLine("\n"); Console.Out.WriteLine("Masukkan Nilai Pembobot W"); // Console.Out.WriteLine("\n"); Console.Out.WriteLine("------------------------------------------"); Matrix W = new Matrix(n, n); int rows = n; int cols = n; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { Console.Out.Write("W[" + i + "," + j + "]: "); W[i, j] = Convert.ToDouble(Console.ReadLine()); } // Console.Out.WriteLine(); } Console.Out.WriteLine("------------------------------------------"); Console.Out.WriteLine("\n"); // Estimasi Nilai parameter LeastSquareEstimator estim = new LeastSquareEstimator(X, W, Y); Console.Out.WriteLine("Nilai X Bintang adalah :"); Console.Out.WriteLine("------------------------------------------"); // int xBintangCol = estim.Xstar.Cols; // int xBintangRows = estim.Xstar.Rows; Matrix xBintang = new Matrix(n,p); xBintang = estim.Xstar; for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { Console.Out.WriteLine("Xstar [" + i + "," + j + "]: {0}", xBintang[i,j]); //xBintang[i, j] = Convert.ToDouble(Console.ReadLine()); } // Console.Out.WriteLine(); } Console.Out.WriteLine("------------------------------------------"); Console.Out.WriteLine("\n"); Console.Out.WriteLine("Nilai X Observasi adalah :"); Console.Out.WriteLine("------------------------------------------"); int xObsCols = estim.Xobs.Cols; int xObsROws = estim.Xobs.Rows; Matrix xObservasi = new Matrix(xObsROws, xObsCols); xObservasi = estim.Xobs; for (int i = 0; i < xObsROws; i++) { for (int j = 0; j < xObsCols; j++) { Console.Out.WriteLine("X Observasi [" + i + "," + j + "]: {0}", xObservasi[i, j]); } } Console.Out.WriteLine("------------------------------------------"); // Console.ReadLine(); // Console.Out.WriteLine("------------------------------------------"); Console.Out.WriteLine("\n"); Console.Out.WriteLine("Nilai X Tranpose X adalah :"); Console.Out.WriteLine("------------------------------------------"); int xObsTxCols = estim.XobsTx .Cols; int xObsTxROws = estim.XobsTx.Rows; Matrix xObsTx = new Matrix(xObsTxROws, xObsTxCols); xObsTx = estim.XobsTx; for (int i = 0; i < xObsTxROws; i++) { for (int j = 0; j < xObsTxCols; j++) { Console.Out.WriteLine("XTranposeX [" + i + "," + j + "]: {0:#,0.####}", xObsTx[i, j]); } } Console.Out.WriteLine("------------------------------------------"); // Console.ReadLine(); // Console.Out.WriteLine("------------------------------------------"); Console.Out.WriteLine("\n"); Console.Out.WriteLine("Nilai X Tranpose Y adalah :"); Console.Out.WriteLine("------------------------------------------"); int xObsTyTuple = estim.XobsTy.Tuples; Vector xObsTy = new Vector(xObsTyTuple); xObsTy = estim.XobsTy; for (int i = 0; i < xObsTxROws; i++) { Console.Out.WriteLine("XTranposeY [" + i + "]: {0:#,0.####}", xObsTy[i]); } Console.Out.WriteLine("------------------------------------------"); Console.Out.WriteLine("\n"); // Console.ReadLine(); Console.Out.WriteLine("Nilai Inverse X Tranpose X adalah :"); Console.Out.WriteLine("------------------------------------------"); int inversXtxRows = estim.XObsTxInverse.Rows; int inversXtxCols = estim.XObsTxInverse.Cols; Matrix inversXobsTx = new Matrix(inversXtxRows, inversXtxCols); inversXobsTx = estim.XObsTxInverse; for (int i = 0; i < inversXtxRows; i++) { for (int j = 0; j < inversXtxCols; j++) { Console.Out.WriteLine("Inverse X Tranpose X [" + i + "," + j + "]: {0:#,0.####}", inversXobsTx[i, j]); } } Console.Out.WriteLine("------------------------------------------"); Console.Out.WriteLine("\n"); // Console.ReadLine(); int parameterCount = estim.Estimator.Tuples; Vector param = new Vector(parameterCount,Vector.VectorType.ColumnVector); param = estim.Estimator; double r = estim.R; double r2 = estim.RSquare; Console.Out.WriteLine(" Estimasi nilai parameter adalah sebagai berikut:"); Console.Out.WriteLine("\n"); Console.Out.WriteLine("------------------------------------------"); Console.Out.WriteLine("parameter \tNilai parameter"); Console.Out.WriteLine("------------------------------------------"); for (int i = 0; i < parameterCount; i++) { Console.WriteLine("parameter [{0}] \t {1:#,0.####} ", i, param[i]); } Console.Out.WriteLine("------------------------------------------\n"); Console.Out.WriteLine("Koefisien Korelasi = {0}", r); Console.Out.WriteLine("Koefisien Determinasi = {0}\n\n", r2); double eVar; eVar = estim.Evariance; Console.Out.WriteLine("Variance dari error = {0}", eVar); Console.Out.WriteLine("\n"); // Console.ReadLine(); Console.Out.WriteLine("Nilai Matrix Variance - Covariance adalah sebagai berikut:"); Console.Out.WriteLine("------------------------------------------"); Matrix varCov = new Matrix(inversXtxRows, inversXtxCols); varCov = estim.SbSquare; for (int i = 0; i < inversXtxRows; i++) { for (int j = 0; j < inversXtxCols; j++) { Console.Out.WriteLine("Variance [" + i + "," + j + "]: {0:#,0.####}", varCov[i, j]); } } Console.Out.WriteLine("------------------------------------------"); Console.Out.WriteLine("\n"); Console.Out.WriteLine("==============PENGUJIAN PARAMETER===============\n"); Console.Out.WriteLine("--Uji Parsial--(uji t)---"); Vector ycap = new Vector(n); Vector error = new Vector(n); // Vector param = new Vector(parameterCount); ycap = estim.YCap; error = estim.E; Vector t, peluangT; t = estim.Ttest; peluangT = estim.Tprob; Console.Out.WriteLine("----------------------------------------------------------"); Console.Out.WriteLine("estimasi parameter \t Nilai T \t pValue"); Console.Out.WriteLine("----------------------------------------------------------"); for (int i = 0; i < parameterCount; i++) { Console.WriteLine("\t{0:#,0.###} \t\t {1:#,0.###} \t\t {2:#,0.###} ", param[i], t[i], peluangT[i]); //1Y[i] = Convert.ToDouble(Console.ReadLine()); } Console.Out.WriteLine("----------------------------------------------------------\n\n"); Console.Out.WriteLine("--Uji Simultan--(uji F)---"); double fValue,peluangF, SSR, SSE, SST, MSR, MSE; int k; fValue = estim.FValue; peluangF = estim.FProb; SSR = estim.SSR; SSE = estim.SSE; SST = estim.SSTO; MSR = estim.MSR; MSE = estim.MSE; k = estim.Xobs.Cols; Console.Out.WriteLine("-------------------------------------------------------------------------"); Console.Out.WriteLine("Source of Var \t Sum Square \t dof \t Mean Square \t F"); Console.Out.WriteLine("-------------------------------------------------------------------------"); Console.Out.WriteLine("Regression \t {0:#,0.####} \t {1} \t {2:#,0.####} \t {3:#,0.####}", SSR, k - 1, MSR, fValue); Console.Out.WriteLine("Residual \t {0:#,0.####} \t {1} \t {2:#,0.####}", SSE, n - k, MSE); Console.Out.WriteLine("-------------------------------------------------------------------------"); Console.Out.WriteLine("Total \t \t {0:#,0.####} \t\t {1}", SST, n - 1); Console.Out.WriteLine("-------------------------------------------------------------------------"); Console.Out.WriteLine("\n\n"); Console.Out.WriteLine("Nilai PValue pada uji F ini adalah = {0:#,0.####}", peluangF); Console.Out.WriteLine("\n\n"); Console.Out.WriteLine (" Perbandingan Y dan Ycap adalah sebagai berikut:"); Console.Out.WriteLine("-------------------------------------------------------------------------"); Console.Out.WriteLine("\t Y \t\t Ycap \t\t error"); Console.Out.WriteLine("-------------------------------------------------------------------------"); for (int i = 0; i < n; i++) { Console.WriteLine("\t {0} \t\t {1:#,0.####} \t {2:#,0.####}", Y[i], ycap[i], error[i]); //1Y[i] = Convert.ToDouble(Console.ReadLine()); } Console.Out.WriteLine("-------------------------------------------------------------------------"); Console.ReadLine(); }