Ejemplo n.º 1
0
        /// <summary>
        /// 共分散行列を計算する。
        /// Calculate the covariance matrix.
        /// </summary>
        /// <param name="designMatrix"></param>
        /// <returns></returns>
        public static double[,] Variance_Covariance_Matrix(double[,] designMatrix)
        {
            double[,] average = DesignMatrix.Average(designMatrix);

            double[,] variance_Covariance_Matrix
                = new double[designMatrix.GetLength(1), designMatrix.GetLength(1)];

            //分散・共分散行列の要素[j,k]を計算する。
            for (int j = 0; j < designMatrix.GetLength(1); j++)
            {
                for (int k = j; k < designMatrix.GetLength(1); k++)
                {
                    //j次元目とk次元目の積の平均値を計算する。 E[XY]
                    for (int n = 0; n < designMatrix.GetLength(0); n++)
                    {
                        variance_Covariance_Matrix[j, k] += designMatrix[n, j] * designMatrix[n, k];
                    }
                    variance_Covariance_Matrix[j, k] /= designMatrix.GetLength(0);

                    //j次元目の平均値とk次元目の平均値の積を引く。-E[X]E[Y]
                    variance_Covariance_Matrix[j, k] -= average[0, j] * average[0, k];

                    //j , kを入れ替えても値は同じ
                    variance_Covariance_Matrix[k, j] = variance_Covariance_Matrix[j, k];
                }
            }


            return(variance_Covariance_Matrix);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 各要素の標準偏差を計算する
        /// </summary>
        /// <param name="designMatrix"></param>
        /// <returns></returns>
        public static double[,] StandardDeviation(double[,] designMatrix)
        {
            double[,] average        = DesignMatrix.Average(designMatrix);
            double[,] average_square = new double[1, designMatrix.GetLength(1)];

            for (int k = 0; k < designMatrix.GetLength(1); k++)
            {
                for (int j = 0; j < designMatrix.GetLength(0); j++)
                {
                    average_square[0, k] += designMatrix[j, k] * designMatrix[j, k];
                }
                average_square[0, k] /= designMatrix.GetLength(0);
            }

            double[,] result = new double[1, designMatrix.GetLength(1)];

            for (int k = 0; k < designMatrix.GetLength(1); k++)
            {
                result[0, k] = Math.Sqrt(average_square[0, k] - average[0, k] * average[0, k]);
            }

            return(result);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 共分散行列の逆行列
 /// Calculate Inverse of the covariance matrix.
 /// </summary>
 /// <param name="designMatrix"></param>
 /// <returns></returns>
 public static double[,] Inverse_Variance_Covariance_Matrix(double[,] designMatrix)
 {
     return(Matrix.Inverse_of_a_Matrix(DesignMatrix.Variance_Covariance_Matrix(designMatrix)));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 相関係数の行列を得る
        /// </summary>
        /// <param name="designMatrix"></param>
        /// <returns></returns>
        public static double[,] CorelationMatrix(double[,] designMatrix)
        {
            double[,] average = DesignMatrix.Average(designMatrix);

            double[,] corelationMatrix
                = new double[designMatrix.GetLength(1), designMatrix.GetLength(1)];

            //分散・共分散行列の要素[j,k]を計算する。
            for (int j = 0; j < designMatrix.GetLength(1); j++)
            {
                for (int k = j; k < designMatrix.GetLength(1); k++)
                {
                    //j次元目とk次元目の積の平均値を計算する。 E[XY]
                    for (int n = 0; n < designMatrix.GetLength(0); n++)
                    {
                        corelationMatrix[j, k] += designMatrix[n, j] * designMatrix[n, k];
                    }
                    corelationMatrix[j, k] /= designMatrix.GetLength(0);

                    //j次元目の平均値とk次元目の平均値の積を引く。-E[X]E[Y]
                    corelationMatrix[j, k] -= average[0, j] * average[0, k];

                    //j , kを入れ替えても値は同じ
                    corelationMatrix[k, j] = corelationMatrix[j, k];
                }
            }


            //対角成分は正の数
            bool minus = false;

            for (int j = 0; j < corelationMatrix.GetLength(0); j++)
            {
                if (corelationMatrix[j, j] < 0)
                {
                    minus = true; break;
                }
            }
            if (minus)
            {
                for (int j = 0; j < corelationMatrix.GetLength(0); j++)
                {
                    for (int k = 0; k < corelationMatrix.GetLength(1); k++)
                    {
                        corelationMatrix[j, k] *= -1;
                    }
                }
            }

            double[] std = new double[corelationMatrix.GetLength(0)];
            for (int j = 0; j < corelationMatrix.GetLength(0); j++)
            {
                std[j] = Math.Sqrt(corelationMatrix[j, j]);
            }
            for (int j = 0; j < corelationMatrix.GetLength(0); j++)
            {
                for (int k = 0; k < corelationMatrix.GetLength(1); k++)
                {
                    corelationMatrix[j, k] /= std[j] * std[k];
                }
            }

            return(corelationMatrix);
        }