1 #-*-coding:iso-8859-1-*-
3 # Copyright (C) 2008-2009 EDF R&D
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 Outil numérique de calcul de la variable de Fisher pour comparer les
23 variances de 2 échantillons
27 - les deux vecteurs (comme liste, array ou matrix) d'échantillons
28 dont on veut comparer la variance,
32 - la valeur de la variable aléatoire,
33 - la réponse au test ainsi que
34 - le message qui interprete la reponse du test.
36 __author__ = "Sophie RICCI - Juillet 2008"
39 from scipy.stats import betai
41 # ==============================================================================
42 def ComputeFisher(vector1 = None, vector2 = None, tolerance = 0.05 ):
44 Outil numérique de calcul de la variable de Fisher pour comparer les
45 variances de 2 échantillons
48 - en input : les deux vecteurs (comme liste, array ou matrix)
49 d'échantillons dont on veut comparer la variance, la
51 - en output : la p-value, la valeur de la variable aléatoire,
52 la réponse au test ainsi que le message qui interprete
55 if (vector1 is None) or (vector2 is None) :
56 raise ValueError("Two vectors must be given to calculate the Fisher value value")
57 V1 = numpy.array(vector1)
58 V2 = numpy.array(vector2)
59 if (V1.size < 1) or (V2.size < 1):
60 raise ValueError("The given vectors must not be empty")
62 # Calcul des variances des echantillons
63 # -------------------------------------
64 # où var est calculee comme : var = somme (xi -xmean)**2 /(n-1)
67 var1 = V1.std() * V1.std()
68 var2 = V2.std() * V2.std()
77 prob1= betai(0.5*df2,0.5*df1,float(df2)/float(df2+df1*f))
78 prob2= (1. - betai(0.5*df1, 0.5*df2, float(df1)/float(df1+df2/f)))
81 # Calcul de la p-value
82 # --------------------
83 areafisher = 100 * prob
87 message = "Il y a %.2f%s de chance de se tromper en refusant l'hypothèse d'égalité des variances des 2 échantillons (si <%.2f%s, on refuse effectivement l'égalité)"%(areafisher,"%",100.*tolerance,"%")
88 if (areafisher < (100.*tolerance)) :
89 answerTestFisher = False
91 answerTestFisher = True
92 # print "La reponse au test est", answerTestFisher
94 return areafisher, f, answerTestFisher, message
96 # ==============================================================================
97 if __name__ == "__main__":
102 x1 = [-1., 0., 4., 2., -1., 3.]
103 x2 = [-1., 0., 4., 2., -1., 3.]
107 [aire, f, reponse, message] = ComputeFisher(
112 print " aire.....:", aire
113 print " f........:", f
114 print " reponse..:", reponse
115 print " message..:", message