Exemple #1
0
        public double[,] GetOptimalA(double[,] X, double[,] y)
        {
            MatrixHelper matrixHelper = new MatrixHelper();

            double[,] Xt     = matrixHelper.Transpose(X);
            double[,] result = matrixHelper.DotProduct(Xt, X);
            result           = matrixHelper.MatrixInverse(result);
            result           = matrixHelper.DotProduct(result, Xt);
            result           = matrixHelper.DotProduct(result, y);

            return(result);
        }
Exemple #2
0
        public static double GetDiferentialSquareErrorL2ForAi(double[,] X, double[,] y, double[,] a, double lambda, int ai)
        {
            int          m            = X.GetLength(0);
            int          n            = X.GetLength(1);
            double       result       = 0;
            object       lockResult   = new object();
            MatrixHelper matrixHelper = new MatrixHelper();

            double[,] at = matrixHelper.Transpose(a);

            Parallel.For(
                // From inclusive
                0,
                // To exclusive
                m,
                // local initial partial result
                () => 0.0d,
                // Loop body
                (i, loopState, partialResult) =>
            {
                double[,] xi = new double[n, 1];
                for (int j = 0; j < n; j++)
                {
                    xi[j, 0] = X[i, j];
                }

                double[,] atDotXi         = matrixHelper.DotProduct(at, xi);
                double localPartialResult = atDotXi[0, 0];
                localPartialResult       -= y[i, 0];
                localPartialResult       *= xi[ai, 0];
                localPartialResult       += (2 * lambda * a[ai, 0]);

                return(localPartialResult + partialResult);
            },
                // Final step for each local context
                (localPartialSum) =>
            {
                lock (lockResult)
                {
                    result += localPartialSum;
                }
            });

            return(result);
        }