-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfitting.js
126 lines (89 loc) · 3.21 KB
/
fitting.js
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
function fit (theory, data, guess) {
'use strict'
var min, results, seek;
results = new FitInfo();
min = curry(parameterization, theory, data);
seek = new Func(min);
results.parameters = seek.minimize(guess);
theory.setParameters(results.parameters);
findParameterCovariance(theory, data, results);
results.chi2 = chi2(data, theory);
results.reducedChi2 = results.chi2 / (data.length - theory.params.length);
return results;
}
function parameterization(theory, data, parameters) {
'use strict';
theory.setParameters(parameters);
return chi2(data, theory);
}
function chi2(data, theory) {
'use strict'
var argument, c2, dataCopy, i, j, result, theoryResult, uncertainty;
dataCopy = [];
for (i = 0; i < data.length; i++) {
dataCopy[i] = [];
for (j = 0; j < data[i].length; j++) {
dataCopy[i][j] = data[i][j];
}
}
c2 = 0;
for (i = 0; i < dataCopy.length; i++) {
argument = dataCopy[i];
uncertainty = argument.pop();
result = argument.pop();
theoryResult = theory.evaluate(argument);
c2 += Math.pow( (result - theoryResult) / uncertainty, 2);
}
return c2;
}
function findParameterCovariance (theory, data, results) {
var aij, argument, d, dataCopy, i, j, nPars, parDer1, parDer2, parDerFunc1, parDerFunc2, result, uncertainty;
//number of parameters
nPars = theory.params.length;
//initialize covariance matrix:
results.covarianceMatrix = new Matrix(nPars, nPars);
//copy of the data matrix
dataCopy = [];
for (i = 0; i < data.length; i++) {
dataCopy[i] = [];
for (j = 0; j < data[i].length; j++) {
dataCopy[i][j] = data[i][j];
}
}
for (d = 0; d < dataCopy.length; d++) {
argument = dataCopy[d];
uncertainty = argument.pop();
result = argument.pop();
parDer1 = curry(parameterFunction, theory, argument);
parDer2 = curry(parameterFunction, theory, argument);
parDerFunc1 = new Func(parDer1);
parDerFunc2 = new Func(parDer2);
for (i = 0; i < nPars; i++) {
for (j = i; j < nPars; j++) {
results.covarianceMatrix.elements[i][j] += parDerFunc1.derivative(theory.params, i) * parDerFunc2.derivative(theory.params, j) / uncertainty / uncertainty;
results.covarianceMatrix.elements[j][i] = results.covarianceMatrix.elements[i][j];
}
}
}
results.covarianceMatrix = results.covarianceMatrix.getInverse();
}
//turns a Func of x with fixed parameters into a function of those
//parameters with fixed x.
function parameterFunction(func, x, pars) {
func.setParameters(pars);
return func.evaluate(x);
}
function curry (fn) {
var slice = Array.prototype.slice,
args = slice.apply(arguments, [1]);
return function () {
return fn.apply(null, args.concat(slice.apply(arguments)));
};
}
function FitInfo() {
'use strict';
this.parameters = [];
this.chi2;
this.reducedChi2;
this.covarianceMatrix;
}