Ejemplo n.º 1
0
        protected double[][] EstimateGamma(double[][][] xi,
                                           ForwardBackwardCalculator fbc)
        {
            double[][] gamma = EngineArray.AllocateDouble2D(xi.Length + 1, xi[0].Length);

            for (int t = 0; t < (xi.Length + 1); t++)
            {
                EngineArray.Fill(gamma[t], 0.0);
            }

            for (int t = 0; t < xi.Length; t++)
            {
                for (int i = 0; i < xi[0].Length; i++)
                {
                    for (int j = 0; j < xi[0].Length; j++)
                    {
                        gamma[t][i] += xi[t][i][j];
                    }
                }
            }

            for (int j = 0; j < xi[0].Length; j++)
            {
                for (int i = 0; i < xi[0].Length; i++)
                {
                    gamma[xi.Length][j] += xi[xi.Length - 1][i][j];
                }
            }

            return(gamma);
        }
Ejemplo n.º 2
0
        public override double[][][] EstimateXi(IMLDataSet sequence,
                                                ForwardBackwardCalculator fbc, HiddenMarkovModel hmm)
        {
            if (sequence.Count <= 1)
            {
                throw new EncogError(
                          "Must have more than one observation");
            }

            double[][][] xi          = EngineArray.AllocDouble3D((int)sequence.Count - 1, hmm.StateCount, hmm.StateCount);
            double       probability = fbc.Probability();

            for (int t = 0; t < (sequence.Count - 1); t++)
            {
                IMLDataPair o = sequence[t + 1];

                for (int i = 0; i < hmm.StateCount; i++)
                {
                    for (int j = 0; j < hmm.StateCount; j++)
                    {
                        xi[t][i][j] = (fbc.AlphaElement(t, i)
                                       * hmm.TransitionProbability[i][j]
                                       * hmm.StateDistributions[j].Probability(o) * fbc
                                       .BetaElement(t + 1, j)) / probability;
                    }
                }
            }

            return(xi);
        }
Ejemplo n.º 3
0
 public abstract double[][][] EstimateXi(IMLDataSet sequence,
                                         ForwardBackwardCalculator fbc, HiddenMarkovModel hmm);
Ejemplo n.º 4
0
        public void Iteration()
        {
            HiddenMarkovModel nhmm;

            nhmm = _method.Clone();

            var allGamma = new double[_training.SequenceCount][][];

            double[][] aijNum = EngineArray.AllocateDouble2D(_method.StateCount, _method.StateCount);
            var        aijDen = new double[_method.StateCount];

            EngineArray.Fill(aijDen, 0.0);
            for (int i = 0; i < _method.StateCount; i++)
            {
                EngineArray.Fill(aijNum[i], 0.0);
            }

            int g = 0;

            foreach (IMLDataSet obsSeq in _training.Sequences)
            {
                ForwardBackwardCalculator fbc = GenerateForwardBackwardCalculator(
                    obsSeq, _method);

                double[][][] xi    = EstimateXi(obsSeq, fbc, _method);
                double[][]   gamma = allGamma[g++] = EstimateGamma(xi, fbc);

                for (int i = 0; i < _method.StateCount; i++)
                {
                    for (int t = 0; t < (obsSeq.Count - 1); t++)
                    {
                        aijDen[i] += gamma[t][i];

                        for (int j = 0; j < _method.StateCount; j++)
                        {
                            aijNum[i][j] += xi[t][i][j];
                        }
                    }
                }
            }

            for (int i = 0; i < _method.StateCount; i++)
            {
                if (aijDen[i] == 0.0)
                {
                    for (int j = 0; j < _method.StateCount; j++)
                    {
                        nhmm.TransitionProbability[i][j] =
                            _method.TransitionProbability[i][j];
                    }
                }
                else
                {
                    for (int j = 0; j < _method.StateCount; j++)
                    {
                        nhmm.TransitionProbability[i][j] = aijNum[i][j]
                                                           / aijDen[i];
                    }
                }
            }

            /* compute pi */
            for (int i = 0; i < _method.StateCount; i++)
            {
                nhmm.Pi[i] = 0.0;
            }

            for (int o = 0; o < _training.SequenceCount; o++)
            {
                for (int i = 0; i < _method.StateCount; i++)
                {
                    nhmm.Pi[i] += (allGamma[o][0][i] / _training
                                   .SequenceCount);
                }
            }

            /* compute pdfs */
            for (int i = 0; i < _method.StateCount; i++)
            {
                var    weights = new double[_training.Count];
                double sum     = 0.0;
                int    j       = 0;

                int o = 0;
                foreach (IMLDataSet obsSeq in _training.Sequences)
                {
                    for (int t = 0; t < obsSeq.Count; t++, j++)
                    {
                        sum += weights[j] = allGamma[o][t][i];
                    }
                    o++;
                }

                for (j--; j >= 0; j--)
                {
                    weights[j] /= sum;
                }

                IStateDistribution opdf = nhmm.StateDistributions[i];
                opdf.Fit(_training, weights);
            }

            _method = nhmm;
            Iterations++;
        }