private void decompose(int n, int freq, double[] season_) { int swindow = 10 * n + 1; stllib.STL stl = new STL(); double[] seaonal = new double[freq]; int[] count = new int[freq]; int limit = (int)Math.Ceiling((double)n / freq); this.freq = freq; this.len = n; unsafe { double *y = (double *)utils.Memory.Alloc(sizeof(double) * n); double *t = (double *)utils.Memory.Alloc(sizeof(double) * n); double *s = (double *)utils.Memory.Alloc(sizeof(double) * n); for (int i = 0; i < n; i++) { y[i] = ts.data[i]; } stl.compute(y, n, freq, swindow, t, s); int k = 0; if (type == ModelType.Explicit || type == ModelType.Implicit) { values = new double[limit]; for (int i = 0; i < limit; i++) { double t1 = 0; double t2 = 0; int l = 0; for (int j = 0; j < freq; j++) { if (k == n) { break; } l++; k++; t1 += ts.data[j + i * freq]; t2 += y[j + i * freq]; } t1 /= l; t2 /= l; values[i] = (t2 + t1) / 2; } } if (type == ModelType.Implicit) { double[] x = ChebyshevReg.Solve(values); values = new double[2]; values[0] = x[0]; values[1] = x[1]; } else if (type == ModelType.Trend) { double[] t_ = new double[n]; for (int i = 0; i < n; i++) { t_[i] = t[i]; } double[] x = ChebyshevReg.Solve(t_); values = new double[2]; values[0] = x[0]; values[1] = x[1]; } //make seaonality perfect for (int i = 0; i < n; i++) { seaonal[i % freq] += s[i]; count[i % freq]++; } for (int i = 0; i < freq; i++) { season_[i] = seaonal[i % freq] / count[i % freq]; } trend = new double[n]; for (int i = 0; i < n; i++) { trend[i] = t[i]; } utils.Memory.Free(y); utils.Memory.Free(s); utils.Memory.Free(t); } }
public void Solve() { if (type == ModelType.PLA) { values = new double[2]; double asum = 0; double bsum = 0; for (int i = 0; i < this.ts.Length; i++) { asum += ((i + 1) - ((ts.Length + 1) / 2)) * ts.data[i]; bsum += ((i + 1) - ((2 * ts.Length + 1) / 3)) * ts.data[i]; } values[0] = 12 * asum / ts.Length / (ts.Length + 1) / (ts.Length - 1); values[1] = 6 * bsum / ts.Length / (1 - ts.Length); return; } int n = ts.Length; int l = 0; freq = ts.freq[0]; while (freq > ts.Length) { if (l == ts.freq.Length) { return; } freq = ts.freq[l++]; } if (freq == 0) { //use regression values = ChebyshevReg.Solve(ts.data); seasonal = null; } else { double[] season_ = new double[freq]; decompose(n, freq, season_); seasonal = new Model(); int[] f; if (ts.freq.Length == 1) { f = null; } else { f = new int[ts.freq.Length - 1]; for (int i = 0; i < ts.freq.Length - 1; i++) { f[i] = ts.freq[i + 1]; } } seasonal.ts = new TimeSeries(season_, f); if (f != null) { seasonal.Solve(); } } }