public void HiddenMarkovModelMultivariateGaussianDistribution_ModelState_ModelCreated()
        {
            var pi = new double[NumberOfStates] {
                0.5, 0.5
            };
            var tpm       = new double[NumberOfStates][] { new [] { 0.5, 0.5 }, new [] { 0.5, 0.5 } };
            var util      = new TestDataUtils();
            var series    = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));
            var emissions = CreateEmissions(series, NumberOfStates);

            var model = HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <IMultivariateDistribution>()
            {
                Pi = pi, TransitionProbabilityMatrix = tpm, Emissions = emissions
            });                                                                                                                                                                           //new HiddenMarkovModelMultivariateGaussianDistribution(pi, tpm, emissions);

            Assert.AreEqual(ModelType.Ergodic, model.Type);
            Assert.AreEqual(NumberOfStates, model.N);
            Assert.IsFalse(model.Normalized);
            for (int n = 0; n < NumberOfStates; n++)
            {
                Assert.AreEqual(0.5, model.Pi[n]);
                Assert.IsNotNull(model.Emission[n]);
                Assert.IsInstanceOfType(model.Emission[n], typeof(NormalDistribution));
                for (int i = 0; i < NumberOfStates; i++)
                {
                    Assert.AreEqual(0.5, model.TransitionProbabilityMatrix[n][i]);
                }
            }
        }
コード例 #2
0
        public void CreateHiddenMarkovModelTest()
        {
            var n = 3;
            var m = 3;
            var startProbabilityVector = new double[] { 0.5, 0.5, 0 };
            var tpm = new double[n][];

            tpm[0] = new double[] { 0, 1 / 3, 2 / 3 };
            tpm[1] = new double[] { 1 / 3, 0, 2 / 3 };
            tpm[2] = new double[] { 1 / 3, 1 / 3, 1 / 3 };

            var emissions = new DiscreteDistribution[n];

            emissions[0] = new DiscreteDistribution(new double[] { 1, 2, 3 }, new[] { 0.1, 0.4, 0.5 });
            emissions[1] = new DiscreteDistribution(new double[] { 1, 2, 3 }, new[] { 0.6, 0.3, 0.1 });
            emissions[2] = new DiscreteDistribution(new double[] { 1, 2, 3 }, new[] { 0.6, 0.3, 0.1 });
            var symbols = new List <Observation>();

            symbols.Add(new Observation(new double[] { 1 }, "1"));
            symbols.Add(new Observation(new double[] { 2 }, "2"));
            symbols.Add(new Observation(new double[] { 3 }, "3"));

            var hmm = HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <DiscreteDistribution>()
            {
                Pi = startProbabilityVector, TransitionProbabilityMatrix = tpm, Emissions = emissions
            });                                                                                                                                                                                        //new HiddenMarkovModel(startProbabilityVector, tpm, emissions);

            Assert.AreEqual(hmm.N, n);
            Assert.AreEqual(hmm.TransitionProbabilityMatrix.Length, n);
            Assert.AreEqual(hmm.Emission.Length, n);
            Assert.AreEqual(hmm.Pi.Length, m);

            //Assert.AreEqual(hmm.NumberOfObservations, m);
            //Assert.AreEqual(hmm.Symbols.Count, m);
        }
コード例 #3
0
ファイル: BeamLikeSearchTest.cs プロジェクト: xg86/HmmDotNet
        public void Search_OneNodeAndKis3AndNis10_3Nodes()
        {
            var util   = new TestDataUtils();
            var crf    = new ChangeRatioFinder();
            var search = new BeamLikeSearch();

            var series     = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));
            var ratios     = crf.GetMaximumChangeRatios(series);
            var population = new double[1][]
            {
                series[series.Length]
            };
            var k = 3;
            var n = 10;
            var numberOfIterations = 10;

            var model = (HiddenMarkovModelMixtureDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfComponents = _NumberOfComponents, NumberOfStates = _NumberOfStates
            });

            model.Normalized = true;
            model.Train(series, _NumberOfIterations, _LikelihoodTolerance);


            var result = search.Search(population, k, n, numberOfIterations, ratios, model);

            Assert.IsNull(result);
        }
コード例 #4
0
        public void Predict_HMMMultivariateAndFTSESeriesLength20_Predicted20Days()
        {
            var util   = new TestDataUtils();
            var series = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));
            var test   = util.GetSvcData(util.FTSEFilePath, new DateTime(2011, 12, 18), new DateTime(2012, 01, 18));

            var model = (HiddenMarkovModelMultivariateGaussianDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <IMultivariateDistribution>()
            {
                NumberOfStates = _NumberOfStates
            });

            model.Normalized = true;
            model.Train(series, _NumberOfIterations, _LikelihoodTolerance);

            var pred    = new ViterbiBasedPredictor();
            var request = new PredictionRequest {
                TrainingSet = series, NumberOfDays = 20, TestSet = test
            };

            pred.NumberOfIterations  = _NumberOfIterations;
            pred.LikelihoodTolerance = _LikelihoodTolerance;
            var predictions = pred.Predict(model, request);

            Assert.AreEqual(20, predictions.Predicted.Length);
        }
コード例 #5
0
        public void HiddenMarkovModelMixtureDistribution_NumberOfStatesAndDelta_RightLeftModelCreated()
        {
            var model = HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfComponents = _NumberOfComponents, NumberOfStates = _NumberOfStatesRightLeft, Delta = _Delta
            });                                                                                                                                                                                                                      //new HiddenMarkovModelMixtureDistribution(NumberOfComponents, NumberOfStatesRightLeft, Delta);

            Assert.AreEqual(ModelType.LeftRight, model.Type);
            Assert.AreEqual(_NumberOfStatesRightLeft, model.N);
            Assert.IsFalse(model.Normalized);
            Assert.IsNull(model.Emission);
            Assert.AreEqual(1d, model.Pi[0]);
            Assert.AreEqual(0.25, model.TransitionProbabilityMatrix[0][0]);
            Assert.AreEqual(0.25, model.TransitionProbabilityMatrix[0][1]);
            Assert.AreEqual(0.25, model.TransitionProbabilityMatrix[0][2]);
            Assert.AreEqual(0.25, model.TransitionProbabilityMatrix[0][3]);
            Assert.AreEqual(0d, model.TransitionProbabilityMatrix[1][0]);
            Assert.AreEqual(1d / 3d, model.TransitionProbabilityMatrix[1][1]);
            Assert.AreEqual(1d / 3d, model.TransitionProbabilityMatrix[1][2]);
            Assert.AreEqual(1d / 3d, model.TransitionProbabilityMatrix[1][3]);
            Assert.AreEqual(0, model.TransitionProbabilityMatrix[2][0]);
            Assert.AreEqual(0, model.TransitionProbabilityMatrix[2][1]);
            Assert.AreEqual(0.5, model.TransitionProbabilityMatrix[2][2]);
            Assert.AreEqual(0.5, model.TransitionProbabilityMatrix[2][3]);
            Assert.AreEqual(0, model.TransitionProbabilityMatrix[3][0]);
            Assert.AreEqual(0, model.TransitionProbabilityMatrix[3][1]);
            Assert.AreEqual(0, model.TransitionProbabilityMatrix[3][2]);
            Assert.AreEqual(1d, model.TransitionProbabilityMatrix[3][3]);
        }
コード例 #6
0
        public void GetModel_DiscreteDistributionParameters_DiscreteDistributionStateCreated()
        {
            var parameters = new ModelCreationParameters <DiscreteDistribution>()
            {
                NumberOfStates = NumberOfStates
            };
            var model = HiddenMarkovModelFactory.GetModel(parameters);

            Assert.IsInstanceOfType(model, typeof(HmmDotNet.MachineLearning.HiddenMarkovModel));
        }
コード例 #7
0
        public void GetModel_UnivariateAndNormalDistributionParameters_UnivariateAndNormalDistributionStateCreated()
        {
            var parameters = new ModelCreationParameters <HmmDotNet.Statistics.Distributions.Univariate.NormalDistribution>()
            {
                NumberOfStates = NumberOfStates
            };
            var model = HiddenMarkovModelFactory.GetModel(parameters);

            Assert.IsInstanceOfType(model, typeof(HiddenMarkovModelGaussianDistribution));
        }
コード例 #8
0
        public void GetModel_MixtureWithIMultivariateDistributionParameters_MixtureWithIMultivariateDistributionStateCreated()
        {
            var parameters = new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfStates = NumberOfStates
            };
            var model = HiddenMarkovModelFactory.GetModel(parameters);

            Assert.IsInstanceOfType(model, typeof(HiddenMarkovModelMixtureDistribution));
        }
コード例 #9
0
        public void Run_DefaultModelAndObservagtions_TrainedModel()
        {
            var startDistribution = new[] { 0.85, 0.15 };
            // s = 0, t = 1
            var tpm = new double[2][];

            tpm[0] = new[] { 0.3, 0.7 };
            tpm[1] = new[] { 0.1, 0.9 };

            var observations = new List <IObservation>
            {
                new Observation(new double[] { 0 }, "A"),
                new Observation(new double[] { 1 }, "B"),
                new Observation(new double[] { 1 }, "B"),
                new Observation(new double[] { 0 }, "A")
            };

            var emissions = new DiscreteDistribution[2];

            emissions[0] = new DiscreteDistribution(new double[] { 0, 1 }, new[] { 0.4, 0.6 });
            emissions[1] = new DiscreteDistribution(new double[] { 0, 1 }, new[] { 0.5, 0.5 });

            var symbols = new List <IObservation> {
                new Observation(new double[] { 0 }, "A"), new Observation(new double[] { 1 }, "B")
            };

            var model = HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <DiscreteDistribution>()
            {
                Pi = startDistribution, TransitionProbabilityMatrix = tpm, Emissions = emissions
            });                                                                                                                                                                                     //new HiddenMarkovModel(startDistribution, tpm, emissions) { LogNormalized = false };

            model.Normalized = false;
            var algo = new BaumWelch(observations, model, symbols);
            var res  = algo.Run(100, LikelihoodTolerance);

            Assert.AreEqual(0.8258482510939813, res.Pi[0]);
            Assert.AreEqual(0.17415174890601867, res.Pi[1]);

            Assert.AreEqual(0.330050127737348, res.TransitionProbabilityMatrix[0][0]);
            Assert.AreEqual(0.669949872262652, res.TransitionProbabilityMatrix[0][1]);
            Assert.AreEqual(0.098712289730350428, res.TransitionProbabilityMatrix[1][0]);
            Assert.AreEqual(0.90128771026964949, res.TransitionProbabilityMatrix[1][1]);

            Assert.AreEqual(0.65845050146912831, res.Emission[0].ProbabilityMassFunction(0));
            Assert.AreEqual(0.34154949853087169, res.Emission[0].ProbabilityMassFunction(1));
            Assert.AreEqual(0.4122484947955643, res.Emission[1].ProbabilityMassFunction(0));
            Assert.AreEqual(0.58775150520443575, res.Emission[1].ProbabilityMassFunction(1));

            Assert.AreEqual(0.0770055392812707, res.Likelihood);
            Assert.AreEqual(1d, res.Pi.Sum());
            Assert.AreEqual(1d, res.TransitionProbabilityMatrix[0].Sum());
            Assert.AreEqual(1d, Math.Round(res.TransitionProbabilityMatrix[1].Sum(), 5));
            Assert.AreEqual(1d, res.Emission[0].ProbabilityMassFunction(0) + res.Emission[0].ProbabilityMassFunction(1));
            Assert.AreEqual(1d, res.Emission[1].ProbabilityMassFunction(0) + res.Emission[1].ProbabilityMassFunction(1));
        }
コード例 #10
0
        public void Run_DefaultModelAndObservagtionsAndNormalized_TrainedMode()
        {
            var startDistribution = new[] { 0.85, 0.15 };

            var tpm = new double[2][];

            tpm[0] = new[] { 0.3, 0.7 };
            tpm[1] = new[] { 0.1, 0.9 };

            var observations = new List <IObservation>
            {
                new Observation(new double[] { 0 }, "A"),
                new Observation(new double[] { 1 }, "B"),
                new Observation(new double[] { 1 }, "B"),
                new Observation(new double[] { 0 }, "A")
            };

            var emissions = new DiscreteDistribution[2];

            emissions[0] = new DiscreteDistribution(new double[] { 0, 1 }, new[] { 0.4, 0.6 });
            emissions[1] = new DiscreteDistribution(new double[] { 0, 1 }, new[] { 0.5, 0.5 });

            var symbols = new List <IObservation> {
                new Observation(new double[] { 0 }, "A"), new Observation(new double[] { 1 }, "B")
            };

            var model = HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <DiscreteDistribution>()
            {
                Pi = startDistribution, TransitionProbabilityMatrix = tpm, Emissions = emissions
            });                                                                                                                                                                                     //new HiddenMarkovModel(startDistribution, tpm, emissions) { LogNormalized = true};

            model.Normalized = true;
            var algo = new BaumWelch(observations, model, symbols);
            var res  = algo.Run(100, LikelihoodTolerance);

            Assert.AreEqual(0.82585, Math.Round(res.Pi[0], 5));
            Assert.AreEqual(0.17415, Math.Round(res.Pi[1], 5));

            Assert.AreEqual(0.33005, Math.Round(res.TransitionProbabilityMatrix[0][0], 5));
            Assert.AreEqual(0.66995, Math.Round(res.TransitionProbabilityMatrix[0][1], 5));
            Assert.AreEqual(0.09871, Math.Round(res.TransitionProbabilityMatrix[1][0], 5));
            Assert.AreEqual(0.90129, Math.Round(res.TransitionProbabilityMatrix[1][1], 5));

            Assert.AreEqual(0.65845, Math.Round(res.Emission[0].ProbabilityMassFunction(0), 5));
            Assert.AreEqual(0.34155, Math.Round(res.Emission[0].ProbabilityMassFunction(1), 5));
            Assert.AreEqual(0.41225, Math.Round(res.Emission[1].ProbabilityMassFunction(0), 5));
            Assert.AreEqual(0.58775, Math.Round(res.Emission[1].ProbabilityMassFunction(1), 5));

            Assert.AreEqual(-2.5638779209981162, res.Likelihood);
            Assert.AreEqual(1d, Math.Round(res.Pi.Sum(), 5));
            Assert.AreEqual(1d, Math.Round(res.TransitionProbabilityMatrix[0].Sum(), 5));
            Assert.AreEqual(1d, Math.Round(res.TransitionProbabilityMatrix[1].Sum(), 5));
            Assert.AreEqual(1d, Math.Round(res.Emission[0].ProbabilityMassFunction(0) + res.Emission[0].ProbabilityMassFunction(1), 5));
            Assert.AreEqual(1d, Math.Round(res.Emission[1].ProbabilityMassFunction(0) + res.Emission[1].ProbabilityMassFunction(1), 5));
        }
コード例 #11
0
        public void Predict_PredictionRequestWithoutParameters_Null()
        {
            var predictor = new SearchBasedPredictor(null);

            var model = (HiddenMarkovModelMixtureDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfComponents = _NumberOfComponents, NumberOfStates = _NumberOfStates
            });
            var request = new SearchBasedPredictionRequest();

            var value = predictor.Predict(model, request);

            Assert.IsNull(value);
        }
コード例 #12
0
        public void Predict_PredictionRequestWihtoutNumberOfSamplePoints_Null()
        {
            var predictor = new SearchBasedPredictor(null);

            var model = (HiddenMarkovModelMixtureDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfComponents = _NumberOfComponents, NumberOfStates = _NumberOfStates
            });
            var request = new SearchBasedPredictionRequest();

            request.AlgorithmSpecificParameters = new Dictionary <string, string>();

            var value = predictor.Predict(model, request);

            Assert.IsNull(value);
        }
        public void Predict_FTSESeriesAndErgodicModel_PredictionResult()
        {
            var util   = new TestDataUtils();
            var series = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));

            var model = (HiddenMarkovModelMultivariateGaussianDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <IMultivariateDistribution>()
            {
                NumberOfStates = NumberOfStates
            });                                                                                                                                                                                            //new HiddenMarkovModelMultivariateGaussianDistribution(NumberOfStates) { LogNormalized = true };

            model.Normalized = true;
            model.Train(series, NumberOfIterations, LikelihoodTolerance);
            var result = model.Predict(PredictorType.HmmLikelihood, series, null);

            Assert.IsNotNull(result);
        }
コード例 #14
0
        public void Train_FTSESeriesAndRightLeftAndLogNormalized_TrainedModel()
        {
            var util   = new TestDataUtils();
            var series = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));

            var model = (HiddenMarkovModelMixtureDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfComponents = _NumberOfComponents, NumberOfStates = _NumberOfStatesRightLeft, Delta = _Delta
            });                                                                                                                                                                                                                                                            //new HiddenMarkovModelMixtureDistribution(NumberOfComponents, NumberOfStatesRightLeft, Delta);

            model.Normalized = true;
            model.Train(series, _NumberOfIterations, _LikelihoodTolerance);

            Assert.AreEqual(ModelType.LeftRight, model.Type);
            Assert.AreEqual(_NumberOfStatesRightLeft, model.Emission.Length);
        }
コード例 #15
0
        public void Predict_FTSESeriesAndLeftRightModel_PredictionResult()
        {
            var util   = new TestDataUtils();
            var series = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));

            var model = (HiddenMarkovModelMixtureDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfComponents = _NumberOfComponents, NumberOfStates = _NumberOfStatesRightLeft, Delta = _Delta
            });                                                                                                                                                                                                                                                            //new HiddenMarkovModelMixtureDistribution(NumberOfComponents, NumberOfStatesRightLeft, Delta) { LogNormalized = true };

            model.Normalized = true;
            model.Train(series, _NumberOfIterations, _LikelihoodTolerance);
            var result = model.Predict(PredictorType.HmmLikelihood, series, _NumberOfIterations, _LikelihoodTolerance);

            Assert.IsNotNull(result);
        }
コード例 #16
0
        public void TestBaumWelchGaussian()
        {
            var algo = new BaumWelchGaussianDistribution(_observations, HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <NormalDistribution>()
            {
                Pi = _startDistribution, TransitionProbabilityMatrix = _tpm, Emissions = _distributions
            }));                                                                                                                                                                                                                                       //new HiddenMarkovModelGaussianDistribution(_startDistribution, _tpm, _distributions));

            var res = algo.Run(100, LikelihoodTolerance);

            Assert.AreEqual(1d, Math.Round(res.Pi.Sum(), 5));
            Assert.AreEqual(1d, Math.Round(res.TransitionProbabilityMatrix[0].Sum(), 5));
            Assert.AreEqual(1d, Math.Round(res.TransitionProbabilityMatrix[1].Sum(), 5));
            Assert.AreEqual(1d, Math.Round(res.TransitionProbabilityMatrix[2].Sum(), 5));
            var emissionValue = res.Emission[0].ProbabilityDensityFunction(new double[] { _observations[0].Value[0] });

            Assert.IsTrue(emissionValue > 0 && emissionValue < 1);
        }
        public IPredictionResult Predict(PredictorType predictorType, double[][] observations, double[] weights)
        {
            var model = (HiddenMarkovModelMultivariateGaussianDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <IMultivariateDistribution> {
                Pi = _pi, TransitionProbabilityMatrix = _transitionProbabilityMatrix, Emissions = _emission
            });                                                                                                                                                                                                                                                      //new HiddenMarkovModelState<IMultivariateDistribution>(_pi, _transitionProbabilityMatrix, _emission);

            model.Normalized = Normalized;
            var request = new PredictionRequest();

            request.TrainingSet  = observations;
            request.NumberOfDays = 1;
            request.Tolerance    = PREDICTION_LIKELIHOOD_TOLERANCE;

            var predictor = HiddenMarkovModelPredictorFactory.GetPredictor(predictorType);

            return(predictor.Predict(model, request));
        }
        public void Predict_FTSESeriesAndRightLeftModelAnd20DaysTestSequence_PredictionResult()
        {
            var util   = new TestDataUtils();
            var series = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));
            var test   = util.GetSvcData(util.FTSEFilePath, new DateTime(2011, 12, 18), new DateTime(2012, 01, 18));

            var model = (HiddenMarkovModelMultivariateGaussianDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <IMultivariateDistribution>()
            {
                NumberOfStates = NumberOfStatesRightLeft, Delta = Delta
            });                                                                                                                                                                                                                    //new HiddenMarkovModelMultivariateGaussianDistribution(NumberOfStatesRightLeft, Delta) { LogNormalized = true };

            model.Normalized = true;
            model.Train(series, NumberOfIterations, LikelihoodTolerance);
            var result = model.Predict(PredictorType.HmmLikelihood, series, test, null, 20);

            Assert.IsNotNull(result);
            Assert.AreEqual(20, result.Predicted.Length);
        }
コード例 #19
0
        public static void Initializer(TestContext context)
        {
            if (_trainingSet == null)
            {
                var util = new TestDataUtils();
                _trainingSet = util.GetSvcData(util.GOOGFilePath, new DateTime(2011, 01, 01), new DateTime(2012, 01, 01));
                _testSet     = util.GetSvcData(util.GOOGFilePath, new DateTime(2012, 01, 01), new DateTime(2013, 04, 05));

                var model = (HiddenMarkovModelMixtureDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
                {
                    NumberOfStates = NumberOfStates, NumberOfComponents = NumberOfComponents
                });                                                                                                                                                                                                                                 //new HiddenMarkovModelMixtureDistribution(NumberOfComponents, NumberOfStates) { LogNormalized = true };
                model.Normalized = true;
                model.Train(_trainingSet, NumberOfIterations, LikelihoodTolerance);
                var result = model.Predict(PredictorType.HmmLikelihood, _trainingSet, _testSet, _testSet.Length, NumberOfIterations, LikelihoodTolerance);
                _predictionSet = result.Predicted;
            }
        }
コード例 #20
0
        public void Predict_EURUSDSeriesAndErgodicModelAnd31DaysTestSequence_PredictionResult()
        {
            var util   = new TestDataUtils();
            var series = util.GetSvcData(util.EURUSDFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));
            var test   = util.GetSvcData(util.EURUSDFilePath, new DateTime(2011, 12, 18), new DateTime(2012, 01, 18));

            var model = (HiddenMarkovModelMixtureDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfComponents = _NumberOfComponents, NumberOfStates = _NumberOfStates
            });

            model.Normalized = true;
            model.Train(series, _NumberOfIterations, _LikelihoodTolerance);
            var result = model.Predict(PredictorType.HmmLikelihood, series, test, 31, _NumberOfIterations, _LikelihoodTolerance);

            Assert.IsNotNull(result);
            Assert.AreEqual(31, result.Predicted.Length);
        }
コード例 #21
0
        public void Solve_PopulationSizeZero_ZeroSizePopulationReturned()
        {
            var selectionMethod    = new TournamentSelection(TournamentSize);
            var crossoverAlgorithm = new Crossover(CrossoverProbability);
            var mutationAlgorithm  = new Mutator(MutationProbability);

            var model = (HiddenMarkovModelMixtureDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> > {
                Pi = new double[3], TransitionProbabilityMatrix = new double[3][], Emissions = new Mixture <IMultivariateDistribution> [3]
            });
            var evaluator = new HmmEvaluator <Mixture <IMultivariateDistribution> >(model, new ForwardBackward(true));

            var parameters = new GeneticSolverParameters();

            var solver = new GeneticSolver(parameters, mutationAlgorithm, crossoverAlgorithm, evaluator, selectionMethod);

            var result = solver.Solve(new List <IChromosome <decimal> >());

            Assert.AreEqual(result.Count, 0);
        }
コード例 #22
0
        public void EvaluatePrediction_FTSESeriesAndRightLeftModelAnd20DaysTestSequence_PredictionEvaluated()
        {
            var util   = new TestDataUtils();
            var series = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));
            var test   = util.GetSvcData(util.FTSEFilePath, new DateTime(2011, 12, 18), new DateTime(2012, 01, 18));

            var model = (HiddenMarkovModelMixtureDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfComponents = _NumberOfComponents, NumberOfStates = _NumberOfStatesRightLeft, Delta = _Delta
            });                                                                                                                                                                                                                                                            //new HiddenMarkovModelMixtureDistribution(NumberOfComponents, NumberOfStatesRightLeft, Delta) { LogNormalized = true };

            model.Normalized = true;
            model.Train(series, _NumberOfIterations, _LikelihoodTolerance);
            var result = model.Predict(PredictorType.HmmLikelihood, series, test, 20, _NumberOfIterations, _LikelihoodTolerance);
            var mape   = model.EvaluatePrediction(result, test);

            Assert.IsNotNull(mape);
            Assert.AreEqual(4, mape.MeanAbsolutePercentError.Length);
        }
コード例 #23
0
        public void BetaEstimator_ABBAObservations_NotNormalizedTest()
        {
            var startDistribution = new[] { 0.85, 0.15 };
            // s = 0, t = 1
            var tpm = new double[2][];

            tpm[0] = new[] { 0.3, 0.7 };
            tpm[1] = new[] { 0.1, 0.9 };

            var observations = new List <IObservation>
            {
                new Observation(new double[] { 0 }, "A"),
                new Observation(new double[] { 1 }, "B"),
                new Observation(new double[] { 1 }, "B"),
                new Observation(new double[] { 0 }, "A")
            };

            var emissions = new DiscreteDistribution[2];

            emissions[0] = new DiscreteDistribution(new double[] { 0, 1 }, new[] { 0.4, 0.6 });
            emissions[1] = new DiscreteDistribution(new double[] { 0, 1 }, new[] { 0.5, 0.5 });

            var model = HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <DiscreteDistribution>()
            {
                Pi = startDistribution, TransitionProbabilityMatrix = tpm, Emissions = emissions
            });                                                                                                                                                                                  //new HiddenMarkovModel(startDistribution, tpm, emissions) { LogNormalized = false };

            model.Normalized = false;

            var betaEstimator = new BetaEstimator <DiscreteDistribution>();
            var beta          = betaEstimator.Estimate(new BasicEstimationParameters <DiscreteDistribution> {
                Model = model, Observations = observations, Normalized = model.Normalized
            });

            Assert.AreEqual(1d, Math.Round(beta[3][0], 9));
            Assert.AreEqual(1d, Math.Round(beta[3][1], 9));
            Assert.AreEqual(0.47, Math.Round(beta[2][0], 9));
            Assert.AreEqual(0.49, Math.Round(beta[2][1], 9));
            Assert.AreEqual(0.2561, Math.Round(beta[1][0], 9));
            Assert.AreEqual(0.2487, Math.Round(beta[1][1], 9));
            Assert.AreEqual(0.133143, Math.Round(beta[0][0], 9));
            Assert.AreEqual(0.127281, Math.Round(beta[0][1], 9));
        }
コード例 #24
0
        public IPredictionResult Predict(PredictorType predictorType, double[][] observations, int numberOfIterations, double likelihoodTolerance)
        {
            var model = (HiddenMarkovModelMixtureDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> > {
                Pi = _pi, TransitionProbabilityMatrix = _transitionProbabilityMatrix, Emissions = _emission
            });                                                                                                                                                                                                                                                  //HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters<Mixture<IMultivariateDistribution>> { Pi = _pi, TransitionProbabilityMatrix = _transitionProbabilityMatrix, Emissions = _emission });

            model.Normalized = Normalized;
            var request = new PredictionRequest();

            request.TrainingSet  = observations;
            request.NumberOfDays = 1;
            request.Tolerance    = PREDICTION_LIKELIHOOD_TOLERANCE;
            request.TrainingLikelihoodTolerance = likelihoodTolerance;
            request.NumberOfTrainingIterations  = numberOfIterations;

            var predictor = HiddenMarkovModelPredictorFactory.GetPredictor(predictorType);

            return(predictor.Predict(model, request));
        }
コード例 #25
0
        public void TestBaumWelchMultivariate()
        {
            var model = HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <IMultivariateDistribution>()
            {
                Pi = _startDistribution, TransitionProbabilityMatrix = _tpm, Emissions = _emissions
            });                                                                                                                                                                                          //new HiddenMarkovModelMultivariateGaussianDistribution(_startDistribution, _tpm, _emissions)

            model.Normalized = false;
            var algo = new BaumWelchMultivariateDistribution(_observations, model)
            {
                Normalized = false
            };
            var res = algo.Run(100, LikelihoodTolerance);

            Assert.AreEqual(1d, res.Pi.Sum());
            Assert.AreEqual(1d, Math.Round(res.TransitionProbabilityMatrix[0].Sum(), 5));
            Assert.AreEqual(1d, Math.Round(res.TransitionProbabilityMatrix[1].Sum(), 5));
            Assert.AreEqual(1d, Math.Round(res.TransitionProbabilityMatrix[2].Sum(), 5));
        }
コード例 #26
0
        public void AlphaEstimator_ABBAObservations_NotNormalizedTest()
        {
            var startDistribution = new[] { 0.85, 0.15 };
            // s = 0, t = 1
            var tpm = new double[2][];

            tpm[0] = new[] { 0.3, 0.7 };
            tpm[1] = new[] { 0.1, 0.9 };

            var observations = new List <IObservation>
            {
                new Observation(new double[] { 0 }, "A"),
                new Observation(new double[] { 1 }, "B"),
                new Observation(new double[] { 1 }, "B"),
                new Observation(new double[] { 0 }, "A")
            };

            var emissions = new DiscreteDistribution[2];

            emissions[0] = new DiscreteDistribution(new double[] { 0, 1 }, new[] { 0.4, 0.6 });
            emissions[1] = new DiscreteDistribution(new double[] { 0, 1 }, new[] { 0.5, 0.5 });

            var model = HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <DiscreteDistribution>()
            {
                Pi = startDistribution, TransitionProbabilityMatrix = tpm, Emissions = emissions
            });                                                                                                                                                                                     //new HiddenMarkovModel(startDistribution, tpm, emissions) { LogNormalized = false };

            model.Normalized = false;

            var alphaEstimator = new AlphaEstimator <DiscreteDistribution>();
            var alpha          = alphaEstimator.Estimate(new BasicEstimationParameters <DiscreteDistribution> {
                Model = model, Observations = observations, Normalized = model.Normalized
            });

            Assert.AreEqual(0.34, Math.Round(alpha[0][0], 9));
            Assert.AreEqual(0.075, Math.Round(alpha[0][1], 9));
            Assert.AreEqual(0.0657, Math.Round(alpha[1][0], 9));
            Assert.AreEqual(0.15275, Math.Round(alpha[1][1], 9));
            Assert.AreEqual(0.020991, Math.Round(alpha[2][0], 9));
            Assert.AreEqual(0.0917325, Math.Round(alpha[2][1], 9));
            Assert.AreEqual(0.00618822, Math.Round(alpha[3][0], 9));
            Assert.AreEqual(0.048626475, Math.Round(alpha[3][1], 9));
        }
        public IEvaluationResult EvaluatePrediction(IPredictionResult results, double[][] observations)
        {
            var model = (HiddenMarkovModelMultivariateGaussianDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <IMultivariateDistribution> {
                Pi = _pi, TransitionProbabilityMatrix = _transitionProbabilityMatrix, Emissions = _emission
            });                                                                                                                                                                                                                                                      //new HiddenMarkovModelState<IMultivariateDistribution>(_pi, _transitionProbabilityMatrix, _emission);

            model.Normalized = Normalized;
            var request = new EvaluationRequest();

            request.PredictionParameters           = new PredictionRequest();
            request.PredictionToEvaluate           = new PredictionResult();
            request.EstimatorType                  = ErrorEstimatorType.Value;
            request.PredictionParameters.Tolerance = PREDICTION_LIKELIHOOD_TOLERANCE;
            request.PredictionParameters.TestSet   = observations;
            request.PredictionToEvaluate.Predicted = results.Predicted;

            var predictor = new LikelihoodBasedPredictor();

            return(predictor.Evaluate(request));
        }
コード例 #28
0
        public void HiddenMarkovModelMixtureDistribution_NumberOfStates_ErgodicModelCreated()
        {
            var model = HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfComponents = _NumberOfComponents, NumberOfStates = _NumberOfStates
            });                                                                                                                                                                                             //new HiddenMarkovModelMixtureDistribution(NumberOfComponents ,NumberOfStates);

            Assert.AreEqual(ModelType.Ergodic, model.Type);
            Assert.AreEqual(_NumberOfStates, model.N);
            Assert.IsFalse(model.Normalized);
            Assert.IsNull(model.Emission);
            for (int n = 0; n < _NumberOfStates; n++)
            {
                Assert.AreEqual(0.5, model.Pi[n]);
                for (int i = 0; i < _NumberOfStates; i++)
                {
                    Assert.AreEqual(0.5, model.TransitionProbabilityMatrix[n][i]);
                }
            }
        }
コード例 #29
0
        public void GeneticSolver_AllDependencies_SolverCreatedWithAllDependecnies()
        {
            var selectionMethod    = new TournamentSelection(TournamentSize);
            var crossoverAlgorithm = new Crossover(CrossoverProbability);
            var mutationAlgorithm  = new Mutator(MutationProbability);

            var model = (HiddenMarkovModelMixtureDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> > {
                Pi = new double[3], TransitionProbabilityMatrix = new double[3][], Emissions = new Mixture <IMultivariateDistribution> [3]
            });
            var evaluator = new HmmEvaluator <Mixture <IMultivariateDistribution> >(model, new ForwardBackward(true));

            var parameters = new GeneticSolverParameters();

            var solver = new GeneticSolver(parameters, mutationAlgorithm, crossoverAlgorithm, evaluator, selectionMethod);

            Assert.IsNotNull(solver.MutationAlgorithm);
            Assert.IsNotNull(solver.CrossoverAlgorithm);
            Assert.IsNotNull(solver.EvaluationMethod);
            Assert.IsNotNull(solver.SelectionMethod);
        }
コード例 #30
0
        public void Evaluate_HMMMultivariateAndFTSESeriesLength20_ErrorEstimatorCalculated()
        {
            var util   = new TestDataUtils();
            var series = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));
            var test   = util.GetSvcData(util.FTSEFilePath, new DateTime(2011, 12, 18), new DateTime(2012, 01, 18));

            var model = (HiddenMarkovModelMultivariateGaussianDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <IMultivariateDistribution>()
            {
                NumberOfStates = _NumberOfStates
            });

            model.Normalized = true;
            model.Train(series, _NumberOfIterations, _LikelihoodTolerance);

            var pred    = new ViterbiBasedPredictor();
            var request = new PredictionRequest {
                TrainingSet = series, NumberOfDays = 20, TestSet = test
            };

            pred.NumberOfIterations  = _NumberOfIterations;
            pred.LikelihoodTolerance = _LikelihoodTolerance;
            var predictions  = pred.Predict(model, request);
            var errorRequest = new EvaluationRequest
            {
                EstimatorType        = ErrorEstimatorType.Value,
                PredictionParameters = request,
                PredictionToEvaluate = predictions
            };

            var errorEstimation = pred.Evaluate(errorRequest);

            for (int i = 0; i < series[0].Length; i++)
            {
                Assert.IsTrue(errorEstimation.CumulativeForecastError[i] > 0);
                Assert.IsTrue(errorEstimation.MeanAbsoluteDeviation[i] > 0);
                Assert.IsTrue(errorEstimation.MeanAbsolutePercentError[i] > 0);
                Assert.IsTrue(errorEstimation.MeanError[i] > 0);
                Assert.IsTrue(errorEstimation.MeanSquaredError[i] > 0);
                Assert.IsTrue(errorEstimation.RootMeanSquaredError[i] > 0);
            }
        }