public void HiddenMarkovModelState_NumberOfStateGreaterThanZero_ErgodicModelCreated()
        {
            var modelState = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <NormalDistribution>()
            {
                NumberOfStates = 4
            });                                                                                                                               //new HiddenMarkovModelState<IDistribution>(4);

            Assert.AreEqual(ModelType.Ergodic, modelState.Type);

            Assert.AreEqual(1, modelState.TransitionProbabilityMatrix[0].Sum());
            Assert.AreEqual(1, modelState.TransitionProbabilityMatrix[1].Sum());
            Assert.AreEqual(1, modelState.TransitionProbabilityMatrix[2].Sum());
            Assert.AreEqual(1, modelState.TransitionProbabilityMatrix[3].Sum());

            Assert.AreEqual(1, modelState.Pi.Sum());
        }
Esempio n. 2
0
        public void Mu_MultivariateAndErgodicAndLogNormalized_MuCalculated()
        {
            var util         = new TestDataUtils();
            var observations = util.GetSvcData(util.FTSEFilePath, new DateTime(2011, 11, 18), new DateTime(2011, 12, 18));
            var sequence     = Helper.Convert(observations);
            var model        = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <NormalDistribution>()
            {
                NumberOfStates = NumberOfStates, Emissions = CreateEmissions(observations, NumberOfStates)
            });                                                                                                                                                                                                  //new HiddenMarkovModelState<NormalDistribution>(NumberOfStates, CreateEmissions(observations, NumberOfStates)) { LogNormalized = true };

            model.Normalized = true;
            var baseParameters = new BasicEstimationParameters <NormalDistribution> {
                Model = model, Observations = sequence, Normalized = model.Normalized
            };
            var alphaEstimator = new AlphaEstimator <NormalDistribution>();
            var alpha          = alphaEstimator.Estimate(baseParameters);
            var betaEstimator  = new BetaEstimator <NormalDistribution>();
            var beta           = betaEstimator.Estimate(baseParameters);

            var @params = new AdvancedEstimationParameters <NormalDistribution>
            {
                Alpha        = alpha,
                Beta         = beta,
                Observations = sequence,
                Model        = model
            };
            var gammaEstimator = new GammaEstimator <NormalDistribution>();
            var estimator      = new MuMultivariateEstimator <NormalDistribution>();
            var muParams       = new MuEstimationParameters <NormalDistribution>
            {
                Gamma        = gammaEstimator.Estimate(@params),
                Model        = model,
                Normalized   = model.Normalized,
                Observations = sequence
            };

            Assert.IsNotNull(estimator);
            var mu = estimator.Estimate(muParams);

            for (int i = 0; i < NumberOfStates; i++)
            {
                for (int j = 0; j < sequence[0].Dimention; j++)
                {
                    Assert.IsTrue(mu[i][j] > 0, string.Format("Failed Mu {0}", mu[i][j]));
                }
            }
        }
Esempio n. 3
0
        public void Estimate_ParametersPassed_PiCalculatedAndReturned()
        {
            const int numberOfStates = 2;

            var util         = new TestDataUtils();
            var observations = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));
            var model        = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <NormalDistribution>()
            {
                NumberOfStates = numberOfStates, Emissions = CreateEmissions(observations, numberOfStates)
            });

            model.Normalized = true;
            var observationsList = new List <IObservation>();

            for (var i = 0; i < observations.Length; i++)
            {
                observationsList.Add(new Observation(observations[i], i.ToString()));
            }
            var baseParameters = new BasicEstimationParameters <NormalDistribution> {
                Model = model, Observations = Helper.Convert(observations), Normalized = model.Normalized
            };
            var alphaEstimator = new AlphaEstimator <NormalDistribution>();
            var alpha          = alphaEstimator.Estimate(baseParameters);
            var betaEstimator  = new BetaEstimator <NormalDistribution>();
            var beta           = betaEstimator.Estimate(baseParameters);
            var @params        = new AdvancedEstimationParameters <NormalDistribution>
            {
                Alpha        = alpha,
                Beta         = beta,
                Observations = observationsList,
                Model        = model,
                Normalized   = model.Normalized
            };
            var gammaEstimator = new GammaEstimator <NormalDistribution>();

            var estimator  = new PiEstimator();
            var parameters = new PiParameters
            {
                Gamma      = gammaEstimator.Estimate(@params),
                N          = model.N,
                Normalized = model.Normalized
            };

            var estimatedPi = estimator.Estimate(parameters);

            Assert.AreEqual(1d, Math.Round(estimatedPi[0] + estimatedPi[1], 5));
        }
Esempio n. 4
0
        public void BetaEstimator_ModelAndObservations_BetaEstimatorCreated()
        {
            const int numberOfStates = 2;

            var util         = new TestDataUtils();
            var observations = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));
            var model        = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <NormalDistribution>()
            {
                NumberOfStates = numberOfStates
            });                                                                                                                                      //new HiddenMarkovModelState<NormalDistribution>(numberOfStates) { LogNormalized = true };

            model.Normalized = true;

            var estimator = new BetaEstimator <NormalDistribution>();

            Assert.IsNotNull(estimator);
        }
        public void HiddenMarkovModelState_CustomModelParametersPassed_ModelTypeIsCustom()
        {
            var pi  = new double[] { 1d / 3d, 1d / 3d, 1d / 3d, 0 };
            var tpm = new double[4][];

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

            var modelState = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <NormalDistribution>()
            {
                Pi = pi, TransitionProbabilityMatrix = tpm, Emissions = new NormalDistribution[4]
            });                                                                                                                                                                                              //new HiddenMarkovModelState<IDistribution>(pi, tpm, new IDistribution[4]);

            Assert.AreEqual(ModelType.Custom, modelState.Type);
        }
        public void HiddenMarkovModelState_NumberOfStateAndDeltaGreaterThanZero_LeftRightModelCreated()
        {
            var modelState = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <NormalDistribution>()
            {
                NumberOfStates = 4, Delta = 2
            });                                                                                                                                          //new HiddenMarkovModelState<IDistribution>(4, 2);

            Assert.AreEqual(ModelType.LeftRight, modelState.Type);

            Assert.AreEqual(1, modelState.TransitionProbabilityMatrix[0].Sum());
            Assert.AreEqual(1, modelState.TransitionProbabilityMatrix[1].Sum());
            Assert.AreEqual(1, modelState.TransitionProbabilityMatrix[2].Sum());
            Assert.AreEqual(1, modelState.TransitionProbabilityMatrix[3].Sum());

            Assert.AreEqual(1, modelState.Pi.Sum());
            Assert.AreEqual(1, modelState.Pi[0]);
        }
Esempio n. 7
0
        public BaumWelchMixtureDistribution(IList <IObservation> observations, IHiddenMarkovModel <Mixture <IMultivariateDistribution> > model) : base(model)
        {
            _observations       = observations;
            _currentModel       = model;
            _estimatedEmissions = new Mixture <IMultivariateDistribution> [_currentModel.N];
            for (var i = 0; i < model.N; i++)
            {
                // BUG : Update emmisions from model. Don't create new ones.
                _estimatedEmissions[i] = new Mixture <IMultivariateDistribution>(model.Emission[0].Components.Length, model.Emission[0].Dimension);
            }

            _estimatedModel = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <Mixture <IMultivariateDistribution> > {
                Pi = _estimatedPi, TransitionProbabilityMatrix = _estimatedTransitionProbabilityMatrix, Emissions = _estimatedEmissions
            });                                                                                                                                                                                                                                                   //new HiddenMarkovModelState<Mixture<IMultivariateDistribution>>(_estimatedPi, _estimatedTransitionProbabilityMatrix, _estimatedEmissions);

            Normalized = _estimatedModel.Normalized = model.Normalized;
        }
Esempio n. 8
0
        public void Mu_ErgodicAndLogNormalized_MuCalculated()
        {
            var util         = new TestDataUtils();
            var observations = util.GetSvcData(util.FTSEFilePath, new DateTime(2011, 11, 18), new DateTime(2011, 12, 18));
            var model        = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfStates = NumberOfStates, Emissions = CreateEmissions(observations, NumberOfStates, NumberOfComponents)
            });                                                                                                                                                                                                                                      //new HiddenMarkovModelState<Mixture<IMultivariateDistribution>>(NumberOfStates, CreateEmissions(observations, NumberOfStates, NumberOfComponents)) { LogNormalized = true };

            model.Normalized = true;
            var baseParameters = new BasicEstimationParameters <Mixture <IMultivariateDistribution> > {
                Model = model, Observations = Helper.Convert(observations), Normalized = model.Normalized
            };
            var alphaEstimator = new AlphaEstimator <Mixture <IMultivariateDistribution> >();
            var alpha          = alphaEstimator.Estimate(baseParameters);
            var betaEstimator  = new BetaEstimator <Mixture <IMultivariateDistribution> >();
            var beta           = betaEstimator.Estimate(baseParameters);

            var mu = new MixtureMuEstimator <Mixture <IMultivariateDistribution> >();
            var mixtureGammaEstimator = new MixtureGammaEstimator <Mixture <IMultivariateDistribution> >();
            var @params = new MixtureCoefficientEstimationParameters <Mixture <IMultivariateDistribution> >
            {
                Model        = model,
                Normalized   = model.Normalized,
                Alpha        = alpha,
                Beta         = beta,
                Observations = Helper.Convert(observations),
                L            = model.Emission[0].Components.Length
            };
            var gamma          = mixtureGammaEstimator.Estimate(@params as AdvancedEstimationParameters <Mixture <IMultivariateDistribution> >);
            var gammaComponens = mixtureGammaEstimator.Estimate(@params);

            @params.Gamma           = gamma;
            @params.GammaComponents = gammaComponens;

            for (int i = 0; i < NumberOfStates; i++)
            {
                for (int l = 0; l < NumberOfComponents; l++)
                {
                    for (int d = 0; d < observations[0].Length; d++)
                    {
                        Assert.IsTrue(mu.Estimate(@params)[i, l][d] > 0, string.Format("Failed Mu {0}", mu.Estimate(@params)[i, l][d]));
                    }
                }
            }
        }
Esempio n. 9
0
        public void Ksi_RightLeftAndNotNormalized_KsiCalculated()
        {
            var delta = 3;
            var numberOfStatesRightLeft = 4;
            var util         = new TestDataUtils();
            var observations = util.GetSvcData(util.FTSEFilePath, new DateTime(2011, 11, 18), new DateTime(2011, 12, 18));
            var model        = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <NormalDistribution>()
            {
                NumberOfStates = numberOfStatesRightLeft, Delta = delta, Emissions = CreateEmissions(observations, numberOfStatesRightLeft)
            });                                                                                                                                                                                                                                   //new HiddenMarkovModelState<NormalDistribution>(numberOfStatesRightLeft, delta, CreateEmissions(observations, numberOfStatesRightLeft)) { LogNormalized = true };

            model.Normalized = false;
            var baseParameters = new BasicEstimationParameters <NormalDistribution> {
                Model = model, Observations = Helper.Convert(observations), Normalized = model.Normalized
            };
            var alphaEstimator = new AlphaEstimator <NormalDistribution>();
            var alpha          = alphaEstimator.Estimate(baseParameters);
            var betaEstimator  = new BetaEstimator <NormalDistribution>();
            var beta           = betaEstimator.Estimate(baseParameters);
            var @params        = new AdvancedEstimationParameters <NormalDistribution>
            {
                Alpha        = alpha,
                Beta         = beta,
                Observations = Helper.Convert(observations),
                Model        = model,
                Normalized   = model.Normalized
            };

            var estimator = new KsiEstimator <NormalDistribution>();

            Assert.IsNotNull(estimator);
            for (int t = 0; t < observations.Length - 1; t++)
            {
                for (int i = 0; i < numberOfStatesRightLeft; i++)
                {
                    for (int j = 0; j < numberOfStatesRightLeft; j++)
                    {
                        Assert.IsTrue(estimator.Estimate(@params)[t][i, j] >= 0 && estimator.Estimate(@params)[t][i, j] < 1, string.Format("Failed Ksi [{1}][{2},{3}]:{0}", estimator.Estimate(@params)[t][i, j], t, i, j));
                    }
                }
            }
        }
Esempio n. 10
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]);
        }
Esempio n. 11
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]);
        }
Esempio n. 12
0
        public void Ksi_ErgodicAndLogNormalized_KsiCalculated()
        {
            var util         = new TestDataUtils();
            var observations = util.GetSvcData(util.FTSEFilePath, new DateTime(2011, 11, 18), new DateTime(2011, 12, 18));
            var model        = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <NormalDistribution>()
            {
                NumberOfStates = NumberOfStates, Emissions = CreateEmissions(observations, NumberOfStates)
            });                                                                                                                                                                                                  //new HiddenMarkovModelState<NormalDistribution>(NumberOfStates, CreateEmissions(observations, NumberOfStates)) { LogNormalized = true };

            model.Normalized = true;
            var baseParameters = new BasicEstimationParameters <NormalDistribution> {
                Model = model, Observations = Helper.Convert(observations), Normalized = model.Normalized
            };
            var alphaEstimator = new AlphaEstimator <NormalDistribution>();
            var alpha          = alphaEstimator.Estimate(baseParameters);
            var betaEstimator  = new BetaEstimator <NormalDistribution>();
            var beta           = betaEstimator.Estimate(baseParameters);
            var @params        = new AdvancedEstimationParameters <NormalDistribution>
            {
                Alpha        = alpha,
                Beta         = beta,
                Observations = Helper.Convert(observations),
                Model        = model,
                Normalized   = model.Normalized
            };
            var estimator = new KsiEstimator <NormalDistribution>();

            Assert.IsNotNull(estimator);
            for (int t = 0; t < observations.Length - 1; t++)
            {
                for (int i = 0; i < NumberOfStates; i++)
                {
                    for (int j = 0; j < NumberOfStates; j++)
                    {
                        Assert.IsTrue(estimator.Estimate(@params)[t][i, j] < 0, string.Format("Failed Ksi {0}", estimator.Estimate(@params)[t][i, j]));
                    }
                }
            }
        }
        public void Estimate_AlphaBetaParameters_TransitionProbabilityMatrixCalculatedAndReturned()
        {
            const int numberOfStates = 2;

            var util         = new TestDataUtils();
            var observations = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));
            var model        = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <NormalDistribution>()
            {
                NumberOfStates = numberOfStates, Emissions = CreateEmissions(observations, numberOfStates)
            });

            model.Normalized = true;
            var baseParameters = new BasicEstimationParameters <NormalDistribution> {
                Model = model, Observations = Helper.Convert(observations), Normalized = model.Normalized
            };
            var alphaEstimator = new AlphaEstimator <NormalDistribution>();
            var alpha          = alphaEstimator.Estimate(baseParameters);

            var betaEstimator = new BetaEstimator <NormalDistribution>();
            var beta          = betaEstimator.Estimate(baseParameters);
            var weights       = new double[observations.Length];

            var estimator  = new TransitionProbabilityEstimator <NormalDistribution>();
            var parameters = new AlphaBetaTransitionProbabiltyMatrixParameters <NormalDistribution>
            {
                Alpha        = alpha,
                Beta         = beta,
                Model        = model,
                Observations = observations,
                Normalized   = model.Normalized,
                Weights      = weights
            };

            var estimatedTransitionProbabilityMatrix = estimator.Estimate(parameters);

            Assert.AreEqual(1d, Math.Round(estimatedTransitionProbabilityMatrix[0][0] + estimatedTransitionProbabilityMatrix[0][1], 5));
            Assert.AreEqual(1d, Math.Round(estimatedTransitionProbabilityMatrix[1][0] + estimatedTransitionProbabilityMatrix[1][1], 5));
        }
Esempio n. 14
0
        public void Train(double[][] observations, int numberOfIterations, double likelihoodTolerance)
        {
            if (_initialize)
            {
                Initialize(observations);
            }
            if (_pi == null || _transitionProbabilityMatrix == null || _emission == null)
            {
                throw new ApplicationException("Initialize the model with initial valuesss");
            }
            var model = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <Mixture <IMultivariateDistribution> > {
                Pi = _pi, TransitionProbabilityMatrix = _transitionProbabilityMatrix, Emissions = _emission
            });                                                                                                                                                                                                                 //new HiddenMarkovModelState<Mixture<IMultivariateDistribution>>(_pi, _transitionProbabilityMatrix, _emission);

            model.Normalized = Normalized;
            var alg = new BaumWelchMixtureDistribution(Helper.Convert(observations), model);
            var estimatedParameters = alg.Run(numberOfIterations, likelihoodTolerance);

            _pi = estimatedParameters.Pi;
            _transitionProbabilityMatrix = estimatedParameters.TransitionProbabilityMatrix;
            _emission  = estimatedParameters.Emission;
            Likelihood = estimatedParameters.Likelihood;
        }
Esempio n. 15
0
        public void TestForwardNormalizedRun1()
        {
            var states = new List <IState> {
                new State(0, "H"), new State(1, "L")
            };

            var startDistribution = new [] { 0.5, 0.5 };

            var tpm = new double[2][];

            tpm[0] = new[] { 0.5, 0.5 };
            tpm[1] = new[] { 0.4, 0.6 };

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


            var emissions = new DiscreteDistribution[2];

            emissions[0] = new DiscreteDistribution(new double[] { 0, 1, 2, 3 }, new[] { 0.2, 0.3, 0.3, 0.2 });
            emissions[1] = new DiscreteDistribution(new double[] { 0, 1, 2, 3 }, new[] { 0.3, 0.2, 0.2, 0.3 });

            var algo = new ForwardBackward(true);
            var res  = algo.RunForward(observations, HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <DiscreteDistribution>()
            {
                Pi = startDistribution, TransitionProbabilityMatrix = tpm, Emissions = emissions
            }));                                                                                                                                                                                                                      //new HiddenMarkovModelState<DiscreteDistribution>(startDistribution, tpm, emissions));

            // TODO : Check for Log
            Assert.AreEqual(-5.5614629361549142, res);
        }
        public void Compare_AlphaBetaAndKsiGammaCalculation_EqualTransitionProbabilityMatrixes()
        {
            const int numberOfStates = 2;

            var util         = new TestDataUtils();
            var observations = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));
            var model        = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <NormalDistribution>()
            {
                NumberOfStates = numberOfStates, Emissions = CreateEmissions(observations, numberOfStates)
            });

            model.Normalized = true;
            var observationsList = new List <IObservation>();
            var weights          = new double[observations.Length];

            for (var i = 0; i < observations.Length; i++)
            {
                observationsList.Add(new Observation(observations[i], i.ToString()));
            }
            var baseParameters = new BasicEstimationParameters <NormalDistribution> {
                Model = model, Observations = Helper.Convert(observations), Normalized = model.Normalized
            };
            var alphaEstimator = new AlphaEstimator <NormalDistribution>();
            var alpha          = alphaEstimator.Estimate(baseParameters);

            var betaEstimator = new BetaEstimator <NormalDistribution>();
            var beta          = betaEstimator.Estimate(baseParameters);

            var @params = new AdvancedEstimationParameters <NormalDistribution>
            {
                Alpha        = alpha,
                Beta         = beta,
                Observations = observationsList,
                Model        = model,
                Normalized   = model.Normalized
            };
            var gammaEstimator = new GammaEstimator <NormalDistribution>();
            var ksiEstimator   = new KsiEstimator <NormalDistribution>();

            var estimatorKsiGamma  = new TransitionProbabilityEstimator <NormalDistribution>();
            var parametersKsiGamma = new KsiGammaTransitionProbabilityMatrixParameters <NormalDistribution>
            {
                Model      = model,
                Ksi        = ksiEstimator.Estimate(@params),
                Gamma      = gammaEstimator.Estimate(@params),
                T          = observations.Length,
                Normalized = model.Normalized
            };
            var estimatorAlphaBeta  = new TransitionProbabilityEstimator <NormalDistribution>();
            var parametersAlphaBeta = new AlphaBetaTransitionProbabiltyMatrixParameters <NormalDistribution>
            {
                Alpha        = alpha,
                Beta         = beta,
                Model        = model,
                Observations = observations,
                Normalized   = model.Normalized,
                Weights      = weights
            };

            var estimatedTransitionProbabilityMatrixKsiGamma  = estimatorKsiGamma.Estimate(parametersKsiGamma);
            var estimatedTransitionProbabilityMatrixAlphaBeta = estimatorAlphaBeta.Estimate(parametersAlphaBeta);

            Assert.AreEqual(Math.Round(estimatedTransitionProbabilityMatrixKsiGamma[0][0], 5), Math.Round(estimatedTransitionProbabilityMatrixAlphaBeta[0][0], 5));
            Assert.AreEqual(Math.Round(estimatedTransitionProbabilityMatrixKsiGamma[0][1], 5), Math.Round(estimatedTransitionProbabilityMatrixAlphaBeta[0][1], 5));
            Assert.AreEqual(Math.Round(estimatedTransitionProbabilityMatrixKsiGamma[1][0], 5), Math.Round(estimatedTransitionProbabilityMatrixAlphaBeta[1][0], 5));
            Assert.AreEqual(Math.Round(estimatedTransitionProbabilityMatrixKsiGamma[1][1], 5), Math.Round(estimatedTransitionProbabilityMatrixAlphaBeta[1][1], 5));
        }
Esempio n. 17
0
        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
                };
                _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));
                EstimateTransitionProbabilityMatrix(_gammaEstimator.Estimate(@params), _ksiEstimator.Estimate(@params), null, _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

                    var coefficients = mixtureCoefficientsEstimator.Estimate(@params)[n];
                    if (Normalized)
                    {
                        mixtureCoefficientsEstimator.Denormalize();
                    }
                    @params.Mu = mixtureMuEstimator.Estimate(@params);
                    for (var l = 0; l < mixturesComponents; l++)
                    {
                        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);
        }