1
2
3
4
5
6
7
8
9 """Error functions helpers.
10
11 PyMVPA can use arbitrary function which takes 2 arguments: predictions
12 and targets and spits out a scalar value. Functions below are for the
13 convinience, and they confirm the agreement that 'smaller' is 'better'"""
14
15 __docformat__ = 'restructuredtext'
16
17
18 import numpy as N
19 from numpy import trapz
20
21 from mvpa.base import externals
22
23
25 """Returns mean power
26
27 Similar to var but without demeaning
28 """
29 return N.mean(N.asanyarray(data)**2)
30
32 """Returns root mean power
33
34 to be comparable against RMSE
35 """
36 return N.sqrt(meanPowerFx(data))
37
38
40 """Common error function interface, computing the difference between
41 some target and some predicted values.
42 """
43
44 """XXX there is no reason to keep this class around imho -- it is
45 just the skeleton for all the _ErrorFxs -- interface they
46 must conform... and there is no reason to have all those ErrorFx
47 as classes... may be they should be just functions?"""
48
50 """Print class name when asked for string
51 """
52 return self.__class__.__name__
53
55 """Proper repr for _ErrorFx
56 """
57 return self.__class__.__name__ + "()"
58
60 """Compute some error value from the given target and predicted
61 values (both sequences).
62 """
63 raise NotImplemented
64
65
67 """Computes the root mean squared error of some target and some
68 predicted values.
69 """
71 """Both 'predicted' and 'target' can be either scalars or sequences,
72 but have to be of the same length.
73 """
74 return N.sqrt(N.mean(N.subtract(predicted, target)**2))
75
76
78 """Computes the percentage of mismatches between some target and some
79 predicted values.
80 """
82 """Both 'predicted' and 'target' can be either scalars or sequences,
83 but have to be of the same length.
84 """
85 return 1 - N.mean( predicted == target )
86
87
89 """Computes the area under the ROC for the given the
90 target and predicted to make the prediction."""
92 """Requires all arguments."""
93
94
95 self.t = t = N.asanyarray(target)[N.argsort(predicted)[::-1]] > 0
96
97
98 self.tp = tp = N.concatenate(
99 ([0], N.cumsum(t)/t.sum(dtype=N.float), [1]))
100
101
102 self.fp = fp = N.concatenate(
103 ([0], N.cumsum(~t)/(~t).sum(dtype=N.float), [1]))
104
105 return trapz(tp, fp)
106
107
108 if externals.exists('scipy'):
109 from scipy.stats import pearsonr
110
112 """Computes the correlation between the target and the
113 predicted values. Resultant value is the 1 - correlation
114 coefficient, so minimization leads to the best value (at 0)
115
116 """
118 """Requires all arguments."""
119 return 1.0-pearsonr(predicted, target)[0]
120
121
123 """Computes p-value of correlation between the target and the predicted
124 values.
125
126 """
128 """Requires all arguments."""
129 return pearsonr(predicted, target)[1]
130
131 else:
132
133
134
136 """Computes the correlation between the target and the predicted
137 values. Return 1-CC
138
139 """
141 """Requires all arguments."""
142 l = len(predicted)
143 return 1.0 - N.corrcoef(N.reshape(predicted, l),
144 N.reshape(target, l))[0,1]
145
146
148 """Computes p-value of correlation between the target and the predicted
149 values.
150
151 """
153 """Requires all arguments."""
154 from mvpa.base import warning
155 warning("p-value for correlation is implemented only when scipy is "
156 "available. Bogus value -1.0 is returned otherwise")
157 return -1.0
158
159
161 """Ratio between RMSE and root mean power of target output.
162
163 So it can be considered as a scaled RMSE -- perfect reconstruction
164 has values near 0, while no reconstruction has values around 1.0.
165 Word of caution -- it is not commutative, ie exchange of predicted
166 and target might lead to completely different answers
167 """
170
171
173 """Ratio of variance described by the first singular value component.
174
175 Of limited use -- left for the sake of not wasting it
176 """
177
179 data = N.vstack( (predicted, target) ).T
180
181 data_demeaned = data - N.mean(data, axis=0)
182 u, s, vh = N.linalg.svd(data_demeaned, full_matrices=0)
183
184 s.sort()
185 s=s[::-1]
186 cvar = s[0]**2 / N.sum(s**2)
187 return cvar
188