-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_analyzer.py
executable file
·86 lines (59 loc) · 2.31 KB
/
error_analyzer.py
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
import sys
import math
class Datapoints(object):
def __init__(self, data = None):
self._values = {}
self._deviations = {}
if data is not None:
self.addData(data)
def addDatapoint(self, param, value, dev):
self._values[param] = value
self._deviations[param] = dev
def addData(self, data):
for param in data.iterkeys():
self[param] = data[param]
def params(self):
return self._values.iterkeys()
def values(self):
return DictView(self._values)
def deviations(self):
return DictView(self._deviations)
def __getitem__(self, param):
return self._values[param], self._deviations[param]
def __setitem__(self, param, datapoint):
value, deviation = datapoint
self.addDatapoint(param, value, deviation)
def __iter__(self):
for param in self.params():
value, deviation = self[param]
yield param, value, deviation
class DictView(object):
def __init__(self, data):
self.data = data
def __getitem__(self, index):
return self.data[index]
def ErrorBars(f, data):
deviations = (CalcDeviation(f, data, p) for p in data.params())
result = math.fsum(dev ** 2 for dev in deviations)
return math.sqrt(result)
def CalcDeviation(f, data, parameter):
derivative = CalcDerivative(f, data, parameter)
return derivative * data._deviations[parameter]
# Used in CalcDerivative to calculate the optimal value of h.
InitH = math.pow(sys.float_info.epsilon, 1.0 / 3)
def CalcDerivative(f, data, parameter):
paramValue = data._values[parameter]
# Numerical differentiation via two-sided difference quotient
# Special thanks to Karen Kopecky
# Paper: http://www.karenkopecky.net/Teaching/eco613614/Notes_NumericalDifferentiation.pdf
hPrime = InitH * max(abs(paramValue), 1)
h = ((paramValue + hPrime) - (paramValue - hPrime)) / 2
rightValue = ModifyAndExecute(f, data, parameter, paramValue + h)
leftValue = ModifyAndExecute(f, data, parameter, paramValue - h)
return (rightValue - leftValue) / (2 * h)
def ModifyAndExecute(f, data, param, paramValue):
origValue = data._values[param]
data._values[param] = paramValue
retVal = f(data.values())
data._values[param] = origValue
return retVal