1
2
3
4
5
6
7
8
9 """Error functions"""
10
11 __docformat__ = 'restructuredtext'
12
13
14 from cmath import sqrt
15 import numpy as N
16 from scipy import trapz
17 from scipy.stats import pearsonr
18
20 """
21 Dummy error function base class
22 """
23 pass
24
25
27 """Common error function interface, computing the difference between
28 some desired and some predicted values.
29 """
31 """Compute some error value from the given desired and predicted
32 values (both sequences).
33 """
34 raise NotImplemented
35
36
38 """Computes the root mean squared error of some desired and some
39 predicted values.
40 """
42 """Both 'predicted' and 'desired' can be either scalars or sequences,
43 but have to be of the same length.
44 """
45 difference = N.subtract(predicted, desired)
46
47 return sqrt(N.dot(difference, difference))
48
49
51 """Computes the percentage of mismatches between some desired and some
52 predicted values.
53 """
55 """Both 'predicted' and 'desired' can be either scalars or sequences,
56 but have to be of the same length.
57 """
58 return 1 - N.mean( predicted == desired )
59
60
62 """Computes the area under the ROC for the given the
63 desired and predicted to make the prediction."""
65 """Requires all arguments."""
66
67
68 t = desired[N.argsort(predicted)[::-1]] > 0
69
70
71 tp = N.concatenate(([0],
72 N.cumsum(t)/t.sum(dtype=N.float),
73 [1]))
74
75
76 fp = N.concatenate(([0],
77 N.cumsum(~t)/(~t).sum(dtype=N.float),
78 [1]))
79
80 return trapz(tp,fp)
81
82
84 """Computes the correlation between the desired and the predicted
85 values."""
87 """Requires all arguments."""
88 return pearsonr(predicted, desired)[0]
89