コード例 #1
0
        public void TestPredictLogisticWithPredictNext()
        {
            double[] logistic = new double[1000];
            logistic[0] = 0.1;
            for (int n = 1; n < 1000; n++)
            {
                logistic[n] = 4.0 * logistic[n - 1] * (1.0 - logistic[n - 1]);
            }
            ChaosAnalysis analysis   = new ChaosAnalysis();
            TimeSpan      sampleTime = new TimeSpan(0, 0, 1);

            analysis.LoadSampledData(logistic, DateTime.Now, sampleTime);
            Assert.IsTrue(analysis.CalculateMeasures(true, false, true));
            logistic[0] = logistic[logistic.Length - 1];
            double error = 0.0;

            for (int n = 1; n < 1000; n++)
            {
                logistic[n] = 4.0 * logistic[n - 1] * (1.0 - logistic[n - 1]);
                double[] result;
                if (n == 1)
                {
                    result = analysis.Predict(1, analysis.latest);
                }
                else
                {
                    result = analysis.PredictNext(1, analysis.latest);
                }
                analysis.AddDataPointRelative(logistic[n], sampleTime);
                double diff = logistic[n] - result[0];
                error += diff * diff;
            }
            error = Math.Sqrt(error / 1000);
            Assert.AreEqual(0.00403954, error, 0.001);
        }
コード例 #2
0
        public void TestRossler()
        {
            Trace.WriteLine("Rossler");
            double h     = 0.05; /* or smaller */
            double a     = 0.2;
            double b     = 0.2;
            double c     = 5.7;
            XYZ    p     = new XYZ(0, 0, 0);;
            XYZ    plast = new XYZ(0.1, 0, 0);

            double[] xDimRossler = new double[10000];
            int      index       = 0;

            for (int i = 0; i < 10100; i++)
            {
                p.x = plast.x + h * (-plast.y - plast.z);
                p.y = plast.y + h * (plast.x + a * plast.y);
                p.z = plast.z + h * (b + plast.z * (plast.x - c));
                if (i > 100)
                {
                    xDimRossler[index++] = p.x;
                }
                plast = p;
            }
            ChaosAnalysis analysis = new ChaosAnalysis();

            analysis.LoadSampledData(xDimRossler, DateTime.Now, new TimeSpan(0, 0, 1));
            analysis.sampleTime = new TimeSpan(0, 0, 1);
            Assert.IsTrue(analysis.CalculateMeasures(true, false, true));
            Assert.AreEqual(2, analysis.optimalEmbeddingDimension);
            Assert.AreEqual(1, analysis.optimalEmbeddingSeparation);
            Assert.AreEqual(0.82081, analysis.Hurst, 0.001);
            Assert.AreEqual(0.838, analysis.Lyapunov, 0.001);
        }
コード例 #3
0
        public void TestLorentz()
        {
            Trace.WriteLine("Lorenz");
            double h     = 0.01;
            double a     = 10.0;
            double b     = 28.0;
            double c     = 8.0 / 3.0;
            XYZ    p     = new XYZ(0, 0, 0);;
            XYZ    plast = new XYZ(0.1, 0, 0);

            double[] xDimLorentz = new double[10000];
            int      index       = 0;

            for (int i = 0; i < 10100; i++)
            {
                p.x = plast.x + h * a * (plast.y - plast.x);
                p.y = plast.y + h * (plast.x * (b - plast.z) - plast.y);
                p.z = plast.z + h * (plast.x * plast.y - c * plast.z);
                if (i > 100)
                {
                    xDimLorentz[index++] = p.x;
                }
                plast = p;
            }
            ChaosAnalysis analysis = new ChaosAnalysis();

            analysis.LoadSampledData(xDimLorentz, DateTime.Now, new TimeSpan(0, 0, 1));
            analysis.sampleTime = new TimeSpan(0, 0, 1);
            Assert.IsTrue(analysis.CalculateMeasures(true, false, true));
            Assert.AreEqual(2, analysis.optimalEmbeddingDimension);
            Assert.AreEqual(4, analysis.optimalEmbeddingSeparation);
            Assert.AreEqual(0.90129, analysis.Hurst, 0.001);
            Assert.AreEqual(0.0789, analysis.Lyapunov, 0.001);
        }
コード例 #4
0
        /// <summary>
        /// Test with a linear series differenced
        /// </summary>
        /// <remarks> This series continually extends its range.
        /// D1fferencing produces more accurate results.
        /// This is also an interesting test because all the differences are the same.
        /// It thus represents an extreme set of cirumstances that must be handled.</remarks>
        public void TestPredictLinearDifferenced()
        {
            double[] linear = new double[1000];
            linear[0] = 0.1;
            for (int n = 1; n < 1000; n++)
            {
                linear[n] = linear[n - 1] + 1.0;
            }
            ChaosAnalysis analysis = new ChaosAnalysis();

            analysis.differencing = true;
            TimeSpan sampleTime = new TimeSpan(0, 0, 1);

            analysis.LoadSampledData(linear, DateTime.Now, sampleTime);
            Assert.IsTrue(analysis.CalculateMeasures(true, false, true));
            linear[0] = linear[linear.Length - 1];
            double error = 0.0;

            for (int n = 1; n < 1000; n++)
            {
                linear[n] = linear[n - 1] + 1.0;
                double[] result = analysis.Predict(1, analysis.latest);
                analysis.AddDataPointRelative(linear[n], sampleTime);
                //we're predicting the difference, so relative to last
                double diff = linear[n] - (result[0] + linear[n - 1]);
                error += diff * diff;
            }
            error = Math.Sqrt(error / 1000);
            Assert.AreEqual(0.0, error, 0.01);
        }
コード例 #5
0
        public void TestTent()
        {
            Trace.WriteLine("Tent");
            double[] tent = new double[samples];
            tent[0] = 0.1;
            for (int n = 1; n < samples; n++)
            {
                if (tent[n - 1] < 0.5)
                {
                    tent[n] = 1.9 * tent[n - 1];
                }
                else
                {
                    tent[n] = 1.9 * (1 - tent[n - 1]);
                }
            }
            ChaosAnalysis analysis = new ChaosAnalysis();

            analysis.LoadSampledData(tent, DateTime.Now, new TimeSpan(0, 0, 1));
            analysis.sampleTime = new TimeSpan(0, 0, 1);
            Assert.IsTrue(analysis.CalculateMeasures(true, false, true));
            Assert.AreEqual(2, analysis.optimalEmbeddingDimension);
            Assert.AreEqual(1, analysis.optimalEmbeddingSeparation);
            Assert.AreEqual(0.4570, analysis.Hurst, 0.001);
            Assert.AreEqual(0.9018, analysis.Lyapunov, 0.001);
        }
コード例 #6
0
        public void TestTemporalDatabase()
        {
            double[] logistic = new double[1000];
            logistic[0] = 0.1;
            for (int n = 1; n < 1000; n++)
            {
                logistic[n] = 4.0 * logistic[n - 1] * (1.0 - logistic[n - 1]);
            }
            ChaosAnalysis analysis = new ChaosAnalysis();
            DateTime      start    = new DateTime(1997, 1, 1, 0, 0, 0, 0);

            analysis.sampleTime = new TimeSpan(0, 0, 1);
            analysis.LoadSampledData(logistic, start, new TimeSpan(0, 0, 1));
            Assert.AreEqual(start, analysis.earliest);
            Assert.AreEqual(start + new TimeSpan(9990000000), analysis.latest);
            Assert.AreEqual(1000, analysis.Count);
            Assert.AreEqual(logistic[0], analysis[0]);
            Assert.AreEqual(logistic[999], analysis[999]);
            Assert.AreEqual(logistic[599], analysis[599]);
            Assert.AreEqual(logistic[768], analysis[768]);
            //remove before start
            analysis.RemoveBefore(new DateTime(1997, 1, 1, 0, 0, 0, 0));
            Assert.AreEqual(start, analysis.earliest);
            Assert.AreEqual(start + new TimeSpan(9990000000), analysis.latest);
            //remove before fixed point
            DateTime newStart = new DateTime(1997, 1, 1, 0, 12, 0, 0);

            analysis.RemoveBefore(newStart);
            Assert.AreEqual(newStart, analysis.earliest);
            Assert.AreEqual(start + new TimeSpan(9990000000), analysis.latest);
            newStart = new DateTime(1997, 1, 1, 1, 12, 0, 0);
            analysis.RemoveBefore(newStart);
            Assert.AreEqual(DateTime.MinValue, analysis.earliest);
            Assert.AreEqual(DateTime.MaxValue, analysis.latest);
        }
コード例 #7
0
        public void TestMackayGlass()
        {
            Trace.WriteLine("MackayGlass");
            double[] mackayGlass = new double[10100];
            for (int n = 0; n < 18; n++)
            {
                mackayGlass[n] = 0.7;
            }
            for (int n = 19; n < 10100; n++)
            {
                mackayGlass[n] = 0.9 * mackayGlass[n - 1] + 0.2 * (mackayGlass[n - 18] / (1.0 + Math.Pow(mackayGlass[n - 18], 10)));
            }
            double[] mackayGlass2 = new double[10000];
            for (int n = 100; n < 10100; n++)
            {
                mackayGlass2[n - 100] = mackayGlass[n];
            }
            ChaosAnalysis analysis = new ChaosAnalysis();

            analysis.LoadSampledData(mackayGlass2, DateTime.Now, new TimeSpan(0, 0, 1));
            analysis.sampleTime = new TimeSpan(0, 0, 1);
            Assert.IsTrue(analysis.CalculateMeasures(true, false, true));
            Assert.AreEqual(2, analysis.optimalEmbeddingDimension);
            Assert.AreEqual(1, analysis.optimalEmbeddingSeparation);
            Assert.AreEqual(0.65412, analysis.Hurst, 0.005);
            Assert.AreEqual(1.07493, analysis.Lyapunov, 0.01);
        }
コード例 #8
0
        public void TestPredictLogisticMultiple()
        {
            double[] logistic = new double[1000];
            logistic[0] = 0.1;
            for (int n = 1; n < 1000; n++)
            {
                logistic[n] = 4.0 * logistic[n - 1] * (1.0 - logistic[n - 1]);
            }
            ChaosAnalysis analysis   = new ChaosAnalysis();
            TimeSpan      sampleTime = new TimeSpan(0, 0, 1);

            analysis.LoadSampledData(logistic, DateTime.Now, sampleTime);
            Assert.IsTrue(analysis.CalculateMeasures(true, false, true));
            logistic[0] = logistic[logistic.Length - 1];
            double error = 0.0;

            double[] result = analysis.Predict(10, analysis.latest);
            double[] errors = new double[10];
            for (int n = 1; n < 11; n++)
            {
                logistic[n] = 4.0 * logistic[n - 1] * (1.0 - logistic[n - 1]);
                double diff = logistic[n] - result[n - 1];
                error        += diff * diff;
                errors[n - 1] = Math.Sqrt(diff * diff);
            }
            error = Math.Sqrt(error / 10);
            Assert.AreEqual(0.00631, errors[0], 0.001);
            Assert.AreEqual(0.01522, errors[1], 0.001);
            Assert.AreEqual(0.01838, errors[2], 0.001);
            Assert.AreEqual(0.05689, errors[3], 0.001);
            Assert.AreEqual(0.08324, errors[4], 0.001);
            Assert.AreEqual(0.29876, error, 0.001);
        }
コード例 #9
0
        public void TestGetSampledPoint()
        {
            double[] linear = new double[1000];
            linear[0] = 0.1;
            for (int n = 1; n < 1000; n++)
            {
                linear[n] = linear[n - 1] + 1.0;
            }
            ChaosAnalysis analysis   = new ChaosAnalysis();
            TimeSpan      sampleTime = new TimeSpan(1, 0, 0);
            DateTime      start      = new DateTime(2000, 1, 1);

            analysis.LoadSampledData(linear, start, sampleTime);
            analysis.CalculateMeasures(true, false, true);
            DateTime offset1 = start + new TimeSpan(sampleTime.Ticks * 3 + sampleTime.Ticks / 2);

            Assert.AreEqual(3.1, analysis.GetSampledPoint(offset1));
            analysis.interpolation = true;
            Assert.AreEqual(3.6, analysis.GetSampledPoint(offset1), 0.01);
            DateTime offset2 = start - new TimeSpan(sampleTime.Ticks * 3 + sampleTime.Ticks / 2);

            Assert.AreEqual(0.0, analysis.GetSampledPoint(offset2), 0.01);
            DateTime offset3 = analysis.latest + new TimeSpan(sampleTime.Ticks * 3 + sampleTime.Ticks / 2);

            Assert.AreEqual(999.1, analysis.GetSampledPoint(offset3), 0.01);
        }
コード例 #10
0
        public void TestPartCrazy()
        {
            Trace.WriteLine("PartCrazy");
            double[] partCrazy = new double[samples];
            for (int n = 0; n < samples; n++)
            {
                partCrazy[n] = (n % 3) / 2;
            }
            ChaosAnalysis analysis = new ChaosAnalysis();

            analysis.LoadSampledData(partCrazy, DateTime.Now, new TimeSpan(0, 0, 1));
            analysis.sampleTime = new TimeSpan(0, 0, 1);
            Assert.IsTrue(analysis.CalculateMeasures(true, false, true));
            Assert.AreEqual(2, analysis.optimalEmbeddingDimension);
            Assert.AreEqual(1, analysis.optimalEmbeddingSeparation);
            Assert.AreEqual(0.0217, analysis.Hurst, 0.001);
            Assert.AreEqual(0.5, analysis.Lyapunov, 0.001);
        }
コード例 #11
0
        public void TestSine()
        {
            Trace.WriteLine("Sine");
            double[] sine = new double[samples];
            for (int n = 0; n < samples; n++)
            {
                sine[n] = Math.Sin(n * Math.PI / 180);
            }
            ChaosAnalysis analysis = new ChaosAnalysis();

            analysis.LoadSampledData(sine, DateTime.Now, new TimeSpan(0, 0, 1));
            analysis.sampleTime = new TimeSpan(0, 0, 1);
            Assert.IsTrue(analysis.CalculateMeasures(true, false, true));
            Assert.AreEqual(2, analysis.optimalEmbeddingDimension);
            Assert.AreEqual(2, analysis.optimalEmbeddingSeparation);
            Assert.AreEqual(0.997, analysis.Hurst, 0.001);
            Assert.AreEqual(0.384, analysis.Lyapunov, 0.001);
        }
コード例 #12
0
        public void TestLinear()
        {
            Trace.WriteLine("Linear");
            double[] linear = new double[samples];
            linear[0] = 0.1;
            for (int n = 1; n < samples; n++)
            {
                linear[n] = linear[n - 1] + 1.0;
            }
            ChaosAnalysis analysis = new ChaosAnalysis();

            analysis.LoadSampledData(linear, DateTime.Now, new TimeSpan(0, 0, 1));
            analysis.sampleTime = new TimeSpan(0, 0, 1);
            Assert.IsTrue(analysis.CalculateMeasures(true, false, true));
            Assert.AreEqual(2, analysis.optimalEmbeddingDimension);
            Assert.AreEqual(1, analysis.optimalEmbeddingSeparation);
            Assert.AreEqual(1.000, analysis.Hurst, 0.001);
            Assert.AreEqual(0.000, analysis.Lyapunov, 0.001);
        }
コード例 #13
0
        public void TestLogistic()
        {
            Trace.WriteLine("logistic");
            double[] logistic = new double[10000];
            logistic[0] = 0.1;
            for (int n = 1; n < 10000; n++)
            {
                logistic[n] = 4.0 * logistic[n - 1] * (1.0 - logistic[n - 1]);
            }
            ChaosAnalysis analysis = new ChaosAnalysis();

            analysis.LoadSampledData(logistic, DateTime.Now, new TimeSpan(0, 0, 1));
            analysis.sampleTime = new TimeSpan(0, 0, 1);
            Assert.IsTrue(analysis.CalculateMeasures(true, false, true));
            Assert.AreEqual(2, analysis.optimalEmbeddingDimension);
            Assert.AreEqual(1, analysis.optimalEmbeddingSeparation);
            Assert.AreEqual(0.55934, analysis.Hurst, 0.001);
            Assert.AreEqual(0.98410, analysis.Lyapunov, 0.001);
        }
コード例 #14
0
        public void TestRandom()
        {
            Trace.WriteLine("Random");
            double[] random = new double[samples];
            Random   rand   = new Random(1234);

            for (int n = 0; n < samples; n++)
            {
                random[n] = rand.NextDouble();
            }
            ChaosAnalysis analysis = new ChaosAnalysis();

            analysis.LoadSampledData(random, DateTime.Now, new TimeSpan(0, 0, 1));
            analysis.sampleTime = new TimeSpan(0, 0, 1);
            Assert.IsFalse(analysis.CalculateMeasures(true, false, true));
            Assert.AreEqual(2, analysis.optimalEmbeddingDimension);
            Assert.AreEqual(2, analysis.optimalEmbeddingSeparation);
            Assert.AreEqual(0.6283, analysis.Hurst, 0.001);
            Assert.AreEqual(2.94264, analysis.Lyapunov, 0.01);
        }
コード例 #15
0
        public void TestGetNextValidTradingTimes()
        {
            ChaosAnalysis analysis   = new ChaosAnalysis();
            TimeSpan      sampleTime = new TimeSpan(1, 0, 0);
            TradingTimes  tr         = new TradingTimes();

            tr.tradingDays = new bool[] { false, true, true, true, true, true, false };
            double[] linear = new double[1000];
            linear[0] = 0.1;
            for (int n = 1; n < 1000; n++)
            {
                linear[n] = linear[n - 1] + 1.0;
            }
            DateTime start = new DateTime(2000, 1, 1);

            analysis.LoadSampledData(linear, start, sampleTime);
            analysis.SortEvents();
            DateTime[] offsets = analysis.GetNextValidTradingTimes(9);
            Assert.AreEqual(9, offsets.Length);
        }
コード例 #16
0
        public void TestCrazy()
        {
            Trace.WriteLine("Crazy");
            double[] linear = new double[samples];
            linear[0] = 0.1;
            for (int n = 1; n < samples; n++)
            {
                if (n % 2 == 0)
                {
                    linear[n] = 0.1;
                }
                else
                {
                    linear[n] = 0.5;
                }
            }
            ChaosAnalysis analysis = new ChaosAnalysis();

            analysis.LoadSampledData(linear, DateTime.Now, new TimeSpan(0, 0, 1));
            analysis.sampleTime = new TimeSpan(0, 0, 1);
            Assert.IsTrue(analysis.CalculateMeasures(true, false, true));
            Assert.AreEqual(0.0000, analysis.Hurst, 0.001);
            Assert.AreEqual(0.000, analysis.Lyapunov, 0.001);
        }
コード例 #17
0
        public void TestTemporalDatabaseWithTaggedData()
        {
            double[]   logistic = new double[1000];
            DateTime[] times    = new DateTime[1000];

            DateTime start = new DateTime(1997, 1, 1, 0, 0, 0, 0);

            logistic[0] = 0.1;
            times[0]    = start;
            TimeSpan offset   = new TimeSpan(0, 1, 0);
            DateTime iterator = start;

            for (int n = 1; n < 1000; n++)
            {
                logistic[n] = 4.0 * logistic[n - 1] * (1.0 - logistic[n - 1]);
                iterator   += offset;
                times[n]    = iterator;
            }
            ChaosAnalysis analysis = new ChaosAnalysis();

            analysis.LoadTaggedData(logistic, times);
            analysis.sampleTime    = offset;
            analysis.interpolation = true;
            Assert.AreEqual(start, analysis.earliest);
            Assert.AreEqual(iterator, analysis.latest);
            Assert.AreEqual(1000, analysis.Count);
            Assert.AreEqual(logistic[0], analysis[0]);
            Assert.AreEqual(logistic[999], analysis[999]);
            Assert.AreEqual(logistic[599], analysis[599]);
            Assert.AreEqual(logistic[768], analysis[768]);
            //test interpolation
            TimeSpan testOfset = new TimeSpan((long)((double)offset.Ticks * 599.5));

            Assert.AreEqual((logistic[599] + logistic[600]) / 2.0, analysis[start + testOfset]);
            testOfset = new TimeSpan((long)((double)offset.Ticks * 200.5));
            Assert.AreEqual((logistic[200] + logistic[201]) / 2.0, analysis[start + testOfset]);
            //			Assert.ReferenceEquals(double.NaN,analysis[DateTime.MinValue]);
            Assert.AreEqual(logistic[999], analysis[DateTime.MaxValue]);

            //remove before start
            analysis.RemoveBefore(new DateTime(1997, 1, 1, 0, 0, 0, 0));
            Assert.AreEqual(start, analysis.earliest);
            Assert.AreEqual(iterator, analysis.latest);
            //remove before fixed point
            DateTime newStart = new DateTime(1997, 1, 1, 0, 12, 0, 0);

            analysis.RemoveBefore(newStart);
            Assert.AreEqual(newStart, analysis.earliest);
            Assert.AreEqual(iterator, analysis.latest);
            newStart = new DateTime(1997, 1, 1, 17, 0, 0, 0);
            analysis.RemoveBefore(newStart);
            Assert.AreEqual(DateTime.MinValue, analysis.earliest);
            Assert.AreEqual(DateTime.MaxValue, analysis.latest);

            //now test unequally spaced events
            double[] data = new double[500];
            times    = new DateTime[500];
            iterator = start;
            for (int n = 0; n < 500; n++)
            {
                data[n]  = (double)n;
                times[n] = iterator;
                TimeSpan testOffset = new TimeSpan((long)((double)offset.Ticks * ((double)n / 100 + 1)));
                iterator += testOffset;
            }
            analysis = new ChaosAnalysis();
            analysis.LoadTaggedData(data, times);
            analysis.sampleTime    = offset;
            analysis.interpolation = true;
            Assert.AreEqual(start, analysis.earliest);
            Assert.AreEqual(1742, analysis.Count);//This is not the count of events, but the count of the number of samples in the database, based on the sample time.
            //			Assert.AreEqual(data[0],analysis[times[0]]);
            Assert.AreEqual(data[199], analysis[times[199]]);
            Assert.AreEqual(data[299], analysis[times[299]]);
            Assert.AreEqual(data[468], analysis[times[468]]);
            //test interpolation
            DateTime testtime = new DateTime((times[199].Ticks + times[200].Ticks) / 2);//halfway between two samples

            Assert.AreEqual((data[199] + data[200]) / 2.0, analysis[testtime]);
            testtime = new DateTime((times[303].Ticks + times[304].Ticks) / 2);//halfway between two samples
            Assert.AreEqual((data[303] + data[304]) / 2.0, analysis[testtime], 0.000001);
        }