Package mvpa :: Package misc :: Module errorfx
[hide private]
[frames] | no frames]

Source Code for Module mvpa.misc.errorfx

 1  #emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- 
 2  #ex: set sts=4 ts=4 sw=4 et: 
 3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
 4  # 
 5  #   See COPYING file distributed along with the PyMVPA package for the 
 6  #   copyright and license terms. 
 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   
19 -class ErrorFunctionBase(object):
20 """ 21 Dummy error function base class 22 """ 23 pass
24 25
26 -class ErrorFunction(ErrorFunctionBase):
27 """Common error function interface, computing the difference between 28 some desired and some predicted values. 29 """
30 - def __call__(self, predicted, desired):
31 """Compute some error value from the given desired and predicted 32 values (both sequences). 33 """ 34 raise NotImplemented
35 36
37 -class RMSErrorFx(ErrorFunction):
38 """Computes the root mean squared error of some desired and some 39 predicted values. 40 """
41 - def __call__(self, predicted, desired):
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
50 -class MeanMismatchErrorFx(ErrorFunction):
51 """Computes the percentage of mismatches between some desired and some 52 predicted values. 53 """
54 - def __call__(self, predicted, desired):
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
61 -class AUCErrorFx(ErrorFunction):
62 """Computes the area under the ROC for the given the 63 desired and predicted to make the prediction."""
64 - def __call__(self, predicted, desired):
65 """Requires all arguments.""" 66 # sort the desired in descending order based on the predicted and 67 # set to boolean 68 t = desired[N.argsort(predicted)[::-1]] > 0 69 70 # calculate the true positives 71 tp = N.concatenate(([0], 72 N.cumsum(t)/t.sum(dtype=N.float), 73 [1])) 74 75 # calculate the false positives 76 fp = N.concatenate(([0], 77 N.cumsum(~t)/(~t).sum(dtype=N.float), 78 [1])) 79 80 return trapz(tp,fp)
81 82
83 -class CorrErrorFx(ErrorFunction):
84 """Computes the correlation between the desired and the predicted 85 values."""
86 - def __call__(self, predicted, desired):
87 """Requires all arguments.""" 88 return pearsonr(predicted, desired)[0]
89