Ejemplo n.º 1
0
        public void CovarianceTest6()
        {
            double[][] matrix = new double[, ]
            {
                { 4.0, 2.0, 0.60 },
                { 4.2, 2.1, 0.59 },
                { 3.9, 2.0, 0.58 },
                { 4.3, 2.1, 0.62 },
                { 4.1, 2.2, 0.63 }
            }.ToArray();


            double[,] expected = new double[, ]
            {
                { 0.02500, 0.00750, 0.00175 },
                { 0.00750, 0.00700, 0.00135 },
                { 0.00175, 0.00135, 0.00043 },
            };

            double[] weights = { 1, 1, 1, 1, 1 };

            double[,] actual = Tools.WeightedCovariance(matrix, weights, 0);
            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0001));

            matrix = matrix.Transpose();

            actual = Tools.WeightedCovariance(matrix, weights, 1);
            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0001));
        }
Ejemplo n.º 2
0
 /// <summary>
 ///   Calculates the scatter matrix of a sample matrix.
 /// </summary>
 ///
 /// <remarks>
 ///   By dividing the Scatter matrix by the sample size, we get the population
 ///   Covariance matrix. By dividing by the sample size minus one, we get the
 ///   sample Covariance matrix.
 /// </remarks>
 ///
 /// <param name="weights">The number of times each sample should be repeated.</param>
 /// <param name="matrix">A number multi-dimensional array containing the matrix values.</param>
 /// <param name="dimension">
 ///   Pass 0 to if mean vector is a row vector, 1 otherwise. Default value is 0.
 /// </param>
 ///
 /// <returns>The covariance matrix.</returns>
 ///
 public static double[,] WeightedCovariance(double[][] matrix, int[] weights, int dimension = 0)
 {
     double[] mean = Tools.WeightedMean(matrix, weights, dimension);
     return(Tools.WeightedCovariance(matrix, weights, mean, dimension));
 }
Ejemplo n.º 3
0
 /// <summary>
 ///   Calculates the scatter matrix of a sample matrix.
 /// </summary>
 ///
 /// <remarks>
 ///   By dividing the Scatter matrix by the sample size, we get the population
 ///   Covariance matrix. By dividing by the sample size minus one, we get the
 ///   sample Covariance matrix.
 /// </remarks>
 ///
 /// <param name="matrix">A number multi-dimensional array containing the matrix values.</param>
 /// <param name="weights">An unit vector containing the importance of each sample
 /// in <see param="values"/>. The sum of this array elements should add up to 1.</param>
 /// <param name="means">The mean value of the given values, if already known.</param>
 ///
 /// <returns>The covariance matrix.</returns>
 ///
 public static double[,] WeightedCovariance(double[][] matrix, double[] weights, double[] means)
 {
     return(Tools.WeightedCovariance(matrix, weights, means, dimension: 0));
 }