public IHiddenMarkovModel <DiscreteDistribution> 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 <DiscreteDistribution> { Pi = _estimatedPi, TransitionProbabilityMatrix = _estimatedTransitionProbabilityMatrix, Emissions = _estimatedEmissions }); //new HiddenMarkovModelState<DiscreteDistribution>(_estimatedPi, _estimatedTransitionProbabilityMatrix, _estimatedEmissions); _currentModel.Normalized = Normalized; _currentModel.Likelihood = _estimatedModel.Likelihood; } // Run Forward-Backward procedure forwardBackward.RunForward(_observations, _currentModel); forwardBackward.RunBackward(_observations, _currentModel); var @params = new AdvancedEstimationParameters <DiscreteDistribution> { Alpha = forwardBackward.Alpha, Beta = forwardBackward.Beta, Observations = _observations, Model = _currentModel, Normalized = _currentModel.Normalized }; _gammaEstimator = new GammaEstimator <DiscreteDistribution>(); _ksiEstimator = new KsiEstimator <DiscreteDistribution>(); // Estimate transition probabilities and start distribution EstimatePi(_gammaEstimator.Estimate(@params)); EstimateTransitionProbabilityMatrix(_gammaEstimator.Estimate(@params), _ksiEstimator.Estimate(@params), null, _observations.Count); // Estimate Emmisions for (var j = 0; j < _currentModel.N; j++) { _estimatedEmissions[j] = (DiscreteDistribution)_estimatedEmissions[j].Evaluate(_discreteObservations, _discreteSymbols, _gammaEstimator.Estimate(@params).GetColumn(j), Normalized); } _estimatedModel = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <DiscreteDistribution> { 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); }
public IHiddenMarkovModel <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 <IMultivariateDistribution> { Pi = _estimatedPi, TransitionProbabilityMatrix = _estimatedTransitionProbabilityMatrix, Emissions = _estimatedEmissions }); //new HiddenMarkovModelState<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); var @params = new AdvancedEstimationParameters <IMultivariateDistribution> { Alpha = forwardBackward.Alpha, Beta = forwardBackward.Beta, Observations = _observations, Model = _currentModel, Normalized = _currentModel.Normalized }; _gammaEstimator = new GammaEstimator <IMultivariateDistribution>(); _ksiEstimator = new KsiEstimator <IMultivariateDistribution>(); _muEstimator = new MuMultivariateEstimator <IMultivariateDistribution>(); _sigmaEstimator = new SigmaMultivariateEstimator <IMultivariateDistribution>(); EstimatePi(_gammaEstimator.Estimate(@params)); EstimateTransitionProbabilityMatrix(_gammaEstimator.Estimate(@params), _ksiEstimator.Estimate(@params), null, _observations.Count); // Estimate observation probabilities var muParams = new MuEstimationParameters <IMultivariateDistribution> { Gamma = _gammaEstimator.Estimate(@params), Model = _currentModel, Normalized = _currentModel.Normalized, Observations = _observations }; var muVector = _muEstimator.Estimate(muParams); var sigmaVector = _sigmaEstimator.Estimate(new SigmaEstimationParameters <IMultivariateDistribution, double[][]>(muParams) { Mean = muVector }); for (var n = 0; n < _currentModel.N; n++) { _estimatedEmissions[n] = new NormalDistribution(muVector[n], sigmaVector[n]); } _estimatedModel = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <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); }
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); }