Ejemplo n.º 1
0
        /// <summary>
        ///   Generates a random vector of observations from the model.
        /// </summary>
        ///
        /// <param name="samples">The number of samples to generate.</param>
        /// <param name="logLikelihood">The log-likelihood of the generated observation sequence.</param>
        /// <param name="path">The Viterbi path of the generated observation sequence.</param>
        ///
        /// <returns>A random vector of observations drawn from the model.</returns>
        ///
        public TObservation[] Generate(int samples, out int[] path, out double logLikelihood)
        {
            double[] transitions = LogInitial;

            logLikelihood = 0; // log(1)
            path          = new int[samples];

            TObservation[] observations = new TObservation[samples];


            // For each observation to be generated
            for (int t = 0; t < observations.Length; t++)
            {
                // Navigate randomly on one of the state transitions
                int state = GeneralDiscreteDistribution.Random(Elementwise.Exp(transitions));

                // Generate a sample for the state
                var          sampling = (ISampleableDistribution <TObservation>)Emissions[state];
                TObservation symbol   = sampling.Generate();

                // Store the sample
                observations[t] = symbol;
                path[t]         = state;

                // Compute log-likelihood up to this point
                logLikelihood += transitions[state] +
                                 Emissions[state].LogProbabilityFunction(symbol);

                // Continue sampling
                transitions = LogTransitions.GetRow(state);
            }

            return(observations);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Creates a new object that is a copy of the current instance.
        /// </summary>
        ///
        /// <returns>
        ///   A new object that is a copy of this instance.
        /// </returns>
        ///
        public override object Clone()
        {
            double[][] A  = LogTransitions.Copy();
            double[][] B  = LogEmissions.Copy();
            double[]   pi = LogInitial.Copy();

            return(new HiddenMarkovModel(A, B, pi, logarithm: true));
        }
Ejemplo n.º 3
0
        /// <summary>
        ///   Creates a new object that is a copy of the current instance.
        /// </summary>
        ///
        /// <returns>
        ///   A new object that is a copy of this instance.
        /// </returns>
        ///
        public virtual object Clone()
        {
            double[][]      A  = LogTransitions.Copy();
            double[]        pi = LogInitial.Copy();
            TDistribution[] B  = Emissions.MemberwiseClone();

            return(new HiddenMarkovModel <TDistribution, TObservation>(A, B, pi, logarithm: true));
        }
Ejemplo n.º 4
0
        /// <summary>
        ///   Creates a new object that is a copy of the current instance.
        /// </summary>
        ///
        /// <returns>
        ///   A new object that is a copy of this instance.
        /// </returns>
        ///
        public override object Clone()
        {
            double[,] A = (double[, ])LogTransitions.Clone();
            double[,] B = (double[, ])Emissions.Clone();
            double[] pi = (double[])LogInitial.Clone();

            return(new HiddenMarkovModel(A, B, pi, logarithm: true));
        }
Ejemplo n.º 5
0
        public HiddenMarkovModel <GeneralDiscreteDistribution> ToContinuousModel()
        {
            var transitions   = (double[, ])LogTransitions.Clone();
            var probabilities = (double[])LogInitial.Clone();

            var emissions = new GeneralDiscreteDistribution[States];

            for (int i = 0; i < emissions.Length; i++)
            {
                emissions[i] = new GeneralDiscreteDistribution(Accord.Math.Matrix.GetRow(Emissions, i));
            }

            return(new HiddenMarkovModel <GeneralDiscreteDistribution>(transitions, emissions, probabilities));
        }