public static LinearRegression Calculate(double[,] xy)
        {
            int    length = xy.GetLength(0);
            double yAvg, sxy, sxx, syy, sserr;
            double xAvg = yAvg = sxy = sxx = syy = sserr = 0;

            for (int i = 0; i < length; i++)
            {
                xAvg += xy[i, 0];
                yAvg += xy[i, 1];
            }
            xAvg /= length;
            yAvg /= length;

            for (int i = 0; i < length; i++)
            {
                double xdiff = xy[i, 0] - xAvg;
                double ydiff = xy[i, 1] - yAvg;
                sxy += xdiff * ydiff;
                sxx += xdiff * xdiff;
                syy += ydiff * ydiff;
            }
            double           m          = sxy / sxx;
            double           b          = yAvg - m * xAvg;
            LinearRegression regression = new LinearRegression(m, b);

            for (int i = 0; i < length; i++)
            {
                sserr += Math.Pow(xy[i, 1] - regression.GetY(xy[i, 0]), 2);
            }
            regression.RSquared = 1 - (sserr / syy);
            return(regression);
        }
示例#2
0
        public static LinearRegression Calculate(double[,] xy)
        {
            int length = xy.GetLength(0);
            double yAvg, sxy, sxx, syy, sserr;
            double xAvg = yAvg = sxy = sxx = syy = sserr = 0;

            for (int i = 0; i < length; i++)
            {
                xAvg += xy[i, 0];
                yAvg += xy[i, 1];
            }
            xAvg /= length;
            yAvg /= length;

            for (int i = 0; i < length; i++)
            {
                double xdiff = xy[i, 0] - xAvg;
                double ydiff = xy[i, 1] - yAvg;
                sxy += xdiff*ydiff;
                sxx += xdiff*xdiff;
                syy += ydiff*ydiff;
            }
            double m = sxy/sxx;
            double b = yAvg - m*xAvg;
            LinearRegression regression = new LinearRegression(m, b);
            for (int i = 0; i < length; i++)
            {
                sserr += Math.Pow(xy[i, 1] - regression.GetY(xy[i, 0]), 2);
            }
            regression.RSquared = 1 - (sserr/syy);
            return regression;
        }
        public static LinearRegression Calculate(double[] x, double[] y)
        {
            int length = x.Length;

            if (length != y.Length)
            {
                throw new ArgumentException("X and Y Dimensions do not match");
            }
            double yAvg, sxy, sxx, syy, sserr;
            double xAvg = yAvg = sxy = sxx = syy = sserr = 0;

            for (int i = 0; i < length; i++)
            {
                xAvg += x[i];
                yAvg += y[i];
            }
            xAvg /= length;
            yAvg /= length;

            for (int i = 0; i < length; i++)
            {
                double xdiff = x[i] - xAvg;
                double ydiff = y[i] - yAvg;
                sxy += xdiff * ydiff;
                sxx += xdiff * xdiff;
                syy += ydiff * ydiff;
            }
            double           m          = sxy / sxx;
            double           b          = yAvg - m * xAvg;
            LinearRegression regression = new LinearRegression(m, b);

            for (int i = 0; i < length; i++)
            {
                sserr += Math.Pow(y[i] - regression.GetY(x[i]), 2);
            }
            regression.RSquared = 1 - (sserr / syy);
            return(regression);
        }
示例#4
0
        public static LinearRegression Calculate(double[] x, double[] y)
        {
            int length = x.Length;
            if (length != y.Length)
            {
                throw new ArgumentException("X and Y Dimensions do not match");
            }
            double yAvg, sxy, sxx, syy, sserr;
            double xAvg = yAvg = sxy = sxx = syy = sserr = 0;

            for (int i = 0; i < length; i++)
            {
                xAvg += x[i];
                yAvg += y[i];
            }
            xAvg /= length;
            yAvg /= length;

            for (int i = 0; i < length; i++)
            {
                double xdiff = x[i] - xAvg;
                double ydiff = y[i] - yAvg;
                sxy += xdiff*ydiff;
                sxx += xdiff*xdiff;
                syy += ydiff*ydiff;
            }
            double m = sxy/sxx;
            double b = yAvg - m*xAvg;
            LinearRegression regression = new LinearRegression(m, b);
            for (int i = 0; i < length; i++)
            {
                sserr += Math.Pow(y[i] - regression.GetY(x[i]), 2);
            }
            regression.RSquared = 1 - (sserr/syy);
            return regression;
        }