Accord.Statistics.Models.Markov.MarkovHelperMethods.checkAndConvert C# (CSharp) Method

checkAndConvert() static private method

Converts a univariate or multivariate array of observations into a two-dimensional jagged array.
static private checkAndConvert ( Array observations, int dimension ) : double[][]
observations System.Array
dimension int
return double[][]
        internal static double[][] checkAndConvert(Array observations, int dimension)
        {
            if (observations == null)
                throw new ArgumentNullException("observations");

            // Test if the observations are multivariate
            {
                double[][] multivariate = observations as double[][];
                if (multivariate != null)
                {
                    for (int i = 0; i < multivariate.Length; i++)
                    {
                        if (multivariate[i].Length != dimension)
                        {
                            throw new DimensionMismatchException("observations",
                                "This model expects observations of length " + dimension);
                        }
                    }

                    return multivariate;
                }

                // Test if the observations are univariate
                double[] univariate = observations as double[];
                if (univariate != null)
                {
                    if (dimension != 1)
                    {
                        throw new DimensionMismatchException("observations",
                            "This model expects univariate observations");
                    }

                    return Accord.Math.Matrix.Split(univariate, dimension);
                }
            }

            {
                // Test if the observations are multivariate integers
                int[][] multivariate = observations as int[][];
                if (multivariate != null)
                {
                    for (int i = 0; i < multivariate.Length; i++)
                    {
                        if (multivariate[i].Length != dimension)
                        {
                            throw new DimensionMismatchException("observations",
                                "This model expects observations of length " + dimension);
                        }
                    }

                    return multivariate.ToDouble();
                }

                // Test if the observations are univariate
                int[] univariate = observations as int[];
                if (univariate != null)
                {
                    if (dimension != 1)
                    {
                        throw new DimensionMismatchException("observations",
                            "This model expects univariate observations");
                    }

                    return Accord.Math.Matrix.Split(univariate, dimension).ToDouble();
                }
            }

            // else
            throw new ArgumentException("Argument should be either of type " +
                    "double[] (for univariate observation) or double[][] (for " +
                    "multivariate observation).", "observations");
        }

Usage Example

示例#1
0
        /// <summary>
        ///   Calculates the log-likelihood that this model has generated the
        ///   given observation sequence along the given state path.
        /// </summary>
        ///
        /// <param name="observations">A sequence of observations. </param>
        /// <param name="path">A sequence of states. </param>
        ///
        /// <returns>
        ///   The log-likelihood that the given sequence of observations has
        ///   been generated by this model along the given sequence of states.
        /// </returns>
        ///
        public double Evaluate(Array observations, int[] path)
        {
            if (observations == null)
            {
                throw new ArgumentNullException("observations");
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (observations.Length == 0)
            {
                return(Double.NegativeInfinity);
            }


            double[][] x = MarkovHelperMethods.checkAndConvert(observations, dimension);


            double logLikelihood = Probabilities[path[0]]
                                   + Emissions[path[0]].LogProbabilityFunction(x[0]);

            for (int i = 1; i < observations.Length; i++)
            {
                logLikelihood = Accord.Math.Special.LogSum(logLikelihood, Transitions[path[i - 1],
                                                                                      path[i]] + Emissions[path[i]].LogProbabilityFunction(x[i]));
            }

            // Return the sequence probability
            return(logLikelihood);
        }
All Usage Examples Of Accord.Statistics.Models.Markov.MarkovHelperMethods::checkAndConvert