1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022013 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 


22  using System;


23  using System.Linq;


24 


25  namespace 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;


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  //TODO: adapt or remove


55  public double CalculateError(double[] dataPoints, double a1, double a0) {


56  double r = 0.0;


57  double avgy = dataPoints.Average();


58  double sstot = 0.0;


59  double sserr = 0.0;


60 


61  for (int i = 0; i < dataPoints.Count(); i++) {


62  double y = a1 * i + a0;


63  sstot += Math.Pow(dataPoints[i]  avgy, 2);


64  sserr += Math.Pow(dataPoints[i]  y, 2);


65  }


66 


67  r = 1.0  (sserr / sstot);


68  return r;


69  }


70 


71  public DataRow CalculateFittedLine(double[] y, double[] x, string rowName) {


72  double k, d;


73  Calculate(y, x, out k, out d);


74 


75  DataRow newRow = new DataRow(rowName);


76  for (int i = 0; i < x.Count(); i++) {


77  newRow.Values.Add(k * x[i] + d);


78  }


79  return newRow;


80  }


81 


82  public DataRow CalculateFittedLine(double[] dataPoints, string rowName) {


83  DataRow newRow = new DataRow(rowName);


84  double c0, c1;


85  Calculate(dataPoints, out c0, out c1);


86  var stdX = Enumerable.Range(0, dataPoints.Count()).Select(x => (double)x).ToArray();


87 


88  for (int i = 0; i < stdX.Count(); i++) {


89  newRow.Values.Add(c0 * stdX[i] + c1);


90  }


91 


92  return newRow;


93  }


94 


95  public override string ToString() {


96  return "Linear Fitting";


97  }


98  }


99  }

