示例#1
0
        public override void SolveTransforamtion()
        {
            //https://numerics.mathdotnet.com/Regression.html#Regularization

            /// <summary>
            /// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),y) to a linear surface y : X -> p0*x0 + p1*x1 + ... + pk*xk,
            /// returning a function y' for the best fitting combination.
            /// If an intercept is added, its coefficient will be prepended to the resulting parameters.
            /// </summary>
            if (records.Count > minNumberOfPointToSolve)
            {
                if (records.Count % caclulationSteps == 0 || records.Count == minNumberOfPointToSolve)
                {
                    //double[] out_x = new double[records.Count];
                    //double[] out_y = new double[records.Count];
                    double[][] out_xy = new double[records.Count][];
                    double[][] in_XY  = new double[records.Count][];
                    int        idx    = 0;
                    foreach (TransformationRecord r in records)
                    {
                        out_xy[idx] = new double[] { r.ArPosition.x, r.ArPosition.z };
                        in_XY[idx]  = new double[] { r.GpsPosition.Longitude, r.GpsPosition.Latitude };
                        idx++;
                    }
                    regression = ols.Learn(in_XY, out_xy);
                    //// We can obtain predictions using
                    //double[][] predictions = regression.Transform(in_XY);

                    //// The prediction error is
                    //double error = new SquareLoss(out_xy).Loss(predictions); // 0
                    //double[] r2 = regression.CoefficientOfDetermination(in_XY, out_xy);
                }
            }
        }
示例#2
0
        private void btnRegress_Click(object sender, EventArgs e)
        {
            if (pls == null || dgvRegressionInput.DataSource == null)
            {
                MessageBox.Show("Please compute the analysis first.");
                return;
            }

            int components = (int)numComponentsRegression.Value;

            regression = pls.CreateRegression(components);

            DataTable table       = dgvRegressionInput.DataSource as DataTable;
            DataTable inputTable  = table.DefaultView.ToTable(false, inputColumnNames);
            DataTable outputTable = table.DefaultView.ToTable(false, outputColumnNames);


            double[][] sourceInput  = Matrix.ToArray(inputTable as DataTable);
            double[][] sourceOutput = Matrix.ToArray(outputTable as DataTable);


            double[,] result = Matrix.ToMatrix(regression.Compute(sourceInput));

            double[] rSquared = regression.CoefficientOfDetermination(sourceInput, sourceOutput, cbAdjusted.Checked);

            dgvRegressionOutput.DataSource = new ArrayDataView(result, outputColumnNames);
            dgvRSquared.DataSource         = new ArrayDataView(rSquared, outputColumnNames);
        }
        /// <summary>
        /// Learns a model that can map the given inputs to the desired outputs.
        /// </summary>
        /// <param name="x">The model inputs.</param>
        /// <param name="weights">The weight of importance for each input sample.</param>
        /// <returns>
        /// A model that has learned how to produce suitable outputs
        /// given the input data <paramref name="x" />.
        /// </returns>
        public MultivariateLinearRegression Learn(double[][] x, double[] weights = null)
        {
            if (weights != null)
            {
                throw new ArgumentException(Accord.Properties.Resources.NotSupportedWeights, "weights");
            }

            // Calculate common measures to speedup other calculations
            this.columnMeans  = Measures.Mean(x, dimension: 0);
            this.columnStdDev = Measures.StandardDeviation(x, columnMeans);

            NumberOfInputs = x.Columns();
            if (NumberOfOutputs == 0)
            {
                NumberOfOutputs = NumberOfInputs;
            }

            // First, the data should be centered by subtracting
            //  the mean of each column in the source data matrix.
            var matrix = Adjust(x, overwriteSourceMatrix);

            // Pre-process the centered data matrix to have unit variance
            var whiten = Statistics.Tools.Whitening(matrix, out whiteningMatrix);

            // Generate a new unitary initial guess for the de-mixing matrix
            var initial = Jagged.Random(NumberOfOutputs, matrix.Columns());


            // Compute the demixing matrix using the selected algorithm
            if (algorithm == IndependentComponentAlgorithm.Deflation)
            {
                revertMatrix = deflation(whiten, NumberOfOutputs, initial);
            }
            else // if (algorithm == IndependentComponentAlgorithm.Parallel)
            {
                revertMatrix = parallel(whiten, NumberOfOutputs, initial);
            }

            // Combine the rotation and demixing matrices
            revertMatrix = whiteningMatrix.DotWithTransposed(revertMatrix);
            revertMatrix.Divide(revertMatrix.Sum(), result: revertMatrix);

            // Compute the original source mixing matrix
            mixingMatrix = Matrix.PseudoInverse(revertMatrix);
            mixingMatrix.Divide(mixingMatrix.Sum(), result: mixingMatrix);

            this.demix = createRegression(revertMatrix, columnMeans, columnStdDev, analysisMethod);
            this.mix   = demix.Inverse();

            // Creates the object-oriented structure to hold the principal components
            var array = new IndependentComponent[NumberOfOutputs];

            for (int i = 0; i < array.Length; i++)
            {
                array[i] = new IndependentComponent(this, i);
            }
            this.componentCollection = new IndependentComponentCollection(array);

            return(demix);
        }
        public override void SolveTransforamtion()
        {
            //https://numerics.mathdotnet.com/Regression.html#Regularization

            if (records.Count > minNumberOfPointToSolve)
            {
                if (records.Count % caclulationSteps == 0 || records.Count == minNumberOfPointToSolve)
                {
                    double[][] out_x  = new double[records.Count][];
                    double[][] out_z  = new double[records.Count][];
                    double[][] out_y  = new double[records.Count][];
                    double[][] in_XY  = new double[records.Count][];
                    double[][] in_XYZ = new double[records.Count][];
                    int        idx    = 0;
                    foreach (TransformationRecord r in records)
                    {
                        out_x[idx] = new double[] { r.ArPosition.x };
                        out_z[idx] = new double[] { r.ArPosition.z };
                        out_y[idx] = new double[] { r.ArPosition.y };

                        in_XY[idx]  = new double[] { r.GpsPosition.Longitude, r.GpsPosition.Latitude };
                        in_XYZ[idx] = new double[] { r.GpsPosition.Longitude, r.GpsPosition.Latitude, r.GpsPosition.Altitude };
                        idx++;
                    }
                    regression_x = ols_x.Learn(in_XY, out_x);
                    regression_z = ols_z.Learn(in_XY, out_z);
                    regression_y = ols_y.Learn(in_XYZ, out_y);
                    if (CalcualteError && testRecords.Count > 0)
                    {
                        List <Vector3> testArLocationsPredicted = TransformGpsToWorld(testRecords.GetGPSLocations());
                        Error_Horizontal = CalculateRMSE(testArLocationsPredicted, testRecords.GetARPositions());
                    }
                }
            }
        }
        public void SimplsComputeTest()
        {
            double[,] inputs;
            double[,] outputs;

            var pls = CreateWineExample(out inputs, out outputs);

            MultivariateLinearRegression regression = pls.CreateRegression();


            // test factor proportions
            double[] expectedX = { 0.86, 0.12, 0.00, 0.86 };
            double[] actualX   = pls.Predictors.FactorProportions;
            Assert.IsTrue(expectedX.IsEqual(actualX, 0.01));

            double[] expectedY = { 0.67, 0.13, 0.17, 0.00 };
            double[] actualY   = pls.Dependents.FactorProportions;
            Assert.IsTrue(expectedY.IsEqual(actualY, 0.01));


            // Test Properties
            double[,] weights = pls.Weights;
            double[,] actual  = pls.Predictors.Result;

            double[,] X0 = (double[, ])pls.Source.Clone(); Tools.Center(X0, inPlace: true);
            double[,] Y0 = (double[, ])pls.Output.Clone(); Tools.Center(Y0, inPlace: true);

            // XSCORES = X0*W
            double[,] expected = X0.Multiply(weights);
            Assert.IsTrue(expected.IsEqual(actual, 0.01));
        }
示例#6
0
        private static void multivariateLinear()
        {
            double[][] inputs =
            {
                // variables:  x1  x2  x3
                new double[] { 1, 1, 1 },    // input sample 1
                new double[] { 2, 1, 1 },    // input sample 2
                new double[] { 3, 1, 1 },    // input sample 3
            };

            double[][] outputs =
            {
                // variables:  y1  y2
                new double[] { 2, 3 },   // corresponding output to sample 1
                new double[] { 4, 6 },   // corresponding output to sample 2
                new double[] { 6, 9 },   // corresponding output to sample 3
            };

            // Use Ordinary Least Squares to create the regression
            OrdinaryLeastSquares ols = new OrdinaryLeastSquares();

            // Now, compute the multivariate linear regression:
            MultivariateLinearRegression regression = ols.Learn(inputs, outputs);

            // We can obtain predictions using
            double[][] predictions = regression.Transform(inputs);

            // The prediction error is
            double error = new SquareLoss(outputs).Loss(predictions); // 0
        }
示例#7
0
        //EXAMPLE: http://accord-framework.net/docs/html/T_Accord_Statistics_Models_Regression_Linear_MultivariateLinearRegression.htm
        static void Main(string[] args)
        {
            CSV_Parser     parser = new CSV_Parser();
            RegressionData data   = parser.ParseDataFile();

            // Use Ordinary Least Squares to create the regression
            OrdinaryLeastSquares ols = new OrdinaryLeastSquares();

            // Now, compute the multivariate linear regression:
            MultivariateLinearRegression regression = ols.Learn(data.InterestRatings, data.MajorRatings);

            // We can obtain predictions using
            double[][] predictions = regression.Transform(data.InterestRatings);

            // The prediction error is
            double error = new SquareLoss(data.MajorRatings).Loss(predictions); // 0

            // We can also check the r-squared coefficients of determination:
            //double[] r2 = regression.CoefficientOfDetermination(topicRatings, majorRatings);
            double[][] r2 = regression.Weights;
            Console.WriteLine("WEIGHTS:");
            //writeCSVfile(data, r2);
            GenerateCSFile(data, r2);

            Console.WriteLine("Coefficient Of Determination");
            double[] r3 = regression.CoefficientOfDetermination(data.InterestRatings, data.MajorRatings);
            for (int i = 0; i < r3.Length; i++)
            {
                Console.WriteLine(r3[i]);
            }

            Console.Read();
        }
        public void SimplsRegressionTest_new_method()
        {
            double[][] inputs;
            double[][] outputs;

            var pls = CreateWineExample_new_method(out inputs, out outputs);

            MultivariateLinearRegression regression = pls.CreateRegression();

            // test regression intercepts
            double[] intercepts = regression.Intercepts;
            double[] expectedI  = { 60.717, -8.509, -4.362 };

            Assert.IsTrue(intercepts.IsEqual(expectedI, 0.01));


            // test regression coefficients
            double[,] coefficients = regression.Coefficients;
            double[,] expectedC    =
            {
                { -1.6981, -0.0566, 0.07075 },
                {  1.2735,  0.2924, 0.57193 },
                { -4.0000,  1.0000, 0.50000 },
                {  1.1792,  0.1226, 0.15919 }
            };

            Assert.IsTrue(coefficients.IsEqual(expectedC, 0.01));


            // Test computation
            double[][] aY = regression.Transform(inputs);

            for (int i = 0; i < outputs.Length; i++)
            {
                for (int j = 0; j < outputs[i].Length; j++)
                {
                    double actualOutput   = aY[i][j];
                    double expectedOutput = outputs[i][j];

                    double delta = System.Math.Abs(actualOutput - expectedOutput);
                    double tol   = 0.21 * expectedOutput;

                    Assert.IsTrue(delta <= tol);
                }
            }

            // Test output transform
            double[][] bY = pls.TransformOutput(outputs);
            //string str = bY.ToCSharp();
            double[][] expected = new double[][] {
                new double[] { 55.1168117173553, 19.9814652854436, 23.1058025430891, 8.50770527860088E-15 },
                new double[] { 22.6848893220127, 7.39445606402423, 6.83504323067901, 3.73246403168482E-15 },
                new double[] { -0.877117473967739, -2.79934089543989, 0.379941427036153, -1.88457062440663E-16 },
                new double[] { -48.8124364962975, -9.63329642474673, -25.0857657504786, -7.17568385594517E-15 },
                new double[] { -28.1121470691027, -14.9432840292812, -5.23502145032562, -4.87602839189987E-15 }
            };

            Assert.IsTrue(expected.IsEqual(bY, 1e-6));
        }
示例#9
0
        public void prediction_test()
        {
            // Example from http://www.real-statistics.com/multiple-regression/confidence-and-prediction-intervals/
            var dt = Accord.IO.CsvReader.FromText(Resources.linreg, true).ToTable();

            double[][] y = dt.ToArray("Poverty");
            double[][] x = dt.ToArray("Infant Mort", "White", "Crime");

            // Use Ordinary Least Squares to learn the regression
            OrdinaryLeastSquares ols = new OrdinaryLeastSquares();

            // Use OLS to learn the multiple linear regression
            MultivariateLinearRegression regression = ols.Learn(x, y);

            Assert.AreEqual(3, regression.NumberOfInputs);
            Assert.AreEqual(1, regression.NumberOfOutputs);

            Assert.AreEqual(0.443650703716698, regression.Intercepts[0], 1e-5);
            Assert.AreEqual(1.2791842411083394, regression.Weights[0][0], 1e-5);
            Assert.AreEqual(0.036259242392669415, regression.Weights[1][0], 1e-5);
            Assert.AreEqual(0.0014225014835705938, regression.Weights[2][0], 1e-5);

            double rse = regression.GetStandardError(x, y)[0];

            Assert.AreEqual(rse, 2.4703520840798507, 1e-5);


            double[][] im  = ols.GetInformationMatrix();
            double[]   mse = regression.GetStandardError(x, y);
            double[][] se  = regression.GetStandardErrors(mse, im);

            Assert.AreEqual(0.30063086032754965, se[0][0], 1e-10);
            Assert.AreEqual(0.033603448179240082, se[0][1], 1e-10);
            Assert.AreEqual(0.0022414548866296342, se[0][2], 1e-10);
            Assert.AreEqual(3.9879881671805824, se[0][3], 1e-10);

            double[] x0 = new double[] { 7, 80, 400 };
            double   y0 = regression.Transform(x0)[0];

            Assert.AreEqual(y0, 12.867680376316864, 1e-5);

            double actual = regression.GetStandardError(x0, mse, im)[0];

            Assert.AreEqual(0.35902764658470271, actual, 1e-10);

            DoubleRange ci = regression.GetConfidenceInterval(x0, mse, x.Length, im)[0];

            Assert.AreEqual(ci.Min, 12.144995206616116, 1e-5);
            Assert.AreEqual(ci.Max, 13.590365546017612, 1e-5);

            actual = regression.GetPredictionStandardError(x0, mse, im)[0];
            Assert.AreEqual(2.4963053239397244, actual, 1e-10);

            DoubleRange pi = regression.GetPredictionInterval(x0, mse, x.Length, im)[0];

            Assert.AreEqual(pi.Min, 7.8428783761994554, 1e-5);
            Assert.AreEqual(pi.Max, 17.892482376434273, 1e-5);
        }
示例#10
0
        public DataObject DoThePCA(DataObject DataSet)
        {
            DataObject NewData = new DataObject(DataSet.ItemsFeatures.Length, DataSet.ItemsFeatures[0].Length)
            {
                ItemsFeatures = DataSet.ItemsFeatures
            };

            var pca = new PrincipalComponentAnalysis()
            {
                Method = PrincipalComponentMethod.Center,
                Whiten = true
            };

            MultivariateLinearRegression transform = pca.Learn(DataSet.ItemsFeatures);

            double[] MeansVector = new double[DataSet.ItemsFeatures[0].Length];
            MeansVector = Measures.Mean(DataSet.ItemsFeatures, 0);

            double[][] CovarianceMatrix = Measures.Covariance(DataSet.ItemsFeatures, MeansVector);
            double[,] CovMat = new double[CovarianceMatrix.Length, CovarianceMatrix.Length];

            for (int i = 0; i < CovarianceMatrix.Length; i++)
            {
                for (int j = 0; j < CovarianceMatrix[0].Length; j++)
                {
                    CovMat[i, j] = CovarianceMatrix[i][j];
                }
            }

            var evd = new EigenvalueDecomposition(CovMat);

            double[] eigenval = evd.RealEigenvalues;
            double[,] eigenvec = evd.Eigenvectors;

            eigenvec = Matrix.Sort(eigenval, eigenvec, new GeneralComparer(ComparerDirection.Descending, true));

            Console.WriteLine("Numero di componenti principali: ");
            ScatterplotBox.Show(eigenval);
            PrincipalComponentsNumber = Convert.ToInt32(Console.ReadLine());

            pca.NumberOfOutputs = PrincipalComponentsNumber;

            double[][] DataProjected = pca.Transform(DataSet.ItemsFeatures);

            if (PrincipalComponentsNumber == 2)
            {
                ScatterplotBox.Show(DataProjected, DataSet.CatIDs);
            }

            NewData.ItemsFeatures = DataProjected;
            NewData.CatIDs        = DataSet.CatIDs;

            return(NewData);
        }
示例#11
0
        public void startPCA()
        {
            var pca = new PrincipalComponentAnalysis()
            {
                Method             = PrincipalComponentMethod.CorrelationMatrix,
                Means              = meanVector,
                StandardDeviations = standardDeviation
            };

            MultivariateLinearRegression transform = pca.Learn();
        }
        public void RegressTest()
        {
            double[][] X =
            {
                new double[] {    4.47 },
                new double[] {  208.30 },
                new double[] { 3400.00 },
            };


            double[][] Y =
            {
                new double[] {    0.51 },
                new double[] {  105.66 },
                new double[] { 1800.00 },
            };



            double eB = 0.5303528166;
            double eA = -3.290915095;

            MultivariateLinearRegression target = new MultivariateLinearRegression(1, 1, true);

            target.Regress(X, Y);

            Assert.AreEqual(target.Coefficients[0, 0], eB, 0.001);
            Assert.AreEqual(target.Intercepts[0], eA, 0.001);

            Assert.AreEqual(target.Inputs, 1);
            Assert.AreEqual(target.Outputs, 1);



            // Test manually including an constant term to generate an intercept
            double[][] X1 =
            {
                new double[] {    4.47, 1 },
                new double[] {  208.30, 1 },
                new double[] { 3400.00, 1 },
            };

            MultivariateLinearRegression target2 = new MultivariateLinearRegression(2, 1, false);

            target2.Regress(X1, Y);

            Assert.AreEqual(target2.Coefficients[0, 0], eB, 0.001);
            Assert.AreEqual(target2.Coefficients[1, 0], eA, 0.001);

            Assert.AreEqual(target2.Inputs, 2);
            Assert.AreEqual(target2.Outputs, 1);
        }
示例#13
0
        public void covariance_new_interface()
        {
            double[]   mean = Measures.Mean(data, dimension: 0);
            double[][] cov  = Measures.Covariance(data.ToJagged());

            #region doc_learn_3
            // Create the Principal Component Analysis
            // specifying the CovarianceMatrix method:
            var pca = new PrincipalComponentAnalysis()
            {
                Method = PrincipalComponentMethod.CovarianceMatrix,
                Means  = mean // pass the original data mean vectors
            };

            // Learn the PCA projection using passing the cov matrix
            MultivariateLinearRegression transform = pca.Learn(cov);

            // Now, we can transform data as usual
            double[,] actual = pca.Transform(data);
            #endregion

            double[,] expected = new double[, ]
            {
                { 0.827970186, -0.175115307 },
                { -1.77758033, 0.142857227 },
                { 0.992197494, 0.384374989 },
                { 0.274210416, 0.130417207 },
                { 1.67580142, -0.209498461 },
                { 0.912949103, 0.175282444 },
                { -0.099109437, -0.349824698 },
                { -1.14457216, 0.046417258 },
                { -0.438046137, 0.017764629 },
                { -1.22382056, -0.162675287 },
            };

            // Verify both are equal with 0.01 tolerance value
            Assert.IsTrue(Matrix.IsEqual(actual, expected, 0.01));

            // Transform
            double[,] image = pca.Transform(data);

            // Reverse
            double[,] reverse = pca.Revert(image);

            // Verify both are equal with 0.01 tolerance value
            Assert.IsTrue(Matrix.IsEqual(reverse, data, 1e-5));

            actual = transform.Transform(data.ToJagged()).ToMatrix();
            Assert.IsTrue(Matrix.IsEqual(actual, expected, 1e-5));
        }
        public void Compute(int components)
        {
            NumberOfOutputs = components;

            // First, the data should be centered by subtracting
            //  the mean of each column in the source data matrix.
            double[][] matrix = Adjust(sourceMatrix.ToJagged(), overwriteSourceMatrix);

            // Pre-process the centered data matrix to have unit variance
            double[][] whiten = Statistics.Tools.Whitening(matrix, out whiteningMatrix);

            // Generate a new unitary initial guess for the de-mixing matrix
            double[][] initial = Jagged.Random(components, matrix.Columns());


            // Compute the demixing matrix using the selected algorithm
            if (algorithm == IndependentComponentAlgorithm.Deflation)
            {
                revertMatrix = deflation(whiten, components, initial);
            }
            else // if (algorithm == IndependentComponentAlgorithm.Parallel)
            {
                revertMatrix = parallel(whiten, components, initial);
            }

            // Combine the rotation and demixing matrices
            revertMatrix = whiteningMatrix.DotWithTransposed(revertMatrix);
            revertMatrix.Divide(revertMatrix.Sum(), result: revertMatrix);

            // Compute the original source mixing matrix
            mixingMatrix = Matrix.PseudoInverse(revertMatrix);
            mixingMatrix.Divide(mixingMatrix.Sum(), result: mixingMatrix);

            // Demix the data into independent components
            resultMatrix = Matrix.Dot(matrix, revertMatrix).ToMatrix();

            this.demix = createRegression(revertMatrix, columnMeans, columnStdDev, analysisMethod);
            this.mix   = demix.Inverse();

            // Creates the object-oriented structure to hold the principal components
            var array = new IndependentComponent[components];

            for (int i = 0; i < array.Length; i++)
            {
                array[i] = new IndependentComponent(this, i);
            }
            this.componentCollection = new IndependentComponentCollection(array);
        }
        public void SimplsRegressionTest()
        {
            double[,] inputs;
            double[,] outputs;

            var pls = CreateWineExample(out inputs, out outputs);

            MultivariateLinearRegression regression = pls.CreateRegression();

            // test regression intercepts
            double[] intercepts = regression.Intercepts;
            double[] expectedI  = { 60.717, -8.509, -4.362 };

            Assert.IsTrue(intercepts.IsEqual(expectedI, 0.01));


            // test regression coefficients
            double[,] coefficients = regression.Coefficients;
            double[,] expectedC    =
            {
                { -1.6981, -0.0566, 0.07075 },
                {  1.2735,  0.2924, 0.57193 },
                { -4.0000,  1.0000, 0.50000 },
                {  1.1792,  0.1226, 0.15919 }
            };

            Assert.IsTrue(coefficients.IsEqual(expectedC, 0.01));


            // Test computation
            double[][] aY = regression.Compute(inputs.ToArray());

            for (int i = 0; i < outputs.GetLength(0); i++)
            {
                for (int j = 0; j < outputs.GetLength(1); j++)
                {
                    double actualOutput   = aY[i][j];
                    double expectedOutput = outputs[i, j];

                    double delta = System.Math.Abs(actualOutput - expectedOutput);
                    double tol   = 0.21 * expectedOutput;

                    Assert.IsTrue(delta <= tol);
                }
            }
        }
示例#16
0
        public MultivariateLinearRegression GetPredictionModel(string userId)
        {
            var dataSet = this.trainings
                          .All()
                          .Where(x => x.UserId == userId)
                          .OrderByDescending(x => x.CreatedOn)
                          .Take(1000)
                          .ToArray();

            var inputDataSet  = new double[dataSet.Length][];
            var outputDataSet = new double[dataSet.Length][];

            int i = 0;

            foreach (var dataSetTraining in dataSet)
            {
                var inputDataRow = new double[]
                {
                    dataSetTraining.Track.Length,
                    dataSetTraining.Track.Ascent,
                    dataSetTraining.Track.AscentLength,
                };

                inputDataSet[i] = inputDataRow;

                var duration      = dataSetTraining.EndDate - dataSetTraining.StartDate;
                var outputDataRow = new double[]
                {
                    dataSetTraining.Calories,
                    dataSetTraining.Water,
                    duration.TotalHours
                };

                outputDataSet[i] = outputDataRow;

                i++;
            }

            var    regression = new MultivariateLinearRegression(3, 3);
            double error      = regression.Regress(inputDataSet, outputDataSet);

            return(regression);
        }
示例#17
0
        public void startPCA()
        {
            var pca = new PrincipalComponentAnalysis()
            {
                Method             = PrincipalComponentMethod.CorrelationMatrix,
                Means              = meanVector,
                StandardDeviations = standardDeviation
            };

            MultivariateLinearRegression transform = pca.Learn(correlationMatrix);

            eigenVectors = pca.ComponentVectors;
            eigenValues  = pca.Eigenvalues;

            sumOfEigenValues = 0;
            for (int i = 0; i < eigenValues.Length; i++)
            {
                sumOfEigenValues += eigenValues[i];
            }
        }
    void Start()
    {
        points = new ParticleSystem.Particle[resolution];
        var pca = new PrincipalComponentAnalysis()
        {
            Method = PrincipalComponentMethod.Center,
            Whiten = true
        };
        MultivariateLinearRegression transform = pca.Learn(data);

        pca.NumberOfOutputs = 3;
        double[][] output2 = pca.Transform(data);
        Debug.Log(output2[3][2]);
        for (int i = 0; i < resolution; i++)
        {
            points [i].position   = new Vector3((float)(output2 [i] [0]) * 6f, (float)(output2 [i] [1]) * 6f, (float)(output2 [i] [2]) * 6f);
            points [i].startColor = new Color((float)(output2 [i] [0]) * 6f, (float)(output2 [i] [1]) * 6f, (float)(output2 [i] [2]) * 6f);
            points [i].startSize  = 0.075f;
        }
    }
示例#19
0
        public void runPCA()         //DataTable mydt, List<string> numvars, string method, string subtitle)
        {
            if (_dt == null || _final_N == 0)
            {
                _subtitle += " *No data rows*";
                _final_N   = 0;
            }
            else
            {
                ////from http://accord-framework.net/docs/html/T_Accord_Statistics_Analysis_PrincipalComponentAnalysis.htm
                PrincipalComponentMethod mymethod = new PrincipalComponentMethod();
                if (method == "Center")
                {
                    mymethod = PrincipalComponentMethod.Center;
                }
                else if (method == "Standardize")
                {
                    mymethod = PrincipalComponentMethod.Standardize;
                }



                var pca = new PrincipalComponentAnalysis()
                {
                    Method = mymethod,
                    Whiten = true
                };

                // Learn the PCA projection using passing the cov matrix
                MultivariateLinearRegression transform = pca.Learn(_data);

                // Now, we can transform data as usual
                double[][] out1 = pca.Transform(_data);

                _pca = pca;

                buildPCAtable();
            }
        }
示例#20
0
        private static Shapes.SimpleMesh AlignShape(Shapes.SimpleMesh mesh)
        {
            // Prepare matrix of all the vectors to present to PCA.
            double[][] matrix = new double[mesh.VertexCount][];
            for (uint i = 0; i < mesh.VertexCount; i++)
            {
                matrix[i] = mesh.GetVertex(i).AsArrayD();
            }
            // Call PCA using The Accord library.
            PrincipalComponentAnalysis pca =
                new PrincipalComponentAnalysis(PrincipalComponentMethod.Center,
                                               false, 3);
            MultivariateLinearRegression regression = pca.Learn(matrix);

            double[][] transformed = regression.Transform(matrix);
            Vector3[]  vectors     = transformed.Vectorize();

            // Provide the new positions into the mesh.
            Shapes.SimpleMesh simple = Shapes.SimpleMesh.CreateFrom(mesh);
            simple.Vertices = vectors;
            return(simple);
        }
示例#21
0
        public override ConfusionMatrix Execute()
        {
            //The Least Squares algorithm
            //It uses a PartialLeastSquaresAnalysis library object using a non-linear iterative partial least squares algorithm
            //and runs on the mean-centered and standardized data

            //Create an analysis
            var pls = new PartialLeastSquaresAnalysis(trainingSet,
                                                      trainingOutput,
                                                      AnalysisMethod.Standardize,
                                                      PartialLeastSquaresAlgorithm.NIPALS);

            pls.Compute();

            //After computing the analysis
            //create a linear model to predict new variables
            MultivariateLinearRegression regression = pls.CreateRegression();

            //This will hold the result of the classifications
            var predictedLifted = new int[testSet.GetLength(0)][];

            for (int i = 0; i < predictedLifted.Length; ++i)
            {
                predictedLifted[i] = regression
                                     .Compute(testSet.GetRow(i))      //Retrieve the row vector of the test set
                                     .Select(x => Convert.ToInt32(x)) // Convert the result to int
                                     .ToArray();
            }

            //Unlift the prediction vector
            var predicted = predictedLifted
                            .SelectMany(x => x)
                            .ToArray();

            //Create a new confusion matrix with the calculated parameters
            ConfusionMatrix cmatrix = new ConfusionMatrix(predicted, expected, POSITIVE, NEGATIVE);

            return(cmatrix);
        }
    public void getPcaValues(int nrOfComponents)
    {
        float[][]  data       = currentData;
        double[][] doubleData = new double[data.Length][];
        for (int x = 0; x < data.Length; x++)
        {
            doubleData[x] = new double[data[x].Length];
            for (int y = 0; y < data[x].Length; y++)
            {
                doubleData[x][y] = data[x][y];
            }
        }
        PrincipalComponentAnalysis pca = new PrincipalComponentAnalysis()
        {
            Method = PrincipalComponentMethod.Center,
            Whiten = true
        };

        MultivariateLinearRegression transform = pca.Learn(doubleData);

        pca.NumberOfOutputs = nrOfComponents;

        double[][] output1 = pca.Transform(doubleData);


        float[][] outAsFloat = new float[output1.Length][];
        for (int x = 0; x < output1.Length; x++)
        {
            outAsFloat[x] = new float[output1[x].Length];
            for (int y = 0; y < output1[x].Length; y++)
            {
                outAsFloat[x][y] = (float)output1[x][y];
            }
        }

        currentPCAValues = outAsFloat;
    }
        static public void Main()
        {
            MultivariateLinearRegression m = new MultivariateLinearRegression();

            m.points = new double[, ] {
                { 10, 100, 25.2 },
                { 10, 100, 27.3 },
                { 10, 100, 28.7 },
                { 15, 225, 29.8 },
                { 15, 225, 31.1 },
                { 15, 225, 27.8 },
                { 20, 400, 31.2 },
                { 20, 400, 32.6 },
                { 20, 400, 29.7 },
                { 25, 625, 31.7 },
                { 25, 625, 30.1 },
                { 25, 625, 32.3 },
                { 30, 900, 29.4 },
                { 30, 900, 30.8 },
                { 30, 900, 32.8 }
            };
            Console.WriteLine(m.B.ToStr("\n\r"));
            Console.Read();
        }
示例#24
0
        public void learn_whiten_success()
        {
            #region doc_learn_1
            // Below is the same data used on the excellent paper "Tutorial
            //   On Principal Component Analysis", by Lindsay Smith (2002).
            double[][] data =
            {
                new double[] { 2.5, 2.4 },
                new double[] { 0.5, 0.7 },
                new double[] { 2.2, 2.9 },
                new double[] { 1.9, 2.2 },
                new double[] { 3.1, 3.0 },
                new double[] { 2.3, 2.7 },
                new double[] { 2.0, 1.6 },
                new double[] { 1.0, 1.1 },
                new double[] { 1.5, 1.6 },
                new double[] { 1.1, 0.9 }
            };

            // Let's create an analysis with centering (covariance method)
            // but no standardization (correlation method) and whitening:
            var pca = new PrincipalComponentAnalysis()
            {
                Method = PrincipalComponentMethod.Center,
                Whiten = true
            };

            // Now we can learn the linear projection from the data
            MultivariateLinearRegression transform = pca.Learn(data);

            // Finally, we can project all the data
            double[][] output1 = pca.Transform(data);

            // Or just its first components by setting
            // NumberOfOutputs to the desired components:
            pca.NumberOfOutputs = 1;

            // And then calling transform again:
            double[][] output2 = pca.Transform(data);

            // We can also limit to 80% of explained variance:
            pca.ExplainedVariance = 0.8;

            // And then call transform again:
            double[][] output3 = pca.Transform(data);
            #endregion

            double[] eigenvalues = { 1.28402771, 0.0490833989 };
            double[] proportion  = eigenvalues.Divide(eigenvalues.Sum());
            double[,] eigenvectors =
            {
                { 0.19940687993951403, -1.1061252858739095 },
                { 0.21626410214440508,  1.0199057073792104 }
            };

            // Everything is alright (up to the 9 decimal places shown in the tutorial)
            Assert.IsTrue(eigenvectors.IsEqual(pca.ComponentMatrix, rtol: 1e-9));
            Assert.IsTrue(proportion.IsEqual(pca.ComponentProportions, rtol: 1e-9));
            Assert.IsTrue(eigenvalues.IsEqual(pca.Eigenvalues, rtol: 1e-5));

            pca.ExplainedVariance = 1.0;
            double[][] actual = pca.Transform(data);

            double[][] expected =
            {
                new double[] {   0.243560157209023, -0.263472650637184 },
                new double[] {  -0.522902576315494,  0.214938218565977 },
                new double[] {   0.291870144299372,  0.578317788814594 },
                new double[] {  0.0806632088164338,   0.19622137941132 },
                new double[] {   0.492962746459375, -0.315204397734004 },
                new double[] {   0.268558011864442,  0.263724118751361 },
                new double[] { -0.0291545644762578, -0.526334573603598 },
                new double[] {  -0.336693495487974, 0.0698378585807067 },
                new double[] {  -0.128858004446015, 0.0267280693333571 },
                new double[] {  -0.360005627922904, -0.244755811482527 }
            };

            // var str = actual.ToString(CSharpJaggedMatrixFormatProvider.InvariantCulture);

            // Everything is correct (up to 8 decimal places)
            Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8));
            Assert.IsTrue(expected.IsEqual(output1, atol: 1e-8));
            Assert.IsTrue(expected.Get(null, 0, 1).IsEqual(output2, atol: 1e-8));
            Assert.IsTrue(expected.Get(null, 0, 1).IsEqual(output3, atol: 1e-8));

            actual = transform.Transform(data);
            Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8));
        }
        private static PartialLeastSquaresAnalysis CreateWineExample(out double[,] inputs, out double[,] outputs)
        {
            // References: http://www.utdallas.edu/~herve/Abdi-PLSR2007-pretty.pdf

            // Following the small example by Hervé Abdi (Hervé Abdi, Partial Least Square Regression),
            // we will create a simple example where the goal is to predict the subjective evaluation of
            // a set of 5 wines. The dependent variables that we want to predict for each wine are its
            // likeability, and how well it goes with meat, or dessert (as rated by a panel of experts).
            // The predictors are the price, the sugar, alcohol, and acidity content of each wine.


            // Here we will list the inputs, or characteristics we would like to use in order to infer
            // information from our wines. Each row denotes a different wine and lists its corresponding
            // observable characteristics. The inputs are usually denoted by X in the literature.

            inputs = new double[, ]
            {
                // Wine | Price | Sugar | Alcohol | Acidity
                { 7, 7, 13, 7 },
                { 4, 3, 14, 7 },
                { 10, 5, 12, 5 },
                { 16, 7, 11, 3 },
                { 13, 3, 10, 3 },
            };


            // Here we will list our dependent variables. Dependent variables are the outputs, or what we
            // would like to infer or predict from our available data, given a new observation. The outputs
            // are usually denotes as Y in the literature.

            outputs = new double[, ]
            {
                // Wine | Hedonic | Goes with meat | Goes with dessert
                { 14, 7, 8 },
                { 10, 7, 6 },
                { 8, 5, 5 },
                { 2, 4, 7 },
                { 6, 2, 4 },
            };


            // Next, we will create our Partial Least Squares Analysis passing the inputs (values for
            // predictor variables) and the associated outputs (values for dependent variables).

            // We will also be using the using the Covariance Matrix/Center method (data will only
            // be mean centered but not normalized) and the NIPALS algorithm.
            PartialLeastSquaresAnalysis pls = new PartialLeastSquaresAnalysis(inputs, outputs,
                                                                              AnalysisMethod.Center, PartialLeastSquaresAlgorithm.SIMPLS);

            // Compute the analysis with all factors. The number of factors
            // could also have been specified in a overload of this method.

            pls.Compute();

            // After computing the analysis, we can create a linear regression model in order
            // to predict new variables. To do that, we may call the CreateRegression() method.

            MultivariateLinearRegression regression = pls.CreateRegression();

            // After the regression has been created, we will be able to classify new instances.
            // For example, we will compute the outputs for the first input sample:

            double[] y = regression.Compute(new double[] { 7, 7, 13, 7 });

            // The y output will be very close to the corresponding output used as reference.
            // In this case, y is a vector of length 3 with values { 14.00, 7.00, 7.75 }.

            Assert.AreEqual(14.00, y[0], 1e-2);
            Assert.AreEqual(+7.00, y[1], 1e-2);
            Assert.AreEqual(+7.75, y[2], 1e-2);

            return(pls);
        }
示例#26
0
        public void learn_test()
        {
            Accord.Math.Random.Generator.Seed = 0;
            #region doc_learn
            // Let's create a random dataset containing
            // 5000 samples of two dimensional samples.
            //
            double[][] source = Jagged.Random(5000, 2);

            // Now, we will mix the samples the dimensions of the samples.
            // A small amount of the second column will be applied to the
            // first, and vice-versa.
            //
            double[][] mix =
            {
                new double[] {  0.25, 0.25 },
                new double[] { -0.25, 0.75 },
            };

            // mix the source data
            double[][] input = source.Dot(mix);

            // Now, we can use ICA to identify any linear mixing between the variables, such
            // as the matrix multiplication we did above. After it has identified it, we will
            // be able to revert the process, retrieving our original samples again

            // Create a new Independent Component Analysis
            var ica = new IndependentComponentAnalysis()
            {
                Algorithm = IndependentComponentAlgorithm.Parallel,
                Contrast  = new Logcosh()
            };

            // Learn the demixing transformation from the data
            MultivariateLinearRegression demix = ica.Learn(input);

            // Now, we can retrieve the mixing and demixing matrices that were
            // used to alter the data. Note that the analysis was able to detect
            // this information automatically:

            double[][] mixingMatrix = ica.MixingMatrix;   // same as the 'mix' matrix
            double[][] revertMatrix = ica.DemixingMatrix; // inverse of the 'mix' matrix

            // We can use the regression to recover the separate sources
            double[][] result = demix.Transform(input);
            #endregion


            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum());
            Assert.IsTrue(mix.IsEqual(mixingMatrix, atol: 0.008));

            Assert.IsTrue(revertMatrix.IsEqual(demix.Weights, atol: 0.008));
            var dm = demix.Inverse().Weights;
            dm = dm.Divide(dm.Sum());
            Assert.IsTrue(mixingMatrix.IsEqual(dm, atol: 0.008));


            // Verify demixing matrix
            double[,] expected =
            {
                { 0.75, -0.25 },
                { 0.25,  0.25 },
            };

            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);
            Assert.AreEqual(ica.Contrast.GetType(), typeof(Logcosh));

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, atol: 0.008));
        }
示例#27
0
        private void btnCompute_Click(object sender, EventArgs e)
        {
            if (dgvAnalysisSource.DataSource == null)
            {
                MessageBox.Show("Please load some data first.");
                return;
            }

            // Finishes and save any pending changes to the given data
            dgvAnalysisSource.EndEdit();
            DataTable table = dgvAnalysisSource.DataSource as DataTable;

            // Creates a matrix from the source data table
            double[,] sourceMatrix = table.ToMatrix(out inputColumnNames);

            // Creates the Simple Descriptive Analysis of the given source
            sda = new DescriptiveAnalysis(sourceMatrix, inputColumnNames);
            sda.Compute();

            // Populates statistics overview tab with analysis data
            dgvDistributionMeasures.DataSource = sda.Measures;


            // Extract variables
            List <string> inputNames = new List <string>();

            foreach (string name in clbInput.CheckedItems)
            {
                inputNames.Add(name);
            }
            this.inputColumnNames = inputNames.ToArray();

            List <string> outputNames = new List <string>();

            foreach (string name in clbOutput.CheckedItems)
            {
                outputNames.Add(name);
            }
            this.outputColumnNames = outputNames.ToArray();

            DataTable inputTable  = table.DefaultView.ToTable(false, inputColumnNames);
            DataTable outputTable = table.DefaultView.ToTable(false, outputColumnNames);

            double[,] inputs  = inputTable.ToMatrix();
            double[,] outputs = outputTable.ToMatrix();



            // Creates the Partial Least Squares of the given source
            pls = new PartialLeastSquaresAnalysis(inputs, outputs,
                                                  (AnalysisMethod)cbMethod.SelectedValue,
                                                  (PartialLeastSquaresAlgorithm)cbAlgorithm.SelectedValue);


            // Computes the Partial Least Squares
            pls.Compute();


            // Populates components overview with analysis data
            dgvWeightMatrix.DataSource = new ArrayDataView(pls.Weights);
            dgvFactors.DataSource      = pls.Factors;

            dgvAnalysisLoadingsInput.DataSource  = new ArrayDataView(pls.Predictors.FactorMatrix);
            dgvAnalysisLoadingsOutput.DataSource = new ArrayDataView(pls.Dependents.FactorMatrix);

            this.regression = pls.CreateRegression();
            dgvRegressionCoefficients.DataSource = new ArrayDataView(regression.Coefficients, outputColumnNames);
            dgvRegressionIntercept.DataSource    = new ArrayDataView(regression.Intercepts, outputColumnNames);

            dgvProjectionComponents.DataSource = pls.Factors;
            numComponents.Maximum = pls.Factors.Count;
            numComponents.Value   = 1;

            dgvRegressionComponents.DataSource = pls.Factors;
            numComponentsRegression.Maximum    = pls.Factors.Count;
            numComponentsRegression.Value      = 1;

            distributionView.DataSource     = pls.Factors;
            cumulativeView.DataSource       = pls.Factors;
            dgvProjectionSourceX.DataSource = inputTable;
            dgvProjectionSourceY.DataSource = outputTable;

            dgvRegressionInput.DataSource = table.DefaultView.ToTable(false,
                                                                      inputColumnNames.Concatenate(outputColumnNames));
        }
示例#28
0
        public void learn_standardize()
        {
            double[][] data =
            {
                new double[] { 2.5, 2.4 },
                new double[] { 0.5, 0.7 },
                new double[] { 2.2, 2.9 },
                new double[] { 1.9, 2.2 },
                new double[] { 3.1, 3.0 },
                new double[] { 2.3, 2.7 },
                new double[] { 2.0, 1.6 },
                new double[] { 1.0, 1.1 },
                new double[] { 1.5, 1.6 },
                new double[] { 1.1, 0.9 }
            };

            var pca = new PrincipalComponentAnalysis()
            {
                Method = PrincipalComponentMethod.Standardize,
                Whiten = false
            };

            MultivariateLinearRegression transform = pca.Learn(data);

            double[][] output1 = pca.Transform(data);

            double[] eigenvalues = { 1.925929272692245, 0.074070727307754519 };
            double[] proportion  = eigenvalues.Divide(eigenvalues.Sum());
            double[,] eigenvectors =
            {
                { 0.70710678118654791, -0.70710678118654791 },
                { 0.70710678118654791,  0.70710678118654791 }
            };

            Assert.IsTrue(eigenvectors.IsEqual(pca.ComponentMatrix, rtol: 1e-9));
            Assert.IsTrue(proportion.IsEqual(pca.ComponentProportions, rtol: 1e-9));
            Assert.IsTrue(eigenvalues.IsEqual(pca.Eigenvalues, rtol: 1e-5));

            pca.ExplainedVariance = 1.0;
            double[][] actual = pca.Transform(data);
            //      var str = actual.ToCSharp();
            double[][] expected =
            {
                new double[] {    1.03068028963519, -0.212053139513466 },
                new double[] {   -2.19045015647317,  0.168942295968493 },
                new double[] {    1.17818776184333,   0.47577321493322 },
                new double[] {   0.323294642065681,  0.161198977394117 },
                new double[] {    2.07219946786664, -0.251171725759119 },
                new double[] {    1.10117414355213,  0.218653302562498 },
                new double[] { -0.0878525068874546, -0.430054465638535 },
                new double[] {   -1.40605089061245, 0.0528100914316325 },
                new double[] {  -0.538118242086245, 0.0202112695602547 },
                new double[] {   -1.48306450890365, -0.204309820939091 }
            };

            Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8));
            Assert.IsTrue(expected.IsEqual(output1, atol: 1e-8));

            actual = transform.Transform(data);
            Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8));
        }
示例#29
0
        private double[][] CreateWeights(List <Stock> listOfStocks)
        {
            // The multivariate linear regression is a generalization of
            // the multiple linear regression. In the multivariate linear
            // regression, not only the input variables are multivariate,
            // but also are the output dependent variables.

            // In the following example, we will perform a regression of
            // a 2-dimensional output variable over a 3-dimensional input
            // variable.

            var inputs  = new double[listOfStocks.Count][];
            var outputs = new double[listOfStocks.Count][];

            for (int i = 0; i < listOfStocks.Count; i++)
            {
                inputs[i] = new[]
                {
                    listOfStocks[i].AdjClose,
                    listOfStocks[i].Close,
                    listOfStocks[i].High,
                    listOfStocks[i].Low,
                    listOfStocks[i].Open,
                };
                outputs[i] = new[] { listOfStocks[i].Volume };
            }



            // With a quick eye inspection, it is possible to see that
            // the first output variable y1 is always the double of the
            // first input variable. The second output variable y2 is
            // always the triple of the first input variable. The other
            // input variables are unused. Nevertheless, we will fit a
            // multivariate regression model and confirm the validity
            // of our impressions:

            // Use Ordinary Least Squares to create the regression
            OrdinaryLeastSquares ols = new OrdinaryLeastSquares();


            // Now, compute the multivariate linear regression:
            MultivariateLinearRegression regression = ols.Learn(inputs, outputs);



            // We can obtain predictions using
            double[][] predictions = regression.Transform(inputs);



            // The prediction error is
            double error = new SquareLoss(outputs).Loss(predictions); // 0

            // At this point, the regression error will be 0 (the fit was
            // perfect). The regression coefficients for the first input
            // and first output variables will be 2. The coefficient for
            // the first input and second output variables will be 3. All
            // others will be 0.
            //
            // regression.Coefficients should be the matrix given by
            //
            // double[,] coefficients = {
            //                              { 2, 3 },
            //                              { 0, 0 },
            //                              { 0, 0 },
            //                          };
            //

            // We can also check the r-squared coefficients of determination:
            double[] r2 = regression.CoefficientOfDetermination(inputs, outputs);

            return(regression.Weights);
        }
        public void RegressTest2()
        {
            // The multivariate linear regression is a generalization of
            // the multiple linear regression. In the multivariate linear
            // regression, not only the input variables are multivariate,
            // but also are the output dependent variables.

            // In the following example, we will perform a regression of
            // a 2-dimensional output variable over a 3-dimensional input
            // variable.

            double[][] inputs =
            {
                // variables:  x1  x2  x3
                new double[] { 1, 1, 1 },    // input sample 1
                new double[] { 2, 1, 1 },    // input sample 2
                new double[] { 3, 1, 1 },    // input sample 3
            };

            double[][] outputs =
            {
                // variables:  y1  y2
                new double[] { 2, 3 },   // corresponding output to sample 1
                new double[] { 4, 6 },   // corresponding output to sample 2
                new double[] { 6, 9 },   // corresponding output to sample 3
            };

            // With a quick eye inspection, it is possible to see that
            // the first output variable y1 is always the double of the
            // first input variable. The second output variable y2 is
            // always the triple of the first input variable. The other
            // input variables are unused. Nevertheless, we will fit a
            // multivariate regression model and confirm the validity
            // of our impressions:

            // Create a new multivariate linear regression with 3 inputs and 2 outputs
            var regression = new MultivariateLinearRegression(3, 2);

            // Now, compute the multivariate linear regression:
            double error = regression.Regress(inputs, outputs);

            // At this point, the regression error will be 0 (the fit was
            // perfect). The regression coefficients for the first input
            // and first output variables will be 2. The coefficient for
            // the first input and second output variables will be 3. All
            // others will be 0.
            //
            // regression.Coefficients should be the matrix given by
            //
            // double[,] coefficients = {
            //                              { 2, 3 },
            //                              { 0, 0 },
            //                              { 0, 0 },
            //                          };
            //

            // The first input variable coefficients will be 2 and 3:
            Assert.AreEqual(2, regression.Coefficients[0, 0], 1e-10);
            Assert.AreEqual(3, regression.Coefficients[0, 1], 1e-10);

            // And all other coefficients will be 0:
            Assert.AreEqual(0, regression.Coefficients[1, 0], 1e-10);
            Assert.AreEqual(0, regression.Coefficients[1, 1], 1e-10);
            Assert.AreEqual(0, regression.Coefficients[2, 0], 1e-10);
            Assert.AreEqual(0, regression.Coefficients[2, 1], 1e-10);

            // We can also check the r-squared coefficients of determination:
            double[] r2 = regression.CoefficientOfDetermination(inputs, outputs);

            // Which should be one for both output variables:
            Assert.AreEqual(1, r2[0]);
            Assert.AreEqual(1, r2[1]);

            foreach (var e in regression.Coefficients)
            {
                Assert.IsFalse(double.IsNaN(e));
            }

            Assert.AreEqual(0, error, 1e-10);
            Assert.IsFalse(double.IsNaN(error));
        }