Salome HOME
Improvement and extension of EnKF algorithm
[modules/adao.git] / src / daComposant / daCore / NumericObjects.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2020 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 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22
23 __doc__ = """
24     Définit les objets numériques génériques.
25 """
26 __author__ = "Jean-Philippe ARGAUD"
27
28 import os, time, copy, types, sys, logging
29 import math, numpy, scipy
30 from daCore.BasicObjects import Operator
31 from daCore.PlatformInfo import PlatformInfo
32 mpr = PlatformInfo().MachinePrecision()
33 mfp = PlatformInfo().MaximumPrecision()
34 # logging.getLogger().setLevel(logging.DEBUG)
35
36 # ==============================================================================
37 def ExecuteFunction( paire ):
38     assert len(paire) == 2, "Incorrect number of arguments"
39     X, funcrepr = paire
40     __X = numpy.asmatrix(numpy.ravel( X )).T
41     __sys_path_tmp = sys.path ; sys.path.insert(0,funcrepr["__userFunction__path"])
42     __module = __import__(funcrepr["__userFunction__modl"], globals(), locals(), [])
43     __fonction = getattr(__module,funcrepr["__userFunction__name"])
44     sys.path = __sys_path_tmp ; del __sys_path_tmp
45     __HX  = __fonction( __X )
46     return numpy.ravel( __HX )
47
48 # ==============================================================================
49 class FDApproximation(object):
50     """
51     Cette classe sert d'interface pour définir les opérateurs approximés. A la
52     création d'un objet, en fournissant une fonction "Function", on obtient un
53     objet qui dispose de 3 méthodes "DirectOperator", "TangentOperator" et
54     "AdjointOperator". On contrôle l'approximation DF avec l'incrément
55     multiplicatif "increment" valant par défaut 1%, ou avec l'incrément fixe
56     "dX" qui sera multiplié par "increment" (donc en %), et on effectue de DF
57     centrées si le booléen "centeredDF" est vrai.
58     """
59     def __init__(self,
60             name                  = "FDApproximation",
61             Function              = None,
62             centeredDF            = False,
63             increment             = 0.01,
64             dX                    = None,
65             avoidingRedundancy    = True,
66             toleranceInRedundancy = 1.e-18,
67             lenghtOfRedundancy    = -1,
68             mpEnabled             = False,
69             mpWorkers             = None,
70             mfEnabled             = False,
71             ):
72         self.__name = str(name)
73         if mpEnabled:
74             try:
75                 import multiprocessing
76                 self.__mpEnabled = True
77             except ImportError:
78                 self.__mpEnabled = False
79         else:
80             self.__mpEnabled = False
81         self.__mpWorkers = mpWorkers
82         if self.__mpWorkers is not None and self.__mpWorkers < 1:
83             self.__mpWorkers = None
84         logging.debug("FDA Calculs en multiprocessing : %s (nombre de processus : %s)"%(self.__mpEnabled,self.__mpWorkers))
85         #
86         if mfEnabled:
87             self.__mfEnabled = True
88         else:
89             self.__mfEnabled = False
90         logging.debug("FDA Calculs en multifonctions : %s"%(self.__mfEnabled,))
91         #
92         if avoidingRedundancy:
93             self.__avoidRC = True
94             self.__tolerBP = float(toleranceInRedundancy)
95             self.__lenghtRJ = int(lenghtOfRedundancy)
96             self.__listJPCP = [] # Jacobian Previous Calculated Points
97             self.__listJPCI = [] # Jacobian Previous Calculated Increment
98             self.__listJPCR = [] # Jacobian Previous Calculated Results
99             self.__listJPPN = [] # Jacobian Previous Calculated Point Norms
100             self.__listJPIN = [] # Jacobian Previous Calculated Increment Norms
101         else:
102             self.__avoidRC = False
103         #
104         if self.__mpEnabled:
105             if isinstance(Function,types.FunctionType):
106                 logging.debug("FDA Calculs en multiprocessing : FunctionType")
107                 self.__userFunction__name = Function.__name__
108                 try:
109                     mod = os.path.join(Function.__globals__['filepath'],Function.__globals__['filename'])
110                 except:
111                     mod = os.path.abspath(Function.__globals__['__file__'])
112                 if not os.path.isfile(mod):
113                     raise ImportError("No user defined function or method found with the name %s"%(mod,))
114                 self.__userFunction__modl = os.path.basename(mod).replace('.pyc','').replace('.pyo','').replace('.py','')
115                 self.__userFunction__path = os.path.dirname(mod)
116                 del mod
117                 self.__userOperator = Operator( name = self.__name, fromMethod = Function, avoidingRedundancy = self.__avoidRC, inputAsMultiFunction = self.__mfEnabled )
118                 self.__userFunction = self.__userOperator.appliedTo # Pour le calcul Direct
119             elif isinstance(Function,types.MethodType):
120                 logging.debug("FDA Calculs en multiprocessing : MethodType")
121                 self.__userFunction__name = Function.__name__
122                 try:
123                     mod = os.path.join(Function.__globals__['filepath'],Function.__globals__['filename'])
124                 except:
125                     mod = os.path.abspath(Function.__func__.__globals__['__file__'])
126                 if not os.path.isfile(mod):
127                     raise ImportError("No user defined function or method found with the name %s"%(mod,))
128                 self.__userFunction__modl = os.path.basename(mod).replace('.pyc','').replace('.pyo','').replace('.py','')
129                 self.__userFunction__path = os.path.dirname(mod)
130                 del mod
131                 self.__userOperator = Operator( name = self.__name, fromMethod = Function, avoidingRedundancy = self.__avoidRC, inputAsMultiFunction = self.__mfEnabled )
132                 self.__userFunction = self.__userOperator.appliedTo # Pour le calcul Direct
133             else:
134                 raise TypeError("User defined function or method has to be provided for finite differences approximation.")
135         else:
136             self.__userOperator = Operator( name = self.__name, fromMethod = Function, avoidingRedundancy = self.__avoidRC, inputAsMultiFunction = self.__mfEnabled )
137             self.__userFunction = self.__userOperator.appliedTo
138         #
139         self.__centeredDF = bool(centeredDF)
140         if abs(float(increment)) > 1.e-15:
141             self.__increment  = float(increment)
142         else:
143             self.__increment  = 0.01
144         if dX is None:
145             self.__dX     = None
146         else:
147             self.__dX     = numpy.asmatrix(numpy.ravel( dX )).T
148         logging.debug("FDA Reduction des doublons de calcul : %s"%self.__avoidRC)
149         if self.__avoidRC:
150             logging.debug("FDA Tolerance de determination des doublons : %.2e"%self.__tolerBP)
151
152     # ---------------------------------------------------------
153     def __doublon__(self, e, l, n, v=None):
154         __ac, __iac = False, -1
155         for i in range(len(l)-1,-1,-1):
156             if numpy.linalg.norm(e - l[i]) < self.__tolerBP * n[i]:
157                 __ac, __iac = True, i
158                 if v is not None: logging.debug("FDA Cas%s déja calculé, récupération du doublon %i"%(v,__iac))
159                 break
160         return __ac, __iac
161
162     # ---------------------------------------------------------
163     def DirectOperator(self, X ):
164         """
165         Calcul du direct à l'aide de la fonction fournie.
166         """
167         logging.debug("FDA Calcul DirectOperator (explicite)")
168         if self.__mfEnabled:
169             _HX = self.__userFunction( X, argsAsSerie = True )
170         else:
171             _X = numpy.asmatrix(numpy.ravel( X )).T
172             _HX = numpy.ravel(self.__userFunction( _X ))
173         #
174         return _HX
175
176     # ---------------------------------------------------------
177     def TangentMatrix(self, X ):
178         """
179         Calcul de l'opérateur tangent comme la Jacobienne par différences finies,
180         c'est-à-dire le gradient de H en X. On utilise des différences finies
181         directionnelles autour du point X. X est un numpy.matrix.
182
183         Différences finies centrées (approximation d'ordre 2):
184         1/ Pour chaque composante i de X, on ajoute et on enlève la perturbation
185            dX[i] à la  composante X[i], pour composer X_plus_dXi et X_moins_dXi, et
186            on calcule les réponses HX_plus_dXi = H( X_plus_dXi ) et HX_moins_dXi =
187            H( X_moins_dXi )
188         2/ On effectue les différences (HX_plus_dXi-HX_moins_dXi) et on divise par
189            le pas 2*dXi
190         3/ Chaque résultat, par composante, devient une colonne de la Jacobienne
191
192         Différences finies non centrées (approximation d'ordre 1):
193         1/ Pour chaque composante i de X, on ajoute la perturbation dX[i] à la
194            composante X[i] pour composer X_plus_dXi, et on calcule la réponse
195            HX_plus_dXi = H( X_plus_dXi )
196         2/ On calcule la valeur centrale HX = H(X)
197         3/ On effectue les différences (HX_plus_dXi-HX) et on divise par
198            le pas dXi
199         4/ Chaque résultat, par composante, devient une colonne de la Jacobienne
200
201         """
202         logging.debug("FDA Début du calcul de la Jacobienne")
203         logging.debug("FDA   Incrément de............: %s*X"%float(self.__increment))
204         logging.debug("FDA   Approximation centrée...: %s"%(self.__centeredDF))
205         #
206         if X is None or len(X)==0:
207             raise ValueError("Nominal point X for approximate derivatives can not be None or void (given X: %s)."%(str(X),))
208         #
209         _X = numpy.asmatrix(numpy.ravel( X )).T
210         #
211         if self.__dX is None:
212             _dX  = self.__increment * _X
213         else:
214             _dX = numpy.asmatrix(numpy.ravel( self.__dX )).T
215         #
216         if (_dX == 0.).any():
217             moyenne = _dX.mean()
218             if moyenne == 0.:
219                 _dX = numpy.where( _dX == 0., float(self.__increment), _dX )
220             else:
221                 _dX = numpy.where( _dX == 0., moyenne, _dX )
222         #
223         __alreadyCalculated  = False
224         if self.__avoidRC:
225             __bidon, __alreadyCalculatedP = self.__doublon__(_X,  self.__listJPCP, self.__listJPPN, None)
226             __bidon, __alreadyCalculatedI = self.__doublon__(_dX, self.__listJPCI, self.__listJPIN, None)
227             if __alreadyCalculatedP == __alreadyCalculatedI > -1:
228                 __alreadyCalculated, __i = True, __alreadyCalculatedP
229                 logging.debug("FDA Cas J déja calculé, récupération du doublon %i"%__i)
230         #
231         if __alreadyCalculated:
232             logging.debug("FDA   Calcul Jacobienne (par récupération du doublon %i)"%__i)
233             _Jacobienne = self.__listJPCR[__i]
234         else:
235             logging.debug("FDA   Calcul Jacobienne (explicite)")
236             if self.__centeredDF:
237                 #
238                 if self.__mpEnabled and not self.__mfEnabled:
239                     funcrepr = {
240                         "__userFunction__path" : self.__userFunction__path,
241                         "__userFunction__modl" : self.__userFunction__modl,
242                         "__userFunction__name" : self.__userFunction__name,
243                     }
244                     _jobs = []
245                     for i in range( len(_dX) ):
246                         _dXi            = _dX[i]
247                         _X_plus_dXi     = numpy.array( _X.A1, dtype=float )
248                         _X_plus_dXi[i]  = _X[i] + _dXi
249                         _X_moins_dXi    = numpy.array( _X.A1, dtype=float )
250                         _X_moins_dXi[i] = _X[i] - _dXi
251                         #
252                         _jobs.append( (_X_plus_dXi,  funcrepr) )
253                         _jobs.append( (_X_moins_dXi, funcrepr) )
254                     #
255                     import multiprocessing
256                     self.__pool = multiprocessing.Pool(self.__mpWorkers)
257                     _HX_plusmoins_dX = self.__pool.map( ExecuteFunction, _jobs )
258                     self.__pool.close()
259                     self.__pool.join()
260                     #
261                     _Jacobienne  = []
262                     for i in range( len(_dX) ):
263                         _Jacobienne.append( numpy.ravel( _HX_plusmoins_dX[2*i] - _HX_plusmoins_dX[2*i+1] ) / (2.*_dX[i]) )
264                     #
265                 elif self.__mfEnabled:
266                     _xserie = []
267                     for i in range( len(_dX) ):
268                         _dXi            = _dX[i]
269                         _X_plus_dXi     = numpy.array( _X.A1, dtype=float )
270                         _X_plus_dXi[i]  = _X[i] + _dXi
271                         _X_moins_dXi    = numpy.array( _X.A1, dtype=float )
272                         _X_moins_dXi[i] = _X[i] - _dXi
273                         #
274                         _xserie.append( _X_plus_dXi )
275                         _xserie.append( _X_moins_dXi )
276                     #
277                     _HX_plusmoins_dX = self.DirectOperator( _xserie )
278                      #
279                     _Jacobienne  = []
280                     for i in range( len(_dX) ):
281                         _Jacobienne.append( numpy.ravel( _HX_plusmoins_dX[2*i] - _HX_plusmoins_dX[2*i+1] ) / (2.*_dX[i]) )
282                     #
283                 else:
284                     _Jacobienne  = []
285                     for i in range( _dX.size ):
286                         _dXi            = _dX[i]
287                         _X_plus_dXi     = numpy.array( _X.A1, dtype=float )
288                         _X_plus_dXi[i]  = _X[i] + _dXi
289                         _X_moins_dXi    = numpy.array( _X.A1, dtype=float )
290                         _X_moins_dXi[i] = _X[i] - _dXi
291                         #
292                         _HX_plus_dXi    = self.DirectOperator( _X_plus_dXi )
293                         _HX_moins_dXi   = self.DirectOperator( _X_moins_dXi )
294                         #
295                         _Jacobienne.append( numpy.ravel( _HX_plus_dXi - _HX_moins_dXi ) / (2.*_dXi) )
296                 #
297             else:
298                 #
299                 if self.__mpEnabled and not self.__mfEnabled:
300                     funcrepr = {
301                         "__userFunction__path" : self.__userFunction__path,
302                         "__userFunction__modl" : self.__userFunction__modl,
303                         "__userFunction__name" : self.__userFunction__name,
304                     }
305                     _jobs = []
306                     _jobs.append( (_X.A1, funcrepr) )
307                     for i in range( len(_dX) ):
308                         _X_plus_dXi    = numpy.array( _X.A1, dtype=float )
309                         _X_plus_dXi[i] = _X[i] + _dX[i]
310                         #
311                         _jobs.append( (_X_plus_dXi, funcrepr) )
312                     #
313                     import multiprocessing
314                     self.__pool = multiprocessing.Pool(self.__mpWorkers)
315                     _HX_plus_dX = self.__pool.map( ExecuteFunction, _jobs )
316                     self.__pool.close()
317                     self.__pool.join()
318                     #
319                     _HX = _HX_plus_dX.pop(0)
320                     #
321                     _Jacobienne = []
322                     for i in range( len(_dX) ):
323                         _Jacobienne.append( numpy.ravel(( _HX_plus_dX[i] - _HX ) / _dX[i]) )
324                     #
325                 elif self.__mfEnabled:
326                     _xserie = []
327                     _xserie.append( _X.A1 )
328                     for i in range( len(_dX) ):
329                         _X_plus_dXi    = numpy.array( _X.A1, dtype=float )
330                         _X_plus_dXi[i] = _X[i] + _dX[i]
331                         #
332                         _xserie.append( _X_plus_dXi )
333                     #
334                     _HX_plus_dX = self.DirectOperator( _xserie )
335                     #
336                     _HX = _HX_plus_dX.pop(0)
337                     #
338                     _Jacobienne = []
339                     for i in range( len(_dX) ):
340                         _Jacobienne.append( numpy.ravel(( _HX_plus_dX[i] - _HX ) / _dX[i]) )
341                    #
342                 else:
343                     _Jacobienne  = []
344                     _HX = self.DirectOperator( _X )
345                     for i in range( _dX.size ):
346                         _dXi            = _dX[i]
347                         _X_plus_dXi     = numpy.array( _X.A1, dtype=float )
348                         _X_plus_dXi[i]  = _X[i] + _dXi
349                         #
350                         _HX_plus_dXi = self.DirectOperator( _X_plus_dXi )
351                         #
352                         _Jacobienne.append( numpy.ravel(( _HX_plus_dXi - _HX ) / _dXi) )
353                 #
354             #
355             _Jacobienne = numpy.asmatrix( numpy.vstack( _Jacobienne ) ).T
356             if self.__avoidRC:
357                 if self.__lenghtRJ < 0: self.__lenghtRJ = 2 * _X.size
358                 while len(self.__listJPCP) > self.__lenghtRJ:
359                     self.__listJPCP.pop(0)
360                     self.__listJPCI.pop(0)
361                     self.__listJPCR.pop(0)
362                     self.__listJPPN.pop(0)
363                     self.__listJPIN.pop(0)
364                 self.__listJPCP.append( copy.copy(_X) )
365                 self.__listJPCI.append( copy.copy(_dX) )
366                 self.__listJPCR.append( copy.copy(_Jacobienne) )
367                 self.__listJPPN.append( numpy.linalg.norm(_X) )
368                 self.__listJPIN.append( numpy.linalg.norm(_Jacobienne) )
369         #
370         logging.debug("FDA Fin du calcul de la Jacobienne")
371         #
372         return _Jacobienne
373
374     # ---------------------------------------------------------
375     def TangentOperator(self, paire ):
376         """
377         Calcul du tangent à l'aide de la Jacobienne.
378         """
379         if self.__mfEnabled:
380             assert len(paire) == 1, "Incorrect lenght of arguments"
381             _paire = paire[0]
382             assert len(_paire) == 2, "Incorrect number of arguments"
383         else:
384             assert len(paire) == 2, "Incorrect number of arguments"
385             _paire = paire
386         X, dX = _paire
387         _Jacobienne = self.TangentMatrix( X )
388         if dX is None or len(dX) == 0:
389             #
390             # Calcul de la forme matricielle si le second argument est None
391             # -------------------------------------------------------------
392             if self.__mfEnabled: return [_Jacobienne,]
393             else:                return _Jacobienne
394         else:
395             #
396             # Calcul de la valeur linéarisée de H en X appliqué à dX
397             # ------------------------------------------------------
398             _dX = numpy.asmatrix(numpy.ravel( dX )).T
399             _HtX = numpy.dot(_Jacobienne, _dX)
400             if self.__mfEnabled: return [_HtX.A1,]
401             else:                return _HtX.A1
402
403     # ---------------------------------------------------------
404     def AdjointOperator(self, paire ):
405         """
406         Calcul de l'adjoint à l'aide de la Jacobienne.
407         """
408         if self.__mfEnabled:
409             assert len(paire) == 1, "Incorrect lenght of arguments"
410             _paire = paire[0]
411             assert len(_paire) == 2, "Incorrect number of arguments"
412         else:
413             assert len(paire) == 2, "Incorrect number of arguments"
414             _paire = paire
415         X, Y = _paire
416         _JacobienneT = self.TangentMatrix( X ).T
417         if Y is None or len(Y) == 0:
418             #
419             # Calcul de la forme matricielle si le second argument est None
420             # -------------------------------------------------------------
421             if self.__mfEnabled: return [_JacobienneT,]
422             else:                return _JacobienneT
423         else:
424             #
425             # Calcul de la valeur de l'adjoint en X appliqué à Y
426             # --------------------------------------------------
427             _Y = numpy.asmatrix(numpy.ravel( Y )).T
428             _HaY = numpy.dot(_JacobienneT, _Y)
429             if self.__mfEnabled: return [_HaY.A1,]
430             else:                return _HaY.A1
431
432 # ==============================================================================
433 def mmqr(
434         func     = None,
435         x0       = None,
436         fprime   = None,
437         bounds   = None,
438         quantile = 0.5,
439         maxfun   = 15000,
440         toler    = 1.e-06,
441         y        = None,
442         ):
443     """
444     Implémentation informatique de l'algorithme MMQR, basée sur la publication :
445     David R. Hunter, Kenneth Lange, "Quantile Regression via an MM Algorithm",
446     Journal of Computational and Graphical Statistics, 9, 1, pp.60-77, 2000.
447     """
448     #
449     # Recuperation des donnees et informations initiales
450     # --------------------------------------------------
451     variables = numpy.ravel( x0 )
452     mesures   = numpy.ravel( y )
453     increment = sys.float_info[0]
454     p         = variables.size
455     n         = mesures.size
456     quantile  = float(quantile)
457     #
458     # Calcul des parametres du MM
459     # ---------------------------
460     tn      = float(toler) / n
461     e0      = -tn / math.log(tn)
462     epsilon = (e0-tn)/(1+math.log(e0))
463     #
464     # Calculs d'initialisation
465     # ------------------------
466     residus  = mesures - numpy.ravel( func( variables ) )
467     poids    = 1./(epsilon+numpy.abs(residus))
468     veps     = 1. - 2. * quantile - residus * poids
469     lastsurrogate = -numpy.sum(residus*veps) - (1.-2.*quantile)*numpy.sum(residus)
470     iteration = 0
471     #
472     # Recherche iterative
473     # -------------------
474     while (increment > toler) and (iteration < maxfun) :
475         iteration += 1
476         #
477         Derivees  = numpy.array(fprime(variables))
478         Derivees  = Derivees.reshape(n,p) # Necessaire pour remettre en place la matrice si elle passe par des tuyaux YACS
479         DeriveesT = Derivees.transpose()
480         M         =   numpy.dot( DeriveesT , (numpy.array(numpy.matrix(p*[poids,]).T)*Derivees) )
481         SM        =   numpy.transpose(numpy.dot( DeriveesT , veps ))
482         step      = - numpy.linalg.lstsq( M, SM, rcond=-1 )[0]
483         #
484         variables = variables + step
485         if bounds is not None:
486             # Attention : boucle infinie à éviter si un intervalle est trop petit
487             while( (variables < numpy.ravel(numpy.asmatrix(bounds)[:,0])).any() or (variables > numpy.ravel(numpy.asmatrix(bounds)[:,1])).any() ):
488                 step      = step/2.
489                 variables = variables - step
490         residus   = mesures - numpy.ravel( func(variables) )
491         surrogate = numpy.sum(residus**2 * poids) + (4.*quantile-2.) * numpy.sum(residus)
492         #
493         while ( (surrogate > lastsurrogate) and ( max(list(numpy.abs(step))) > 1.e-16 ) ) :
494             step      = step/2.
495             variables = variables - step
496             residus   = mesures - numpy.ravel( func(variables) )
497             surrogate = numpy.sum(residus**2 * poids) + (4.*quantile-2.) * numpy.sum(residus)
498         #
499         increment     = lastsurrogate-surrogate
500         poids         = 1./(epsilon+numpy.abs(residus))
501         veps          = 1. - 2. * quantile - residus * poids
502         lastsurrogate = -numpy.sum(residus * veps) - (1.-2.*quantile)*numpy.sum(residus)
503     #
504     # Mesure d'écart
505     # --------------
506     Ecart = quantile * numpy.sum(residus) - numpy.sum( residus[residus<0] )
507     #
508     return variables, Ecart, [n,p,iteration,increment,0]
509
510 # ==============================================================================
511 def senkf(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
512     """
513     Stochastic EnKF (Envensen 1994, Burgers 1998)
514
515     selfA est identique au "self" d'algorithme appelant et contient les
516     valeurs.
517     """
518     if selfA._parameters["EstimationOf"] == "Parameters":
519         selfA._parameters["StoreInternalVariables"] = True
520     #
521     # Opérateurs
522     # ----------
523     H = HO["Direct"].appliedControledFormTo
524     #
525     if selfA._parameters["EstimationOf"] == "State":
526         M = EM["Direct"].appliedControledFormTo
527     #
528     if CM is not None and "Tangent" in CM and U is not None:
529         Cm = CM["Tangent"].asMatrix(Xb)
530     else:
531         Cm = None
532     #
533     # Nombre de pas identique au nombre de pas d'observations
534     # -------------------------------------------------------
535     if hasattr(Y,"stepnumber"):
536         duration = Y.stepnumber()
537         __p = numpy.cumprod(Y.shape())[-1]
538     else:
539         duration = 2
540         __p = numpy.array(Y).size
541     #
542     # Précalcul des inversions de B et R
543     # ----------------------------------
544     if selfA._parameters["StoreInternalVariables"] \
545         or selfA._toStore("CostFunctionJ") \
546         or selfA._toStore("CostFunctionJb") \
547         or selfA._toStore("CostFunctionJo") \
548         or selfA._toStore("CurrentOptimum") \
549         or selfA._toStore("APosterioriCovariance"):
550         BI = B.getI()
551         RI = R.getI()
552     #
553     # Initialisation
554     # --------------
555     __n = Xb.size
556     __m = selfA._parameters["NumberOfMembers"]
557     Xn = numpy.asmatrix(numpy.dot( Xb.reshape(__n,1), numpy.ones((1,__m)) ))
558     if hasattr(B,"asfullmatrix"): Pn = B.asfullmatrix(__n)
559     else:                         Pn = B
560     if hasattr(R,"asfullmatrix"): Rn = R.asfullmatrix(__p)
561     else:                         Rn = R
562     if hasattr(Q,"asfullmatrix"): Qn = Q.asfullmatrix(__n)
563     else:                         Qn = Q
564     #
565     if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
566         selfA.StoredVariables["Analysis"].store( numpy.ravel(Xb) )
567         if selfA._toStore("APosterioriCovariance"):
568             selfA.StoredVariables["APosterioriCovariance"].store( Pn )
569             covarianceXa = Pn
570     #
571     previousJMinimum = numpy.finfo(float).max
572     #
573     # Predimensionnement
574     Xn_predicted = numpy.asmatrix(numpy.zeros((__n,__m)))
575     HX_predicted = numpy.asmatrix(numpy.zeros((__p,__m)))
576     #
577     for step in range(duration-1):
578         if hasattr(Y,"store"):
579             Ynpu = numpy.asmatrix(numpy.ravel( Y[step+1] )).T
580         else:
581             Ynpu = numpy.asmatrix(numpy.ravel( Y )).T
582         #
583         if U is not None:
584             if hasattr(U,"store") and len(U)>1:
585                 Un = numpy.asmatrix(numpy.ravel( U[step] )).T
586             elif hasattr(U,"store") and len(U)==1:
587                 Un = numpy.asmatrix(numpy.ravel( U[0] )).T
588             else:
589                 Un = numpy.asmatrix(numpy.ravel( U )).T
590         else:
591             Un = None
592         #
593         if selfA._parameters["EstimationOf"] == "State": # Forecast + Q and observation of forecast
594             for i in range(__m):
595                 qi = numpy.asmatrix(numpy.random.multivariate_normal(numpy.zeros(__n), Qn, (1,1,1))).T
596                 Xn_predicted[:,i] = numpy.asmatrix(numpy.ravel( M((Xn[:,i], Un)) )).T + qi
597                 HX_predicted[:,i] = numpy.asmatrix(numpy.ravel( H((Xn_predicted[:,i], Un)) )).T
598             if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
599                 Cm = Cm.reshape(__n,Un.size) # ADAO & check shape
600                 Xn_predicted = Xn_predicted + Cm * Un
601         elif selfA._parameters["EstimationOf"] == "Parameters": # Observation of forecast
602             # --- > Par principe, M = Id, Q = 0
603             Xn_predicted = Xn
604             for i in range(__m):
605                 HX_predicted[:,i] = numpy.asmatrix(numpy.ravel( H((Xn_predicted[:,i], Un)) )).T
606         #
607         # Mean of forecast and observation of forecast
608         Xfm  = numpy.asmatrix(numpy.ravel(Xn_predicted.mean(axis=1, dtype=mfp).astype('float'))).T
609         Hfm  = numpy.asmatrix(numpy.ravel(HX_predicted.mean(axis=1, dtype=mfp).astype('float'))).T
610         #
611         PfHT, HPfHT = 0., 0.
612         for i in range(__m):
613             Exfi = Xn_predicted[:,i] - Xfm
614             Eyfi = HX_predicted[:,i] - Hfm
615             PfHT  += Exfi * Eyfi.T
616             HPfHT += Eyfi * Eyfi.T
617         PfHT  = (1./(__m-1)) * PfHT
618         HPfHT = (1./(__m-1)) * HPfHT
619         K     = PfHT * ( R + HPfHT ).I
620         del PfHT, HPfHT
621         #
622         for i in range(__m):
623             ri = numpy.asmatrix(numpy.random.multivariate_normal(numpy.zeros(__p), Rn, (1,1,1))).T
624             Xn[:,i] = Xn_predicted[:,i] + K * (Ynpu + ri - HX_predicted[:,i])
625         #
626         Xa = Xn.mean(axis=1, dtype=mfp).astype('float')
627         #
628         if selfA._parameters["StoreInternalVariables"] \
629             or selfA._toStore("CostFunctionJ") \
630             or selfA._toStore("CostFunctionJb") \
631             or selfA._toStore("CostFunctionJo") \
632             or selfA._toStore("APosterioriCovariance") \
633             or selfA._toStore("InnovationAtCurrentAnalysis") \
634             or selfA._toStore("SimulatedObservationAtCurrentAnalysis") \
635             or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
636             _HXa = numpy.asmatrix(numpy.ravel( H((Xa, Un)) )).T
637             _Innovation = Ynpu - _HXa
638         #
639         selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
640         # ---> avec analysis
641         selfA.StoredVariables["Analysis"].store( Xa )
642         if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
643             selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( _HXa )
644         if selfA._toStore("InnovationAtCurrentAnalysis"):
645             selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
646         # ---> avec current state
647         if selfA._parameters["StoreInternalVariables"] \
648             or selfA._toStore("CurrentState"):
649             selfA.StoredVariables["CurrentState"].store( Xn )
650         if selfA._toStore("ForecastState"):
651             selfA.StoredVariables["ForecastState"].store( Xn_predicted )
652         if selfA._toStore("BMA"):
653             selfA.StoredVariables["BMA"].store( Xn_predicted - Xa )
654         if selfA._toStore("InnovationAtCurrentState"):
655             selfA.StoredVariables["InnovationAtCurrentState"].store( - HX_predicted + Ynpu )
656         if selfA._toStore("SimulatedObservationAtCurrentState") \
657             or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
658             selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HX_predicted )
659         # ---> autres
660         if selfA._parameters["StoreInternalVariables"] \
661             or selfA._toStore("CostFunctionJ") \
662             or selfA._toStore("CostFunctionJb") \
663             or selfA._toStore("CostFunctionJo") \
664             or selfA._toStore("CurrentOptimum") \
665             or selfA._toStore("APosterioriCovariance"):
666             Jb  = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
667             Jo  = float( 0.5 * _Innovation.T * RI * _Innovation )
668             J   = Jb + Jo
669             selfA.StoredVariables["CostFunctionJb"].store( Jb )
670             selfA.StoredVariables["CostFunctionJo"].store( Jo )
671             selfA.StoredVariables["CostFunctionJ" ].store( J )
672             #
673             if selfA._toStore("IndexOfOptimum") \
674                 or selfA._toStore("CurrentOptimum") \
675                 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
676                 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
677                 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
678                 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
679                 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
680             if selfA._toStore("IndexOfOptimum"):
681                 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
682             if selfA._toStore("CurrentOptimum"):
683                 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
684             if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
685                 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
686             if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
687                 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
688             if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
689                 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
690             if selfA._toStore("CostFunctionJAtCurrentOptimum"):
691                 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
692         if selfA._toStore("APosterioriCovariance"):
693             Eai = (1/numpy.sqrt(__m-1)) * (Xn - Xa.reshape((__n,-1))) # Anomalies
694             Pn = Eai @ Eai.T
695             Pn = 0.5 * (Pn + Pn.T)
696             selfA.StoredVariables["APosterioriCovariance"].store( Pn )
697         if selfA._parameters["EstimationOf"] == "Parameters" \
698             and J < previousJMinimum:
699             previousJMinimum    = J
700             XaMin               = Xa
701             if selfA._toStore("APosterioriCovariance"):
702                 covarianceXaMin = Pn
703     #
704     # Stockage final supplémentaire de l'optimum en estimation de paramètres
705     # ----------------------------------------------------------------------
706     if selfA._parameters["EstimationOf"] == "Parameters":
707         selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
708         selfA.StoredVariables["Analysis"].store( XaMin )
709         if selfA._toStore("APosterioriCovariance"):
710             selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
711         if selfA._toStore("BMA"):
712             selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
713     #
714     return 0
715
716 # ==============================================================================
717 def etkf(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
718     """
719     Ensemble-Transform EnKF (ETKF or Deterministic EnKF: Bishop 2001, Hunt 2007)
720
721     selfA est identique au "self" d'algorithme appelant et contient les
722     valeurs.
723     """
724     if selfA._parameters["EstimationOf"] == "Parameters":
725         selfA._parameters["StoreInternalVariables"] = True
726     #
727     # Opérateurs
728     # ----------
729     H = HO["Direct"].appliedControledFormTo
730     #
731     if selfA._parameters["EstimationOf"] == "State":
732         M = EM["Direct"].appliedControledFormTo
733     #
734     if CM is not None and "Tangent" in CM and U is not None:
735         Cm = CM["Tangent"].asMatrix(Xb)
736     else:
737         Cm = None
738     #
739     # Nombre de pas identique au nombre de pas d'observations
740     # -------------------------------------------------------
741     if hasattr(Y,"stepnumber"):
742         duration = Y.stepnumber()
743         __p = numpy.cumprod(Y.shape())[-1]
744     else:
745         duration = 2
746         __p = numpy.array(Y).size
747     #
748     # Précalcul des inversions de B et R
749     # ----------------------------------
750     if selfA._parameters["StoreInternalVariables"] \
751         or selfA._toStore("CostFunctionJ") \
752         or selfA._toStore("CostFunctionJb") \
753         or selfA._toStore("CostFunctionJo") \
754         or selfA._toStore("CurrentOptimum") \
755         or selfA._toStore("APosterioriCovariance"):
756         BI = B.getI()
757         RI = R.getI()
758     RIdemi = R.choleskyI()
759     #
760     # Initialisation
761     # --------------
762     __n = Xb.size
763     __m = selfA._parameters["NumberOfMembers"]
764     Xn = numpy.asmatrix(numpy.dot( Xb.reshape(__n,1), numpy.ones((1,__m)) ))
765     if hasattr(B,"asfullmatrix"): Pn = B.asfullmatrix(__n)
766     else:                         Pn = B
767     if hasattr(R,"asfullmatrix"): Rn = R.asfullmatrix(__p)
768     else:                         Rn = R
769     if hasattr(Q,"asfullmatrix"): Qn = Q.asfullmatrix(__n)
770     else:                         Qn = Q
771     #
772     if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
773         selfA.StoredVariables["Analysis"].store( numpy.ravel(Xb) )
774         if selfA._toStore("APosterioriCovariance"):
775             selfA.StoredVariables["APosterioriCovariance"].store( Pn )
776             covarianceXa = Pn
777     #
778     previousJMinimum = numpy.finfo(float).max
779     #
780     # Predimensionnement
781     Xn_predicted = numpy.asmatrix(numpy.zeros((__n,__m)))
782     HX_predicted = numpy.asmatrix(numpy.zeros((__p,__m)))
783     #
784     for step in range(duration-1):
785         if hasattr(Y,"store"):
786             Ynpu = numpy.asmatrix(numpy.ravel( Y[step+1] )).T
787         else:
788             Ynpu = numpy.asmatrix(numpy.ravel( Y )).T
789         #
790         if U is not None:
791             if hasattr(U,"store") and len(U)>1:
792                 Un = numpy.asmatrix(numpy.ravel( U[step] )).T
793             elif hasattr(U,"store") and len(U)==1:
794                 Un = numpy.asmatrix(numpy.ravel( U[0] )).T
795             else:
796                 Un = numpy.asmatrix(numpy.ravel( U )).T
797         else:
798             Un = None
799         #
800         if selfA._parameters["EstimationOf"] == "State": # Forecast + Q and observation of forecast
801             for i in range(__m):
802                 qi = numpy.asmatrix(numpy.random.multivariate_normal(numpy.zeros(__n), Qn, (1,1,1))).T
803                 Xn_predicted[:,i] = numpy.asmatrix(numpy.ravel( M((Xn[:,i], Un)) )).T + qi
804                 HX_predicted[:,i] = numpy.asmatrix(numpy.ravel( H((Xn_predicted[:,i], Un)) )).T
805             if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
806                 Cm = Cm.reshape(__n,Un.size) # ADAO & check shape
807                 Xn_predicted = Xn_predicted + Cm * Un
808         elif selfA._parameters["EstimationOf"] == "Parameters": # Observation of forecast
809             # --- > Par principe, M = Id, Q = 0
810             Xn_predicted = Xn
811             for i in range(__m):
812                 HX_predicted[:,i] = numpy.asmatrix(numpy.ravel( H((Xn_predicted[:,i], Un)) )).T
813         #
814         # Mean of forecast and observation of forecast
815         Xfm  = Xn_predicted.mean(axis=1, dtype=mfp).astype('float')
816         Hfm  = HX_predicted.mean(axis=1, dtype=mfp).astype('float')
817         #
818         EaX   = (Xn_predicted - Xfm.reshape((__n,-1))) / numpy.sqrt(__m-1)
819         EaHX  = (HX_predicted - Hfm.reshape((__p,-1))) / numpy.sqrt(__m-1)
820         #
821         mS    = RIdemi * EaHX
822         delta = RIdemi * ( Ynpu.reshape((__p,-1)) - Hfm.reshape((__p,-1)) )
823         mT    = numpy.linalg.inv( numpy.eye(__m) + mS.T @ mS )
824         vw    = mT @ mS.transpose() @ delta
825         #
826         Tdemi = numpy.linalg.cholesky(mT)
827         mU    = numpy.eye(__m)
828         #
829         Xn = Xfm.reshape((__n,-1)) + EaX @ ( vw.reshape((__m,-1)) + numpy.sqrt(__m-1) * Tdemi @ mU )
830         #
831         Xa = Xn.mean(axis=1, dtype=mfp).astype('float')
832         #
833         if selfA._parameters["StoreInternalVariables"] \
834             or selfA._toStore("CostFunctionJ") \
835             or selfA._toStore("CostFunctionJb") \
836             or selfA._toStore("CostFunctionJo") \
837             or selfA._toStore("APosterioriCovariance") \
838             or selfA._toStore("InnovationAtCurrentAnalysis") \
839             or selfA._toStore("SimulatedObservationAtCurrentAnalysis") \
840             or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
841             _HXa = numpy.asmatrix(numpy.ravel( H((Xa, Un)) )).T
842             _Innovation = Ynpu - _HXa
843         #
844         selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
845         # ---> avec analysis
846         selfA.StoredVariables["Analysis"].store( Xa )
847         if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
848             selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( _HXa )
849         if selfA._toStore("InnovationAtCurrentAnalysis"):
850             selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
851         # ---> avec current state
852         if selfA._parameters["StoreInternalVariables"] \
853             or selfA._toStore("CurrentState"):
854             selfA.StoredVariables["CurrentState"].store( Xn )
855         if selfA._toStore("ForecastState"):
856             selfA.StoredVariables["ForecastState"].store( Xn_predicted )
857         if selfA._toStore("BMA"):
858             selfA.StoredVariables["BMA"].store( Xn_predicted - Xa )
859         if selfA._toStore("InnovationAtCurrentState"):
860             selfA.StoredVariables["InnovationAtCurrentState"].store( - HX_predicted + Ynpu )
861         if selfA._toStore("SimulatedObservationAtCurrentState") \
862             or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
863             selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HX_predicted )
864         # ---> autres
865         if selfA._parameters["StoreInternalVariables"] \
866             or selfA._toStore("CostFunctionJ") \
867             or selfA._toStore("CostFunctionJb") \
868             or selfA._toStore("CostFunctionJo") \
869             or selfA._toStore("CurrentOptimum") \
870             or selfA._toStore("APosterioriCovariance"):
871             Jb  = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
872             Jo  = float( 0.5 * _Innovation.T * RI * _Innovation )
873             J   = Jb + Jo
874             selfA.StoredVariables["CostFunctionJb"].store( Jb )
875             selfA.StoredVariables["CostFunctionJo"].store( Jo )
876             selfA.StoredVariables["CostFunctionJ" ].store( J )
877             #
878             if selfA._toStore("IndexOfOptimum") \
879                 or selfA._toStore("CurrentOptimum") \
880                 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
881                 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
882                 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
883                 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
884                 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
885             if selfA._toStore("IndexOfOptimum"):
886                 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
887             if selfA._toStore("CurrentOptimum"):
888                 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
889             if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
890                 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
891             if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
892                 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
893             if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
894                 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
895             if selfA._toStore("CostFunctionJAtCurrentOptimum"):
896                 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
897         if selfA._toStore("APosterioriCovariance"):
898             Eai = (1/numpy.sqrt(__m-1)) * (Xn - Xa.reshape((__n,-1))) # Anomalies
899             Pn = Eai @ Eai.T
900             Pn = 0.5 * (Pn + Pn.T)
901             selfA.StoredVariables["APosterioriCovariance"].store( Pn )
902         if selfA._parameters["EstimationOf"] == "Parameters" \
903             and J < previousJMinimum:
904             previousJMinimum    = J
905             XaMin               = Xa
906             if selfA._toStore("APosterioriCovariance"):
907                 covarianceXaMin = Pn
908     #
909     # Stockage final supplémentaire de l'optimum en estimation de paramètres
910     # ----------------------------------------------------------------------
911     if selfA._parameters["EstimationOf"] == "Parameters":
912         selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
913         selfA.StoredVariables["Analysis"].store( XaMin )
914         if selfA._toStore("APosterioriCovariance"):
915             selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
916         if selfA._toStore("BMA"):
917             selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
918     #
919     return 0
920
921 # ==============================================================================
922 if __name__ == "__main__":
923     print('\n AUTODIAGNOSTIC\n')