/// <summary> /// Prosedur untuk menghitung inisialisasi nilai awal /// Nilai awal ditentukan dengan menggunakan lse pada 6 observasi pertama /// </summary> private void Init() { Matrix x = new Matrix(this.n, 2); for (int i = 0; i < this.n; ++i) { x[i, 0] = 1; x[i, 1] = i + 1; } LeastSquareEstimator lse = new LeastSquareEstimator(x, y); this.smoothing[0] = this.a * this.y[0] + ((1 - this.a) * (lse.B[0] + lse.B[1])); this.trend[0] = this.g * (this.smoothing[0] - lse.B[0]) + (1 - this.g) * lse.B[1]; this.predicted[0] = lse.B[0] + lse.B[1]; this.residual[0] = this.y[0] - this.predicted[0]; }
/// <summary> /// Prosedur untuk menghitung inisialisasi Pemulusan dan trend periode 0 (nol) /// Dihitung lse untuk data sebanyak panjang musiman pertama /// </summary> private void EstimateValue() { //Menghitung nilai detrend Matrix xL = new Matrix(this.l, 2); for (int i = 0; i < this.l; ++i) { xL[i, 0] = 1; xL[i, 1] = i + 1; } Vector yL = new Vector(this.l); for (int i = 0; i < this.l; ++i) { yL[i] = this.y[i]; } lse = new LeastSquareEstimator(xL, yL); }
/// <summary> /// Prosedur untuk menghitung inisialisasi nilai awal /// nilai awal ditentukan dengan menggunakan lse /// </summary> private void Init() { Matrix x = new Matrix(this.n, 2); for (int i = 0; i < this.n; ++i) { x[i, 0] = 1; x[i, 1] = i + 1; } LeastSquareEstimator lse = new LeastSquareEstimator(x, y); double[] parameters = lse.B.GetData(); double w = 1 - this.a; double a0 = (double)(parameters[0] - ((w / this.a) * parameters[1])); double b0 = (double)(parameters[0] - (2 * (w / this.a) * parameters[1])); this.smoothing1[0] = this.a * this.y[0] + w * a0; this.smoothing2[0] = this.a * this.smoothing1[0] + w * b0;; }
/// <summary> /// Mengestimasi parameter /// </summary> private void estimateParameters() { if (this.linearRdb.Checked) { x = new Matrix(this.y.Tuples, 2); for (int i = 0; i < this.y.Tuples; ++i) { x[i, 0] = 1; x[i, 1] = i + 1; } lse = new LeastSquareEstimator(x, this.y); this.trendProperties.initialModel = 1; this.trendProperties.includedObservations = this.y.Tuples; this.trendProperties.parameters = lse.B.GetData(); this.trendProperties.sse = lse.SSE; this.trendProperties.mse = lse.MSE; this.trendProperties.r = lse.R; this.trendProperties.rSquare = lse.RSquare; this.trendProperties.rSquareAdjusted = 1 - (Math.Sqrt(lse.SSE) / lse.SSTO); this.predicted = lse.YCap.GetData(); this.residual = lse.E.GetData(); } else if (this.quadraticRdb.Checked) { x = new Matrix(this.y.Tuples, 3); for (int i = 0; i < this.y.Tuples; ++i) { x[i, 0] = 1; x[i, 1] = i + 1; x[i, 2] = Math.Pow(i + 1, 2); } lse = new LeastSquareEstimator(x, this.y); this.trendProperties.initialModel = 2; this.trendProperties.includedObservations = this.y.Tuples; this.trendProperties.parameters = lse.B.GetData(); this.trendProperties.sse = lse.SSE; this.trendProperties.mse = lse.MSE; this.trendProperties.r = lse.R; this.trendProperties.rSquare = lse.RSquare; this.trendProperties.rSquareAdjusted = 1 - (Math.Sqrt(lse.SSE) / lse.SSTO); this.predicted = lse.YCap.GetData(); this.residual = lse.E.GetData(); } else if (this.expGrowthRdb.Checked) { x = new Matrix(this.y.Tuples, 2); for (int i = 0; i < this.y.Tuples; ++i) { x[i, 0] = 1; x[i, 1] = i + 1; } Vector z = new Vector(this.y.Tuples); for (int i = 0; i < this.y.Tuples; ++i) { z[i] = Math.Log10(this.y[i]); } lse = new LeastSquareEstimator(x, z); this.trendProperties.parameters = lse.B.GetData(); this.trendProperties.includedObservations = this.y.Tuples; this.trendProperties.initialModel = 4; for (int i = 0; i < 2; ++i) { this.trendProperties.parameters[i] = Math.Pow(10, this.trendProperties.parameters[i]); } predicted = new double[this.y.Tuples]; residual = new double[this.y.Tuples]; for (int i = 0; i < this.y.Tuples; ++i) { this.predicted[i] = this.trendProperties.parameters[0] * Math.Pow(this.trendProperties.parameters[1], (i + 1)); this.residual[i] = this.y[i] - this.predicted[i]; } this.trendProperties.sse = lse.SSE; this.trendProperties.mse = lse.MSE; this.trendProperties.r = lse.R; this.trendProperties.rSquare = lse.RSquare; this.trendProperties.rSquareAdjusted = 1 - (Math.Sqrt(lse.SSE) / lse.SSTO); } else if (this.cubicRdb.Checked) { x = new Matrix(this.y.Tuples, 4); for (int i = 0; i < this.y.Tuples; ++i) { x[i, 0] = 1; x[i, 1] = i + 1; x[i, 2] = Math.Pow(i + 1, 2); x[i, 3] = Math.Pow(i + 1, 3); } lse = new LeastSquareEstimator(x, this.y); this.trendProperties.initialModel = 3; this.trendProperties.includedObservations = this.y.Tuples; this.trendProperties.parameters = lse.B.GetData(); this.trendProperties.sse = lse.SSE; this.trendProperties.mse = lse.MSE; this.trendProperties.r = lse.R; this.trendProperties.rSquare = lse.RSquare; this.trendProperties.rSquareAdjusted = 1 - (Math.Sqrt(lse.SSE) / lse.SSTO); this.predicted = lse.YCap.GetData(); this.residual = lse.E.GetData(); } }
/// <summary> /// Inisialisasi nilai awal untuk tipe model multiplikatif /// Nilai awal musiman ditentukan dengan menggunakan Dummy Variabel dari data detrend /// </summary> private void InitMultiplicative() { //Menghitung nilai detrend Matrix x = new Matrix(this.n, 2); for (int i = 0; i < this.n; ++i) { x[i, 0] = 1; x[i, 1] = i + 1; } LeastSquareEstimator lse1 = new LeastSquareEstimator(x, this.y); Vector detrend = new Vector(this.n); for (int i = 0; i < this.n; i++) { detrend[i] = this.y[i] / lse1.YCap[i]; } //Mencari indeks musiman dengan Dummy Regresi Matrix z = new Matrix(this.n, this.l); for (int i = 0; i < this.n; i++) { for (int j = 0; j < this.l; j++) { if (j == this.term[i]) { z[i, j] = 1; } else { z[i, j] = 0; } } } LeastSquareEstimator lse2 = new LeastSquareEstimator(z, detrend); double[] seasonalIndex = lse2.B.GetData(); this.EstimateValue(); double smoothing0 = lse.B[0]; double trend0 = lse.B[1]; this.smoothing[0] = (this.alpha * this.y[0] / seasonalIndex[0]) + (1 - this.alpha) * (smoothing0 + trend0); this.trend[0] = this.gamma * (this.smoothing[0] - smoothing0) + (1 - this.gamma) * trend0; this.seasonal[0] = (this.beta * this.y[0] / this.smoothing[0]) + (1 - this.beta) * seasonalIndex[0]; this.predicted[0] = (smoothing0 + trend0) * seasonalIndex[0]; this.residual[0] = this.y[0] - this.predicted[0]; for (int i = 1; i < l; i++) { this.smoothing[i] = (this.alpha * this.y[i] / seasonalIndex[i]) + (1 - this.alpha) * (this.smoothing[i - 1] + this.trend[i - 1]); this.trend[i] = this.gamma * (this.smoothing[i] - this.smoothing[i - 1]) + (1 - this.gamma) * this.trend[i - 1]; this.seasonal[i] = (this.beta * this.y[i] / this.smoothing[i]) + (1 - this.beta) * seasonalIndex[i]; this.predicted[i] = (this.smoothing[i - 1] + this.trend[i - 1]) * seasonalIndex[i]; this.residual[i] = this.y[i] - this.predicted[i]; } }
private void ComponentTrend() { switch (this.initialTrend) { case 1: { //Model tren linier x = new Matrix(this.n, 2); for (int i = 0; i < this.n; ++i) { x[i, 0] = 1; x[i, 1] = i + 1; } Vector z = new Vector(this.deseasonal); lse = new LeastSquareEstimator(x, z); this.parameters = lse.B.GetData(); for (int i = 0; i < this.n; i++) { this.trend[i] = this.parameters[0] + this.parameters[1] * (i + 1); } break; } case 2: { //model tren kuadratik x = new Matrix(this.n, 3); for (int i = 0; i < this.n; ++i) { x[i, 0] = 1; x[i, 1] = i + 1; x[i, 2] = Math.Pow(i + 1, 2); } Vector z = new Vector(this.deseasonal); lse = new LeastSquareEstimator(x, z); this.parameters = lse.B.GetData(); for (int i = 0; i < this.n; i++) { this.trend[i] = this.parameters[0] + this.parameters[1] * (i + 1) + this.parameters[2] * Math.Pow(i + 1, 2); } break; } case 3: { //model tren kubik x = new Matrix(this.n, 4); for (int i = 0; i < this.n; ++i) { x[i, 0] = 1; x[i, 1] = i + 1; x[i, 2] = Math.Pow(i + 1, 2); x[i, 3] = Math.Pow(i + 1, 3); } Vector z = new Vector(this.deseasonal); lse = new LeastSquareEstimator(x, z); this.parameters = lse.B.GetData(); for (int i = 0; i < this.n; i++) { this.trend[i] = this.parameters[0] + this.parameters[1] * (i + 1) + this.parameters[2] * Math.Pow(i + 1, 2) + this.parameters[3] * Math.Pow(i + 1, 3); } break; } case 4: { //model tren eksponensial x = new Matrix(this.n, 2); for (int i = 0; i < this.n; ++i) { x[i, 0] = 1; x[i, 1] = i + 1; } Vector z = new Vector(this.n); for (int i = 0; i < this.n; ++i) { z[i] = Math.Log10(this.deseasonal[i]); } lse = new LeastSquareEstimator(x, z); this.parameters = lse.B.GetData(); for (int i = 0; i < 2; ++i) { this.parameters[i] = Math.Pow(10, this.parameters[i]); } for (int i = 0; i < this.n; i++) { this.trend[i] = this.parameters[0] * Math.Pow(this.parameters[1], (i + 1)); } break; } default: goto case 1; } }