Example #1
0
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            int firstItem  = comboValues1.SelectedIndex;
            int secondItem = comboValues2.SelectedIndex;

            for (int i = 2; i < 302; i++)
            {
                // if (Convert.ToDouble((range.Cells[i, 1] as Excel.Range).Value2.ToString().Split(',')[secondItem].CompareTo(".")))
                yLine[i - 2] = Convert.ToDouble((range.Cells[i, 1] as Excel.Range).Value2.ToString().Split(',')[secondItem]);
                // if(Convert.ToDouble((range.Cells[i, 1] as Excel.Range).Value2.ToString().Split(',')[firstItem].CompareTo(".")))
                xLine[i - 2] = Convert.ToDouble((range.Cells[i, 1] as Excel.Range).Value2.ToString().Split(',')[firstItem].Replace('.', ','));
            }
            KeyValuePair <double, double>[] valueList = new KeyValuePair <double, double> [xLine.Length];

            LinearRegression LR = new LinearRegression(xLine, yLine);

            labelEq.Content = LR.GetEquation();
            labelR.Content  = LR.GetDetermination();

            for (int i = 0; i < xLine.Length; i++)
            {
                valueList[i] = new KeyValuePair <double, double>(xLine[i], yLine[i]);
            }

            KeyValuePair <double, double>[] regressionLine = new KeyValuePair <double, double>[2] {
                new KeyValuePair <double, double>(xLine.Min(), LR.Predict(xLine.Min())), new KeyValuePair <double, double>(xLine.Max(), LR.Predict(xLine.Max()))
            };

            ((LineSeries)Graph.Series[0]).ItemsSource = valueList;
            ((LineSeries)Graph.Series[1]).ItemsSource = regressionLine;
            Gr2.Title = "Regression Line";
            Gr1.Title = "Data points";
        }
        /// <summary>
        /// a simple method to test the algorithm with ternary linear regression.
        /// </summary>
        static void Test()
        {
            List <List <double> > X = new List <List <double> >()
            {
                new List <double>()
                {
                    1, 0, 0, 0
                },
                new List <double>()
                {
                    1, 13, 169, 2197
                },
                new List <double>()
                {
                    1, 34, 1156, 39304
                },
                new List <double>()
                {
                    1, 47, 2209, 103823
                },
                new List <double>()
                {
                    1, 67, 4489, 300763
                },
                new List <double>()
                {
                    1, 97, 9409, 912673
                },
                new List <double>()
                {
                    1, 122, 14884, 1815848
                },
                new List <double>()
                {
                    1, 150, 22500, 3375000
                },
                new List <double>()
                {
                    1, 164, 26896, 4410944
                },
                new List <double>()
                {
                    1, 229, 52441, 12008989
                },
                new List <double>()
                {
                    1, 291, 84681, 24642171
                },
                new List <double>()
                {
                    1, 351, 123201, 43243551
                },
                new List <double>()
                {
                    1, 364, 132496, 48228544
                },
                new List <double>()
                {
                    1, 422, 178084, 75151448
                },
                new List <double>()
                {
                    1, 493, 243049, 119823157
                }
            };

            List <double> y = new List <double> {
                54, 75, 97, 108, 120, 136, 145, 161, 164, 185, 211, 243, 271, 296, 320
            };

            // init

            //for (int i = 0; i < x.Count; i++)
            //{
            //    var example = new List<double> { 1, x[i], Math.Pow(x[i], 2), Math.Pow(x[i], 3) };
            //    X.Add(example);
            //}

            // call function


            var liner = new LinearRegression(1, 100000);
            var theta = liner.GradeDecent(X, y);

            // write result
            for (int i = 0; i < theta.Count; i++)
            {
                Console.Write("{0}, ", theta[i]);
            }
            Console.WriteLine();
        }