[9706] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 |
|
---|
| 25 | namespace HeuristicLab.Analysis.Statistics {
|
---|
| 26 | public class ExpFitting : IFitting {
|
---|
[11914] | 27 | private void ExpFunc(double[] c, double[] x, ref double func, object obj) {
|
---|
[9706] | 28 | func = Math.Exp(-c[0] * Math.Pow(x[0], 2));
|
---|
| 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 |
|
---|
[11914] | 36 | public void Calculate(double[] dataPoints, out double p0) {
|
---|
[9706] | 37 | var stdX = GetDefaultXValues(dataPoints.Count());
|
---|
[11914] | 38 | Calculate(dataPoints, stdX, out p0);
|
---|
[9706] | 39 | }
|
---|
| 40 |
|
---|
[11914] | 41 | public void Calculate(double[] y, double[] x, out double p0) {
|
---|
[9706] | 42 | if (y.Count() != x.Count()) {
|
---|
[17931] | 43 | throw new ArgumentException("The length of x and y needs do be equal. ");
|
---|
[9706] | 44 | }
|
---|
| 45 |
|
---|
| 46 | double[] c = new double[] { 0.3 };
|
---|
| 47 | double epsx = 0.000001;
|
---|
| 48 | int maxits = 0;
|
---|
| 49 | int info;
|
---|
| 50 | alglib.lsfitstate state;
|
---|
| 51 | alglib.lsfitreport rep;
|
---|
| 52 | double diffstep = 0.0001;
|
---|
| 53 | double[,] xx = new double[x.Count(), 1];
|
---|
| 54 |
|
---|
| 55 | for (int i = 0; i < x.Count(); i++) {
|
---|
| 56 | xx[i, 0] = x[i];
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | alglib.lsfitcreatef(xx, y, c, diffstep, out state);
|
---|
[17931] | 60 | alglib.lsfitsetcond(state, epsx, maxits);
|
---|
[11914] | 61 | alglib.lsfitfit(state, ExpFunc, null, null);
|
---|
[9706] | 62 | alglib.lsfitresults(state, out info, out c, out rep);
|
---|
| 63 |
|
---|
| 64 | p0 = c[0];
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[11914] | 67 | public DataRow CalculateFittedLine(double[] dataPoints) {
|
---|
| 68 | DataRow newRow = new DataRow();
|
---|
| 69 | double c0;
|
---|
| 70 | Calculate(dataPoints, out c0);
|
---|
[9706] | 71 | var stdX = GetDefaultXValues(dataPoints.Count());
|
---|
| 72 |
|
---|
| 73 | for (int i = 0; i < stdX.Count(); i++) {
|
---|
| 74 | newRow.Values.Add(Math.Exp(-c0 * Math.Pow(stdX[i], 2)));
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return newRow;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[11914] | 80 | public DataRow CalculateFittedLine(double[] y, double[] x) {
|
---|
| 81 | DataRow newRow = new DataRow();
|
---|
| 82 | double c0;
|
---|
| 83 | Calculate(y, x, out c0);
|
---|
[9706] | 84 |
|
---|
| 85 | for (int i = 0; i < x.Count(); i++) {
|
---|
| 86 | newRow.Values.Add(Math.Exp(-c0 * Math.Pow(x[i], 2)));
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | return newRow;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | public override string ToString() {
|
---|
| 93 | return "Exponential Fitting";
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|