public void getModels(int len, double error, ref int length) { length = 0; int kk = (int)Math.Floor((double)ts.Length / len); models = new Model[kk]; int x = 0; this.len = len; for (int i = 0; i < kk; i++) { if (i == kk - 1) len = ts.Length - x; int s = x; double[] u = new double[len]; for (int j = 0; j < len; j++) u[j] = ts.data[x++]; TimeSeries t = new TimeSeries(u, ts.freq); Model m = new Model(t); length += m.countError(error); m.start = s; models[i] = m; } }
static ArrayList Level(ArrayList ranges, double errror_level) { ArrayList tss = null; ArrayList R = new ArrayList(); foreach (Range r in ranges) R.Add(new Range(r.s, r.e)); ArrayList X = ranges; // should ont use computed and matched for (; ; ) { int count = 0; tss = ts.Divide(X); foreach (TimeSeries t in tss) { if (t.r.computed == 1) {if (t.r.matched== 1) count++; continue; } t.r.computed = 1; t.r.matched = 0; Model m; if(Global.quick) m=Model.ModelQuick(t); else m = new Model(t);// if (m.error < errror_level) { t.r.matched = 1; bool ignore = false; // may be also be called as do not add to the count++; //remove ranges ArrayList todel = new ArrayList(); foreach (Range r in R) { if ((t.r.overlap(r)) && ((int)t.r.len != (int)r.len)) todel.Add(r); else if (t.r.overlap(r)) ignore = true; } foreach (Range r in todel) R.Remove(r); if(ignore==false) R.Add(t.r); } } if (count == 0) break; //if (count == 1) break; X.Sort(); X = combine(X); // we need to deset matched or not } return R; }
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(); } }
public static Model ModelQuick(TimeSeries ts) { Model m = new Model(); m.ts = ts; m.seasonal = null; m.values = null; m.errors = null; m.type = ModelType.Explicit; m.Solve(); m.CalcError(); // m.ComputeErrorRange(); m.error = m.Error(Global.confidence); m.len = ts.Length; return m; }
public Model() { ts = null; seasonal = null; values = null; errors = null; }
public Model(TimeSeries ts) { this.ts = ts; seasonal = null; values = null; errors = null; ModelType best = ModelType.Trend; double min = double.MaxValue; foreach (ModelType t in Enum.GetValues(typeof(ModelType))) { this.type = t; this.Solve(); this.CalcError(); double rt = this.Size() * Error(Global.confidence); if (min > rt) { min = rt; best = t; } } type = best; this.Solve(); this.CalcError(); ComputeErrorRange(); error = Error(Global.confidence); this.len = ts.Length; }