/// <summary>
        ///  Creates a new linear regression from the regression coefficients.
        /// </summary>
        ///
        /// <param name="coefficients">The linear coefficients.</param>
        /// <param name="intercept">The intercept (bias) values.</param>
        ///
        /// <returns>A linear regression with the given coefficients.</returns>
        ///
        public static MultivariateLinearRegression FromCoefficients(double[][] coefficients, double[] intercept)
        {
            var regression = new MultivariateLinearRegression(coefficients.Length, coefficients[0].Length);

            regression.coefficients = coefficients.ToMatrix();
            regression.intercepts   = intercept;
            return(regression);
        }
Exemple #2
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 MultivariateLinearRegression FromData(double[][] x, double[][] y)
        {
            var regression = new MultivariateLinearRegression(x[0].Length, y[0].Length);

            regression.Regress(x, y);

            return(regression);
        }
        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;

            var 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 },
            };

            var 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);
        }
Exemple #4
0
 public bool Learn()
 {
     try
     {
         this.Regression = this.ols.Learn(this.Inputs, this.Outputs);
         this.IsTrained  = true;
         return(true);
     }
     catch (Exception e)
     {
         throw new Exception(
                   "Failed to learn using specified training data." + Environment.NewLine +
                   "Inner exception : " + e.Message
                   );
     }
     // return this as IAlgorithm;
 }
Exemple #5
0
        public MultivariateLinearRegression()
        {
            this.Name           = "Multivariate Linear Regression";
            this.Type           = AlgorithmType.Regression;
            this.IsTrained      = false;
            this.PredictionType = typeof(double[][]);
            this.ResultType     = typeof(double[][]);
            this.Inputs         = null;
            this.Outputs        = null;
            this.TestValue      = null;
            this.Result         = null;

            // initialise seed value for Accord framework
            Generator.Seed = new Random().Next();
            // set up linear regression using OrdinaryLeastSquares
            this.Regression = new Accord.Statistics.Models.Regression.Linear.MultivariateLinearRegression();
            this.ols        = new OrdinaryLeastSquares();
        }
        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;
        }
Exemple #7
0
        /// <summary>
        /// Construct a new Simple Linear Regression algorithm, using the specified training data.
        /// </summary>
        /// <param name="inputList">Use inputList as rows with equal numbers of featurs, which used for learning.</param>
        /// <param name="outputList">Use outputList as the rows that define the result column for each</param>
        public MultivariateLinearRegression(List <List <double> > inputList, List <List <double> > outputList)
        {
            Name           = "Multivariate Linear Regression";
            Type           = AlgorithmType.Regression;
            IsTrained      = false;
            PredictionType = typeof(double[][]);
            ResultType     = typeof(double[][]);
            Inputs         = null;
            Outputs        = null;
            TestValue      = null;
            Result         = null;

            // initialise seed value for Accord framework
            Generator.Seed = new Random().Next();

            // Process training data
            LoadTrainingData(inputList, outputList);

            // set up linear regression using OrdinaryLeastSquares
            regression = new Accord.Statistics.Models.Regression.Linear.MultivariateLinearRegression();
            ols        = new OrdinaryLeastSquares();
        }
        public List<Dish> CalculatePredictedRateForUnratedDishes(List<Rating> rates, List<Dish> unratedDishes)
        {
            List<Dish> recommendeDishes = new List<Dish>();
            double[][] inputs = new double[rates.Count][];
            double[][] outputs = new double[rates.Count][];
            for (int i = 0; i < rates.Count; i++)
            {
                inputs[i] = rates[i].DishParameters;
                outputs[i] = new double[] {rates[i].Rate};
            }

            MultivariateLinearRegression regression = new MultivariateLinearRegression(6, 1);
            double errorRegression = regression.Regress(inputs, outputs);

            foreach (var unratedDish in unratedDishes)
            {
                unratedDish.PredictedRate = regression.Compute(unratedDish.DishParameters)[0];
            }

            recommendeDishes.AddRange(unratedDishes.OrderByDescending(dish => dish.PredictedRate).ToList().Take(3));

            return recommendeDishes;
        }
        public double[] CalculateParametersForUser(List<Rating> ratesAndParametersOfDishesRatedByUser)
        {
            double[][] inputs = new double[ratesAndParametersOfDishesRatedByUser.Count][];
            double[][] outputs = new double[ratesAndParametersOfDishesRatedByUser.Count][];
            for (int i = 0; i < ratesAndParametersOfDishesRatedByUser.Count; i++)
            {
                inputs[i] = ratesAndParametersOfDishesRatedByUser[i].DishParameters;
            }
            for (int i = 0; i < ratesAndParametersOfDishesRatedByUser.Count; i++)
            {
                outputs[i] = new double[] { ratesAndParametersOfDishesRatedByUser[i].Rate};
            }

            MultivariateLinearRegression regression = new MultivariateLinearRegression(6, 1);

            double errorRegression = regression.Regress(inputs, outputs);
            double[] coef = new double[6];
            for (int k = 0; k < regression.Coefficients.Length; k++)
            {
                coef[k] = regression.Coefficients[k, 0];
            }
            return coef;
        }
        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));
        }
        /// <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 MultivariateLinearRegression FromData(double[][] x, double[][] y)
        {
            var regression = new MultivariateLinearRegression(x[0].Length, y[0].Length);

            regression.Regress(x, y);

            return regression;
        }
Exemple #12
0
        private void btnRunAnalysis_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;

            CreateComponentCumulativeDistributionGraph(graphCurve);
            CreateComponentDistributionGraph(graphShare);

            dgvProjectionSourceX.DataSource = inputTable;
            dgvProjectionSourceY.DataSource = outputTable;

            dgvRegressionInput.DataSource = table.DefaultView.ToTable(false, inputColumnNames.Concatenate(outputColumnNames));
        }
Exemple #13
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);
        }