forked from fair-acc/chart-fx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GaussianFitSample.java
164 lines (134 loc) · 6.24 KB
/
GaussianFitSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package de.gsi.math.samples;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.gsi.dataset.DataSet;
import de.gsi.dataset.spi.DefaultDataSet;
import de.gsi.dataset.spi.DefaultErrorDataSet;
import de.gsi.math.TMath;
import de.gsi.math.fitter.NonLinearRegressionFitter;
import de.gsi.math.functions.AbstractFunction1D;
import de.gsi.math.samples.utils.AbstractDemoApplication;
import de.gsi.math.samples.utils.DemoChart;
import javafx.application.Application;
import javafx.scene.Node;
/**
* example illustrating fitting of a Gaussian Distribution
*
* @author rstein
*/
public class GaussianFitSample extends AbstractDemoApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(GaussianFitSample.class);
private static final int MAX_POINTS = 101;
private DataSet fmodel;
private DataSet fdataOrig;
private DataSet fdataFitted;
@Override
public Node getContent() {
initData();
final DemoChart chart = new DemoChart();
chart.getRenderer(0).getDatasets().addAll(fmodel, fdataOrig, fdataFitted);
return chart;
}
private void initData() {
// user specific fitting function, here: normalised Gaussian Function
// y := scale*1/sqrt(2*Pi*sigma^2)*exp(-0.5*(x-mu)^2/sigma^2)
// ... MyGaussianFunction(name, double[]{mu, sigma, scale})
final MyGaussianFunction func = new MyGaussianFunction("gauss1", new double[] { -3.0, 1.0, 10.0 });
LOGGER.atInfo().log("before fit");
func.printParameters();
double[] xValues = new double[MAX_POINTS];
double[] yValues = new double[MAX_POINTS];
double[] yModel = new double[MAX_POINTS];
double[] yErrors = new double[MAX_POINTS];
for (int i = 0; i < xValues.length; i++) {
final double error = 0.5 * RANDOM.nextGaussian();
xValues[i] = (i - xValues.length / 2.0) * 30.0 / MAX_POINTS; // equidistant
// sampling
final double value = func.getValue(xValues[i]);
// add some slope and offset to make the fit a bit more tricky
// remember: in this example, the slope is not part of the fitting
// check whether fit converged via chi^2
// value += xValues[i]*0.1+0.5;
// may converge depending on parameter values
// if you need to fit this -> add a slope, offset parameter to your
// Gaussian function
yModel[i] = value;
yValues[i] = value + error;
yErrors[i] = Math.abs(error);
}
final NonLinearRegressionFitter fitter = new NonLinearRegressionFitter(xValues, yValues, yErrors);
// initial estimates
double[] start = new double[3];
start[0] = 0.0; // initial estimate of mu
start[1] = 1.0; // initial estimate of sigma
start[2] = 0.6; // initial estimate of the scale
// initial step sizes
double[] step = new double[3];
step[0] = 0.6; // initial step size for mu
step[1] = 0.05; // initial step size for sigma
step[2] = 0.1; // initial step size for scale
fitter.simplex(func, start, step);
final double[] fittedParameter = fitter.getBestEstimates();
final double[] fittedParameterError = fitter.getBestEstimatesErrors();
func.setParameterValues(fittedParameter);
for (int i = 0; i < func.getParameterCount(); i++) {
final double value = fittedParameter[i];
final double error = fittedParameterError[i];
func.setParameterRange(i, value - error, value + error);
}
final double[] yPredicted = func.getValues(xValues);
final double[] yPredictedError = new double[yPredicted.length];
LOGGER.atInfo().log("after fit");
func.printParameters();
LOGGER.atInfo().log("fit results chi^2 =" + fitter.getChiSquare() + ":");
for (int i = 0; i < 3; i++) {
LOGGER.atInfo().log("fitted-parameter '%s' = %f -> %f +- %f\n", func.getParameterName(i), start[i],
fittedParameter[i], fittedParameterError[i]);
}
fmodel = new DefaultDataSet("design model", xValues, yModel, xValues.length, true);
fdataOrig = new DefaultErrorDataSet("data seed with errors", xValues, yValues, yErrors, yErrors, xValues.length,
true);
fdataFitted = new DefaultErrorDataSet("fitted model", xValues, yPredicted, yPredictedError, yPredictedError,
xValues.length, true);
// plot fitting results
/*
* for (int i=0; i < func.getParameterCount(); i++) { LOGGER.atInfo().log("fitted parameter '%s': %f +- %f\n",
* func.getParameterName(i), func.getParameterValue(i),
* 0.5*(func.getParameterRangeMaximum(i)-func.getParameterRangeMinimum(i ))); }
*/
}
public static void main(final String[] args) {
Application.launch(args);
}
/**
* example fitting function y = scale/(sqrt(2*pi*sigma)*exp(- 0.5*(x-mu)^2/sigma^2)
*/
protected class MyGaussianFunction extends AbstractFunction1D {
public MyGaussianFunction(final String name, final double[] parameter) {
super(name, new double[3]);
// declare parameter names
this.setParameterName(0, "mu");
this.setParameterName(1, "sigma");
this.setParameterName(2, "scale");
if (parameter == null) {
// set some default values
setParameterValue(0, 0.0); // mu
setParameterValue(0, 1.0); // sigma
setParameterValue(0, 1.0); // scale
return;
}
// assign default values
final int maxIndex = TMath.Min(parameter.length, this.getParameterCount());
for (int i = 0; i < maxIndex; i++) {
setParameterValue(i, parameter[i]);
}
}
@Override
public double getValue(final double x) {
final double mu = fparameter[0];
final double sigma = fparameter[1];
final double scale = fparameter[2];
return scale * 1.0 / (Math.sqrt(TMath.TwoPi()) * sigma) * Math.exp(-0.5 * Math.pow((x - mu) / sigma, 2));
}
}
}