-
Notifications
You must be signed in to change notification settings - Fork 0
/
na-error-propagation.js
229 lines (192 loc) · 6.25 KB
/
na-error-propagation.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*!
* na-error-propagation
* @see https://github.com/tfoxy/na-error-propagation
* @version 0.1.3
* @author Tomás Fox <tomas.c.fox@gmail.com>
* @license MIT
*/
/* global define */
(function(root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.ErrorPropagation = factory();
}
}(this, function() {
'use strict';
function InputError(message) {
this.name = 'InputError';
this.message = message;
}
InputError.prototype = Object.create(Error.prototype);
InputError.prototype.constructor = InputError;
function ErrorPropagation(options) {
options = options || {};
switch (options.correlation) {
case undefined:
case 'correlated':
this._calculateError = calculateCorrelatedError;
break;
case 'uncorrelated':
this._calculateError = calculateUncorrelatedError;
break;
case 'both':
this._calculateError = calculateBothErrors;
break;
default:
throw new InputError(
'Wrong value for correlation: ' + options.correlation +
'. It must be one of correlated, uncorrelated and both'
);
}
}
ErrorPropagation.InputError = InputError;
ErrorPropagation.nerdamer = nerdamerNotSet;
ErrorPropagation.setEventEmitter = setEventEmitter;
ErrorPropagation.setEventEmitter(getEventEmitter());
return ErrorPropagation;
////////////////
function nerdamerNotSet() {
throw new Error('ErrorPropagation.nerdamer must be set');
}
function setEventEmitter(EventEmitter) {
ErrorPropagation.prototype = Object.create(EventEmitter.prototype);
ErrorPropagation.prototype.constructor = ErrorPropagation;
ErrorPropagation.prototype.calculate = calculate;
ErrorPropagation.prototype._calculate = _calculate;
}
function calculate(expression, variables) {
try {
return this._calculate(expression, variables);
} catch (err) {
this.emit('error', err);
}
}
function _calculate(expression, variables) {
this.emit('input', expression, variables);
if (typeof expression !== 'string') {
throw new InputError('expression must be a string. Got ' + typeof expression);
}
variables = variables || {};
var valueMap = getValueMap(variables);
var value = calculateValue(expression, valueMap);
var error = this._calculateError(expression, variables, valueMap, this);
var result = {
value: value,
error: error
};
this.emit('result', result);
return result;
}
function calculateValue(expression, valueMap, expressionStorage) {
var e = ErrorPropagation.nerdamer(expression);
ErrorPropagation.nerdamer.clear('last');
if (expressionStorage) {
expressionStorage.e = e;
}
return e.evaluate(valueMap).valueOf();
}
function calculateErrors(expression, variables, valueMap, emitter) {
var errors = [];
for (var variableName in variables) {
var variable = variables[variableName];
if (variable.error) {
var expressionStorage = {};
var diffExpression = 'diff(' + expression + ',' + variableName + ')';
var evaluatedDiffValue = calculateValue(diffExpression, valueMap, expressionStorage);
var variableError = evaluatedDiffValue * variable.error;
emitter.emit('differential', {
value: evaluatedDiffValue,
variableName: variableName,
variable: variable,
valueWithError: variableError,
expression: expressionStorage.e
});
errors.push(variableError);
}
}
return errors;
}
function calculateCorrelatedError(expression, variables, valueMap, emitter) {
var error = 0;
var errors = calculateErrors(expression, variables, valueMap, emitter);
errors.forEach(function(variableError) {
error += variableError;
});
return error;
}
function calculateUncorrelatedError(expression, variables, valueMap, emitter) {
var error = 0;
var errors = calculateErrors(expression, variables, valueMap, emitter);
errors.forEach(function(variableError) {
error += Math.pow(variableError, 2);
});
return Math.sqrt(error);
}
function calculateBothErrors(expression, variables, valueMap, emitter) {
var correlatedError = 0;
var uncorrelatedError = 0;
var errors = calculateErrors(expression, variables, valueMap, emitter);
errors.forEach(function(variableError) {
correlatedError += variableError;
uncorrelatedError += Math.pow(variableError, 2);
});
return {
correlated: correlatedError,
uncorrelated: Math.sqrt(uncorrelatedError)
};
}
function getValueMap(variables) {
var map = {};
for (var variableName in variables) {
var variable = variables[variableName];
verifyVariable(variable, variableName);
map[variableName] = variable.value;
}
return map;
}
function verifyVariable(variable, variableName) {
if (typeof variable !== 'object') {
throw new InputError('Variable ' + variableName + ' is not an object.' +
' Got ' + typeof variable);
}
if (variable === null) {
throw new InputError('Variable ' + variableName + ' must not be null');
}
if (!('value' in variable)) {
throw new InputError('No value for variable ' + variableName);
}
}
function getEventEmitter() {
var EventEmitter = {
prototype: {
on: function() {
throw new Error('An EventEmitter library needs to be set using' +
' HermiteInterpolation.setEventEmitter(EventEmitter) method.' +
' The /on/ function cannot be used otherwise');
},
emit: function(eventName, param) {
if (eventName === 'error') {
throw param;
}
}
}
};
if (typeof module === 'object' && module.exports) {
try {
EventEmitter = require('events').EventEmitter;
} catch (err) {
// noop
}
}
return EventEmitter;
}
}));