/// <summary>
        // https://www.cs.ubc.ca/~nando/340-2012/lectures/l21.pdf
        /// </summary>
        /// <param name="inputs"></param>
        /// <param name="outputs"></param>
        public void Regress(Matrix inputs, Vector outputs)
        {
            inputs = inputs.AddColumn(1);
            var theta         = new Vector(inputs.NbColumns);
            var previousTheta = new Vector(inputs.NbColumns);

            for (int i = 0; i < inputs.NbColumns; i++)
            {
                previousTheta[i] = double.MaxValue;
            }

            while (theta.Substract(previousTheta).Abs().Max().Key.GetNumber() >= 1e-6)
            {
                Vector a       = inputs.Multiply(theta.SetHorizontal()).SumAllRows();
                var    pi      = a.Transform(_ => Inverse(_.GetNumber()));
                var    sx      = inputs.Multiply(pi.Substract(pi.Multiply(pi)).SetVertical()).Evaluate();
                var    xsx     = inputs.Transpose().Multiply(sx).Evaluate();
                Vector sxTheta = sx.Multiply(theta.SetHorizontal()).SumAllRows();
                var    result  = _multipleLinearRegression.Regress(xsx,
                                                                   inputs.Transpose().Multiply(sxTheta.Sum(outputs.Substract(pi))).SumAllRows().SetHorizontal(),
                                                                   false);
                previousTheta = theta;
                theta         = result.LinearRegression.GetCoefficients();
            }

            _multipleLinearRegression.UpdateInformation(inputs, outputs, useCovariance: false);
        }
Exemple #2
0
        public SimpleLinearRegression Regress(double[] inputs, double[] outputs)
        {
            var newInputs = new double[inputs.Length][];

            for (int i = 0; i < inputs.Length; i++)
            {
                newInputs[i] = new double[] { inputs[i] };
            }

            var result = _multipleLinearRegression.Regress(newInputs, outputs);

            _linearRegression = result.LinearRegression;
            return(this);
        }