Пример #1
0
 public TimeSeries(TimeSeries ts, Range r)
 {
     this.data = new double[(int)r.len];
     this.r = r;
     for (int i = (int)r.s; i <= (int)r.e; i++)
         data[i - (int)r.s] = ts.data[i];
     this.freq = ts.freq;
 }
Пример #2
0
        public ArrayList Divide(ArrayList ranges)
        {
            ArrayList tss = new ArrayList();

            foreach( Range r in ranges) {
                ArrayList tmp=new ArrayList();
                for(int i=(int)r.s;i<=(int)r.e;i++){
                    tmp.Add(data[i]);
                }
                TimeSeries ts=new TimeSeries(tmp, freq);
                    ts.r = r;
                tss.Add(ts);
            }
            return tss;
        }
Пример #3
0
        public RTree(ArrayList ranges, TimeSeries ts)
        {
            ArrayList R_j = (ArrayList)ranges[ranges.Count - 1];

            root = (Range)R_j[0];
            ArrayList R_i;

            for (int i = ranges.Count - 1; i >= 0; i--)
            {
                R_i = (ArrayList)ranges[i];
                foreach (Range r_i in R_i)
                {
                    TimeSeries ts_ = new TimeSeries(ts, r_i);
                    Model      m   = new Model(ts_);
                    m.Clean();
                    m.Set();
                    ModelTree t = m.construct();
                    t.range    = r_i;
                    t.children = null;
                    r_i.t      = t;
                }
            }

            for (int i = ranges.Count - 2; i >= 0; i--)
            {
                R_i = (ArrayList)ranges[i];
                R_j = (ArrayList)ranges[i + 1];

                foreach (Range r_i in R_i)
                {
                    foreach (Range r_j in R_j)
                    {
                        if (r_j.overlap(r_i))
                        {
                            r_j.children.Add(r_i);
                        }
                    }
                }
            }
            for (int i = ranges.Count - 2; i >= 0; i--)
            {
                R_i = (ArrayList)ranges[i];
                R_j = (ArrayList)ranges[i + 1];

                foreach (Range r_i in R_i)
                {
                    foreach (Range r_j in R_j)
                    {
                        if (r_j.overlap(r_i))
                        {
                            if (r_j.len > r_i.len)
                            {
                                if (r_i.t.error > 1)
                                {
                                    r_j.t.childs.Add(r_i.t);
                                    r_i.t.parent = r_j.t;
                                }
                            }
                        }
                    }
                }
            }
            //convert childs to children
            for (int i = ranges.Count - 1; i >= 0; i--)
            {
                R_i = (ArrayList)ranges[i];
                foreach (Range r_i in R_i)
                {
                    r_i.t.convertchildstochildren();
                }
            }
        }
Пример #4
0
        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;
            }
        }
Пример #5
0
 public ModelSet(TimeSeries ts)
 {
     this.ts = ts;
 }
Пример #6
0
        void setModels(int len, double[] errors, int shift, int done)
        {
            double[] newerrors = null;
            if (errors.Length > 1)
            {
                newerrors = new double[errors.Length - 1];
                for (int i = 0; i < errors.Length - 1; i++)
                {
                    newerrors[i] = errors[i];
                }
            }
            int kk = (int)Math.Floor((double)ts.Length / len);

            children = new ModelTree[kk];
            int x = 0;

            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);
                ModelTree m = null;
                if (shift == 1)
                    m = new ModelTree(t, newerrors, s);
                else
                    m = new ModelTree(t, errors, s);
                if (done == 0)
                {
                    m.BuildTree();
                }
                children[i] = m;
                m.start = s;
            }
        }
Пример #7
0
 public ModelTree(TimeSeries ts, double[] errors = null, int start = 0)
     : base(ts)
 {
     this.range = new Range(start, start + ts.Length - 1);
     this.errors = errors;
     this.children = null;
 }
Пример #8
0
 public virtual void Clean()
 {
     errors = null;
     trend = null;
     if ((seasonal != null) && (values != null)) { ts = null; }
     if (seasonal != null) seasonal.Clean();
 }
Пример #9
0
        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;
        }
Пример #10
0
 public Model()
 {
     ts = null;
     seasonal = null;
     values = null;
     errors = null;
 }
Пример #11
0
        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;
        }
Пример #12
0
 public ModelSet(TimeSeries ts)
 {
     this.ts = ts;
 }
Пример #13
0
 public ArrayList Divide(int length)
 {
     ArrayList tss = new ArrayList();
     ArrayList tmp= new ArrayList();
     for (int i = 0; i < Length; i++)
     {
         tmp.Add(data[i]);
         if(tmp.Count== length){
             TimeSeries ts = new TimeSeries(tmp, freq);
             ts.r = new Range(i-tmp.Count+1, i );
             tss.Add(ts);
             tmp.Clear();
         }
     }
     if (tmp.Count > 0)
     {
         TimeSeries ts = new TimeSeries(tmp, freq);
         ts.r = new Range(Length - tmp.Count - 1, Length);
         tss.Add(ts);
     }
     return tss;
 }
Пример #14
0
        public static void GenUK()
        {
            //first read ukc
            int[]      freq = { 17520 };
            double[]   uk   = utils.File.ReadData("c:/data/ukc.txt");
            TimeSeries ts   = new TimeSeries(uk, freq);
            Model      m    = new Model(ts);

            double [] errors = new double[m.trend.Length];
            for (int i = 0; i < m.trend.Length; i++)
            {
                errors[i] = ts.data[i] - m.Eval(i);
            }
            double[] t = new double[m.trend.Length * 100];
            double[] s = new double[m.trend.Length * 100];
            double[] e = new double[m.trend.Length * 100];
            Random   r = new Random();

            for (int i = 0; i < m.trend.Length; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                    t[j * m.trend.Length + i] = m.trend[i];
                }
            }
            for (int i = 0; i < m.trend.Length * 100; i++)
            {
                double mult = 1;
                if ((i < m.trend.Length * 10) && (i > 0))
                {
                    mult = 1.1;
                }
                if ((i < m.trend.Length * 20) && (i > m.trend.Length * 10))
                {
                    mult = 1.1;
                }
                if ((i < m.trend.Length * 30) && (i > m.trend.Length * 20))
                {
                    mult = 1.0;
                }
                if ((i < m.trend.Length * 40) && (i > m.trend.Length * 30))
                {
                    mult = 1.4;
                }
                if ((i < m.trend.Length * 50) && (i > m.trend.Length * 40))
                {
                    mult = 1.2;
                }
                if ((i < m.trend.Length * 60) && (i > m.trend.Length * 50))
                {
                    mult = 1.3;
                }
                if ((i < m.trend.Length * 70) && (i > m.trend.Length * 60))
                {
                    mult = 1.5;
                }
                if ((i < m.trend.Length * 80) && (i > m.trend.Length * 70))
                {
                    mult = 1.2;
                }
                if ((i < m.trend.Length * 90) && (i > m.trend.Length * 80))
                {
                    mult = 1.1;
                }
                if ((i < m.trend.Length * 100) && (i > m.trend.Length * 90))
                {
                    mult = 1.0;
                }
                s[i] = m.seasonal.Eval(i % freq[0]) * mult;
            }
            for (int i = 0; i < m.trend.Length * 100; i++)
            {
                e[i] = errors[i % m.trend.Length] * (r.NextDouble() / 2 + 0.5) * 0.001;
            }
            StreamWriter sw = new StreamWriter("c:/data/n/uk3.txt");

            for (int i = 0; i < m.trend.Length * 30; i++)
            {
                double tt = t[i] + s[i] + e[i];
                sw.WriteLine((int)tt);
            }
            sw.Close();
        }