Beispiel #1
0
        public IPredictionResult Predict <TDistribution>(IHiddenMarkovModel <TDistribution> model, IPredictionRequest request) where TDistribution : IDistribution
        {
            if (!request.ValidateAlgorithmSpecificParameters())
            {
                return(null);
            }

            var n = Convert.ToInt32(request.AlgorithmSpecificParameters["NumberOfSamplePoints"]);
            var k = Convert.ToInt32(request.AlgorithmSpecificParameters["NumberOfWinningPoints"]);

            //var ratios = GetMaximumChangeRatios(request.TrainingSet);

            //var candidates = CreateStartingPool(request.TestSet[0], n, ratios);

/*            do
 *          {
 *              //var winningCandidate = CalculateWinningCandidates(k, candidates, model);
 *              //candidates = CreateCandidatePoolFromArray(winningCandidate, n, ratios);
 *          } while (Continue(request, candidates));*/

            var result = new PredictionResult()
            {
                Predicted = new double[1][]
            };

            //result.Predicted[0] = GetBestProbabilityCandidate(candidates);

            return(result);
        }
Beispiel #2
0
        public double RunForward <TEmmisionType>(IList <IObservation> observations, IHiddenMarkovModel <TEmmisionType> model) where TEmmisionType : IDistribution
        {
            var alphaEstimator = new AlphaEstimator <TEmmisionType>();

            _alpha = alphaEstimator.Estimate(new BasicEstimationParameters <TEmmisionType> {
                Model = model, Observations = observations, Normalized = Normalized
            });
            var T      = observations.Count;
            var result = (Normalized) ? double.NaN : 0d;

            // Calculate results
            for (var i = 0; i < model.N; i++)
            {
                if (Normalized)
                {
                    result = LogExtention.eLnSum(result, _alpha[T - 1][i]);
                }
                else
                {
                    result += _alpha[T - 1][i];
                }
            }

            return(result);
        }
Beispiel #3
0
        public BaumWelch(IList <IObservation> observations, IHiddenMarkovModel <DiscreteDistribution> model, IList <IObservation> symbols) : base(model)
        {
            _currentModel = model;

            _observations = observations;

            _discreteSymbols      = new double[_currentModel.M];
            _discreteObservations = new double[_observations.Count];

            for (var i = 0; i < _currentModel.M; i++)
            {
                _discreteSymbols[i] = symbols[i].Value[0];
            }
            for (var i = 0; i < _observations.Count; i++)
            {
                _discreteObservations[i] = observations[i].Value[0];
            }

            _estimatedEmissions = new DiscreteDistribution[_currentModel.N];
            for (var i = 0; i < _currentModel.N; i++)
            {
                _estimatedEmissions[i] = new DiscreteDistribution(_discreteSymbols, _discreteObservations);
            }
            _estimatedModel = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <DiscreteDistribution> {
                Pi = _estimatedPi, TransitionProbabilityMatrix = _estimatedTransitionProbabilityMatrix, Emissions = _estimatedEmissions
            });                                                                                                                                                                                                                                     //new HiddenMarkovModelState<DiscreteDistribution>(_estimatedPi, _estimatedTransitionProbabilityMatrix, _estimatedEmissions);
            Normalized = model.Normalized;
        }
Beispiel #4
0
        private double[] PredictNextValue <TDistribution>(IHiddenMarkovModel <TDistribution> model, IPredictionRequest request, double[][] trainingSet)
            where TDistribution : IDistribution
        {
            var N      = trainingSet.Length;
            var K      = trainingSet[0].Length;
            var result = new double[K];

            var yesterday           = trainingSet[N - 1];
            var forwardBackward     = new ForwardBackward(model.Normalized);
            var yesterdayLikelihood = forwardBackward.RunForward(Helper.Convert(new[] { yesterday }), model);

            Debug.WriteLine("Yesterday Likelihood : " + new Vector(yesterday) + " : " + yesterdayLikelihood + " ");

            var guessess       = FindMostSimilarObservations(model, trainingSet, yesterdayLikelihood, request.Tolerance);
            var bestGuessPlace = FindBestGuess(request, guessess);
            var tomorrow       = trainingSet[bestGuessPlace.PlaceInSequence + 1];
            var mostSimilar    = trainingSet[bestGuessPlace.PlaceInSequence];

            for (var k = 0; k < K; k++)
            {
                if (bestGuessPlace.PlaceInSequence != trainingSet.Length)
                {
                    result[k] = yesterday[k] + (tomorrow[k] - mostSimilar[k]);
                }
            }

            Debug.WriteLine("Predicted (for day " + N + ") : " + new Vector(result) + " : " + forwardBackward.RunForward(Helper.Convert(result), model));

            return(result);
        }
        /// <summary>
        ///     Viterbi based prediction algorithm. For each observation in test set we perform following action :
        ///     1. Run Viterbi and find best state that generated last day observation
        ///     2. Prediction for next day is weighted average of means of predicting state emissions
        ///     3. Re-Train the model with new value added from training set
        ///     4. Return to step 1
        /// </summary>
        /// <typeparam name="TDistribution"></typeparam>
        /// <param name="model"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public IPredictionResult Predict <TDistribution>(IHiddenMarkovModel <TDistribution> model, IPredictionRequest request) where TDistribution : IDistribution
        {
            NumberOfIterations  = request.NumberOfTrainingIterations;
            LikelihoodTolerance = request.TrainingLikelihoodTolerance;
            var predictions = new PredictionResult {
                Predicted = new double[request.NumberOfDays][]
            };
            var trainingSet = (double[][])request.TrainingSet.Clone();

            for (int i = 0; i < request.NumberOfDays; i++)
            {
                var prediction = PredictNextValue(model, trainingSet);
                if (request.NumberOfDays > 1)
                {
                    trainingSet = new double[request.TrainingSet.Length + i + 1][];
                    request.TrainingSet.CopyTo(trainingSet, 0);
                    for (var j = 0; j < i + 1; j++)
                    {
                        trainingSet[request.TrainingSet.Length + j] = request.TestSet[j];
                    }
                    var iterations = NumberOfIterations;
                    ((IMachineLearningMultivariateModel)model).Train(trainingSet, iterations, LikelihoodTolerance);
                }
                predictions.Predicted[i] = prediction;
            }

            return(predictions);
        }
Beispiel #6
0
        public IPredictionResult Predict <TDistribution>(IHiddenMarkovModel <TDistribution> model, IPredictionRequest request)
            where TDistribution : IDistribution
        {
            var trainingSet = (double[][])request.TrainingSet.Clone();
            var predictions = new PredictionResult {
                Predicted = new double[request.NumberOfDays][]
            };

            for (int i = 0; i < request.NumberOfDays; i++)
            {
                var prediction = PredictNextValue(model, request, trainingSet);
                if (request.NumberOfDays > 1)
                {
                    trainingSet = new double[request.TrainingSet.Length + i + 1][];
                    request.TrainingSet.CopyTo(trainingSet, 0);
                    for (var j = 0; j < i + 1; j++)
                    {
                        trainingSet[request.TrainingSet.Length + j] = request.TestSet[j];
                    }
                }
                predictions.Predicted[i] = prediction;
            }

            return(predictions);
        }
Beispiel #7
0
 public ParameterEstimations(IHiddenMarkovModel <TDistribution> model, IList <IObservation> observations, double[][] alpha, double[][] beta)
 {
     _model        = model;
     _observations = observations;
     _alpha        = alpha;
     _beta         = beta;
 }
 public bool Equals(IHiddenMarkovModel <TDistribution> other)
 {
     if (!(VectorExtentions.EqualsTo(Pi, other.Pi) && TransitionProbabilityMatrix.EqualsTo(other.TransitionProbabilityMatrix) && N == other.N && M == other.M && Likelihood == other.Likelihood))
     {
         return(false);
     }
     return(!Emission.Where((t, i) => !t.Equals(other.Emission[i])).Any());
 }
Beispiel #9
0
 public ViterbiEngine(IHiddenMarkovModel <S, T> model)
 {
     LatestViterbiStates = new List <ViterbiState <S, T> >()
     {
         ViterbiState <S, T> .InitialState()
     };
     Model = model;
 }
Beispiel #10
0
        public double HeuristicFunction <TDistribution>(double[] node, IHiddenMarkovModel <TDistribution> model)
            where TDistribution : IDistribution
        {
            //var arr = trainingSet.Concat(new []{ node });
            var forwardBackward = new ForwardBackward(model.Normalized);
            var h = forwardBackward.RunForward(Helper.Convert(new[] { node }), model);

            return(h);
        }
        /// <summary>
        ///   Initializes a new instance of the <see cref="BaseRunningMarkovStatistics"/> class.
        /// </summary>
        ///
        /// <param name="model">The Markov model.</param>
        ///
        public BaseRunningMarkovStatistics(IHiddenMarkovModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            current = new double[model.States];
        }
Beispiel #12
0
        private double[] PredictNextValue <TDistribution>(IHiddenMarkovModel <TDistribution> model, double[][] trainingSet) where TDistribution : IDistribution
        {
            var alg = new Viterbi(model.Normalized);
            var mpp = alg.Run(Helper.Convert(trainingSet), model.GetStates(), model.Pi, model.TransitionProbabilityMatrix, model.GetEmissions());

            var emission   = model.Emission[mpp[trainingSet.Length - 1].Index];
            var prediction = CalculatePredictionValue(emission, trainingSet);

            return(prediction);
        }
Beispiel #13
0
        public static IList <IState> GetStates <TDistribution>(this IHiddenMarkovModel <TDistribution> model) where TDistribution : IDistribution
        {
            var result = new List <IState>();

            for (int n = 0; n < model.N; n++)
            {
                result.Add(new State(n, n.ToString()));
            }
            return(result);
        }
Beispiel #14
0
        public static IDistribution[] GetEmissions <TDistribution>(this IHiddenMarkovModel <TDistribution> model) where TDistribution : IDistribution
        {
            var result = new IDistribution[model.N];

            for (int n = 0; n < model.N; n++)
            {
                result[n] = model.Emission[n];
            }
            return(result);
        }
Beispiel #15
0
        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);
        }
Beispiel #16
0
        public IPredictionResult Predict <TDistribution>(IHiddenMarkovModel <TDistribution> model, IPredictionRequest request) where TDistribution : IDistribution
        {
            var selectionMethod    = new TournamentSelection(TournamentSize);
            var crossoverAlgorithm = new Crossover(CrossoverProbability);
            var mutationAlgorithm  = new Mutator(MutationProbability);
            var evaluator          = new HmmEvaluator <TDistribution>(model, new ForwardBackward(true));

            var parameters = new GeneticSolverParameters
            {
                CrossOverProbability = CrossoverProbability,
                MutationProbability  = MutationProbability,
                NumberOfGenerations  = NumberOfGenerations,
                PopulationSize       = request.NumberOfDays * 10,
                TournamentSize       = TournamentSize
            };
            var predictions = new PredictionResult {
                Predicted = new double[request.NumberOfDays][]
            };

            var solver = new GeneticSolver(parameters, mutationAlgorithm, crossoverAlgorithm, evaluator, selectionMethod);
            var populationInitializer = new FromTimeSeriesRandomInitializer(request.TrainingSet);
            var population            = populationInitializer.Initialize <decimal>(parameters.PopulationSize, request.NumberOfDays, MutationProbability);

            for (int i = 0; i < population.Count; i++)
            {
                var chromosome = population[i];
                population[i].FintnessValue = evaluator.Evaluate(chromosome);
            }

            var result = solver.Solve(population);
            // Get best fitted chromosome
            var maximumFitness = result[0].FintnessValue;
            var solution       = result[0];

            foreach (var chromosome in result)
            {
                if (maximumFitness <= chromosome.FintnessValue)
                {
                    solution       = chromosome;
                    maximumFitness = chromosome.FintnessValue;
                }
            }
            // Convert it to array
            for (int i = 0; i < solution.Representation.Length; i++)
            {
                predictions.Predicted[i] = Array.ConvertAll(solution.Representation[i].Representation, x => (double)Convert.ChangeType(x, typeof(double)));
            }

            return(predictions);
        }
Beispiel #17
0
        public BaumWelchMultivariateDistribution(IList <IObservation> observations, IHiddenMarkovModel <IMultivariateDistribution> model) : base(model)
        {
            _currentModel       = model;
            _observations       = observations;
            _estimatedEmissions = new IMultivariateDistribution[model.N];
            for (var i = 0; i < model.N; i++)
            {
                _estimatedEmissions[i] = new NormalDistribution(0);
            }

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

            Normalized = _estimatedModel.Normalized = model.Normalized;
        }
Beispiel #18
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;
        }
Beispiel #19
0
        public double RunBackward <TEmmisionType>(IList <IObservation> observations, IHiddenMarkovModel <TEmmisionType> model) where TEmmisionType : IDistribution
        {
            var betaEstimator = new BetaEstimator <TEmmisionType>();

            _beta = betaEstimator.Estimate(new BasicEstimationParameters <TEmmisionType> {
                Model = model, Observations = observations, Normalized = Normalized
            });
            var result = (Normalized) ? double.NaN : 0d;

            for (var j = 0; j < model.N; j++)
            {
                if (Normalized)
                {
                    result = LogExtention.eLnSum(result,
                                                 LogExtention.eLnProduct(LogExtention.eLn(model.Pi[j]), _beta[1][j]));
                }
                else
                {
                    result += model.Pi[j] * _beta[1][j];
                }
            }

            return(result);
        }
 /// <summary>
 ///   Initializes a new instance of the <see cref="BaseBaumWelchLearning"/> class.
 /// </summary>
 ///
 protected BaseBaumWelchLearning(IHiddenMarkovModel model)
 {
     this.convergence = new AbsoluteConvergence();
     this.model       = model;
 }
 /// <summary>
 ///   Initializes a new instance of the <see cref="BaseBaumWelchLearning"/> class.
 /// </summary>
 /// 
 protected BaseBaumWelchLearning(IHiddenMarkovModel model)
 {
     this.model = model;
 }
Beispiel #22
0
        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);
        }
Beispiel #23
0
        public double[][] Search <TDistribution>(double[][] population, int k, int n, int numberOfIterations, MaximumChangeRatios ratios, IHiddenMarkovModel <TDistribution> model)
            where TDistribution : IDistribution
        {
            var current = ExpandCurrentPopulation(population, n, ratios.Down, ratios.Up);;

            for (var i = 0; i < numberOfIterations; i++)
            {
                var next = GenerateNextStagePopulation(current, k, n, ratios, model);
                current = next;
            }

            return(current);
        }
Beispiel #24
0
 /**
  * Create a Fixed-Lag-Smoothing implementation, that sets up the required
  * persistent values.
  *
  * @param hmm
  *            a hidden Markov model with S * S transition matrix <b>T</b>
  * @param d
  *            d, the length of the lag for smoothing
  */
 public FixedLagSmoothing(IHiddenMarkovModel hmm, int d)
 {
     this.hmm = hmm;
     this.d   = d;
     initPersistent();
 }
Beispiel #25
0
        public double[][] GenerateNextStagePopulation <TDistribution>(double[][] current, int k, int n, MaximumChangeRatios ratios, IHiddenMarkovModel <TDistribution> model)
            where TDistribution : IDistribution
        {
            var dic = new SortedList <double, double[]>();

            for (var j = 0; j < current.Length; j++)
            {
                var h = HeuristicFunction(current[j], model);
                if (dic.Count < k)
                {
                    dic.Add(h, current[j]);
                }
                else
                {
                    if (dic.Keys[dic.Count] < h)
                    {
                        dic.Remove(dic.Keys[dic.Count]);
                        dic.Add(h, current[j]);
                    }
                }
            }

            current = (from e in dic select e.Value).ToArray();
            current = ExpandCurrentPopulation(current, n, ratios.Down, ratios.Up);
            return(current);
        }
Beispiel #26
0
        private IList <ObservationWithLikelihood <double[]> > FindMostSimilarObservations <TDistribution>(IHiddenMarkovModel <TDistribution> model, double[][] trainingSet, double yesterdayLikelihood, double tolerance)
            where TDistribution : IDistribution
        {
            var N               = trainingSet.Length;
            var guessess        = new List <ObservationWithLikelihood <double[]> >();
            var forwardBackward = new ForwardBackward(model.Normalized);

            for (var n = N - 2; n > 0; n--)
            {
                var x          = Helper.Convert(new[] { trainingSet[n] });
                var likelihood = forwardBackward.RunForward(x, model);
                //Debug.Write((new Vector(observations[n])).ToString() + " : " + likelihood + " " + Environment.NewLine);

                if (Math.Abs(yesterdayLikelihood) - tolerance < Math.Abs(likelihood) && Math.Abs(yesterdayLikelihood) + tolerance > Math.Abs(likelihood))
                {
                    guessess.Add(new ObservationWithLikelihood <double[]>()
                    {
                        LogLikelihood = likelihood, Observation = trainingSet[n], PlaceInSequence = n - 1
                    });
                }
            }

            return(guessess);
        }
Beispiel #27
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);
        }
Beispiel #28
0
 public HmmEvaluator(IHiddenMarkovModel <TEmmisionType> model, IForwardBackward fitnessClaculator)
 {
     _fitnessClaculator = fitnessClaculator;
     _model             = model;
 }
 public HMMForwardBackward(IHiddenMarkovModel hmm)
 {
     this.hmm = hmm;
 }
 public HMMForwardBackwardConstantSpace(IHiddenMarkovModel hmm)
     : base(hmm)
 {
 }
Beispiel #31
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="BaumWelchLearningBase"/> class.
 /// </summary>
 ///
 protected BaumWelchLearningBase(IHiddenMarkovModel model)
 {
     this.model = model;
 }
 /// <summary>
 ///   Initializes a new instance of the <see cref="BaseBaumWelchLearning"/> class.
 /// </summary>
 /// 
 protected BaseBaumWelchLearning(IHiddenMarkovModel model)
 {
     this.convergence = new AbsoluteConvergence();
     this.model = model;
 }
        /// <summary>
        ///   Initializes a new instance of the <see cref="BaseRunningMarkovStatistics"/> class.
        /// </summary>
        /// 
        /// <param name="model">The Markov model.</param>
        /// 
        public BaseRunningMarkovStatistics(IHiddenMarkovModel model)
        {
            if (model == null)
                throw new ArgumentNullException("model");

            current = new double[model.States];
        }