Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Analysis/3.3/Statistics/Fitting/LinearLeastSquaresFitting.cs @ 12154

Last change on this file since 12154 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Linq;
24
25namespace HeuristicLab.Analysis.Statistics {
26  public class LinearLeastSquaresFitting : IFitting {
27    public void Calculate(double[] dataPoints, out double slope, out double intercept) {
28      var stdX = Enumerable.Range(0, dataPoints.Count()).Select(x => (double)x).ToArray();
29      Calculate(dataPoints, stdX, out slope, out intercept);
30    }
31
32    public void Calculate(double[] y, double[] x, out double slope, out double intercept) {
33      if (y.Count() != x.Count()) {
34        throw new ArgumentException("The lenght of x and y needs do be equal. ");
35      }
36
37      double sxy = 0.0;
38      double sxx = 0.0;
39      int n = y.Count();
40      double sy = y.Sum();
41      double sx = ((n - 1) * n) / 2.0;
42      double avgy = sy / n;
43      double avgx = sx / n;
44
45      for (int i = 0; i < n; i++) {
46        sxy += x[i] * y[i];
47        sxx += x[i] * x[i];
48      }
49
50      slope = (sxy - (n * avgx * avgy)) / (sxx - (n * avgx * avgx));
51      intercept = avgy - slope * avgx;
52    }
53
54    public double CalculateError(double[] dataPoints, double slope, double intercept) {
55      double r;
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++) {
61        double y = slope * i + intercept;
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    }
69
70    public DataRow CalculateFittedLine(double[] y, double[] x) {
71      double slope, intercept;
72      Calculate(y, x, out slope, out intercept);
73
74      DataRow newRow = new DataRow();
75      for (int i = 0; i < x.Count(); i++) {
76        newRow.Values.Add(slope * x[i] + intercept);
77      }
78      return newRow;
79    }
80
81    public DataRow CalculateFittedLine(double[] dataPoints) {
82      DataRow newRow = new DataRow();
83      double slope, intercept;
84      Calculate(dataPoints, out slope, out intercept);
85      var stdX = Enumerable.Range(0, dataPoints.Count()).Select(x => (double)x).ToArray();
86
87      for (int i = 0; i < stdX.Count(); i++) {
88        newRow.Values.Add(slope * stdX[i] + intercept);
89      }
90
91      return newRow;
92    }
93
94    public override string ToString() {
95      return "Linear Fitting";
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.