1 #-*-coding:iso-8859-1-*-
3 # Copyright (C) 2008-2013 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
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
24 Implémentation informatique de l'algorithme MMQR, basée sur la publication :
25 David R. Hunter, Kenneth Lange, "Quantile Regression via an MM Algorithm",
26 Journal of Computational and Graphical Statistics, 9, 1, pp.60-77, 2000.
28 __author__ = "Jean-Philippe ARGAUD"
31 from numpy import sum, array, matrix, dot, linalg, asarray, asmatrix, ravel
33 # ==============================================================================
44 # Recuperation des donnees et informations initiales
45 # --------------------------------------------------
46 variables = asmatrix(ravel( x0 ))
47 mesures = asmatrix(ravel( y )).T
48 increment = sys.float_info[0]
49 p = len(variables.flat)
51 quantile = float(quantile)
53 # Calcul des parametres du MM
54 # ---------------------------
56 e0 = -tn / math.log(tn)
57 epsilon = (e0-tn)/(1+math.log(e0))
59 # Calculs d'initialisation
60 # ------------------------
61 residus = asmatrix( mesures - func( variables ) ).A1
62 poids = asarray( 1./(epsilon+abs(residus)) )
63 veps = 1. - 2. * quantile - residus * poids
64 lastsurrogate = -sum(residus*veps) - (1.-2.*quantile)*sum(residus)
69 while (increment > toler) and (iteration < maxfun) :
72 Derivees = array(fprime(variables))
73 Derivees = Derivees.reshape(n,p) # Necessaire pour remettre en place la matrice si elle passe par des tuyaux YACS
74 DeriveesT = array(matrix(Derivees).T)
75 M = - dot( DeriveesT , (array(matrix(p*[poids,]).T)*Derivees) )
76 SM = dot( DeriveesT , veps ).T
77 step = linalg.lstsq( M, SM )[0]
79 variables = variables + step
80 residus = asmatrix( mesures - func(variables) ).A1
81 surrogate = sum(residus**2 * poids) + (4.*quantile-2.) * sum(residus)
83 while ( (surrogate > lastsurrogate) and ( max(list(abs(step))) > 1.e-16 ) ) :
85 variables = variables - step
86 residus = ( mesures-func(variables) ).A1
87 surrogate = sum(residus**2 * poids) + (4.*quantile-2.) * sum(residus)
89 increment = lastsurrogate-surrogate
90 poids = 1./(epsilon+abs(residus))
91 veps = 1. - 2. * quantile - residus * poids
92 lastsurrogate = -sum(residus * veps) - (1.-2.*quantile)*sum(residus)
94 # Mesure d'écart : q*Sum(residus)-sum(residus negatifs)
96 Ecart = quantile * sum(residus) - sum( residus[residus<0] )
98 return variables, Ecart, [n,p,iteration,increment,0]
100 # ==============================================================================
101 if __name__ == "__main__":
102 print '\n AUTODIAGNOSTIC \n'