]> SALOME platform Git repositories - modules/adao.git/blob - src/daComposant/daDiagnostics/CompareMeanIndependantVectorsDifferentVariance.py
Salome HOME
Initial Commit
[modules/adao.git] / src / daComposant / daDiagnostics / CompareMeanIndependantVectorsDifferentVariance.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2009  EDF R&D
4 #
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.
9 #
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.
14 #
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
18 #
19 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 __doc__ = """
22     Diagnostic qui effectue le test d egalite des moyennes de 2 vecteurs 
23     independants supposes de variances differentes au sens du test de Student.
24     En input :  la tolerance
25     En output : le resultat du diagnostic est une reponse booleenne au test : 
26         True si les moyennes sont egales au sens du Test de Student
27         False dans le cas contraire. 
28 """
29 __author__ = "Sophie RICCI - Octobre 2008"
30
31 import sys ; sys.path.insert(0, "../daCore")
32
33 import numpy
34 import Persistence
35 from BasicObjects import Diagnostic
36 from ComputeStudent import IndependantVectorsDifferentVariance
37 import logging
38
39 # ==============================================================================
40 class ElementaryDiagnostic(Diagnostic,Persistence.OneScalar):
41     """
42     Diagnostic qui effectue le test d egalite des moyennes de 2 vecteurs 
43     independants supposes de variances differentes au sens du test de Student.
44     En input :  la tolerance
45     En output : le resultat du diagnostic est une reponse booleenne au test : 
46         True si les moyennes sont egales au sens du Test de Student
47         False dans le cas contraire. 
48     """
49     def __init__(self, name="", unit="", basetype = None, parameters = {} ):
50         Diagnostic.__init__(self, name, parameters)
51         Persistence.OneScalar.__init__( self, name, unit, basetype = bool)
52         if not self.parameters.has_key("tolerance"):
53             raise ValueError("A parameter named \"tolerance\" is required.")
54
55     def formula(self, V1, V2):
56         """
57         Effectue le calcul de la p-value de Student pour deux vecteurs
58         independants supposes de variances differentes.
59         """
60         [aire, Q, reponse, message] = IndependantVectorsDifferentVariance(
61             vector1 = V1, 
62             vector2 = V2, 
63             tolerance = self.parameters["tolerance"],
64             )
65         logging.info( message )
66         answerStudentTest = False
67         if (aire < (100.*self.parameters["tolerance"])) :
68             answerStudentTest = False
69         else:
70             answerStudentTest = True
71         return answerStudentTest
72
73     def calculate(self, vector1 = None, vector2 = None,  step = None):
74         """
75         Active la formule de calcul
76         """
77         if (vector1 is None) or (vector2 is None) :
78             raise ValueError("Two vectors must be given to calculate the Student value")
79         V1 = numpy.array(vector1)
80         V2 = numpy.array(vector2)
81         if (V1.size < 1) or (V2.size < 1):
82             raise ValueError("The given vectors must not be empty")
83         if V1.size != V2.size:
84             raise ValueError("The two given vectors must have the same size, or the vector types are incompatible")
85         value = self.formula( V1, V2 )
86         self.store( value = value,  step = step)
87
88 # ==============================================================================
89 if __name__ == "__main__":
90     print '\n AUTODIAGNOSTIC \n'
91
92     print " Test d'égalite des moyennes au sens de Student pour deux vecteurs"
93     print " indépendants supposés de variances différentes."
94     print
95     #
96     # Initialisation des inputs et appel du diagnostic
97     # --------------------------------------------------------------------
98     tolerance = 0.05
99     D = ElementaryDiagnostic("IndependantVectorsDifferentVariance", parameters = {
100                  "tolerance":tolerance,
101                  })
102     #
103     # Tirage de l'echantillon aleatoire 
104     # --------------------------------------------------------------------
105     x1 = numpy.array(([-0.23262176, 1.36065207,  0.32988102, 0.24400551, -0.66765848, -0.19088483, -0.31082575,  0.56849814,  1.21453443,  0.99657516]))
106     x2 = numpy.array(([-0.23, 1.36,  0.32, 0.24, -0.66, -0.19, -0.31,  0.56,  1.21,  0.99]))
107     #
108     # Calcul 
109     # --------------------------------------------------------------------
110     D.calculate(x1, x2)
111 #
112     if D.valueserie(0) :
113             print " L'hypothèse d'égalité des moyennes est valide."
114             print
115     else :
116             raise ValueError("The egality of the means is NOT valid")
117