Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/LinearLeastSquaresFitting.cs @ 11375

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

#2031 updated license headers and version

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 p0, out double p1) {
28      var stdX = Enumerable.Range(0, dataPoints.Count()).Select(x => (double)x).ToArray();
29      Calculate(dataPoints, stdX, out p0, out p1);
30    }
31
32    public void Calculate(double[] y, double[] x, out double p0, out double p1) {
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      p0 = (sxy - (n * avgx * avgy)) / (sxx - (n * avgx * avgx));
51      p1 = avgy - p0 * avgx;
52    }
53
54    public double CalculateError(double[] dataPoints, double p0, double p1) {
55      double r = 0.0;
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 = p0 * i + p1;
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, string rowName) {
71      double k, d;
72      Calculate(y, x, out k, out d);
73
74      DataRow newRow = new DataRow(rowName);
75      for (int i = 0; i < x.Count(); i++) {
76        newRow.Values.Add(k * x[i] + d);
77      }
78      return newRow;
79    }
80
81    public DataRow CalculateFittedLine(double[] dataPoints, string rowName) {
82      DataRow newRow = new DataRow(rowName);
83      double c0, c1;
84      Calculate(dataPoints, out c0, out c1);
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(c0 * stdX[i] + c1);
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.