Ejemplo n.º 1
0
        /// <summary>
        /// Computes the loss between the expected values (ground truth)
        /// and the given actual values that have been predicted.
        /// </summary>
        /// <param name="actual">The actual values that have been predicted.</param>
        /// <returns>
        /// The loss value between the expected values and
        /// the actual predicted values.
        /// </returns>
        public override double[] Loss(double[][] actual)
        {
            int N = actual.Length;
            int M = actual.Columns();
            int P = NumberOfInputs;

            double[] SSe = new double[M];
            double[] SSt = new double[M];
            double[] r2  = new double[M];

            // For each output variable
            double[] avg = Expected.WeightedMean(Weights);

            // Calculate SSe and SSt
            for (int i = 0; i < N; i++)
            {
                for (int c = 0; c < M; c++)
                {
                    double w = Weights[i];

                    double d;
                    d       = Expected[i][c] - actual[i][c];
                    SSe[c] += w * d * d;

                    d       = Expected[i][c] - avg[c];
                    SSt[c] += w * d * d;
                }
            }

            // Calculate R-Squared
            for (int c = 0; c < M; c++)
            {
                r2[c] = 1 - (((Math.Abs(SSt[c] - SSe[c]) / N) > 1e-16) ? SSe[c] / SSt[c] : 0);
            }

            if (Adjust)
            {
                // Return adjusted R-Squared
                for (int c = 0; c < M; c++)
                {
                    if (r2[c] == 1.0)
                    {
                        continue;
                    }

                    if (N == P + 1)
                    {
                        r2[c] = double.NaN;
                    }
                    else
                    {
                        r2[c] = 1.0 - (1.0 - r2[c]) * ((N - 1.0) / (N - P - 1.0));
                    }
                }
            }

            return(r2);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Computes the loss between the expected values (ground truth)
        /// and the given actual values that have been predicted.
        /// </summary>
        /// <param name="actual">The actual values that have been predicted.</param>
        /// <returns>
        /// The loss value between the expected values and
        /// the actual predicted values.
        /// </returns>
        public double Loss(double[] actual)
        {
            // R-squared = 100 * SS(regression) / SS(total)

            int    p   = NumberOfInputs;
            int    n   = actual.Length;
            double SSe = 0.0;
            double SSt = 0.0;

            // Calculate output mean
            double avg = Expected.WeightedMean(Weights).Mean();

            // Calculate SSe and SSt
            for (int i = 0; i < Expected.Length; i++)
            {
                double w = Weights[i];

                double d = Expected[i][0] - actual[i];
                SSe += w * d * d;

                d    = Expected[i][0] - avg;
                SSt += w * d * d;
            }

            // Calculate R-Squared
            double r2 = 1 - (((Math.Abs(SSt - SSe) / n) > 1e-16) ? SSe / SSt : 0);

            if (!Adjust)
            {
                // Return ordinary R-Squared
                return(r2);
            }

            if (r2 == 1)
            {
                return(1);
            }

            if (n - p == 1.0)
            {
                return(double.NaN);
            }

            // Return adjusted R-Squared
            return(1.0 - (1.0 - r2) * ((n - 1.0) / (n - p - 1.0)));
        }