示例#1
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);
        }
示例#2
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 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));
        }
示例#4
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);
        }
示例#5
0
        /// <summary>
        /// 求出输入数据的一次模型  和  二次模型   输出顺序为 一次斜率k 截距b  模型拟合程度r  二次参数a 一次参数b  截距c  模型拟合程度r
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static List <PValue> getModel(List <PValue> input)
        {
            double MTAEquRLK = 0;  //k
            double MTAEquRLB = 0;  //b
            double MTAEquRLR = 0;  //r
            double MTAEquRQA = 0;  //a
            double MTAEquRQB = 0;  //b
            double MTAEquRQC = 0;  //c
            double MTAEquRQR = 0;  //r
            int    length    = input.Count;

            double[] X = new double[length];
            double[] Y = new double[length];
            for (int i = 0; i < length; i++)
            {
                X[i] = i + 1;
                Y[i] = input[i].Value;
            }
            OrdinaryLeastSquares   ols        = new OrdinaryLeastSquares();
            SimpleLinearRegression regression = ols.Learn(X, Y);

            MTAEquRLK = regression.Slope;                                       //一次斜率
            MTAEquRLB = regression.Intercept;                                   //截距
            MTAEquRLR = regression.CoefficientOfDetermination(X, Y);            //R^2代表模型拟合程度

            double[][] MultiX = new double[X.Length][];
            for (int i = 0; i < X.Length; i++)
            {
                double x = X[i];
                MultiX[i] = new double[] { x *x, x };
            }
            double[][] MultiY = new double[X.Length][];
            //MultiY =X'
            for (int i = 0; i < X.Length; i++)
            {
                double y = Y[i];
                MultiY[i] = new double[] { y };
            }
            OrdinaryLeastSquares         olsMulti        = new OrdinaryLeastSquares();
            MultivariateLinearRegression regressionMulti = olsMulti.Learn(MultiX, MultiY);
            double[][] weights = regressionMulti.Weights;
            MTAEquRQA = weights[0][0];                                                 //二次参数
            MTAEquRQB = weights[1][0];                                                 //一次参数
            MTAEquRQC = regressionMulti.Intercepts[0];                                 //截距
            MTAEquRQR = regressionMulti.CoefficientOfDetermination(MultiX, MultiY)[0]; //R^2代表模型拟合程度
            PValue        LK   = new PValue(); LK.Value = MTAEquRLK;
            PValue        LB   = new PValue(); LB.Value = MTAEquRLB;
            PValue        LR   = new PValue(); LR.Value = MTAEquRLR;
            PValue        QA   = new PValue(); QA.Value = MTAEquRQA;
            PValue        QB   = new PValue(); QB.Value = MTAEquRQB;
            PValue        QC   = new PValue(); QC.Value = MTAEquRQC;
            PValue        QR   = new PValue(); QR.Value = MTAEquRQR;
            List <PValue> list = new List <PValue>();
            list.Add(LK);
            list.Add(LB);
            list.Add(LR);
            list.Add(QA);
            list.Add(QB);
            list.Add(QC);
            list.Add(QR);
            return(list);
        }
示例#6
0
        public static Results Calcu(List <PValue>[] inputs, CalcuInfo calcuinfo)
        {
            //公用变量
            bool   _errorFlag   = false;
            string _errorInfo   = "";
            bool   _warningFlag = false;
            string _warningInfo = "";
            bool   _fatalFlag   = false;
            string _fatalInfo   = "";

            int i;

            //0输出初始化:该算法如果没有有效输入值(inputs为null)或者输入值得有效值为null,则应当有输出,大部分的输出项为0。个别输出项为空
            List <PValue>[] results = new List <PValue> [7];
            for (i = 0; i < results.Length; i++)
            {
                results[i] = new List <PValue>();
                results[i].Add(new PValue(0, calcuinfo.fstarttime, calcuinfo.fendtime, (long)StatusConst.InputIsNull));
            }

            try
            {
                //数据量分析
                //——MMTAAnaBase对偏差的计算结果,有19个值,一个设备100个温度点,那么也就1900个点。readmulti读取速度不会太慢。
                //——本计算仅用到19个计算结果中的均值,但是为了配置方便,仍然按MMTAAnaBase计算结果全部取得方式
                int MMTAAnaBaseOutputNumber = 19;   //本算法是直接读取MMTAAnaBase算法所有计算结果。MMTAAnaBase算法有19个计算结果。要靠次参数完成对计算结果的解析。
                int validDataNumber         = 0;
                //0、输入:输入是同一设备上所有温度点的小时均值,或者天均值
                //0.1、输入处理:输入长度。当输入为空时,则输出项也为空.
                if (inputs == null)
                {
                    return(new Results(results, _errorFlag, _errorInfo, _warningFlag, _warningInfo, _fatalFlag, _fatalInfo));        //不报错,直接返回默认值。整体为空计算引擎已经记录报警或错误。
                }
                //0.2、输入处理:截止时刻值。该算法不需要截止时刻点参与计算。
                //输入的是该设备上所有温度测点每小时基本统计值,每个统计值也就是每个测点,仅有两个值,一个是小时值,一个是截止时刻值
                string[] sourtagname = calcuinfo.sourcetagname.Split(';');              //本算法是做管子位置与温度的回归。管子的位置需要从标签名称中取
                List <List <PValue> > AvgInputsList  = new List <List <PValue> >();     //将MMTAAnaBase算法结果中的均值取出来
                List <string>         AvgtagnameList = new List <string>();             //将均值对应的标签名称取出来
                for (i = 0; i < inputs.Length; i++)
                {
                    //按MMTAAnaBase算法,将均值(MMTAAnaBaseOutputNumber==1 )取出
                    //对于设备来说,某些单点状态位异常,直接过滤而不进行报警。只有这一类计算的结果全部异常才报警。

                    if (i % MMTAAnaBaseOutputNumber == 1 && inputs[i] != null && inputs[i].Count >= 2 && inputs[i][0].Status == 0)
                    {
                        //找到所有有效均值的标签名
                        AvgtagnameList.Add(sourtagname[i]);

                        //找到数据
                        //if (AvgInputsList[i / MMTAAnaBaseOutputNumber] == null)
                        AvgInputsList.Add(new List <PValue>());

                        //只取第一个值
                        AvgInputsList[validDataNumber].Add(new PValue(inputs[i][0].Value, inputs[i][0].Timestamp, inputs[i][0].Endtime, inputs[i][0].Status));

                        validDataNumber = validDataNumber + 1;
                    }
                }//end for
                string[]        Avgtagname = AvgtagnameList.ToArray();      //有效点不同,标签数量不同,删除后面的空位。
                List <PValue>[] AvgInputs  = AvgInputsList.ToArray();   //与标签点对应添加的有效数据
                if (validDataNumber < 2)
                {
                    _warningFlag = true;
                    _warningInfo = "当前时间段过滤非正常状态点后,全部点的均值有效个数少于2个,无法进行回归计算!";
                    return(new Results(results, _errorFlag, _errorInfo, _warningFlag, _warningInfo, _fatalFlag, _fatalInfo));
                }

                //定义变量
                double MTAEquRLK = 0;
                double MTAEquRLB = 0;
                double MTAEquRLR = 0;
                double MTAEquRQA = 0;
                double MTAEquRQB = 0;
                double MTAEquRQC = 0;
                double MTAEquRQR = 0;

                //计算
                //1、线性回归
                //参考 http://accord-framework.net/docs/html/T_Accord_Statistics_Models_Regression_Linear_SimpleLinearRegression.htm
                List <double> ArrayX = new List <double>();       //回归分析,自变量是管号
                List <double> ArrayY = new List <double>();       //回归分析,应变量是温度
                for (i = 0; i < AvgInputs.Length; i++)
                {
                    double xposition = double.Parse(Avgtagname[i].Substring(7, 2));
                    for (int j = 0; j < AvgInputs[i].Count; j++)
                    {
                        ArrayX.Add(xposition);
                        ArrayY.Add(AvgInputs[i][j].Value);
                    }
                }

                /*
                 * //测试数据
                 * ArrayX = new List<double> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                 * ArrayY = new List<double> { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
                 * //按上述测试数据,MTAEquRLK=2,MTAEquRLB=0,MTAEquRLRR=1
                 *
                 *
                 * ArrayX = new double[10] { 1.2, 1.9, 3.1, 3.7, 5, 6, 7.1, 7.9, 9, 10 };
                 * ArrayY = new double[10] { 2.1, 4.2, 6.8, 8.5, 10, 12, 14.6, 14, 18, 20 };
                 */
                //按上述测试数据,MTAEquRLK=1.911,MTAEquRLB=0.526,MTAEquRLRR=0.985

                double[] X = ArrayX.ToArray();
                double[] Y = ArrayY.ToArray();;

                OrdinaryLeastSquares   ols        = new OrdinaryLeastSquares();
                SimpleLinearRegression regression = ols.Learn(X, Y);

                MTAEquRLK = regression.Slope;                                       //一次斜率
                MTAEquRLB = regression.Intercept;                                   //截距
                MTAEquRLR = regression.CoefficientOfDetermination(X, Y);            //R^2代表模型拟合程度

                //2、多项式回归
                //参考 http://accord-framework.net/docs/html/T_Accord_Statistics_Models_Regression_Linear_MultivariateLinearRegression.htm
                //MultiX =[X^2 ,X]
                double[][] MultiX = new double[X.Length][];
                for (i = 0; i < X.Length; i++)
                {
                    double x = X[i];
                    MultiX[i] = new double[] { x *x, x };
                }
                double[][] MultiY = new double[X.Length][];
                //MultiY =X'
                for (i = 0; i < X.Length; i++)
                {
                    double y = Y[i];
                    MultiY[i] = new double[] { y };
                }

                //测试数据

                /*
                 * MultiX = new double[5][];
                 * MultiX[0] = new double[2] { 1, 1};
                 * MultiX[1] = new double[2] { 4, 2};
                 * MultiX[2] = new double[2] { 9, 3};
                 * MultiX[3] = new double[2] { 16, 4};
                 * MultiX[4] = new double[2] { 25, 5};
                 *
                 * MultiY = new double[5][];
                 * MultiY[0] = new double[1] { 7 };
                 * MultiY[1] = new double[1] { 17 };
                 * MultiY[2] = new double[1] { 31 };
                 * MultiY[3] = new double[1] { 49 };
                 * MultiY[4] = new double[1] { 71 };
                 */
                //按上述测试数据,MTAEquRQA=2,MTAEquRQB=4,MTAEquRQC=1,MTAEquRQR=1

                /*
                 * MultiX = new double[5][];
                 * MultiX[0] = new double[2] { 1, 1 };
                 * MultiX[1] = new double[2] { 4, 2 };
                 * MultiX[2] = new double[2] { 9, 3 };
                 * MultiX[3] = new double[2] { 16, 4 };
                 * MultiX[4] = new double[2] { 25, 5 };
                 *
                 * MultiY = new double[5][];
                 * MultiY[0] = new double[1] { 7.1 };
                 * MultiY[1] = new double[1] { 16.9 };
                 * MultiY[2] = new double[1] { 32 };
                 * MultiY[3] = new double[1] { 51 };
                 * MultiY[4] = new double[1] { 69 };
                 */
                //按上述测试数据,MTAEquRQA=1.45,MTAEquRQB=7.08,MTAEquRQC=-2,MTAEquRQR=0.998

                OrdinaryLeastSquares         olsMulti        = new OrdinaryLeastSquares();
                MultivariateLinearRegression regressionMulti = olsMulti.Learn(MultiX, MultiY);

                double[][] weights = regressionMulti.Weights;
                MTAEquRQA = weights[0][0];                                                 //二次参数
                MTAEquRQB = weights[1][0];                                                 //一次参数
                MTAEquRQC = regressionMulti.Intercepts[0];                                 //截距
                MTAEquRQR = regressionMulti.CoefficientOfDetermination(MultiX, MultiY)[0]; //R^2代表模型拟合程度

                //组织计算结果
                results[0] = new List <PValue>();
                results[0].Add(new PValue(MTAEquRLK, calcuinfo.fstarttime, calcuinfo.fendtime, 0));

                results[1] = new List <PValue>();
                results[1].Add(new PValue(MTAEquRLB, calcuinfo.fstarttime, calcuinfo.fendtime, 0));

                results[2] = new List <PValue>();
                results[2].Add(new PValue(MTAEquRLR, calcuinfo.fstarttime, calcuinfo.fendtime, 0));

                results[3] = new List <PValue>();
                results[3].Add(new PValue(MTAEquRQA, calcuinfo.fstarttime, calcuinfo.fendtime, 0));

                results[4] = new List <PValue>();
                results[4].Add(new PValue(MTAEquRQB, calcuinfo.fstarttime, calcuinfo.fendtime, 0));

                results[5] = new List <PValue>();
                results[5].Add(new PValue(MTAEquRQC, calcuinfo.fstarttime, calcuinfo.fendtime, 0));

                results[6] = new List <PValue>();
                results[6].Add(new PValue(MTAEquRQR, calcuinfo.fstarttime, calcuinfo.fendtime, 0));

                return(new Results(results, _errorFlag, _errorInfo, _warningFlag, _warningInfo, _fatalFlag, _fatalInfo));
            }
            catch (Exception ex)
            {
                //计算中出任何错误,则需要记录log
                //LogHelper.Write(LogType.Error, "计算模块错误!");
                //记录计算模块的名称、当前标签、起始时间、结束时间
                //string moduleInfo = string.Format("——计算模块的名称是:{0},当前计算源标签是:{1},计算起始时间是:{2},计算结束时间是:{3}。", calcuinfo.fmodulename, calcuInfo.sourcetagname, calcuinfo.fstarttime.ToString(), calcuinfo.fendtime.ToString());
                //LogHelper.Write(LogType.Error, moduleInfo);
                //计算引擎报错具体信息
                //string errInfo = string.Format("——具体报错信息:{0}。", ex.ToString());
                //LogHelper.Write(LogType.Error, errInfo);
                //返回null供计算引擎处理
                _fatalFlag = true;
                _fatalInfo = ex.ToString();
                return(new Results(results, _errorFlag, _errorInfo, _warningFlag, _warningInfo, _fatalFlag, _fatalInfo));
            }
        }