예제 #1
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 void Train(double[] observations)
        {
            if (_pi == null || _transitionProbabilityMatrix == null || _emission == null)
            {
                throw new ApplicationException("Initialize the model with initial valuesss");
            }
            var model = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <NormalDistribution> {
                Pi = _pi, TransitionProbabilityMatrix = _transitionProbabilityMatrix, Emissions = _emission
            });                                                                                                                                                                                                 //new HiddenMarkovModelState<NormalDistribution>(_pi, _transitionProbabilityMatrix, _emission);

            model.Normalized = Normalized;
            var alg = new BaumWelchGaussianDistribution(Helper.Convert(observations), model);
            var estimatedParameters = alg.Run(100, 20);

            _pi = estimatedParameters.Pi;
            _transitionProbabilityMatrix = estimatedParameters.TransitionProbabilityMatrix;
            _emission  = estimatedParameters.Emission as NormalDistribution[];
            Likelihood = estimatedParameters.Likelihood;
        }