Example #1
0
        /// <summary>
        ///   Creates a new linear regression directly from data points.
        /// </summary>
        ///
        /// <param name="x">The input vectors <c>x</c>.</param>
        /// <param name="y">The output vectors <c>y</c>.</param>
        ///
        /// <returns>A linear regression f(x) that most approximates y.</returns>
        ///
        public static MultipleLinearRegression FromData(double[][] x, double[] y)
        {
            var regression = new MultipleLinearRegression(x[0].Length);

            regression.Regress(x, y);

            return(regression);
        }
Example #2
0
        /// <summary>
        ///   Performs the regression using the input and output
        ///   data, returning the sum of squared errors of the fit.
        /// </summary>
        ///
        /// <param name="inputs">The input data.</param>
        /// <param name="outputs">The output data.</param>
        /// <returns>The regression Sum-of-Squares error.</returns>
        ///
        public double Regress(double[] inputs, double[] outputs)
        {
            if (inputs.Length != outputs.Length)
            {
                throw new ArgumentException("Number of input and output samples does not match", "outputs");
            }

            double[][] X = new double[inputs.Length][];

            for (int i = 0; i < inputs.Length; i++)
            {
                // b[0]*1 + b[1]*inputs[i]
                X[i] = new double[] { 1.0, inputs[i] };
            }

            return(regression.Regress(X, outputs));
        }
Example #3
0
        /// <summary>
        ///   Performs the regression using the input and output
        ///   data, returning the sum of squared errors of the fit.
        /// </summary>
        ///
        /// <param name="inputs">The input data.</param>
        /// <param name="outputs">The output data.</param>
        ///
        /// <returns>The regression Sum-of-Squares error.</returns>
        ///
        public double Regress(double[] inputs, double[] outputs)
        {
            if (inputs.Length != outputs.Length)
            {
                throw new ArgumentException("Number of input and output samples does not match", "outputs");
            }

            int N     = inputs.Length;
            int order = this.Degree + 1;

            // Create polynomials to regress over
            double[][] X = new double[N][];
            for (int i = 0; i < inputs.Length; i++)
            {
                X[i] = new double[order];

                for (int j = 0; j < order; j++)
                {
                    X[i][j] = Math.Pow(inputs[i], order - j - 1);
                }
            }

            return(regression.Regress(X, outputs));
        }