예제 #1
0
        public void ZipLagIssue11Test()
        {
            OptimizationSettings.CombineFilterMapDelegates = true;

            var data = new SortedMap <DateTime, double>();

            var count = 5000;

            for (int i = 0; i < count; i++)
            {
                data.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
            }

            var sma = data.SMA(20, true).Lag(1u);

            Console.WriteLine($"laggedSma count: {sma.Count()}");

            var deviation = sma;

            // this line or any other intermediate enumeration affect the last line
            Console.WriteLine($"deviation count: {deviation.Count()}");

            var direction = deviation;//.Map(x => (Math.Sign(x)));

            Console.WriteLine($"direction count: {direction.Count()}");

            var diff = direction.ZipLag(1u, (c, p) => c - p); //.ToSortedMap();

            Console.WriteLine($"Count: {diff.Count()}");

            Assert.IsTrue(diff.Count() > 0);
        }
예제 #2
0
        public void CouldCalculateSMAInRealTime() {
            var sm = new SortedMap<int, double>();

            Task.Run(async () => {

                for (int i = 0; i < 20; i++) {
                    sm.Add(i, i);
                }

                await Task.Delay(100);

                for (int i = 20; i < 100; i++) {
                    await Task.Delay(1); // 15 msec
                    sm.Add(i, i);
                }
                sm.IsMutable = false;
            });


            var sma = sm.SMA(10, true);

            var c = sma.GetCursor();

            while (c.MoveNext(CancellationToken.None).Result) {
                Console.WriteLine("Key: {0}, value: {1}", c.CurrentKey, c.CurrentValue);
            }

        }
예제 #3
0
        public void CouldCalculateSMAInRealTime()
        {
            var sm = new SortedMap <int, double>();

            Task.Run(async() => {
                for (int i = 0; i < 20; i++)
                {
                    sm.Add(i, i);
                }

                await Task.Delay(100);

                for (int i = 20; i < 100; i++)
                {
                    await Task.Delay(1); // 15 msec
                    sm.Add(i, i);
                }
                sm.Complete();
            });


            var sma = sm.SMA(10, true);

            var c = sma.GetCursor();

            while (c.MoveNext(CancellationToken.None).Result)
            {
                Console.WriteLine("Key: {0}, value: {1}", c.CurrentKey, c.CurrentValue);
            }
        }
예제 #4
0
        public void CouldCalculateIncompleteMovingAverage()
        {
            var sm = new SortedMap <int, double>();

            for (int i = 0; i < 20; i++)
            {
                sm.Add(i, i);
            }

            var sma = sm.SMA(2, true).ToSortedMap();

            var c = 0;

            foreach (var kvp in sma)
            {
                if (c == 0)
                {
                    Assert.AreEqual(c, kvp.Value);
                }
                else
                {
                    Assert.AreEqual(0.5 * (c + (double)(c - 1)), kvp.Value);
                }

                c++;
            }
        }
예제 #5
0
        public void CouldCalculateMovingAverage()
        {
            var sm = new SortedMap <DateTime, double>();

            var count = 1000000;

            for (int i = 0; i < count; i++)
            {
                sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
            }

            // slow implementation
            var sw = new Stopwatch();

            sw.Start();
            var ma = sm.SMA(20); //.ToSortedMap();

            var cursor = ma.GetCursor();

            cursor.MoveNext();
            //var cc = 0;
            //while (cursor.MoveNext())
            //{
            //    cc++;
            //}
            //Console.WriteLine(cc);
            //if (cursor.MoveNext())
            //{
            //    throw new ApplicationException("Moved next after MoveNext() returned false");
            //}
            //cursor.MoveFirst();
            var c = 1;

            //foreach (var m in ma) {
            cursor.Reset();
            while (cursor.MoveNext())
            {
                if (cursor.CurrentValue != c + 8.5)
                {
                    Console.WriteLine(cursor.CurrentValue);// m.Value);
                    Console.WriteLine($"Error c: {c}");
                    throw new ApplicationException("Invalid value");
                }
                c++;
                if (c == 999982)
                {
                    Console.WriteLine("Catch me");
                }
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("SMA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
            ma = null;

            GC.Collect(3, GCCollectionMode.Forced, true);
            Thread.Sleep(2000);
            // NB! In release mode this must print that ToSortedMap task exited, in Debug mode GC does not collect SM and weak reference stays alive
        }
예제 #6
0
        public void SmaDeviationTest()
        {
            var data = new SortedMap <DateTime, double>();

            for (int i = 1; i < 101; i++)
            {
                data.Add(DateTime.UtcNow.Date.AddMinutes(i), i);
            }

            var sma          = data.SMA(20, true);
            var deviation    = (data / sma - 1);
            var deviationSm  = deviation;
            var smaDirection = deviation.Map(Math.Sign);

            Assert.AreEqual(100, smaDirection.Count());
            Assert.AreEqual(100, deviation.Count());
        }
예제 #7
0
        public void CouldCalculateIncompleteMovingAverage() {
            var sm = new SortedMap<int, double>();
            for (int i = 0; i < 20; i++) {
                sm.Add(i, i);
            }

            var sma = sm.SMA(2, true).ToSortedMap();

            var c = 0;
            foreach (var kvp in sma) {
                if (c == 0) {
                    Assert.AreEqual(c, kvp.Value);
                } else {
                    Assert.AreEqual(0.5 * (c + (double)(c - 1)), kvp.Value);
                }

                c++;
            }

        }
예제 #8
0
        public void ZipLagIssue11Test()
        {
            OptimizationSettings.CombineFilterMapDelegates = true;

            var data = new SortedMap <int, double>();

            var count = 5000;

            for (int i = 0; i < count; i++)
            {
                data.Add(i, i);
            }

            var sma = data.SMA(20, true).ZipLag(1u, (c, p) => p).Lag(1u);

            Console.WriteLine($"laggedSma count: {sma.Count()}");

            var deviation = sma.StDev(10);
            var e         = (deviation as IEnumerable <KeyValuePair <int, double> >).GetEnumerator();
            var cnt       = 0;

            while (e.MoveNext())
            {
                cnt++;
            }
            Console.WriteLine(cnt);
            // this line or any other intermediate enumeration affect the last line
            Console.WriteLine($"deviation count: {deviation.Count()}");

            var direction = deviation;//.Map(x => (Math.Sign(x)));

            Console.WriteLine($"direction count: {direction.Count()}");

            var diff = direction.ZipLag(1u, (c, p) => c - p); //.ToSortedMap();

            Console.WriteLine($"Count: {diff.Count()}");

            Assert.IsTrue(diff.Count() > 0);
        }
예제 #9
0
        public void NestedSMABenchmark()
        {
            var count = 100000;

            #region Create data

            var sm = new SortedMap <int, double>();
            sm.Add(0, 0); // make irregular, it's faster but more memory
            for (int i = 2; i <= count; i++)
            {
                sm.Add(i, i);
            }

            #endregion Create data

            for (int round = 0; round < 20; round++)
            {
                var window = sm.SMA(20).SMA(20).SMA(20).SMA(20).SMA(20);

                var c = 0;
                using (Benchmark.Run("SMA", count))
                {
                    foreach (var keyValuePair in window)
                    {
                        if (keyValuePair.Key >= 0)
                        {
                            c++;
                        }
                    }
                }

                if (c < 0)
                {
                    Console.WriteLine($"Count: {c}");
                }
            }

            Benchmark.Dump("MOPS is scaled by x1M, so it is OPS");
        }
예제 #10
0
        public void CouldCalculateMovingAverageIncomplete()
        {
            var sm = new SortedMap <DateTime, double>();

            var count = 100000;

            for (int i = 0; i < count; i++)
            {
                sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
            }

            // slow implementation
            var sw = new Stopwatch();

            sw.Start();
            var ma = sm.SMA(20, true); //.ToSortedMap();
            var c  = 1;

            foreach (var m in ma)
            {
                if (c <= 20)
                {
                    //Console.WriteLine(m.Value);
                }
                else if (m.Value != c - 19 + 8.5)
                {
                    Console.WriteLine(m.Value);
                    throw new ApplicationException("Invalid value");
                }
                c++;
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("SMA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
            //
        }
예제 #11
0
        public void ZipLagIssue11Test() {
            OptimizationSettings.CombineFilterMapDelegates = true;

            var data = new SortedMap<DateTime, double>();

            var count = 5000;

            for (int i = 0; i < count; i++) {
                data.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
            }

            var sma = data.SMA(20, true).Lag(1u);
            Console.WriteLine($"laggedSma count: {sma.Count()}");

            var deviation = sma;

            // this line or any other intermediate enumeration affect the last line
            Console.WriteLine($"deviation count: {deviation.Count()}");

            var direction = deviation;//.Map(x => (Math.Sign(x)));

            Console.WriteLine($"direction count: {direction.Count()}");

            var diff = direction.ZipLag(1u, (c, p) => c - p); //.ToSortedMap();

            Console.WriteLine($"Count: {diff.Count()}");

            Assert.IsTrue(diff.Count() > 0);
        }
예제 #12
0
        public void CouldCalculateMovingAverageIncomplete() {
            var sm = new SortedMap<DateTime, double>();

            var count = 100000;

            for (int i = 0; i < count; i++) {
                sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
            }

            // slow implementation
            var sw = new Stopwatch();
            sw.Start();
            var ma = sm.SMA(20, true); //.ToSortedMap();
            var c = 1;
            foreach (var m in ma) {
                if (c <= 20) {
                    //Console.WriteLine(m.Value);
                } else if (m.Value != c - 19 + 8.5) {
                    Console.WriteLine(m.Value);
                    throw new ApplicationException("Invalid value");
                }
                c++;
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("SMA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
            // 
        }
예제 #13
0
        public void CouldCalculateMovingAverage() {
            var sm = new SortedMap<DateTime, double>();

            var count = 1000000;

            for (int i = 0; i < count; i++) {
                sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
            }

            // slow implementation
            var sw = new Stopwatch();
            sw.Start();
            var ma = sm.SMA(20); //.ToSortedMap();

            var cursor = ma.GetCursor();
            cursor.MoveNext();
            //var cc = 0;
            //while (cursor.MoveNext())
            //{
            //    cc++;
            //}
            //Console.WriteLine(cc);
            //if (cursor.MoveNext())
            //{
            //    throw new ApplicationException("Moved next after MoveNext() returned false");
            //}
            //cursor.MoveFirst();
            var c = 1;
            //foreach (var m in ma) {
            cursor.Reset();
            while (cursor.MoveNext()) {
                if (cursor.CurrentValue != c + 8.5)
                {
                    Console.WriteLine(cursor.CurrentValue);// m.Value);
                    Console.WriteLine($"Error c: {c}");
                    throw new ApplicationException("Invalid value");
                }
                c++;
                if (c == 999982)
                {
                    Console.WriteLine("Catch me");
                }
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("SMA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
            ma = null;
            
            GC.Collect(3, GCCollectionMode.Forced, true);
            Thread.Sleep(2000);
            // NB! In release mode this must print that ToSortedMap task exited, in Debug mode GC does not collect SM and weak reference stays alive
             
        }
예제 #14
0
        public void SmaDeviationTest()
        {
            var data = new SortedMap<DateTime, double>();
            for (int i = 1; i < 101; i++)
            {
                data.Add(DateTime.UtcNow.Date.AddMinutes(i), i);
            }

            var sma = data.SMA(20, true);
            var deviation = (data/sma - 1);
            var deviationSm = deviation;
            var smaDirection = deviation.Map(Math.Sign);
            Assert.AreEqual(100, smaDirection.Count());
            Assert.AreEqual(100, deviation.Count());
            
        }
예제 #15
0
        public void SMADirectAndIndirectSpanOpBenchmark()
        {
            Settings.DoAdditionalCorrectnessChecks = false;

            var count = 1000000;
            var width = 20;
            var sm    = new SortedMap <int, double>();

            sm.Add(0, 0); // make irregular, it's faster but more memory
            for (int i = 2; i <= count; i++)
            {
                sm.Add(i, i);
            }

            var directSMA =
                new SpanOpImpl <int,
                                double,
                                double,
                                MAvgCount <int, double, SortedMapCursor <int, double> >,
                                SortedMapCursor <int, double>
                                >(sm.GetEnumerator(), new MAvgCount <int, double, SortedMapCursor <int, double> >(width, false)).Source;

            var indirectSma =
                new SpanOpImpl <int,
                                double,
                                double,
                                SpanOpCount <int, double, double, SortedMapCursor <int, double>, SumAvgOnlineOp <int, double, SortedMapCursor <int, double> > >,
                                SortedMapCursor <int, double>
                                >(sm.GetEnumerator(),
                                  new SpanOpCount <int, double, double, SortedMapCursor <int, double>, SumAvgOnlineOp <int, double, SortedMapCursor <int, double> > >(width, false, new SumAvgOnlineOp <int, double, SortedMapCursor <int, double> >())).Source;

            var indirectSmaCombined =
                new SpanOpImpl <int,
                                double,
                                double,
                                SpanOp <int, double, double, SortedMapCursor <int, double>, SumAvgOnlineOp <int, double, SortedMapCursor <int, double> > >,
                                SortedMapCursor <int, double>
                                >(sm.GetEnumerator(),
                                  new SpanOp <int, double, double, SortedMapCursor <int, double>, SumAvgOnlineOp <int, double, SortedMapCursor <int, double> > >(width, false, new SumAvgOnlineOp <int, double, SortedMapCursor <int, double> >(), sm.comparer)).Source;

            var extensionSma = sm.SMA(width);

            var windowSma = sm.Window(width).Map(x =>
            {
                var sum = 0.0;
                var c   = 0;
                foreach (var keyValuePair in x)
                {
                    sum += keyValuePair.Value;
                    c++;
                }
                return(sum / c); // x.CursorDefinition.Count TODO
            });

            for (int round = 0; round < 20; round++)
            {
                double sum1 = 0.0;
                using (Benchmark.Run("SMA Direct", count * width))
                {
                    foreach (var keyValuePair in directSMA)
                    {
                        sum1 += keyValuePair.Value;
                    }
                }

                double sum2 = 0.0;
                using (Benchmark.Run("SMA Indirect", count * width))
                {
                    foreach (var keyValuePair in indirectSma)
                    {
                        sum2 += keyValuePair.Value;
                    }
                }

                double sum2_2 = 0.0;
                using (Benchmark.Run("SMA Indirect Combined", count * width))
                {
                    foreach (var keyValuePair in indirectSmaCombined)
                    {
                        sum2_2 += keyValuePair.Value;
                    }
                }

                double sum2_3 = 0.0;
                using (Benchmark.Run("SMA Extension", count * width))
                {
                    foreach (var keyValuePair in extensionSma)
                    {
                        sum2_3 += keyValuePair.Value;
                    }
                }

                double sum3 = 0.0;
                using (Benchmark.Run("SMA Window", count * width))
                {
                    foreach (var keyValuePair in windowSma)
                    {
                        sum3 += keyValuePair.Value;
                    }
                }

                var sumxx = 0.0;
                using (Benchmark.Run("SortedMap", count))
                {
                    foreach (var keyValuePair in sm)
                    {
                        sumxx += keyValuePair.Value;
                    }
                }

                Assert.AreEqual(sum1, sum2);
                Assert.AreEqual(sum1, sum2_2);
                Assert.AreEqual(sum1, sum2_3);
                Assert.AreEqual(sum1, sum3);
            }

            Benchmark.Dump($"The window width is {width}. SMA MOPS are calculated as a number of calculated values multiplied by width, " +
                           $"which is equivalent to the total number of cursor moves for Window case. SortedMap line is for reference - it is the " +
                           $"speed of raw iteration over SM without Windows overheads.");
        }