[9353] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9353] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 |
|
---|
| 25 | namespace HeuristicLab.Analysis.Statistics {
|
---|
[9706] | 26 | public class LinearLeastSquaresFitting : IFitting {
|
---|
[11914] | 27 | public void Calculate(double[] dataPoints, out double slope, out double intercept) {
|
---|
[9706] | 28 | var stdX = Enumerable.Range(0, dataPoints.Count()).Select(x => (double)x).ToArray();
|
---|
[11914] | 29 | Calculate(dataPoints, stdX, out slope, out intercept);
|
---|
[9706] | 30 | }
|
---|
| 31 |
|
---|
[11914] | 32 | public void Calculate(double[] y, double[] x, out double slope, out double intercept) {
|
---|
[9706] | 33 | if (y.Count() != x.Count()) {
|
---|
| 34 | throw new ArgumentException("The lenght of x and y needs do be equal. ");
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[9353] | 37 | double sxy = 0.0;
|
---|
| 38 | double sxx = 0.0;
|
---|
[9706] | 39 | int n = y.Count();
|
---|
| 40 | double sy = y.Sum();
|
---|
[10017] | 41 | double sx = ((n - 1) * n) / 2.0;
|
---|
[9353] | 42 | double avgy = sy / n;
|
---|
| 43 | double avgx = sx / n;
|
---|
| 44 |
|
---|
| 45 | for (int i = 0; i < n; i++) {
|
---|
[9706] | 46 | sxy += x[i] * y[i];
|
---|
| 47 | sxx += x[i] * x[i];
|
---|
[9353] | 48 | }
|
---|
| 49 |
|
---|
[11914] | 50 | slope = (sxy - (n * avgx * avgy)) / (sxx - (n * avgx * avgx));
|
---|
| 51 | intercept = avgy - slope * avgx;
|
---|
[9353] | 52 | }
|
---|
| 53 |
|
---|
[11914] | 54 | public double CalculateError(double[] dataPoints, double slope, double intercept) {
|
---|
[11699] | 55 | double r;
|
---|
[9353] | 56 | double avgy = dataPoints.Average();
|
---|
| 57 | double sstot = 0.0;
|
---|
| 58 | double sserr = 0.0;
|
---|
| 59 |
|
---|
| 60 | for (int i = 0; i < dataPoints.Count(); i++) {
|
---|
[11914] | 61 | double y = slope * i + intercept;
|
---|
[9353] | 62 | sstot += Math.Pow(dataPoints[i] - avgy, 2);
|
---|
| 63 | sserr += Math.Pow(dataPoints[i] - y, 2);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | r = 1.0 - (sserr / sstot);
|
---|
| 67 | return r;
|
---|
| 68 | }
|
---|
[9706] | 69 |
|
---|
[11914] | 70 | public DataRow CalculateFittedLine(double[] y, double[] x) {
|
---|
| 71 | double slope, intercept;
|
---|
| 72 | Calculate(y, x, out slope, out intercept);
|
---|
[9706] | 73 |
|
---|
[11914] | 74 | DataRow newRow = new DataRow();
|
---|
[9706] | 75 | for (int i = 0; i < x.Count(); i++) {
|
---|
[11914] | 76 | newRow.Values.Add(slope * x[i] + intercept);
|
---|
[9706] | 77 | }
|
---|
| 78 | return newRow;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[11914] | 81 | public DataRow CalculateFittedLine(double[] dataPoints) {
|
---|
| 82 | DataRow newRow = new DataRow();
|
---|
| 83 | double slope, intercept;
|
---|
| 84 | Calculate(dataPoints, out slope, out intercept);
|
---|
[9706] | 85 | var stdX = Enumerable.Range(0, dataPoints.Count()).Select(x => (double)x).ToArray();
|
---|
| 86 |
|
---|
| 87 | for (int i = 0; i < stdX.Count(); i++) {
|
---|
[11914] | 88 | newRow.Values.Add(slope * stdX[i] + intercept);
|
---|
[9706] | 89 | }
|
---|
| 90 |
|
---|
| 91 | return newRow;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public override string ToString() {
|
---|
| 95 | return "Linear Fitting";
|
---|
| 96 | }
|
---|
[9353] | 97 | }
|
---|
| 98 | }
|
---|