示例#1
0
        public void TestBackwardRun2()
        {
            var states = new List <IState> {
                new State(0, "s"), new State(1, "t")
            };

            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 algo = new ForwardBackward(false);
            var res  = algo.RunBackward(observations, HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <DiscreteDistribution>()
            {
                Pi = startDistribution, TransitionProbabilityMatrix = tpm, Emissions = emissions
            }));                                                                                                                                                                                                                       //new HiddenMarkovModelState<DiscreteDistribution>(startDistribution, tpm, emissions));

            Assert.AreEqual(0.25499, res);
            Assert.AreEqual(1, algo.Beta[3][0]);
            Assert.AreEqual(1, algo.Beta[3][1]);
            Assert.AreEqual(0.47, algo.Beta[2][0]);
            Assert.AreEqual(0.49, algo.Beta[2][1]);
            Assert.AreEqual(0.2561, algo.Beta[1][0]);
            Assert.AreEqual(0.2487, algo.Beta[1][1]);
            Assert.AreEqual(0.133143, algo.Beta[0][0]);
            Assert.AreEqual(0.127281, algo.Beta[0][1]);
        }
示例#2
0
        public void TestBackwardNormalizedRun2()
        {
            var states = new List <IState> {
                new State(0, "s"), new State(1, "t")
            };

            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 algo = new ForwardBackward(true);
            var res  = algo.RunBackward(observations, HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <DiscreteDistribution>()
            {
                Pi = startDistribution, TransitionProbabilityMatrix = tpm, Emissions = emissions
            }));                                                                                                                                                                                                                       //new HiddenMarkovModelState<DiscreteDistribution>(startDistribution, tpm, emissions));

            Assert.AreEqual(-1.3665309502789404, res);
            Assert.AreEqual(0d, algo.Beta[3][0]);
            Assert.AreEqual(0d, algo.Beta[3][1]);
            Assert.AreEqual(-0.75502258427803293, algo.Beta[2][0]);
            Assert.AreEqual(-0.71334988787746456, algo.Beta[2][1]);
            Assert.AreEqual(-1.3621872857766575, algo.Beta[1][0]);
            Assert.AreEqual(-1.3915079281727778, algo.Beta[1][1]);
            Assert.AreEqual(-2.0163315403910613, algo.Beta[0][0]);
            Assert.AreEqual(-2.0613580382895655, algo.Beta[0][1]);
        }
        public IHiddenMarkovModel <Mixture <IMultivariateDistribution> > Run(int maxIterations, double likelihoodTolerance)
        {
            // Initialize responce object
            var forwardBackward = new ForwardBackward(Normalized);

            do
            {
                maxIterations--;
                if (!_estimatedModel.Likelihood.EqualsTo(0))
                {
                    _currentModel = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <Mixture <IMultivariateDistribution> > {
                        Pi = _estimatedPi, TransitionProbabilityMatrix = _estimatedTransitionProbabilityMatrix, Emissions = _estimatedEmissions
                    });                                                                                                                                                                                                                                                 //new HiddenMarkovModelState<Mixture<IMultivariateDistribution>>(_estimatedPi, _estimatedTransitionProbabilityMatrix, _estimatedEmissions) { LogNormalized = _estimatedModel.LogNormalized };
                    _currentModel.Normalized = Normalized;
                    _currentModel.Likelihood = _estimatedModel.Likelihood;
                }
                // Run Forward-Backward procedure
                forwardBackward.RunForward(_observations, _currentModel);
                forwardBackward.RunBackward(_observations, _currentModel);
                // Calculate Gamma and Xi
                var @params = new MixtureSigmaEstimationParameters <Mixture <IMultivariateDistribution> >
                {
                    Alpha              = forwardBackward.Alpha,
                    Beta               = forwardBackward.Beta,
                    Observations       = _observations,
                    Model              = _currentModel,
                    Normalized         = _currentModel.Normalized,
                    L                  = _currentModel.Emission[0].Components.Length,
                    ObservationWeights = _observationWeights
                };
                _gammaEstimator = new GammaEstimator <Mixture <IMultivariateDistribution> >();
                _ksiEstimator   = new KsiEstimator <Mixture <IMultivariateDistribution> >();
                var mixtureCoefficientsEstimator = new MixtureCoefficientsEstimator <Mixture <IMultivariateDistribution> >();
                var mixtureMuEstimator           = new MixtureMuEstimator <Mixture <IMultivariateDistribution> >();    // Mean
                var mixtureSigmaEstimator        = new MixtureSigmaEstimator <Mixture <IMultivariateDistribution> >(); // Covariance
                var mixtureGammaEstimator        = new MixtureGammaEstimator <Mixture <IMultivariateDistribution> >();
                @params.Gamma           = _gammaEstimator.Estimate(@params);
                @params.GammaComponents = mixtureGammaEstimator.Estimate(@params);


                EstimatePi(_gammaEstimator.Estimate(@params));
                // TODO : weights for A
                EstimateTransitionProbabilityMatrix(_gammaEstimator.Estimate(@params), _ksiEstimator.Estimate(@params), _observationWeights, _observations.Count);

                for (var n = 0; n < _currentModel.N; n++)
                {
                    var mixturesComponents = _currentModel.Emission[n].Coefficients.Length;
                    var distributions      = new IMultivariateDistribution[mixturesComponents];
                    // Calculate coefficients for state n
                    // TODO : weights for W
                    var coefficients = mixtureCoefficientsEstimator.Estimate(@params)[n];
                    if (Normalized)
                    {
                        mixtureCoefficientsEstimator.Denormalize();
                    }
                    // TODO : weights Mu
                    @params.Mu = mixtureMuEstimator.Estimate(@params);
                    for (var l = 0; l < mixturesComponents; l++)
                    {
                        // TODO : weights Sigma
                        distributions[l] = new NormalDistribution(mixtureMuEstimator.Estimate(@params)[n, l], mixtureSigmaEstimator.Estimate(@params)[n, l]);
                    }
                    _estimatedEmissions[n] = new Mixture <IMultivariateDistribution>(coefficients, distributions);
                }
                _estimatedModel = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <Mixture <IMultivariateDistribution> > {
                    Pi = _estimatedPi, TransitionProbabilityMatrix = _estimatedTransitionProbabilityMatrix, Emissions = _estimatedEmissions
                });
                _estimatedModel.Normalized = Normalized;
                _estimatedModel.Likelihood = forwardBackward.RunForward(_observations, _estimatedModel);
                _likelihoodDelta           = Math.Abs(Math.Abs(_currentModel.Likelihood) - Math.Abs(_estimatedModel.Likelihood));
                Debug.WriteLine("Iteration {3} , Current {0}, Estimate {1} Likelihood delta {2}", _currentModel.Likelihood, _estimatedModel.Likelihood, _likelihoodDelta, maxIterations);
            }while (_currentModel != _estimatedModel && maxIterations > 0 && _likelihoodDelta > likelihoodTolerance);

            return(_estimatedModel);
        }