Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Analysis/3.3/Statistics/Fitting/LogFitting.cs @ 16472

Last change on this file since 16472 was 16472, checked in by gkronber, 6 years ago

#2520: checked all projects from top to HeuristicLab.Common and added StorableType attributes.

File size: 3.0 KB
RevLine 
[9706]1#region License Information
2/* HeuristicLab
[16453]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9706]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 LogFitting : IFitting {
27    private void LogFunc(double[] c, double[] x, ref double func, object obj) {
[11699]28      func = c[0] * Math.Exp(c[1] / x[0]);
[9706]29    }
30
31    private double[] GetDefaultXValues(int n) {
32      var stdX = Enumerable.Range(1, n).Select(x => (double)x).ToArray();
33      return stdX;
34    }
35
36    public void Calculate(double[] dataPoints, out double p0, out double p1) {
37      var stdX = GetDefaultXValues(dataPoints.Count());
38      Calculate(dataPoints, stdX, out p0, out p1);
39    }
40
41    public void Calculate(double[] y, double[] x, out double p0, out double p1) {
42      if (y.Count() != x.Count()) {
[16472]43        throw new ArgumentException("The length of x and y needs do be equal. ");
[9706]44      }
45
46      double[] c = new double[] { 0.3, 0.3 };
47      double epsf = 0;
48      double epsx = 0.000001;
49      int maxits = 0;
50      int info;
51      alglib.lsfitstate state;
52      alglib.lsfitreport rep;
53      double diffstep = 0.0001;
54      double[,] xx = new double[x.Count(), 1];
55
56      for (int i = 0; i < x.Count(); i++) {
57        xx[i, 0] = x[i];
58      }
59
60      alglib.lsfitcreatef(xx, y, c, diffstep, out state);
61      alglib.lsfitsetcond(state, epsf, epsx, maxits);
62      alglib.lsfitfit(state, LogFunc, null, null);
63      alglib.lsfitresults(state, out info, out c, out rep);
64
65      p0 = c[0];
66      p1 = c[1];
67    }
68
[11914]69    public DataRow CalculateFittedLine(double[] dataPoints) {
70      DataRow newRow = new DataRow();
[9706]71      double c0, c1;
72      Calculate(dataPoints, out c0, out c1);
73      var stdX = GetDefaultXValues(dataPoints.Count());
74
75      for (int i = 0; i < stdX.Count(); i++) {
[11699]76        newRow.Values.Add(c0 * Math.Exp(c1 / stdX[i]));
[9706]77      }
78
79      return newRow;
80    }
81
[11914]82    public DataRow CalculateFittedLine(double[] y, double[] x) {
83      DataRow newRow = new DataRow();
[9706]84      double c0, c1;
85      Calculate(y, x, out c0, out c1);
86
87      for (int i = 0; i < x.Count(); i++) {
[11699]88        newRow.Values.Add(c0 * Math.Exp(c1 / x[i]));
[9706]89      }
90
91      return newRow;
92    }
93
94    public override string ToString() {
95      return "Logarithmic Fitting";
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.