Beispiel #1
0
        public void ChangePointDetectionWithSeasonality()
        {
            using (var env = new ConsoleEnvironment(conc: 1))
            {
                const int ChangeHistorySize         = 10;
                const int SeasonalitySize           = 10;
                const int NumberOfSeasonsInTraining = 5;
                const int MaxTrainingSize           = NumberOfSeasonsInTraining * SeasonalitySize;

                List <Data> data     = new List <Data>();
                var         dataView = env.CreateStreamingDataView(data);

                var args = new SsaChangePointDetector.Arguments()
                {
                    Confidence          = 95,
                    Source              = "Value",
                    Name                = "Change",
                    ChangeHistoryLength = ChangeHistorySize,
                    TrainingWindowSize  = MaxTrainingSize,
                    SeasonalWindowSize  = SeasonalitySize
                };

                for (int j = 0; j < NumberOfSeasonsInTraining; j++)
                {
                    for (int i = 0; i < SeasonalitySize; i++)
                    {
                        data.Add(new Data(i));
                    }
                }

                for (int i = 0; i < ChangeHistorySize; i++)
                {
                    data.Add(new Data(i * 100));
                }

                // Train
                var detector = new SsaChangePointEstimator(env, args).Fit(dataView);
                // Transform
                var output = detector.Transform(dataView);
                // Get predictions
                var           enumerator     = output.AsEnumerable <Prediction>(env, true).GetEnumerator();
                Prediction    row            = null;
                List <double> expectedValues = new List <double>()
                {
                    0, -3.31410598754883, 0.5, 5.12000000000001E-08, 0, 1.5700820684432983, 5.2001145245395008E-07,
                    0.012414560443710681, 0, 1.2854313254356384, 0.28810801662678009, 0.02038940454467935, 0, -1.0950627326965332, 0.36663890634019225, 0.026956459625565483
                };

                int index = 0;
                while (enumerator.MoveNext() && index < expectedValues.Count)
                {
                    row = enumerator.Current;
                    Assert.Equal(expectedValues[index++], row.Change[0], precision: 7);  // Alert
                    Assert.Equal(expectedValues[index++], row.Change[1], precision: 7);  // Raw score
                    Assert.Equal(expectedValues[index++], row.Change[2], precision: 7);  // P-Value score
                    Assert.Equal(expectedValues[index++], row.Change[3], precision: 7);  // Martingale score
                }
            }
        }
        public void ChangePointDetectionWithSeasonality()
        {
            var       env = new MLContext(1);
            const int changeHistorySize         = 10;
            const int seasonalitySize           = 10;
            const int numberOfSeasonsInTraining = 5;
            const int maxTrainingSize           = numberOfSeasonsInTraining * seasonalitySize;

            List <Data> data     = new List <Data>();
            var         dataView = env.Data.LoadFromEnumerable(data);

            var args = new SsaChangePointDetector.Options()
            {
                Confidence          = 95,
                Source              = "Value",
                Name                = "Change",
                ChangeHistoryLength = changeHistorySize,
                TrainingWindowSize  = maxTrainingSize,
                SeasonalWindowSize  = seasonalitySize
            };

            for (int j = 0; j < numberOfSeasonsInTraining; j++)
            {
                for (int i = 0; i < seasonalitySize; i++)
                {
                    data.Add(new Data(i));
                }
            }

            for (int i = 0; i < changeHistorySize; i++)
            {
                data.Add(new Data(i * 100));
            }

            // Train
            var detector = new SsaChangePointEstimator(env, args).Fit(dataView);
            // Transform
            var output = detector.Transform(dataView);
            // Get predictions
            var        enumerator = env.Data.CreateEnumerable <Prediction>(output, true).GetEnumerator();
            Prediction row        = null;

            // [TEST_STABILITY]: dotnet core 3.1 generates slightly different result
#if NETCOREAPP3_1
            List <double> expectedValues = new List <double>()
            {
                0, -3.31410551071167, 0.5, 5.12000000000001E-08, 0, 1.570083498954773, 5.2001145245395008E-07,
                0.012414560443710681, 0, 1.2854313850402832, 0.2881081472302483, 0.020389485008225454, 0, -1.0950632095336914, 0.3666388047550645, 0.02695657272695535
            };
#else
            List <double> expectedValues = new List <double>()
            {
                0, -3.31410598754883, 0.5, 5.12000000000001E-08, 0, 1.5700820684432983, 5.2001145245395008E-07,
                0.012414560443710681, 0, 1.2854313254356384, 0.28810801662678009, 0.02038940454467935, 0, -1.0950627326965332, 0.36663890634019225, 0.026956459625565483
            };
#endif

            int index = 0;
            while (enumerator.MoveNext() && index < expectedValues.Count)
            {
                row = enumerator.Current;
                Assert.Equal(expectedValues[index++], row.Change[0], precision: 7);  // Alert
                Assert.Equal(expectedValues[index++], row.Change[1], precision: 7);  // Raw score
                Assert.Equal(expectedValues[index++], row.Change[2], precision: 7);  // P-Value score
                Assert.Equal(expectedValues[index++], row.Change[3], precision: 7);  // Martingale score
            }
        }