1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2021 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 Définit les objets numériques génériques.
26 __author__ = "Jean-Philippe ARGAUD"
28 import os, time, copy, types, sys, logging
29 import math, numpy, scipy, scipy.optimize, scipy.version
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)
36 # ==============================================================================
37 def ExecuteFunction( triplet ):
38 assert len(triplet) == 3, "Incorrect number of arguments"
39 X, xArgs, funcrepr = triplet
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 if isinstance(xArgs, dict):
46 __HX = __fonction( __X, **xArgs )
48 __HX = __fonction( __X )
49 return numpy.ravel( __HX )
51 # ==============================================================================
52 class FDApproximation(object):
54 Cette classe sert d'interface pour définir les opérateurs approximés. A la
55 création d'un objet, en fournissant une fonction "Function", on obtient un
56 objet qui dispose de 3 méthodes "DirectOperator", "TangentOperator" et
57 "AdjointOperator". On contrôle l'approximation DF avec l'incrément
58 multiplicatif "increment" valant par défaut 1%, ou avec l'incrément fixe
59 "dX" qui sera multiplié par "increment" (donc en %), et on effectue de DF
60 centrées si le booléen "centeredDF" est vrai.
63 name = "FDApproximation",
68 extraArguments = None,
69 avoidingRedundancy = True,
70 toleranceInRedundancy = 1.e-18,
71 lenghtOfRedundancy = -1,
76 self.__name = str(name)
77 self.__extraArgs = extraArguments
80 import multiprocessing
81 self.__mpEnabled = True
83 self.__mpEnabled = False
85 self.__mpEnabled = False
86 self.__mpWorkers = mpWorkers
87 if self.__mpWorkers is not None and self.__mpWorkers < 1:
88 self.__mpWorkers = None
89 logging.debug("FDA Calculs en multiprocessing : %s (nombre de processus : %s)"%(self.__mpEnabled,self.__mpWorkers))
92 self.__mfEnabled = True
94 self.__mfEnabled = False
95 logging.debug("FDA Calculs en multifonctions : %s"%(self.__mfEnabled,))
97 if avoidingRedundancy:
99 self.__tolerBP = float(toleranceInRedundancy)
100 self.__lenghtRJ = int(lenghtOfRedundancy)
101 self.__listJPCP = [] # Jacobian Previous Calculated Points
102 self.__listJPCI = [] # Jacobian Previous Calculated Increment
103 self.__listJPCR = [] # Jacobian Previous Calculated Results
104 self.__listJPPN = [] # Jacobian Previous Calculated Point Norms
105 self.__listJPIN = [] # Jacobian Previous Calculated Increment Norms
107 self.__avoidRC = False
110 if isinstance(Function,types.FunctionType):
111 logging.debug("FDA Calculs en multiprocessing : FunctionType")
112 self.__userFunction__name = Function.__name__
114 mod = os.path.join(Function.__globals__['filepath'],Function.__globals__['filename'])
116 mod = os.path.abspath(Function.__globals__['__file__'])
117 if not os.path.isfile(mod):
118 raise ImportError("No user defined function or method found with the name %s"%(mod,))
119 self.__userFunction__modl = os.path.basename(mod).replace('.pyc','').replace('.pyo','').replace('.py','')
120 self.__userFunction__path = os.path.dirname(mod)
122 self.__userOperator = Operator( name = self.__name, fromMethod = Function, avoidingRedundancy = self.__avoidRC, inputAsMultiFunction = self.__mfEnabled, extraArguments = self.__extraArgs )
123 self.__userFunction = self.__userOperator.appliedTo # Pour le calcul Direct
124 elif isinstance(Function,types.MethodType):
125 logging.debug("FDA Calculs en multiprocessing : MethodType")
126 self.__userFunction__name = Function.__name__
128 mod = os.path.join(Function.__globals__['filepath'],Function.__globals__['filename'])
130 mod = os.path.abspath(Function.__func__.__globals__['__file__'])
131 if not os.path.isfile(mod):
132 raise ImportError("No user defined function or method found with the name %s"%(mod,))
133 self.__userFunction__modl = os.path.basename(mod).replace('.pyc','').replace('.pyo','').replace('.py','')
134 self.__userFunction__path = os.path.dirname(mod)
136 self.__userOperator = Operator( name = self.__name, fromMethod = Function, avoidingRedundancy = self.__avoidRC, inputAsMultiFunction = self.__mfEnabled, extraArguments = self.__extraArgs )
137 self.__userFunction = self.__userOperator.appliedTo # Pour le calcul Direct
139 raise TypeError("User defined function or method has to be provided for finite differences approximation.")
141 self.__userOperator = Operator( name = self.__name, fromMethod = Function, avoidingRedundancy = self.__avoidRC, inputAsMultiFunction = self.__mfEnabled, extraArguments = self.__extraArgs )
142 self.__userFunction = self.__userOperator.appliedTo
144 self.__centeredDF = bool(centeredDF)
145 if abs(float(increment)) > 1.e-15:
146 self.__increment = float(increment)
148 self.__increment = 0.01
152 self.__dX = numpy.asmatrix(numpy.ravel( dX )).T
153 logging.debug("FDA Reduction des doublons de calcul : %s"%self.__avoidRC)
155 logging.debug("FDA Tolerance de determination des doublons : %.2e"%self.__tolerBP)
157 # ---------------------------------------------------------
158 def __doublon__(self, e, l, n, v=None):
159 __ac, __iac = False, -1
160 for i in range(len(l)-1,-1,-1):
161 if numpy.linalg.norm(e - l[i]) < self.__tolerBP * n[i]:
162 __ac, __iac = True, i
163 if v is not None: logging.debug("FDA Cas%s déja calculé, récupération du doublon %i"%(v,__iac))
167 # ---------------------------------------------------------
168 def DirectOperator(self, X, **extraArgs ):
170 Calcul du direct à l'aide de la fonction fournie.
172 NB : les extraArgs sont là pour assurer la compatibilité d'appel, mais
173 ne doivent pas être données ici à la fonction utilisateur.
175 logging.debug("FDA Calcul DirectOperator (explicite)")
177 _HX = self.__userFunction( X, argsAsSerie = True )
179 _X = numpy.asmatrix(numpy.ravel( X )).T
180 _HX = numpy.ravel(self.__userFunction( _X ))
184 # ---------------------------------------------------------
185 def TangentMatrix(self, X ):
187 Calcul de l'opérateur tangent comme la Jacobienne par différences finies,
188 c'est-à-dire le gradient de H en X. On utilise des différences finies
189 directionnelles autour du point X. X est un numpy.matrix.
191 Différences finies centrées (approximation d'ordre 2):
192 1/ Pour chaque composante i de X, on ajoute et on enlève la perturbation
193 dX[i] à la composante X[i], pour composer X_plus_dXi et X_moins_dXi, et
194 on calcule les réponses HX_plus_dXi = H( X_plus_dXi ) et HX_moins_dXi =
196 2/ On effectue les différences (HX_plus_dXi-HX_moins_dXi) et on divise par
198 3/ Chaque résultat, par composante, devient une colonne de la Jacobienne
200 Différences finies non centrées (approximation d'ordre 1):
201 1/ Pour chaque composante i de X, on ajoute la perturbation dX[i] à la
202 composante X[i] pour composer X_plus_dXi, et on calcule la réponse
203 HX_plus_dXi = H( X_plus_dXi )
204 2/ On calcule la valeur centrale HX = H(X)
205 3/ On effectue les différences (HX_plus_dXi-HX) et on divise par
207 4/ Chaque résultat, par composante, devient une colonne de la Jacobienne
210 logging.debug("FDA Début du calcul de la Jacobienne")
211 logging.debug("FDA Incrément de............: %s*X"%float(self.__increment))
212 logging.debug("FDA Approximation centrée...: %s"%(self.__centeredDF))
214 if X is None or len(X)==0:
215 raise ValueError("Nominal point X for approximate derivatives can not be None or void (given X: %s)."%(str(X),))
217 _X = numpy.asmatrix(numpy.ravel( X )).T
219 if self.__dX is None:
220 _dX = self.__increment * _X
222 _dX = numpy.asmatrix(numpy.ravel( self.__dX )).T
224 if (_dX == 0.).any():
227 _dX = numpy.where( _dX == 0., float(self.__increment), _dX )
229 _dX = numpy.where( _dX == 0., moyenne, _dX )
231 __alreadyCalculated = False
233 __bidon, __alreadyCalculatedP = self.__doublon__(_X, self.__listJPCP, self.__listJPPN, None)
234 __bidon, __alreadyCalculatedI = self.__doublon__(_dX, self.__listJPCI, self.__listJPIN, None)
235 if __alreadyCalculatedP == __alreadyCalculatedI > -1:
236 __alreadyCalculated, __i = True, __alreadyCalculatedP
237 logging.debug("FDA Cas J déja calculé, récupération du doublon %i"%__i)
239 if __alreadyCalculated:
240 logging.debug("FDA Calcul Jacobienne (par récupération du doublon %i)"%__i)
241 _Jacobienne = self.__listJPCR[__i]
243 logging.debug("FDA Calcul Jacobienne (explicite)")
244 if self.__centeredDF:
246 if self.__mpEnabled and not self.__mfEnabled:
248 "__userFunction__path" : self.__userFunction__path,
249 "__userFunction__modl" : self.__userFunction__modl,
250 "__userFunction__name" : self.__userFunction__name,
253 for i in range( len(_dX) ):
255 _X_plus_dXi = numpy.array( _X.A1, dtype=float )
256 _X_plus_dXi[i] = _X[i] + _dXi
257 _X_moins_dXi = numpy.array( _X.A1, dtype=float )
258 _X_moins_dXi[i] = _X[i] - _dXi
260 _jobs.append( (_X_plus_dXi, self.__extraArgs, funcrepr) )
261 _jobs.append( (_X_moins_dXi, self.__extraArgs, funcrepr) )
263 import multiprocessing
264 self.__pool = multiprocessing.Pool(self.__mpWorkers)
265 _HX_plusmoins_dX = self.__pool.map( ExecuteFunction, _jobs )
270 for i in range( len(_dX) ):
271 _Jacobienne.append( numpy.ravel( _HX_plusmoins_dX[2*i] - _HX_plusmoins_dX[2*i+1] ) / (2.*_dX[i]) )
273 elif self.__mfEnabled:
275 for i in range( len(_dX) ):
277 _X_plus_dXi = numpy.array( _X.A1, dtype=float )
278 _X_plus_dXi[i] = _X[i] + _dXi
279 _X_moins_dXi = numpy.array( _X.A1, dtype=float )
280 _X_moins_dXi[i] = _X[i] - _dXi
282 _xserie.append( _X_plus_dXi )
283 _xserie.append( _X_moins_dXi )
285 _HX_plusmoins_dX = self.DirectOperator( _xserie )
288 for i in range( len(_dX) ):
289 _Jacobienne.append( numpy.ravel( _HX_plusmoins_dX[2*i] - _HX_plusmoins_dX[2*i+1] ) / (2.*_dX[i]) )
293 for i in range( _dX.size ):
295 _X_plus_dXi = numpy.array( _X.A1, dtype=float )
296 _X_plus_dXi[i] = _X[i] + _dXi
297 _X_moins_dXi = numpy.array( _X.A1, dtype=float )
298 _X_moins_dXi[i] = _X[i] - _dXi
300 _HX_plus_dXi = self.DirectOperator( _X_plus_dXi )
301 _HX_moins_dXi = self.DirectOperator( _X_moins_dXi )
303 _Jacobienne.append( numpy.ravel( _HX_plus_dXi - _HX_moins_dXi ) / (2.*_dXi) )
307 if self.__mpEnabled and not self.__mfEnabled:
309 "__userFunction__path" : self.__userFunction__path,
310 "__userFunction__modl" : self.__userFunction__modl,
311 "__userFunction__name" : self.__userFunction__name,
314 _jobs.append( (_X.A1, self.__extraArgs, funcrepr) )
315 for i in range( len(_dX) ):
316 _X_plus_dXi = numpy.array( _X.A1, dtype=float )
317 _X_plus_dXi[i] = _X[i] + _dX[i]
319 _jobs.append( (_X_plus_dXi, self.__extraArgs, funcrepr) )
321 import multiprocessing
322 self.__pool = multiprocessing.Pool(self.__mpWorkers)
323 _HX_plus_dX = self.__pool.map( ExecuteFunction, _jobs )
327 _HX = _HX_plus_dX.pop(0)
330 for i in range( len(_dX) ):
331 _Jacobienne.append( numpy.ravel(( _HX_plus_dX[i] - _HX ) / _dX[i]) )
333 elif self.__mfEnabled:
335 _xserie.append( _X.A1 )
336 for i in range( len(_dX) ):
337 _X_plus_dXi = numpy.array( _X.A1, dtype=float )
338 _X_plus_dXi[i] = _X[i] + _dX[i]
340 _xserie.append( _X_plus_dXi )
342 _HX_plus_dX = self.DirectOperator( _xserie )
344 _HX = _HX_plus_dX.pop(0)
347 for i in range( len(_dX) ):
348 _Jacobienne.append( numpy.ravel(( _HX_plus_dX[i] - _HX ) / _dX[i]) )
352 _HX = self.DirectOperator( _X )
353 for i in range( _dX.size ):
355 _X_plus_dXi = numpy.array( _X.A1, dtype=float )
356 _X_plus_dXi[i] = _X[i] + _dXi
358 _HX_plus_dXi = self.DirectOperator( _X_plus_dXi )
360 _Jacobienne.append( numpy.ravel(( _HX_plus_dXi - _HX ) / _dXi) )
363 _Jacobienne = numpy.asmatrix( numpy.vstack( _Jacobienne ) ).T
365 if self.__lenghtRJ < 0: self.__lenghtRJ = 2 * _X.size
366 while len(self.__listJPCP) > self.__lenghtRJ:
367 self.__listJPCP.pop(0)
368 self.__listJPCI.pop(0)
369 self.__listJPCR.pop(0)
370 self.__listJPPN.pop(0)
371 self.__listJPIN.pop(0)
372 self.__listJPCP.append( copy.copy(_X) )
373 self.__listJPCI.append( copy.copy(_dX) )
374 self.__listJPCR.append( copy.copy(_Jacobienne) )
375 self.__listJPPN.append( numpy.linalg.norm(_X) )
376 self.__listJPIN.append( numpy.linalg.norm(_Jacobienne) )
378 logging.debug("FDA Fin du calcul de la Jacobienne")
382 # ---------------------------------------------------------
383 def TangentOperator(self, paire, **extraArgs ):
385 Calcul du tangent à l'aide de la Jacobienne.
387 NB : les extraArgs sont là pour assurer la compatibilité d'appel, mais
388 ne doivent pas être données ici à la fonction utilisateur.
391 assert len(paire) == 1, "Incorrect lenght of arguments"
393 assert len(_paire) == 2, "Incorrect number of arguments"
395 assert len(paire) == 2, "Incorrect number of arguments"
398 _Jacobienne = self.TangentMatrix( X )
399 if dX is None or len(dX) == 0:
401 # Calcul de la forme matricielle si le second argument est None
402 # -------------------------------------------------------------
403 if self.__mfEnabled: return [_Jacobienne,]
404 else: return _Jacobienne
407 # Calcul de la valeur linéarisée de H en X appliqué à dX
408 # ------------------------------------------------------
409 _dX = numpy.asmatrix(numpy.ravel( dX )).T
410 _HtX = numpy.dot(_Jacobienne, _dX)
411 if self.__mfEnabled: return [_HtX.A1,]
414 # ---------------------------------------------------------
415 def AdjointOperator(self, paire, **extraArgs ):
417 Calcul de l'adjoint à l'aide de la Jacobienne.
419 NB : les extraArgs sont là pour assurer la compatibilité d'appel, mais
420 ne doivent pas être données ici à la fonction utilisateur.
423 assert len(paire) == 1, "Incorrect lenght of arguments"
425 assert len(_paire) == 2, "Incorrect number of arguments"
427 assert len(paire) == 2, "Incorrect number of arguments"
430 _JacobienneT = self.TangentMatrix( X ).T
431 if Y is None or len(Y) == 0:
433 # Calcul de la forme matricielle si le second argument est None
434 # -------------------------------------------------------------
435 if self.__mfEnabled: return [_JacobienneT,]
436 else: return _JacobienneT
439 # Calcul de la valeur de l'adjoint en X appliqué à Y
440 # --------------------------------------------------
441 _Y = numpy.asmatrix(numpy.ravel( Y )).T
442 _HaY = numpy.dot(_JacobienneT, _Y)
443 if self.__mfEnabled: return [_HaY.A1,]
446 # ==============================================================================
447 def EnsembleOfCenteredPerturbations( _bgcenter, _bgcovariance, _nbmembers ):
448 "Génération d'un ensemble de taille _nbmembers-1 d'états aléatoires centrés"
450 _bgcenter = numpy.ravel(_bgcenter)[:,None]
452 raise ValueError("Number of members has to be strictly more than 1 (given number: %s)."%(str(_nbmembers),))
454 if _bgcovariance is None:
455 BackgroundEnsemble = numpy.tile( _bgcenter, _nbmembers)
457 _Z = numpy.random.multivariate_normal(numpy.zeros(_bgcenter.size), _bgcovariance, size=_nbmembers).T
458 BackgroundEnsemble = numpy.tile( _bgcenter, _nbmembers) + _Z
460 return BackgroundEnsemble
462 # ==============================================================================
463 def EnsembleOfBackgroundPerturbations( _bgcenter, _bgcovariance, _nbmembers, _withSVD = True):
464 "Génération d'un ensemble de taille _nbmembers-1 d'états aléatoires centrés"
465 def __CenteredRandomAnomalies(Zr, N):
467 Génère une matrice de N anomalies aléatoires centrées sur Zr selon les
468 notes manuscrites de MB et conforme au code de PS avec eps = -1
471 Q = numpy.identity(N-1)-numpy.ones((N-1,N-1))/numpy.sqrt(N)/(numpy.sqrt(N)-eps)
472 Q = numpy.concatenate((Q, [eps*numpy.ones(N-1)/numpy.sqrt(N)]), axis=0)
473 R, _ = numpy.linalg.qr(numpy.random.normal(size = (N-1,N-1)))
478 _bgcenter = numpy.ravel(_bgcenter).reshape((-1,1))
480 raise ValueError("Number of members has to be strictly more than 1 (given number: %s)."%(str(_nbmembers),))
481 if _bgcovariance is None:
482 BackgroundEnsemble = numpy.tile( _bgcenter, _nbmembers)
485 U, s, V = numpy.linalg.svd(_bgcovariance, full_matrices=False)
486 _nbctl = _bgcenter.size
487 if _nbmembers > _nbctl:
488 _Z = numpy.concatenate((numpy.dot(
489 numpy.diag(numpy.sqrt(s[:_nbctl])), V[:_nbctl]),
490 numpy.random.multivariate_normal(numpy.zeros(_nbctl),_bgcovariance,_nbmembers-1-_nbctl)), axis = 0)
492 _Z = numpy.dot(numpy.diag(numpy.sqrt(s[:_nbmembers-1])), V[:_nbmembers-1])
493 _Zca = __CenteredRandomAnomalies(_Z, _nbmembers)
494 BackgroundEnsemble = _bgcenter + _Zca
496 if max(abs(_bgcovariance.flatten())) > 0:
497 _nbctl = _bgcenter.size
498 _Z = numpy.random.multivariate_normal(numpy.zeros(_nbctl),_bgcovariance,_nbmembers-1)
499 _Zca = __CenteredRandomAnomalies(_Z, _nbmembers)
500 BackgroundEnsemble = _bgcenter + _Zca
502 BackgroundEnsemble = numpy.tile( _bgcenter, _nbmembers)
504 return BackgroundEnsemble
506 # ==============================================================================
507 def EnsembleOfAnomalies( Ensemble, OptMean = None, Normalisation = 1.):
508 "Renvoie les anomalies centrées à partir d'un ensemble TailleEtat*NbMembres"
510 __Em = numpy.asarray(Ensemble).mean(axis=1, dtype=mfp).astype('float').reshape((-1,1))
512 __Em = numpy.ravel(OptMean).reshape((-1,1))
514 return Normalisation * (numpy.asarray(Ensemble) - __Em)
516 # ==============================================================================
517 def EnsembleErrorCovariance( Ensemble, __quick = False ):
518 "Renvoie l'estimation empirique de la covariance d'ensemble"
520 # Covariance rapide mais rarement définie positive
521 __Covariance = numpy.cov(Ensemble)
523 # Résultat souvent identique à numpy.cov, mais plus robuste
524 __n, __m = numpy.asarray(Ensemble).shape
525 __Anomalies = EnsembleOfAnomalies( Ensemble )
526 # Estimation empirique
527 __Covariance = (__Anomalies @ __Anomalies.T) / (__m-1)
529 __Covariance = (__Covariance + __Covariance.T) * 0.5
530 # Assure la positivité
531 __epsilon = mpr*numpy.trace(__Covariance)
532 __Covariance = __Covariance + __epsilon * numpy.identity(__n)
536 # ==============================================================================
537 def EnsemblePerturbationWithGivenCovariance( __Ensemble, __Covariance, __Seed=None ):
538 "Ajout d'une perturbation à chaque membre d'un ensemble selon une covariance prescrite"
539 if hasattr(__Covariance,"assparsematrix"):
540 if (abs(__Ensemble).mean() > mpr) and (abs(__Covariance.assparsematrix())/abs(__Ensemble).mean() < mpr).all():
541 # Traitement d'une covariance nulle ou presque
543 if (abs(__Ensemble).mean() <= mpr) and (abs(__Covariance.assparsematrix()) < mpr).all():
544 # Traitement d'une covariance nulle ou presque
547 if (abs(__Ensemble).mean() > mpr) and (abs(__Covariance)/abs(__Ensemble).mean() < mpr).all():
548 # Traitement d'une covariance nulle ou presque
550 if (abs(__Ensemble).mean() <= mpr) and (abs(__Covariance) < mpr).all():
551 # Traitement d'une covariance nulle ou presque
554 __n, __m = __Ensemble.shape
555 if __Seed is not None: numpy.random.seed(__Seed)
557 if hasattr(__Covariance,"isscalar") and __Covariance.isscalar():
558 # Traitement d'une covariance multiple de l'identité
560 __std = numpy.sqrt(__Covariance.assparsematrix())
561 __Ensemble += numpy.random.normal(__zero, __std, size=(__m,__n)).T
563 elif hasattr(__Covariance,"isvector") and __Covariance.isvector():
564 # Traitement d'une covariance diagonale avec variances non identiques
565 __zero = numpy.zeros(__n)
566 __std = numpy.sqrt(__Covariance.assparsematrix())
567 __Ensemble += numpy.asarray([numpy.random.normal(__zero, __std) for i in range(__m)]).T
569 elif hasattr(__Covariance,"ismatrix") and __Covariance.ismatrix():
570 # Traitement d'une covariance pleine
571 __Ensemble += numpy.random.multivariate_normal(numpy.zeros(__n), __Covariance.asfullmatrix(__n), size=__m).T
573 elif isinstance(__Covariance, numpy.ndarray):
574 # Traitement d'une covariance numpy pleine, sachant qu'on arrive ici en dernier
575 __Ensemble += numpy.random.multivariate_normal(numpy.zeros(__n), __Covariance, size=__m).T
578 raise ValueError("Error in ensemble perturbation with inadequate covariance specification")
582 # ==============================================================================
583 def CovarianceInflation(
585 InflationType = None,
586 InflationFactor = None,
587 BackgroundCov = None,
590 Inflation applicable soit sur Pb ou Pa, soit sur les ensembles EXb ou EXa
592 Synthèse : Hunt 2007, section 2.3.5
594 if InflationFactor is None:
597 InflationFactor = float(InflationFactor)
599 if InflationType in ["MultiplicativeOnAnalysisCovariance", "MultiplicativeOnBackgroundCovariance"]:
600 if InflationFactor < 1.:
601 raise ValueError("Inflation factor for multiplicative inflation has to be greater or equal than 1.")
602 if InflationFactor < 1.+mpr:
604 OutputCovOrEns = InflationFactor**2 * InputCovOrEns
606 elif InflationType in ["MultiplicativeOnAnalysisAnomalies", "MultiplicativeOnBackgroundAnomalies"]:
607 if InflationFactor < 1.:
608 raise ValueError("Inflation factor for multiplicative inflation has to be greater or equal than 1.")
609 if InflationFactor < 1.+mpr:
611 InputCovOrEnsMean = InputCovOrEns.mean(axis=1, dtype=mfp).astype('float')
612 OutputCovOrEns = InputCovOrEnsMean[:,numpy.newaxis] \
613 + InflationFactor * (InputCovOrEns - InputCovOrEnsMean[:,numpy.newaxis])
615 elif InflationType in ["AdditiveOnAnalysisCovariance", "AdditiveOnBackgroundCovariance"]:
616 if InflationFactor < 0.:
617 raise ValueError("Inflation factor for additive inflation has to be greater or equal than 0.")
618 if InflationFactor < mpr:
620 __n, __m = numpy.asarray(InputCovOrEns).shape
622 raise ValueError("Additive inflation can only be applied to squared (covariance) matrix.")
623 OutputCovOrEns = (1. - InflationFactor) * InputCovOrEns + InflationFactor * numpy.identity(__n)
625 elif InflationType == "HybridOnBackgroundCovariance":
626 if InflationFactor < 0.:
627 raise ValueError("Inflation factor for hybrid inflation has to be greater or equal than 0.")
628 if InflationFactor < mpr:
630 __n, __m = numpy.asarray(InputCovOrEns).shape
632 raise ValueError("Additive inflation can only be applied to squared (covariance) matrix.")
633 if BackgroundCov is None:
634 raise ValueError("Background covariance matrix B has to be given for hybrid inflation.")
635 if InputCovOrEns.shape != BackgroundCov.shape:
636 raise ValueError("Ensemble covariance matrix has to be of same size than background covariance matrix B.")
637 OutputCovOrEns = (1. - InflationFactor) * InputCovOrEns + InflationFactor * BackgroundCov
639 elif InflationType == "Relaxation":
640 raise NotImplementedError("InflationType Relaxation")
643 raise ValueError("Error in inflation type, '%s' is not a valid keyword."%InflationType)
645 return OutputCovOrEns
647 # ==============================================================================
648 def QuantilesEstimations(selfA, A, Xa, HXa = None, Hm = None, HtM = None):
649 "Estimation des quantiles a posteriori (selfA est modifié)"
650 nbsamples = selfA._parameters["NumberOfSamplesForQuantiles"]
652 # Traitement des bornes
653 if "StateBoundsForQuantiles" in selfA._parameters:
654 LBounds = selfA._parameters["StateBoundsForQuantiles"] # Prioritaire
655 elif "Bounds" in selfA._parameters:
656 LBounds = selfA._parameters["Bounds"] # Défaut raisonnable
659 if LBounds is not None:
660 def NoneRemove(paire):
662 if bmin is None: bmin = numpy.finfo('float').min
663 if bmax is None: bmax = numpy.finfo('float').max
665 LBounds = numpy.matrix( [NoneRemove(paire) for paire in LBounds] )
667 # Échantillonnage des états
670 if selfA._parameters["SimulationForQuantiles"] == "Linear" and HXa is not None:
671 HXa = numpy.matrix(numpy.ravel( HXa )).T
672 for i in range(nbsamples):
673 if selfA._parameters["SimulationForQuantiles"] == "Linear" and HtM is not None:
674 dXr = numpy.matrix(numpy.random.multivariate_normal(numpy.ravel(Xa),A) - numpy.ravel(Xa)).T
675 if LBounds is not None: # "EstimateProjection" par défaut
676 dXr = numpy.max(numpy.hstack((dXr,LBounds[:,0]) - Xa),axis=1)
677 dXr = numpy.min(numpy.hstack((dXr,LBounds[:,1]) - Xa),axis=1)
678 dYr = numpy.matrix(numpy.ravel( HtM * dXr )).T
680 if selfA._toStore("SampledStateForQuantiles"): Xr = Xa + dXr
681 elif selfA._parameters["SimulationForQuantiles"] == "NonLinear" and Hm is not None:
682 Xr = numpy.matrix(numpy.random.multivariate_normal(numpy.ravel(Xa),A)).T
683 if LBounds is not None: # "EstimateProjection" par défaut
684 Xr = numpy.max(numpy.hstack((Xr,LBounds[:,0])),axis=1)
685 Xr = numpy.min(numpy.hstack((Xr,LBounds[:,1])),axis=1)
686 Yr = numpy.matrix(numpy.ravel( Hm( Xr ) )).T
688 raise ValueError("Quantile simulations has only to be Linear or NonLinear.")
692 if selfA._toStore("SampledStateForQuantiles"): EXr = numpy.ravel(Xr)
694 YfQ = numpy.hstack((YfQ,Yr))
695 if selfA._toStore("SampledStateForQuantiles"): EXr = numpy.vstack((EXr,numpy.ravel(Xr)))
697 # Extraction des quantiles
700 for quantile in selfA._parameters["Quantiles"]:
701 if not (0. <= float(quantile) <= 1.): continue
702 indice = int(nbsamples * float(quantile) - 1./nbsamples)
703 if YQ is None: YQ = YfQ[:,indice]
704 else: YQ = numpy.hstack((YQ,YfQ[:,indice]))
705 selfA.StoredVariables["SimulationQuantiles"].store( YQ )
706 if selfA._toStore("SampledStateForQuantiles"):
707 selfA.StoredVariables["SampledStateForQuantiles"].store( EXr.T )
711 # ==============================================================================
712 def enks(selfA, Xb, Y, U, HO, EM, CM, R, B, Q, VariantM="EnKS16-KalmanFilterFormula"):
718 H = HO["Direct"].appliedControledFormTo
720 if selfA._parameters["EstimationOf"] == "State":
721 M = EM["Direct"].appliedControledFormTo
723 if CM is not None and "Tangent" in CM and U is not None:
724 Cm = CM["Tangent"].asMatrix(Xb)
728 # Précalcul des inversions de B et R
731 # Durée d'observation et tailles
732 LagL = selfA._parameters["SmootherLagL"]
733 if (not hasattr(Y,"store")) or (not hasattr(Y,"stepnumber")):
734 raise ValueError("Fixed-lag smoother requires a series of observation")
735 if Y.stepnumber() < LagL:
736 raise ValueError("Fixed-lag smoother requires a series of observation greater then the lag L")
737 duration = Y.stepnumber()
738 __p = numpy.cumprod(Y.shape())[-1]
740 __m = selfA._parameters["NumberOfMembers"]
742 if hasattr(B,"asfullmatrix"): Pn = B.asfullmatrix(__n)
744 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
745 selfA.StoredVariables["Analysis"].store( Xb )
746 if selfA._toStore("APosterioriCovariance"):
747 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
750 # Calcul direct initial (on privilégie la mémorisation au recalcul)
751 __seed = numpy.random.get_state()
752 selfB = copy.deepcopy(selfA)
753 selfB._parameters["StoreSupplementaryCalculations"] = ["CurrentEnsembleState"]
754 if VariantM == "EnKS16-KalmanFilterFormula":
755 etkf(selfB, Xb, Y, U, HO, EM, CM, R, B, Q, VariantM = "KalmanFilterFormula")
757 raise ValueError("VariantM has to be chosen in the authorized methods list.")
759 EL = selfB.StoredVariables["CurrentEnsembleState"][LagL-1]
761 EL = EnsembleOfBackgroundPerturbations( Xb, None, __m ) # Cf. etkf
762 selfA._parameters["SetSeed"] = numpy.random.set_state(__seed)
764 for step in range(LagL,duration-1):
766 sEL = selfB.StoredVariables["CurrentEnsembleState"][step+1-LagL:step+1]
769 if hasattr(Y,"store"):
770 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
772 Ynpu = numpy.ravel( Y ).reshape((__p,1))
775 if hasattr(U,"store") and len(U)>1:
776 Un = numpy.asmatrix(numpy.ravel( U[step] )).T
777 elif hasattr(U,"store") and len(U)==1:
778 Un = numpy.asmatrix(numpy.ravel( U[0] )).T
780 Un = numpy.asmatrix(numpy.ravel( U )).T
784 #--------------------------
785 if VariantM == "EnKS16-KalmanFilterFormula":
786 if selfA._parameters["EstimationOf"] == "State": # Forecast
787 EL = M( [(EL[:,i], Un) for i in range(__m)],
789 returnSerieAsArrayMatrix = True )
790 EL = EnsemblePerturbationWithGivenCovariance( EL, Q )
791 EZ = H( [(EL[:,i], Un) for i in range(__m)],
793 returnSerieAsArrayMatrix = True )
794 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
795 Cm = Cm.reshape(__n,Un.size) # ADAO & check shape
797 elif selfA._parameters["EstimationOf"] == "Parameters":
798 # --- > Par principe, M = Id, Q = 0
799 EZ = H( [(EL[:,i], Un) for i in range(__m)],
801 returnSerieAsArrayMatrix = True )
803 vEm = EL.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
804 vZm = EZ.mean(axis=1, dtype=mfp).astype('float').reshape((__p,1))
806 mS = RIdemi @ EnsembleOfAnomalies( EZ, vZm, 1./math.sqrt(__m-1) )
807 delta = RIdemi @ ( Ynpu - vZm )
808 mT = numpy.linalg.inv( numpy.identity(__m) + mS.T @ mS )
809 vw = mT @ mS.T @ delta
811 Tdemi = numpy.real(scipy.linalg.sqrtm(mT))
812 mU = numpy.identity(__m)
813 wTU = (vw.reshape((__m,1)) + math.sqrt(__m-1) * Tdemi @ mU)
815 EX = EnsembleOfAnomalies( EL, vEm, 1./math.sqrt(__m-1) )
819 for irl in range(LagL): # Lissage des L précédentes analysis
820 vEm = sEL[irl].mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
821 EX = EnsembleOfAnomalies( sEL[irl], vEm, 1./math.sqrt(__m-1) )
822 sEL[irl] = vEm + EX @ wTU
824 # Conservation de l'analyse retrospective d'ordre 0 avant rotation
825 Xa = sEL[0].mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
826 if selfA._toStore("APosterioriCovariance"):
829 for irl in range(LagL):
830 sEL[irl] = sEL[irl+1]
832 #--------------------------
834 raise ValueError("VariantM has to be chosen in the authorized methods list.")
836 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
838 selfA.StoredVariables["Analysis"].store( Xa )
839 if selfA._toStore("APosterioriCovariance"):
840 selfA.StoredVariables["APosterioriCovariance"].store( EnsembleErrorCovariance(EXn) )
842 # Stockage des dernières analyses incomplètement remises à jour
843 for irl in range(LagL):
844 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
845 Xa = sEL[irl].mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
846 selfA.StoredVariables["Analysis"].store( Xa )
850 # ==============================================================================
851 def etkf(selfA, Xb, Y, U, HO, EM, CM, R, B, Q, VariantM="KalmanFilterFormula"):
853 Ensemble-Transform EnKF
855 if selfA._parameters["EstimationOf"] == "Parameters":
856 selfA._parameters["StoreInternalVariables"] = True
860 H = HO["Direct"].appliedControledFormTo
862 if selfA._parameters["EstimationOf"] == "State":
863 M = EM["Direct"].appliedControledFormTo
865 if CM is not None and "Tangent" in CM and U is not None:
866 Cm = CM["Tangent"].asMatrix(Xb)
870 # Nombre de pas identique au nombre de pas d'observations
871 # -------------------------------------------------------
872 if hasattr(Y,"stepnumber"):
873 duration = Y.stepnumber()
874 __p = numpy.cumprod(Y.shape())[-1]
877 __p = numpy.array(Y).size
879 # Précalcul des inversions de B et R
880 # ----------------------------------
881 if selfA._parameters["StoreInternalVariables"] \
882 or selfA._toStore("CostFunctionJ") \
883 or selfA._toStore("CostFunctionJb") \
884 or selfA._toStore("CostFunctionJo") \
885 or selfA._toStore("CurrentOptimum") \
886 or selfA._toStore("APosterioriCovariance"):
889 elif VariantM != "KalmanFilterFormula":
891 if VariantM == "KalmanFilterFormula":
897 __m = selfA._parameters["NumberOfMembers"]
898 if hasattr(B,"asfullmatrix"): Pn = B.asfullmatrix(__n)
900 Xn = EnsembleOfBackgroundPerturbations( Xb, None, __m )
901 #~ Xn = EnsembleOfBackgroundPerturbations( Xb, Pn, __m )
903 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
904 selfA.StoredVariables["Analysis"].store( Xb )
905 if selfA._toStore("APosterioriCovariance"):
906 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
909 previousJMinimum = numpy.finfo(float).max
911 for step in range(duration-1):
912 if hasattr(Y,"store"):
913 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
915 Ynpu = numpy.ravel( Y ).reshape((__p,1))
918 if hasattr(U,"store") and len(U)>1:
919 Un = numpy.asmatrix(numpy.ravel( U[step] )).T
920 elif hasattr(U,"store") and len(U)==1:
921 Un = numpy.asmatrix(numpy.ravel( U[0] )).T
923 Un = numpy.asmatrix(numpy.ravel( U )).T
927 if selfA._parameters["InflationType"] == "MultiplicativeOnBackgroundAnomalies":
928 Xn = CovarianceInflation( Xn,
929 selfA._parameters["InflationType"],
930 selfA._parameters["InflationFactor"],
933 if selfA._parameters["EstimationOf"] == "State": # Forecast + Q and observation of forecast
934 EMX = M( [(Xn[:,i], Un) for i in range(__m)],
936 returnSerieAsArrayMatrix = True )
937 Xn_predicted = EnsemblePerturbationWithGivenCovariance( EMX, Q )
938 HX_predicted = H( [(Xn_predicted[:,i], Un) for i in range(__m)],
940 returnSerieAsArrayMatrix = True )
941 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
942 Cm = Cm.reshape(__n,Un.size) # ADAO & check shape
943 Xn_predicted = Xn_predicted + Cm * Un
944 elif selfA._parameters["EstimationOf"] == "Parameters": # Observation of forecast
945 # --- > Par principe, M = Id, Q = 0
947 HX_predicted = H( [(Xn_predicted[:,i], Un) for i in range(__m)],
949 returnSerieAsArrayMatrix = True )
951 # Mean of forecast and observation of forecast
952 Xfm = Xn_predicted.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
953 Hfm = HX_predicted.mean(axis=1, dtype=mfp).astype('float').reshape((__p,1))
956 EaX = EnsembleOfAnomalies( Xn_predicted, Xfm )
957 EaHX = EnsembleOfAnomalies( HX_predicted, Hfm)
959 #--------------------------
960 if VariantM == "KalmanFilterFormula":
961 mS = RIdemi * EaHX / math.sqrt(__m-1)
962 delta = RIdemi * ( Ynpu - Hfm )
963 mT = numpy.linalg.inv( numpy.identity(__m) + mS.T @ mS )
964 vw = mT @ mS.T @ delta
966 Tdemi = numpy.real(scipy.linalg.sqrtm(mT))
967 mU = numpy.identity(__m)
969 EaX = EaX / math.sqrt(__m-1)
970 Xn = Xfm + EaX @ ( vw.reshape((__m,1)) + math.sqrt(__m-1) * Tdemi @ mU )
971 #--------------------------
972 elif VariantM == "Variational":
973 HXfm = H((Xfm[:,None], Un)) # Eventuellement Hfm
975 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
976 _Jo = 0.5 * _A.T @ (RI * _A)
977 _Jb = 0.5 * (__m-1) * w.T @ w
980 def GradientOfCostFunction(w):
981 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
982 _GardJo = - EaHX.T @ (RI * _A)
983 _GradJb = (__m-1) * w.reshape((__m,1))
984 _GradJ = _GardJo + _GradJb
985 return numpy.ravel(_GradJ)
986 vw = scipy.optimize.fmin_cg(
988 x0 = numpy.zeros(__m),
989 fprime = GradientOfCostFunction,
994 Hto = EaHX.T @ (RI * EaHX)
995 Htb = (__m-1) * numpy.identity(__m)
998 Pta = numpy.linalg.inv( Hta )
999 EWa = numpy.real(scipy.linalg.sqrtm((__m-1)*Pta)) # Partie imaginaire ~= 10^-18
1001 Xn = Xfm + EaX @ (vw[:,None] + EWa)
1002 #--------------------------
1003 elif VariantM == "FiniteSize11": # Jauge Boc2011
1004 HXfm = H((Xfm[:,None], Un)) # Eventuellement Hfm
1005 def CostFunction(w):
1006 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1007 _Jo = 0.5 * _A.T @ (RI * _A)
1008 _Jb = 0.5 * __m * math.log(1 + 1/__m + w.T @ w)
1011 def GradientOfCostFunction(w):
1012 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1013 _GardJo = - EaHX.T @ (RI * _A)
1014 _GradJb = __m * w.reshape((__m,1)) / (1 + 1/__m + w.T @ w)
1015 _GradJ = _GardJo + _GradJb
1016 return numpy.ravel(_GradJ)
1017 vw = scipy.optimize.fmin_cg(
1019 x0 = numpy.zeros(__m),
1020 fprime = GradientOfCostFunction,
1025 Hto = EaHX.T @ (RI * EaHX)
1027 ( (1 + 1/__m + vw.T @ vw) * numpy.identity(__m) - 2 * vw @ vw.T ) \
1028 / (1 + 1/__m + vw.T @ vw)**2
1031 Pta = numpy.linalg.inv( Hta )
1032 EWa = numpy.real(scipy.linalg.sqrtm((__m-1)*Pta)) # Partie imaginaire ~= 10^-18
1034 Xn = Xfm + EaX @ (vw.reshape((__m,1)) + EWa)
1035 #--------------------------
1036 elif VariantM == "FiniteSize15": # Jauge Boc2015
1037 HXfm = H((Xfm[:,None], Un)) # Eventuellement Hfm
1038 def CostFunction(w):
1039 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1040 _Jo = 0.5 * _A.T * RI * _A
1041 _Jb = 0.5 * (__m+1) * math.log(1 + 1/__m + w.T @ w)
1044 def GradientOfCostFunction(w):
1045 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1046 _GardJo = - EaHX.T @ (RI * _A)
1047 _GradJb = (__m+1) * w.reshape((__m,1)) / (1 + 1/__m + w.T @ w)
1048 _GradJ = _GardJo + _GradJb
1049 return numpy.ravel(_GradJ)
1050 vw = scipy.optimize.fmin_cg(
1052 x0 = numpy.zeros(__m),
1053 fprime = GradientOfCostFunction,
1058 Hto = EaHX.T @ (RI * EaHX)
1060 ( (1 + 1/__m + vw.T @ vw) * numpy.identity(__m) - 2 * vw @ vw.T ) \
1061 / (1 + 1/__m + vw.T @ vw)**2
1064 Pta = numpy.linalg.inv( Hta )
1065 EWa = numpy.real(scipy.linalg.sqrtm((__m-1)*Pta)) # Partie imaginaire ~= 10^-18
1067 Xn = Xfm + EaX @ (vw.reshape((__m,1)) + EWa)
1068 #--------------------------
1069 elif VariantM == "FiniteSize16": # Jauge Boc2016
1070 HXfm = H((Xfm[:,None], Un)) # Eventuellement Hfm
1071 def CostFunction(w):
1072 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1073 _Jo = 0.5 * _A.T @ (RI * _A)
1074 _Jb = 0.5 * (__m+1) * math.log(1 + 1/__m + w.T @ w / (__m-1))
1077 def GradientOfCostFunction(w):
1078 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1079 _GardJo = - EaHX.T @ (RI * _A)
1080 _GradJb = ((__m+1) / (__m-1)) * w.reshape((__m,1)) / (1 + 1/__m + w.T @ w / (__m-1))
1081 _GradJ = _GardJo + _GradJb
1082 return numpy.ravel(_GradJ)
1083 vw = scipy.optimize.fmin_cg(
1085 x0 = numpy.zeros(__m),
1086 fprime = GradientOfCostFunction,
1091 Hto = EaHX.T @ (RI * EaHX)
1092 Htb = ((__m+1) / (__m-1)) * \
1093 ( (1 + 1/__m + vw.T @ vw / (__m-1)) * numpy.identity(__m) - 2 * vw @ vw.T / (__m-1) ) \
1094 / (1 + 1/__m + vw.T @ vw / (__m-1))**2
1097 Pta = numpy.linalg.inv( Hta )
1098 EWa = numpy.real(scipy.linalg.sqrtm((__m-1)*Pta)) # Partie imaginaire ~= 10^-18
1100 Xn = Xfm + EaX @ (vw[:,None] + EWa)
1101 #--------------------------
1103 raise ValueError("VariantM has to be chosen in the authorized methods list.")
1105 if selfA._parameters["InflationType"] == "MultiplicativeOnAnalysisAnomalies":
1106 Xn = CovarianceInflation( Xn,
1107 selfA._parameters["InflationType"],
1108 selfA._parameters["InflationFactor"],
1111 Xa = Xn.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
1112 #--------------------------
1114 if selfA._parameters["StoreInternalVariables"] \
1115 or selfA._toStore("CostFunctionJ") \
1116 or selfA._toStore("CostFunctionJb") \
1117 or selfA._toStore("CostFunctionJo") \
1118 or selfA._toStore("APosterioriCovariance") \
1119 or selfA._toStore("InnovationAtCurrentAnalysis") \
1120 or selfA._toStore("SimulatedObservationAtCurrentAnalysis") \
1121 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1122 _HXa = numpy.asmatrix(numpy.ravel( H((Xa, Un)) )).T
1123 _Innovation = Ynpu - _HXa
1125 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1126 # ---> avec analysis
1127 selfA.StoredVariables["Analysis"].store( Xa )
1128 if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
1129 selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( _HXa )
1130 if selfA._toStore("InnovationAtCurrentAnalysis"):
1131 selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
1132 # ---> avec current state
1133 if selfA._parameters["StoreInternalVariables"] \
1134 or selfA._toStore("CurrentState"):
1135 selfA.StoredVariables["CurrentState"].store( Xn )
1136 if selfA._toStore("ForecastState"):
1137 selfA.StoredVariables["ForecastState"].store( EMX )
1138 if selfA._toStore("BMA"):
1139 selfA.StoredVariables["BMA"].store( EMX - Xa.reshape((__n,1)) )
1140 if selfA._toStore("InnovationAtCurrentState"):
1141 selfA.StoredVariables["InnovationAtCurrentState"].store( - HX_predicted + Ynpu )
1142 if selfA._toStore("SimulatedObservationAtCurrentState") \
1143 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1144 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HX_predicted )
1146 if selfA._parameters["StoreInternalVariables"] \
1147 or selfA._toStore("CostFunctionJ") \
1148 or selfA._toStore("CostFunctionJb") \
1149 or selfA._toStore("CostFunctionJo") \
1150 or selfA._toStore("CurrentOptimum") \
1151 or selfA._toStore("APosterioriCovariance"):
1152 Jb = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
1153 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
1155 selfA.StoredVariables["CostFunctionJb"].store( Jb )
1156 selfA.StoredVariables["CostFunctionJo"].store( Jo )
1157 selfA.StoredVariables["CostFunctionJ" ].store( J )
1159 if selfA._toStore("IndexOfOptimum") \
1160 or selfA._toStore("CurrentOptimum") \
1161 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
1162 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
1163 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
1164 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1165 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
1166 if selfA._toStore("IndexOfOptimum"):
1167 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
1168 if selfA._toStore("CurrentOptimum"):
1169 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
1170 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1171 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
1172 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
1173 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
1174 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
1175 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
1176 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
1177 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
1178 if selfA._toStore("APosterioriCovariance"):
1179 selfA.StoredVariables["APosterioriCovariance"].store( EnsembleErrorCovariance(Xn) )
1180 if selfA._parameters["EstimationOf"] == "Parameters" \
1181 and J < previousJMinimum:
1182 previousJMinimum = J
1184 if selfA._toStore("APosterioriCovariance"):
1185 covarianceXaMin = Pn
1186 # ---> Pour les smoothers
1187 if selfA._toStore("CurrentEnsembleState"):
1188 selfA.StoredVariables["CurrentEnsembleState"].store( Xn )
1190 # Stockage final supplémentaire de l'optimum en estimation de paramètres
1191 # ----------------------------------------------------------------------
1192 if selfA._parameters["EstimationOf"] == "Parameters":
1193 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1194 selfA.StoredVariables["Analysis"].store( XaMin )
1195 if selfA._toStore("APosterioriCovariance"):
1196 selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
1197 if selfA._toStore("BMA"):
1198 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
1202 # ==============================================================================
1203 def ienkf(selfA, Xb, Y, U, HO, EM, CM, R, B, Q, VariantM="IEnKF12",
1204 BnotT=False, _epsilon=1.e-3, _e=1.e-7, _jmax=15000):
1208 if selfA._parameters["EstimationOf"] == "Parameters":
1209 selfA._parameters["StoreInternalVariables"] = True
1213 H = HO["Direct"].appliedControledFormTo
1215 if selfA._parameters["EstimationOf"] == "State":
1216 M = EM["Direct"].appliedControledFormTo
1218 if CM is not None and "Tangent" in CM and U is not None:
1219 Cm = CM["Tangent"].asMatrix(Xb)
1223 # Nombre de pas identique au nombre de pas d'observations
1224 # -------------------------------------------------------
1225 if hasattr(Y,"stepnumber"):
1226 duration = Y.stepnumber()
1227 __p = numpy.cumprod(Y.shape())[-1]
1230 __p = numpy.array(Y).size
1232 # Précalcul des inversions de B et R
1233 # ----------------------------------
1234 if selfA._parameters["StoreInternalVariables"] \
1235 or selfA._toStore("CostFunctionJ") \
1236 or selfA._toStore("CostFunctionJb") \
1237 or selfA._toStore("CostFunctionJo") \
1238 or selfA._toStore("CurrentOptimum") \
1239 or selfA._toStore("APosterioriCovariance"):
1246 __m = selfA._parameters["NumberOfMembers"]
1247 if hasattr(B,"asfullmatrix"): Pn = B.asfullmatrix(__n)
1249 if hasattr(R,"asfullmatrix"): Rn = R.asfullmatrix(__p)
1251 if hasattr(Q,"asfullmatrix"): Qn = Q.asfullmatrix(__n)
1253 Xn = EnsembleOfBackgroundPerturbations( Xb, Pn, __m )
1255 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
1256 selfA.StoredVariables["Analysis"].store( Xb )
1257 if selfA._toStore("APosterioriCovariance"):
1258 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
1261 previousJMinimum = numpy.finfo(float).max
1263 for step in range(duration-1):
1264 if hasattr(Y,"store"):
1265 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
1267 Ynpu = numpy.ravel( Y ).reshape((__p,1))
1270 if hasattr(U,"store") and len(U)>1:
1271 Un = numpy.asmatrix(numpy.ravel( U[step] )).T
1272 elif hasattr(U,"store") and len(U)==1:
1273 Un = numpy.asmatrix(numpy.ravel( U[0] )).T
1275 Un = numpy.asmatrix(numpy.ravel( U )).T
1279 if selfA._parameters["InflationType"] == "MultiplicativeOnBackgroundAnomalies":
1280 Xn = CovarianceInflation( Xn,
1281 selfA._parameters["InflationType"],
1282 selfA._parameters["InflationFactor"],
1285 #--------------------------
1286 if VariantM == "IEnKF12":
1287 Xfm = numpy.ravel(Xn.mean(axis=1, dtype=mfp).astype('float'))
1288 EaX = EnsembleOfAnomalies( Xn ) / math.sqrt(__m-1)
1292 Ta = numpy.identity(__m)
1293 vw = numpy.zeros(__m)
1294 while numpy.linalg.norm(Deltaw) >= _e and __j <= _jmax:
1295 vx1 = (Xfm + EaX @ vw).reshape((__n,1))
1298 E1 = vx1 + _epsilon * EaX
1300 E1 = vx1 + math.sqrt(__m-1) * EaX @ Ta
1302 if selfA._parameters["EstimationOf"] == "State": # Forecast + Q
1303 E2 = M( [(E1[:,i,numpy.newaxis], Un) for i in range(__m)],
1305 returnSerieAsArrayMatrix = True )
1306 elif selfA._parameters["EstimationOf"] == "Parameters":
1307 # --- > Par principe, M = Id
1309 vx2 = E2.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
1310 vy1 = H((vx2, Un)).reshape((__p,1))
1312 HE2 = H( [(E2[:,i,numpy.newaxis], Un) for i in range(__m)],
1314 returnSerieAsArrayMatrix = True )
1315 vy2 = HE2.mean(axis=1, dtype=mfp).astype('float').reshape((__p,1))
1318 EaY = (HE2 - vy2) / _epsilon
1320 EaY = ( (HE2 - vy2) @ numpy.linalg.inv(Ta) ) / math.sqrt(__m-1)
1322 GradJ = numpy.ravel(vw[:,None] - EaY.transpose() @ (RI * ( Ynpu - vy1 )))
1323 mH = numpy.identity(__m) + EaY.transpose() @ (RI * EaY)
1324 Deltaw = - numpy.linalg.solve(mH,GradJ)
1329 Ta = numpy.real(scipy.linalg.sqrtm(numpy.linalg.inv( mH )))
1333 A2 = EnsembleOfAnomalies( E2 )
1336 Ta = numpy.real(scipy.linalg.sqrtm(numpy.linalg.inv( mH )))
1337 A2 = math.sqrt(__m-1) * A2 @ Ta / _epsilon
1340 #--------------------------
1342 raise ValueError("VariantM has to be chosen in the authorized methods list.")
1344 if selfA._parameters["InflationType"] == "MultiplicativeOnAnalysisAnomalies":
1345 Xn = CovarianceInflation( Xn,
1346 selfA._parameters["InflationType"],
1347 selfA._parameters["InflationFactor"],
1350 Xa = Xn.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
1351 #--------------------------
1353 if selfA._parameters["StoreInternalVariables"] \
1354 or selfA._toStore("CostFunctionJ") \
1355 or selfA._toStore("CostFunctionJb") \
1356 or selfA._toStore("CostFunctionJo") \
1357 or selfA._toStore("APosterioriCovariance") \
1358 or selfA._toStore("InnovationAtCurrentAnalysis") \
1359 or selfA._toStore("SimulatedObservationAtCurrentAnalysis") \
1360 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1361 _HXa = numpy.asmatrix(numpy.ravel( H((Xa, Un)) )).T
1362 _Innovation = Ynpu - _HXa
1364 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1365 # ---> avec analysis
1366 selfA.StoredVariables["Analysis"].store( Xa )
1367 if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
1368 selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( _HXa )
1369 if selfA._toStore("InnovationAtCurrentAnalysis"):
1370 selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
1371 # ---> avec current state
1372 if selfA._parameters["StoreInternalVariables"] \
1373 or selfA._toStore("CurrentState"):
1374 selfA.StoredVariables["CurrentState"].store( Xn )
1375 if selfA._toStore("ForecastState"):
1376 selfA.StoredVariables["ForecastState"].store( E2 )
1377 if selfA._toStore("BMA"):
1378 selfA.StoredVariables["BMA"].store( E2 - Xa )
1379 if selfA._toStore("InnovationAtCurrentState"):
1380 selfA.StoredVariables["InnovationAtCurrentState"].store( - HE2 + Ynpu )
1381 if selfA._toStore("SimulatedObservationAtCurrentState") \
1382 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1383 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HE2 )
1385 if selfA._parameters["StoreInternalVariables"] \
1386 or selfA._toStore("CostFunctionJ") \
1387 or selfA._toStore("CostFunctionJb") \
1388 or selfA._toStore("CostFunctionJo") \
1389 or selfA._toStore("CurrentOptimum") \
1390 or selfA._toStore("APosterioriCovariance"):
1391 Jb = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
1392 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
1394 selfA.StoredVariables["CostFunctionJb"].store( Jb )
1395 selfA.StoredVariables["CostFunctionJo"].store( Jo )
1396 selfA.StoredVariables["CostFunctionJ" ].store( J )
1398 if selfA._toStore("IndexOfOptimum") \
1399 or selfA._toStore("CurrentOptimum") \
1400 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
1401 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
1402 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
1403 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1404 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
1405 if selfA._toStore("IndexOfOptimum"):
1406 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
1407 if selfA._toStore("CurrentOptimum"):
1408 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
1409 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1410 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
1411 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
1412 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
1413 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
1414 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
1415 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
1416 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
1417 if selfA._toStore("APosterioriCovariance"):
1418 selfA.StoredVariables["APosterioriCovariance"].store( EnsembleErrorCovariance(Xn) )
1419 if selfA._parameters["EstimationOf"] == "Parameters" \
1420 and J < previousJMinimum:
1421 previousJMinimum = J
1423 if selfA._toStore("APosterioriCovariance"):
1424 covarianceXaMin = Pn
1426 # Stockage final supplémentaire de l'optimum en estimation de paramètres
1427 # ----------------------------------------------------------------------
1428 if selfA._parameters["EstimationOf"] == "Parameters":
1429 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1430 selfA.StoredVariables["Analysis"].store( XaMin )
1431 if selfA._toStore("APosterioriCovariance"):
1432 selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
1433 if selfA._toStore("BMA"):
1434 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
1438 # ==============================================================================
1439 def incr3dvar(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
1447 # Opérateur non-linéaire pour la boucle externe
1448 Hm = HO["Direct"].appliedTo
1450 # Précalcul des inversions de B et R
1454 # Point de démarrage de l'optimisation
1455 Xini = selfA._parameters["InitializationPoint"]
1457 HXb = numpy.asmatrix(numpy.ravel( Hm( Xb ) )).T
1458 Innovation = Y - HXb
1465 Xr = Xini.reshape((-1,1))
1466 while abs(DeltaJ) >= selfA._parameters["CostDecrementTolerance"] and iOuter <= selfA._parameters["MaximumNumberOfSteps"]:
1470 Ht = HO["Tangent"].asMatrix(Xr)
1471 Ht = Ht.reshape(Y.size,Xr.size) # ADAO & check shape
1473 # Définition de la fonction-coût
1474 # ------------------------------
1475 def CostFunction(dx):
1476 _dX = numpy.asmatrix(numpy.ravel( dx )).T
1477 if selfA._parameters["StoreInternalVariables"] or \
1478 selfA._toStore("CurrentState") or \
1479 selfA._toStore("CurrentOptimum"):
1480 selfA.StoredVariables["CurrentState"].store( Xb + _dX )
1482 _HdX = numpy.asmatrix(numpy.ravel( _HdX )).T
1483 _dInnovation = Innovation - _HdX
1484 if selfA._toStore("SimulatedObservationAtCurrentState") or \
1485 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1486 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HXb + _HdX )
1487 if selfA._toStore("InnovationAtCurrentState"):
1488 selfA.StoredVariables["InnovationAtCurrentState"].store( _dInnovation )
1490 Jb = float( 0.5 * _dX.T * BI * _dX )
1491 Jo = float( 0.5 * _dInnovation.T * RI * _dInnovation )
1494 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["CostFunctionJ"]) )
1495 selfA.StoredVariables["CostFunctionJb"].store( Jb )
1496 selfA.StoredVariables["CostFunctionJo"].store( Jo )
1497 selfA.StoredVariables["CostFunctionJ" ].store( J )
1498 if selfA._toStore("IndexOfOptimum") or \
1499 selfA._toStore("CurrentOptimum") or \
1500 selfA._toStore("CostFunctionJAtCurrentOptimum") or \
1501 selfA._toStore("CostFunctionJbAtCurrentOptimum") or \
1502 selfA._toStore("CostFunctionJoAtCurrentOptimum") or \
1503 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1504 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
1505 if selfA._toStore("IndexOfOptimum"):
1506 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
1507 if selfA._toStore("CurrentOptimum"):
1508 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["CurrentState"][IndexMin] )
1509 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1510 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin] )
1511 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
1512 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
1513 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
1514 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
1515 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
1516 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
1519 def GradientOfCostFunction(dx):
1520 _dX = numpy.asmatrix(numpy.ravel( dx )).T
1522 _HdX = numpy.asmatrix(numpy.ravel( _HdX )).T
1523 _dInnovation = Innovation - _HdX
1525 GradJo = - Ht.T @ (RI * _dInnovation)
1526 GradJ = numpy.ravel( GradJb ) + numpy.ravel( GradJo )
1529 # Minimisation de la fonctionnelle
1530 # --------------------------------
1531 nbPreviousSteps = selfA.StoredVariables["CostFunctionJ"].stepnumber()
1533 if selfA._parameters["Minimizer"] == "LBFGSB":
1534 # Minimum, J_optimal, Informations = scipy.optimize.fmin_l_bfgs_b(
1535 if "0.19" <= scipy.version.version <= "1.1.0":
1536 import lbfgsbhlt as optimiseur
1538 import scipy.optimize as optimiseur
1539 Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
1540 func = CostFunction,
1541 x0 = numpy.zeros(Xini.size),
1542 fprime = GradientOfCostFunction,
1544 bounds = selfA._parameters["Bounds"],
1545 maxfun = selfA._parameters["MaximumNumberOfSteps"]-1,
1546 factr = selfA._parameters["CostDecrementTolerance"]*1.e14,
1547 pgtol = selfA._parameters["ProjectedGradientTolerance"],
1548 iprint = selfA._parameters["optiprint"],
1550 nfeval = Informations['funcalls']
1551 rc = Informations['warnflag']
1552 elif selfA._parameters["Minimizer"] == "TNC":
1553 Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
1554 func = CostFunction,
1555 x0 = numpy.zeros(Xini.size),
1556 fprime = GradientOfCostFunction,
1558 bounds = selfA._parameters["Bounds"],
1559 maxfun = selfA._parameters["MaximumNumberOfSteps"],
1560 pgtol = selfA._parameters["ProjectedGradientTolerance"],
1561 ftol = selfA._parameters["CostDecrementTolerance"],
1562 messages = selfA._parameters["optmessages"],
1564 elif selfA._parameters["Minimizer"] == "CG":
1565 Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
1567 x0 = numpy.zeros(Xini.size),
1568 fprime = GradientOfCostFunction,
1570 maxiter = selfA._parameters["MaximumNumberOfSteps"],
1571 gtol = selfA._parameters["GradientNormTolerance"],
1572 disp = selfA._parameters["optdisp"],
1575 elif selfA._parameters["Minimizer"] == "NCG":
1576 Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
1578 x0 = numpy.zeros(Xini.size),
1579 fprime = GradientOfCostFunction,
1581 maxiter = selfA._parameters["MaximumNumberOfSteps"],
1582 avextol = selfA._parameters["CostDecrementTolerance"],
1583 disp = selfA._parameters["optdisp"],
1586 elif selfA._parameters["Minimizer"] == "BFGS":
1587 Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
1589 x0 = numpy.zeros(Xini.size),
1590 fprime = GradientOfCostFunction,
1592 maxiter = selfA._parameters["MaximumNumberOfSteps"],
1593 gtol = selfA._parameters["GradientNormTolerance"],
1594 disp = selfA._parameters["optdisp"],
1598 raise ValueError("Error in Minimizer name: %s"%selfA._parameters["Minimizer"])
1600 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
1601 MinJ = selfA.StoredVariables["CostFunctionJ"][IndexMin]
1603 if selfA._parameters["StoreInternalVariables"] or selfA._toStore("CurrentState"):
1604 Minimum = selfA.StoredVariables["CurrentState"][IndexMin]
1605 Minimum = numpy.asmatrix(numpy.ravel( Minimum )).T
1607 Minimum = Xb + numpy.asmatrix(numpy.ravel( Minimum )).T
1610 DeltaJ = selfA.StoredVariables["CostFunctionJ" ][-1] - J
1611 iOuter = selfA.StoredVariables["CurrentIterationNumber"][-1]
1613 # Obtention de l'analyse
1614 # ----------------------
1617 selfA.StoredVariables["Analysis"].store( Xa )
1619 if selfA._toStore("OMA") or \
1620 selfA._toStore("SigmaObs2") or \
1621 selfA._toStore("SimulationQuantiles") or \
1622 selfA._toStore("SimulatedObservationAtOptimum"):
1623 if selfA._toStore("SimulatedObservationAtCurrentState"):
1624 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin]
1625 elif selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1626 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"][-1]
1630 # Calcul de la covariance d'analyse
1631 # ---------------------------------
1632 if selfA._toStore("APosterioriCovariance") or \
1633 selfA._toStore("SimulationQuantiles") or \
1634 selfA._toStore("JacobianMatrixAtOptimum") or \
1635 selfA._toStore("KalmanGainAtOptimum"):
1636 HtM = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
1637 HtM = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
1638 if selfA._toStore("APosterioriCovariance") or \
1639 selfA._toStore("SimulationQuantiles") or \
1640 selfA._toStore("KalmanGainAtOptimum"):
1641 HaM = HO["Adjoint"].asMatrix(ValueForMethodForm = Xa)
1642 HaM = HaM.reshape(Xa.size,Y.size) # ADAO & check shape
1643 if selfA._toStore("APosterioriCovariance") or \
1644 selfA._toStore("SimulationQuantiles"):
1648 _ee = numpy.matrix(numpy.zeros(nb)).T
1650 _HtEE = numpy.dot(HtM,_ee)
1651 _HtEE = numpy.asmatrix(numpy.ravel( _HtEE )).T
1652 HessienneI.append( numpy.ravel( BI*_ee + HaM * (RI * _HtEE) ) )
1653 HessienneI = numpy.matrix( HessienneI )
1655 if min(A.shape) != max(A.shape):
1656 raise ValueError("The %s a posteriori covariance matrix A is of shape %s, despites it has to be a squared matrix. There is an error in the observation operator, please check it."%(selfA._name,str(A.shape)))
1657 if (numpy.diag(A) < 0).any():
1658 raise ValueError("The %s a posteriori covariance matrix A has at least one negative value on its diagonal. There is an error in the observation operator, please check it."%(selfA._name,))
1659 if logging.getLogger().level < logging.WARNING: # La verification n'a lieu qu'en debug
1661 L = numpy.linalg.cholesky( A )
1663 raise ValueError("The %s a posteriori covariance matrix A is not symmetric positive-definite. Please check your a priori covariances and your observation operator."%(selfA._name,))
1664 if selfA._toStore("APosterioriCovariance"):
1665 selfA.StoredVariables["APosterioriCovariance"].store( A )
1666 if selfA._toStore("JacobianMatrixAtOptimum"):
1667 selfA.StoredVariables["JacobianMatrixAtOptimum"].store( HtM )
1668 if selfA._toStore("KalmanGainAtOptimum"):
1669 if (Y.size <= Xb.size): KG = B * HaM * (R + numpy.dot(HtM, B * HaM)).I
1670 elif (Y.size > Xb.size): KG = (BI + numpy.dot(HaM, RI * HtM)).I * HaM * RI
1671 selfA.StoredVariables["KalmanGainAtOptimum"].store( KG )
1673 # Calculs et/ou stockages supplémentaires
1674 # ---------------------------------------
1675 if selfA._toStore("Innovation") or \
1676 selfA._toStore("SigmaObs2") or \
1677 selfA._toStore("MahalanobisConsistency") or \
1678 selfA._toStore("OMB"):
1680 if selfA._toStore("Innovation"):
1681 selfA.StoredVariables["Innovation"].store( numpy.ravel(d) )
1682 if selfA._toStore("BMA"):
1683 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
1684 if selfA._toStore("OMA"):
1685 selfA.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
1686 if selfA._toStore("OMB"):
1687 selfA.StoredVariables["OMB"].store( numpy.ravel(d) )
1688 if selfA._toStore("SigmaObs2"):
1689 TraceR = R.trace(Y.size)
1690 selfA.StoredVariables["SigmaObs2"].store( float( (d.T * (numpy.asmatrix(numpy.ravel(Y)).T-numpy.asmatrix(numpy.ravel(HXa)).T)) ) / TraceR )
1691 if selfA._toStore("MahalanobisConsistency"):
1692 selfA.StoredVariables["MahalanobisConsistency"].store( float( 2.*MinJ/d.size ) )
1693 if selfA._toStore("SimulationQuantiles"):
1694 QuantilesEstimations(selfA, A, Xa, HXa, Hm, HtM)
1695 if selfA._toStore("SimulatedObservationAtBackground"):
1696 selfA.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
1697 if selfA._toStore("SimulatedObservationAtOptimum"):
1698 selfA.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
1702 # ==============================================================================
1703 def mlef(selfA, Xb, Y, U, HO, EM, CM, R, B, Q, VariantM="MLEF13",
1704 BnotT=False, _epsilon=1.e-3, _e=1.e-7, _jmax=15000):
1706 Maximum Likelihood Ensemble Filter
1708 if selfA._parameters["EstimationOf"] == "Parameters":
1709 selfA._parameters["StoreInternalVariables"] = True
1713 H = HO["Direct"].appliedControledFormTo
1715 if selfA._parameters["EstimationOf"] == "State":
1716 M = EM["Direct"].appliedControledFormTo
1718 if CM is not None and "Tangent" in CM and U is not None:
1719 Cm = CM["Tangent"].asMatrix(Xb)
1723 # Nombre de pas identique au nombre de pas d'observations
1724 # -------------------------------------------------------
1725 if hasattr(Y,"stepnumber"):
1726 duration = Y.stepnumber()
1727 __p = numpy.cumprod(Y.shape())[-1]
1730 __p = numpy.array(Y).size
1732 # Précalcul des inversions de B et R
1733 # ----------------------------------
1734 if selfA._parameters["StoreInternalVariables"] \
1735 or selfA._toStore("CostFunctionJ") \
1736 or selfA._toStore("CostFunctionJb") \
1737 or selfA._toStore("CostFunctionJo") \
1738 or selfA._toStore("CurrentOptimum") \
1739 or selfA._toStore("APosterioriCovariance"):
1746 __m = selfA._parameters["NumberOfMembers"]
1747 if hasattr(B,"asfullmatrix"): Pn = B.asfullmatrix(__n)
1749 if hasattr(R,"asfullmatrix"): Rn = R.asfullmatrix(__p)
1751 Xn = EnsembleOfBackgroundPerturbations( Xb, None, __m )
1753 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
1754 selfA.StoredVariables["Analysis"].store( Xb )
1755 if selfA._toStore("APosterioriCovariance"):
1756 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
1759 previousJMinimum = numpy.finfo(float).max
1761 for step in range(duration-1):
1762 if hasattr(Y,"store"):
1763 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
1765 Ynpu = numpy.ravel( Y ).reshape((__p,1))
1768 if hasattr(U,"store") and len(U)>1:
1769 Un = numpy.asmatrix(numpy.ravel( U[step] )).T
1770 elif hasattr(U,"store") and len(U)==1:
1771 Un = numpy.asmatrix(numpy.ravel( U[0] )).T
1773 Un = numpy.asmatrix(numpy.ravel( U )).T
1777 if selfA._parameters["InflationType"] == "MultiplicativeOnBackgroundAnomalies":
1778 Xn = CovarianceInflation( Xn,
1779 selfA._parameters["InflationType"],
1780 selfA._parameters["InflationFactor"],
1783 if selfA._parameters["EstimationOf"] == "State": # Forecast + Q and observation of forecast
1784 EMX = M( [(Xn[:,i], Un) for i in range(__m)],
1786 returnSerieAsArrayMatrix = True )
1787 Xn_predicted = EnsemblePerturbationWithGivenCovariance( EMX, Q )
1788 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
1789 Cm = Cm.reshape(__n,Un.size) # ADAO & check shape
1790 Xn_predicted = Xn_predicted + Cm * Un
1791 elif selfA._parameters["EstimationOf"] == "Parameters": # Observation of forecast
1792 # --- > Par principe, M = Id, Q = 0
1795 #--------------------------
1796 if VariantM == "MLEF13":
1797 Xfm = numpy.ravel(Xn_predicted.mean(axis=1, dtype=mfp).astype('float'))
1798 EaX = EnsembleOfAnomalies( Xn_predicted, Xfm, 1./math.sqrt(__m-1) )
1799 Ua = numpy.identity(__m)
1803 Ta = numpy.identity(__m)
1804 vw = numpy.zeros(__m)
1805 while numpy.linalg.norm(Deltaw) >= _e and __j <= _jmax:
1806 vx1 = (Xfm + EaX @ vw).reshape((__n,1))
1809 E1 = vx1 + _epsilon * EaX
1811 E1 = vx1 + math.sqrt(__m-1) * EaX @ Ta
1813 HE2 = H( [(E1[:,i,numpy.newaxis], Un) for i in range(__m)],
1815 returnSerieAsArrayMatrix = True )
1816 vy2 = HE2.mean(axis=1, dtype=mfp).astype('float').reshape((__p,1))
1819 EaY = (HE2 - vy2) / _epsilon
1821 EaY = ( (HE2 - vy2) @ numpy.linalg.inv(Ta) ) / math.sqrt(__m-1)
1823 GradJ = numpy.ravel(vw[:,None] - EaY.transpose() @ (RI * ( Ynpu - vy2 )))
1824 mH = numpy.identity(__m) + EaY.transpose() @ (RI * EaY)
1825 Deltaw = - numpy.linalg.solve(mH,GradJ)
1830 Ta = numpy.real(scipy.linalg.sqrtm(numpy.linalg.inv( mH )))
1835 Ta = numpy.real(scipy.linalg.sqrtm(numpy.linalg.inv( mH )))
1837 Xn = vx1 + math.sqrt(__m-1) * EaX @ Ta @ Ua
1838 #--------------------------
1840 raise ValueError("VariantM has to be chosen in the authorized methods list.")
1842 if selfA._parameters["InflationType"] == "MultiplicativeOnAnalysisAnomalies":
1843 Xn = CovarianceInflation( Xn,
1844 selfA._parameters["InflationType"],
1845 selfA._parameters["InflationFactor"],
1848 Xa = Xn.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
1849 #--------------------------
1851 if selfA._parameters["StoreInternalVariables"] \
1852 or selfA._toStore("CostFunctionJ") \
1853 or selfA._toStore("CostFunctionJb") \
1854 or selfA._toStore("CostFunctionJo") \
1855 or selfA._toStore("APosterioriCovariance") \
1856 or selfA._toStore("InnovationAtCurrentAnalysis") \
1857 or selfA._toStore("SimulatedObservationAtCurrentAnalysis") \
1858 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1859 _HXa = numpy.asmatrix(numpy.ravel( H((Xa, Un)) )).T
1860 _Innovation = Ynpu - _HXa
1862 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1863 # ---> avec analysis
1864 selfA.StoredVariables["Analysis"].store( Xa )
1865 if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
1866 selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( _HXa )
1867 if selfA._toStore("InnovationAtCurrentAnalysis"):
1868 selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
1869 # ---> avec current state
1870 if selfA._parameters["StoreInternalVariables"] \
1871 or selfA._toStore("CurrentState"):
1872 selfA.StoredVariables["CurrentState"].store( Xn )
1873 if selfA._toStore("ForecastState"):
1874 selfA.StoredVariables["ForecastState"].store( EMX )
1875 if selfA._toStore("BMA"):
1876 selfA.StoredVariables["BMA"].store( EMX - Xa )
1877 if selfA._toStore("InnovationAtCurrentState"):
1878 selfA.StoredVariables["InnovationAtCurrentState"].store( - HE2 + Ynpu )
1879 if selfA._toStore("SimulatedObservationAtCurrentState") \
1880 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1881 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HE2 )
1883 if selfA._parameters["StoreInternalVariables"] \
1884 or selfA._toStore("CostFunctionJ") \
1885 or selfA._toStore("CostFunctionJb") \
1886 or selfA._toStore("CostFunctionJo") \
1887 or selfA._toStore("CurrentOptimum") \
1888 or selfA._toStore("APosterioriCovariance"):
1889 Jb = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
1890 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
1892 selfA.StoredVariables["CostFunctionJb"].store( Jb )
1893 selfA.StoredVariables["CostFunctionJo"].store( Jo )
1894 selfA.StoredVariables["CostFunctionJ" ].store( J )
1896 if selfA._toStore("IndexOfOptimum") \
1897 or selfA._toStore("CurrentOptimum") \
1898 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
1899 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
1900 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
1901 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1902 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
1903 if selfA._toStore("IndexOfOptimum"):
1904 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
1905 if selfA._toStore("CurrentOptimum"):
1906 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
1907 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1908 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
1909 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
1910 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
1911 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
1912 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
1913 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
1914 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
1915 if selfA._toStore("APosterioriCovariance"):
1916 selfA.StoredVariables["APosterioriCovariance"].store( EnsembleErrorCovariance(Xn) )
1917 if selfA._parameters["EstimationOf"] == "Parameters" \
1918 and J < previousJMinimum:
1919 previousJMinimum = J
1921 if selfA._toStore("APosterioriCovariance"):
1922 covarianceXaMin = Pn
1924 # Stockage final supplémentaire de l'optimum en estimation de paramètres
1925 # ----------------------------------------------------------------------
1926 if selfA._parameters["EstimationOf"] == "Parameters":
1927 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1928 selfA.StoredVariables["Analysis"].store( XaMin )
1929 if selfA._toStore("APosterioriCovariance"):
1930 selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
1931 if selfA._toStore("BMA"):
1932 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
1936 # ==============================================================================
1948 Implémentation informatique de l'algorithme MMQR, basée sur la publication :
1949 David R. Hunter, Kenneth Lange, "Quantile Regression via an MM Algorithm",
1950 Journal of Computational and Graphical Statistics, 9, 1, pp.60-77, 2000.
1953 # Recuperation des donnees et informations initiales
1954 # --------------------------------------------------
1955 variables = numpy.ravel( x0 )
1956 mesures = numpy.ravel( y )
1957 increment = sys.float_info[0]
1960 quantile = float(quantile)
1962 # Calcul des parametres du MM
1963 # ---------------------------
1964 tn = float(toler) / n
1965 e0 = -tn / math.log(tn)
1966 epsilon = (e0-tn)/(1+math.log(e0))
1968 # Calculs d'initialisation
1969 # ------------------------
1970 residus = mesures - numpy.ravel( func( variables ) )
1971 poids = 1./(epsilon+numpy.abs(residus))
1972 veps = 1. - 2. * quantile - residus * poids
1973 lastsurrogate = -numpy.sum(residus*veps) - (1.-2.*quantile)*numpy.sum(residus)
1976 # Recherche iterative
1977 # -------------------
1978 while (increment > toler) and (iteration < maxfun) :
1981 Derivees = numpy.array(fprime(variables))
1982 Derivees = Derivees.reshape(n,p) # Necessaire pour remettre en place la matrice si elle passe par des tuyaux YACS
1983 DeriveesT = Derivees.transpose()
1984 M = numpy.dot( DeriveesT , (numpy.array(numpy.matrix(p*[poids,]).T)*Derivees) )
1985 SM = numpy.transpose(numpy.dot( DeriveesT , veps ))
1986 step = - numpy.linalg.lstsq( M, SM, rcond=-1 )[0]
1988 variables = variables + step
1989 if bounds is not None:
1990 # Attention : boucle infinie à éviter si un intervalle est trop petit
1991 while( (variables < numpy.ravel(numpy.asmatrix(bounds)[:,0])).any() or (variables > numpy.ravel(numpy.asmatrix(bounds)[:,1])).any() ):
1993 variables = variables - step
1994 residus = mesures - numpy.ravel( func(variables) )
1995 surrogate = numpy.sum(residus**2 * poids) + (4.*quantile-2.) * numpy.sum(residus)
1997 while ( (surrogate > lastsurrogate) and ( max(list(numpy.abs(step))) > 1.e-16 ) ) :
1999 variables = variables - step
2000 residus = mesures - numpy.ravel( func(variables) )
2001 surrogate = numpy.sum(residus**2 * poids) + (4.*quantile-2.) * numpy.sum(residus)
2003 increment = lastsurrogate-surrogate
2004 poids = 1./(epsilon+numpy.abs(residus))
2005 veps = 1. - 2. * quantile - residus * poids
2006 lastsurrogate = -numpy.sum(residus * veps) - (1.-2.*quantile)*numpy.sum(residus)
2010 Ecart = quantile * numpy.sum(residus) - numpy.sum( residus[residus<0] )
2012 return variables, Ecart, [n,p,iteration,increment,0]
2014 # ==============================================================================
2015 def multi3dvar(selfA, Xb, Y, U, HO, EM, CM, R, B, Q, oneCycle):
2017 3DVAR multi-pas et multi-méthodes
2022 Xn = numpy.ravel(Xb).reshape((-1,1))
2024 if selfA._parameters["EstimationOf"] == "State":
2025 M = EM["Direct"].appliedTo
2027 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
2028 selfA.StoredVariables["Analysis"].store( Xn )
2029 if selfA._toStore("APosterioriCovariance"):
2030 if hasattr(B,"asfullmatrix"): Pn = B.asfullmatrix(Xn.size)
2032 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
2033 if selfA._toStore("ForecastState"):
2034 selfA.StoredVariables["ForecastState"].store( Xn )
2036 if hasattr(Y,"stepnumber"):
2037 duration = Y.stepnumber()
2043 for step in range(duration-1):
2044 if hasattr(Y,"store"):
2045 Ynpu = numpy.ravel( Y[step+1] ).reshape((-1,1))
2047 Ynpu = numpy.ravel( Y ).reshape((-1,1))
2049 if selfA._parameters["EstimationOf"] == "State": # Forecast
2050 Xn = selfA.StoredVariables["Analysis"][-1]
2051 Xn_predicted = M( Xn )
2052 if selfA._toStore("ForecastState"):
2053 selfA.StoredVariables["ForecastState"].store( Xn_predicted )
2054 elif selfA._parameters["EstimationOf"] == "Parameters": # No forecast
2055 # --- > Par principe, M = Id, Q = 0
2057 Xn_predicted = numpy.ravel(Xn_predicted).reshape((-1,1))
2059 oneCycle(selfA, Xn_predicted, Ynpu, U, HO, None, None, R, B, None)
2063 # ==============================================================================
2064 def psas3dvar(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
2073 Hm = HO["Direct"].appliedTo
2075 # Utilisation éventuelle d'un vecteur H(Xb) précalculé
2076 if HO["AppliedInX"] is not None and "HXb" in HO["AppliedInX"]:
2077 HXb = Hm( Xb, HO["AppliedInX"]["HXb"] )
2080 HXb = numpy.asmatrix(numpy.ravel( HXb )).T
2081 if Y.size != HXb.size:
2082 raise ValueError("The size %i of observations Y and %i of observed calculation H(X) are different, they have to be identical."%(Y.size,HXb.size))
2083 if max(Y.shape) != max(HXb.shape):
2084 raise ValueError("The shapes %s of observations Y and %s of observed calculation H(X) are different, they have to be identical."%(Y.shape,HXb.shape))
2086 if selfA._toStore("JacobianMatrixAtBackground"):
2087 HtMb = HO["Tangent"].asMatrix(ValueForMethodForm = Xb)
2088 HtMb = HtMb.reshape(Y.size,Xb.size) # ADAO & check shape
2089 selfA.StoredVariables["JacobianMatrixAtBackground"].store( HtMb )
2091 Ht = HO["Tangent"].asMatrix(Xb)
2093 HBHTpR = R + Ht * BHT
2094 Innovation = Y - HXb
2096 # Point de démarrage de l'optimisation
2097 Xini = numpy.zeros(Xb.shape)
2099 # Définition de la fonction-coût
2100 # ------------------------------
2101 def CostFunction(w):
2102 _W = numpy.asmatrix(numpy.ravel( w )).T
2103 if selfA._parameters["StoreInternalVariables"] or \
2104 selfA._toStore("CurrentState") or \
2105 selfA._toStore("CurrentOptimum"):
2106 selfA.StoredVariables["CurrentState"].store( Xb + BHT * _W )
2107 if selfA._toStore("SimulatedObservationAtCurrentState") or \
2108 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2109 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( Hm( Xb + BHT * _W ) )
2110 if selfA._toStore("InnovationAtCurrentState"):
2111 selfA.StoredVariables["InnovationAtCurrentState"].store( Innovation )
2113 Jb = float( 0.5 * _W.T * HBHTpR * _W )
2114 Jo = float( - _W.T * Innovation )
2117 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["CostFunctionJ"]) )
2118 selfA.StoredVariables["CostFunctionJb"].store( Jb )
2119 selfA.StoredVariables["CostFunctionJo"].store( Jo )
2120 selfA.StoredVariables["CostFunctionJ" ].store( J )
2121 if selfA._toStore("IndexOfOptimum") or \
2122 selfA._toStore("CurrentOptimum") or \
2123 selfA._toStore("CostFunctionJAtCurrentOptimum") or \
2124 selfA._toStore("CostFunctionJbAtCurrentOptimum") or \
2125 selfA._toStore("CostFunctionJoAtCurrentOptimum") or \
2126 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2127 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
2128 if selfA._toStore("IndexOfOptimum"):
2129 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
2130 if selfA._toStore("CurrentOptimum"):
2131 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["CurrentState"][IndexMin] )
2132 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2133 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin] )
2134 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
2135 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
2136 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
2137 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
2138 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
2139 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
2142 def GradientOfCostFunction(w):
2143 _W = numpy.asmatrix(numpy.ravel( w )).T
2144 GradJb = HBHTpR * _W
2145 GradJo = - Innovation
2146 GradJ = numpy.ravel( GradJb ) + numpy.ravel( GradJo )
2149 # Minimisation de la fonctionnelle
2150 # --------------------------------
2151 nbPreviousSteps = selfA.StoredVariables["CostFunctionJ"].stepnumber()
2153 if selfA._parameters["Minimizer"] == "LBFGSB":
2154 if "0.19" <= scipy.version.version <= "1.1.0":
2155 import lbfgsbhlt as optimiseur
2157 import scipy.optimize as optimiseur
2158 Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
2159 func = CostFunction,
2161 fprime = GradientOfCostFunction,
2163 bounds = selfA._parameters["Bounds"],
2164 maxfun = selfA._parameters["MaximumNumberOfSteps"]-1,
2165 factr = selfA._parameters["CostDecrementTolerance"]*1.e14,
2166 pgtol = selfA._parameters["ProjectedGradientTolerance"],
2167 iprint = selfA._parameters["optiprint"],
2169 nfeval = Informations['funcalls']
2170 rc = Informations['warnflag']
2171 elif selfA._parameters["Minimizer"] == "TNC":
2172 Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
2173 func = CostFunction,
2175 fprime = GradientOfCostFunction,
2177 bounds = selfA._parameters["Bounds"],
2178 maxfun = selfA._parameters["MaximumNumberOfSteps"],
2179 pgtol = selfA._parameters["ProjectedGradientTolerance"],
2180 ftol = selfA._parameters["CostDecrementTolerance"],
2181 messages = selfA._parameters["optmessages"],
2183 elif selfA._parameters["Minimizer"] == "CG":
2184 Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
2187 fprime = GradientOfCostFunction,
2189 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2190 gtol = selfA._parameters["GradientNormTolerance"],
2191 disp = selfA._parameters["optdisp"],
2194 elif selfA._parameters["Minimizer"] == "NCG":
2195 Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
2198 fprime = GradientOfCostFunction,
2200 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2201 avextol = selfA._parameters["CostDecrementTolerance"],
2202 disp = selfA._parameters["optdisp"],
2205 elif selfA._parameters["Minimizer"] == "BFGS":
2206 Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
2209 fprime = GradientOfCostFunction,
2211 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2212 gtol = selfA._parameters["GradientNormTolerance"],
2213 disp = selfA._parameters["optdisp"],
2217 raise ValueError("Error in Minimizer name: %s"%selfA._parameters["Minimizer"])
2219 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
2220 MinJ = selfA.StoredVariables["CostFunctionJ"][IndexMin]
2222 # Correction pour pallier a un bug de TNC sur le retour du Minimum
2223 # ----------------------------------------------------------------
2224 if selfA._parameters["StoreInternalVariables"] or selfA._toStore("CurrentState"):
2225 Minimum = selfA.StoredVariables["CurrentState"][IndexMin]
2226 Minimum = numpy.asmatrix(numpy.ravel( Minimum )).T
2228 Minimum = Xb + BHT * numpy.asmatrix(numpy.ravel( Minimum )).T
2230 # Obtention de l'analyse
2231 # ----------------------
2234 selfA.StoredVariables["Analysis"].store( Xa )
2236 if selfA._toStore("OMA") or \
2237 selfA._toStore("SigmaObs2") or \
2238 selfA._toStore("SimulationQuantiles") or \
2239 selfA._toStore("SimulatedObservationAtOptimum"):
2240 if selfA._toStore("SimulatedObservationAtCurrentState"):
2241 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin]
2242 elif selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2243 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"][-1]
2247 # Calcul de la covariance d'analyse
2248 # ---------------------------------
2249 if selfA._toStore("APosterioriCovariance") or \
2250 selfA._toStore("SimulationQuantiles") or \
2251 selfA._toStore("JacobianMatrixAtOptimum") or \
2252 selfA._toStore("KalmanGainAtOptimum"):
2253 HtM = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
2254 HtM = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
2255 if selfA._toStore("APosterioriCovariance") or \
2256 selfA._toStore("SimulationQuantiles") or \
2257 selfA._toStore("KalmanGainAtOptimum"):
2258 HaM = HO["Adjoint"].asMatrix(ValueForMethodForm = Xa)
2259 HaM = HaM.reshape(Xa.size,Y.size) # ADAO & check shape
2260 if selfA._toStore("APosterioriCovariance") or \
2261 selfA._toStore("SimulationQuantiles"):
2267 _ee = numpy.matrix(numpy.zeros(nb)).T
2269 _HtEE = numpy.dot(HtM,_ee)
2270 _HtEE = numpy.asmatrix(numpy.ravel( _HtEE )).T
2271 HessienneI.append( numpy.ravel( BI*_ee + HaM * (RI * _HtEE) ) )
2272 HessienneI = numpy.matrix( HessienneI )
2274 if min(A.shape) != max(A.shape):
2275 raise ValueError("The %s a posteriori covariance matrix A is of shape %s, despites it has to be a squared matrix. There is an error in the observation operator, please check it."%(selfA._name,str(A.shape)))
2276 if (numpy.diag(A) < 0).any():
2277 raise ValueError("The %s a posteriori covariance matrix A has at least one negative value on its diagonal. There is an error in the observation operator, please check it."%(selfA._name,))
2278 if logging.getLogger().level < logging.WARNING: # La verification n'a lieu qu'en debug
2280 L = numpy.linalg.cholesky( A )
2282 raise ValueError("The %s a posteriori covariance matrix A is not symmetric positive-definite. Please check your a priori covariances and your observation operator."%(selfA._name,))
2283 if selfA._toStore("APosterioriCovariance"):
2284 selfA.StoredVariables["APosterioriCovariance"].store( A )
2285 if selfA._toStore("JacobianMatrixAtOptimum"):
2286 selfA.StoredVariables["JacobianMatrixAtOptimum"].store( HtM )
2287 if selfA._toStore("KalmanGainAtOptimum"):
2288 if (Y.size <= Xb.size): KG = B * HaM * (R + numpy.dot(HtM, B * HaM)).I
2289 elif (Y.size > Xb.size): KG = (BI + numpy.dot(HaM, RI * HtM)).I * HaM * RI
2290 selfA.StoredVariables["KalmanGainAtOptimum"].store( KG )
2292 # Calculs et/ou stockages supplémentaires
2293 # ---------------------------------------
2294 if selfA._toStore("Innovation") or \
2295 selfA._toStore("SigmaObs2") or \
2296 selfA._toStore("MahalanobisConsistency") or \
2297 selfA._toStore("OMB"):
2299 if selfA._toStore("Innovation"):
2300 selfA.StoredVariables["Innovation"].store( numpy.ravel(d) )
2301 if selfA._toStore("BMA"):
2302 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
2303 if selfA._toStore("OMA"):
2304 selfA.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
2305 if selfA._toStore("OMB"):
2306 selfA.StoredVariables["OMB"].store( numpy.ravel(d) )
2307 if selfA._toStore("SigmaObs2"):
2308 TraceR = R.trace(Y.size)
2309 selfA.StoredVariables["SigmaObs2"].store( float( (d.T * (numpy.asmatrix(numpy.ravel(Y)).T-numpy.asmatrix(numpy.ravel(HXa)).T)) ) / TraceR )
2310 if selfA._toStore("MahalanobisConsistency"):
2311 selfA.StoredVariables["MahalanobisConsistency"].store( float( 2.*MinJ/d.size ) )
2312 if selfA._toStore("SimulationQuantiles"):
2313 QuantilesEstimations(selfA, A, Xa, HXa, Hm, HtM)
2314 if selfA._toStore("SimulatedObservationAtBackground"):
2315 selfA.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
2316 if selfA._toStore("SimulatedObservationAtOptimum"):
2317 selfA.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
2321 # ==============================================================================
2322 def senkf(selfA, Xb, Y, U, HO, EM, CM, R, B, Q, VariantM="KalmanFilterFormula"):
2326 if selfA._parameters["EstimationOf"] == "Parameters":
2327 selfA._parameters["StoreInternalVariables"] = True
2330 H = HO["Direct"].appliedControledFormTo
2332 if selfA._parameters["EstimationOf"] == "State":
2333 M = EM["Direct"].appliedControledFormTo
2335 if CM is not None and "Tangent" in CM and U is not None:
2336 Cm = CM["Tangent"].asMatrix(Xb)
2340 # Durée d'observation et tailles
2341 if hasattr(Y,"stepnumber"):
2342 duration = Y.stepnumber()
2343 __p = numpy.cumprod(Y.shape())[-1]
2346 __p = numpy.array(Y).size
2348 # Précalcul des inversions de B et R
2349 if selfA._parameters["StoreInternalVariables"] \
2350 or selfA._toStore("CostFunctionJ") \
2351 or selfA._toStore("CostFunctionJb") \
2352 or selfA._toStore("CostFunctionJo") \
2353 or selfA._toStore("CurrentOptimum") \
2354 or selfA._toStore("APosterioriCovariance"):
2359 __m = selfA._parameters["NumberOfMembers"]
2361 if hasattr(B,"asfullmatrix"): Pn = B.asfullmatrix(__n)
2363 if hasattr(R,"asfullmatrix"): Rn = R.asfullmatrix(__p)
2365 Xn = EnsembleOfBackgroundPerturbations( Xb, None, __m )
2367 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
2368 selfA.StoredVariables["Analysis"].store( Xb )
2369 if selfA._toStore("APosterioriCovariance"):
2370 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
2373 previousJMinimum = numpy.finfo(float).max
2375 for step in range(duration-1):
2376 if hasattr(Y,"store"):
2377 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
2379 Ynpu = numpy.ravel( Y ).reshape((__p,1))
2382 if hasattr(U,"store") and len(U)>1:
2383 Un = numpy.asmatrix(numpy.ravel( U[step] )).T
2384 elif hasattr(U,"store") and len(U)==1:
2385 Un = numpy.asmatrix(numpy.ravel( U[0] )).T
2387 Un = numpy.asmatrix(numpy.ravel( U )).T
2391 if selfA._parameters["InflationType"] == "MultiplicativeOnBackgroundAnomalies":
2392 Xn = CovarianceInflation( Xn,
2393 selfA._parameters["InflationType"],
2394 selfA._parameters["InflationFactor"],
2397 if selfA._parameters["EstimationOf"] == "State": # Forecast + Q and observation of forecast
2398 EMX = M( [(Xn[:,i], Un) for i in range(__m)],
2400 returnSerieAsArrayMatrix = True )
2401 Xn_predicted = EnsemblePerturbationWithGivenCovariance( EMX, Q )
2402 HX_predicted = H( [(Xn_predicted[:,i], Un) for i in range(__m)],
2404 returnSerieAsArrayMatrix = True )
2405 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
2406 Cm = Cm.reshape(__n,Un.size) # ADAO & check shape
2407 Xn_predicted = Xn_predicted + Cm * Un
2408 elif selfA._parameters["EstimationOf"] == "Parameters": # Observation of forecast
2409 # --- > Par principe, M = Id, Q = 0
2411 HX_predicted = H( [(Xn_predicted[:,i], Un) for i in range(__m)],
2413 returnSerieAsArrayMatrix = True )
2415 # Mean of forecast and observation of forecast
2416 Xfm = Xn_predicted.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
2417 Hfm = HX_predicted.mean(axis=1, dtype=mfp).astype('float').reshape((__p,1))
2419 #--------------------------
2420 if VariantM == "KalmanFilterFormula05":
2421 PfHT, HPfHT = 0., 0.
2422 for i in range(__m):
2423 Exfi = Xn_predicted[:,i].reshape((__n,1)) - Xfm
2424 Eyfi = HX_predicted[:,i].reshape((__p,1)) - Hfm
2425 PfHT += Exfi * Eyfi.T
2426 HPfHT += Eyfi * Eyfi.T
2427 PfHT = (1./(__m-1)) * PfHT
2428 HPfHT = (1./(__m-1)) * HPfHT
2429 Kn = PfHT * ( R + HPfHT ).I
2432 for i in range(__m):
2433 ri = numpy.random.multivariate_normal(numpy.zeros(__p), Rn)
2434 Xn[:,i] = numpy.ravel(Xn_predicted[:,i]) + Kn @ (numpy.ravel(Ynpu) + ri - HX_predicted[:,i])
2435 #--------------------------
2436 elif VariantM == "KalmanFilterFormula16":
2437 EpY = EnsembleOfCenteredPerturbations(Ynpu, Rn, __m)
2438 EpYm = EpY.mean(axis=1, dtype=mfp).astype('float').reshape((__p,1))
2440 EaX = EnsembleOfAnomalies( Xn_predicted ) / math.sqrt(__m-1)
2441 EaY = (HX_predicted - Hfm - EpY + EpYm) / math.sqrt(__m-1)
2443 Kn = EaX @ EaY.T @ numpy.linalg.inv( EaY @ EaY.T)
2445 for i in range(__m):
2446 Xn[:,i] = numpy.ravel(Xn_predicted[:,i]) + Kn @ (numpy.ravel(EpY[:,i]) - HX_predicted[:,i])
2447 #--------------------------
2449 raise ValueError("VariantM has to be chosen in the authorized methods list.")
2451 if selfA._parameters["InflationType"] == "MultiplicativeOnAnalysisAnomalies":
2452 Xn = CovarianceInflation( Xn,
2453 selfA._parameters["InflationType"],
2454 selfA._parameters["InflationFactor"],
2457 Xa = Xn.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
2458 #--------------------------
2460 if selfA._parameters["StoreInternalVariables"] \
2461 or selfA._toStore("CostFunctionJ") \
2462 or selfA._toStore("CostFunctionJb") \
2463 or selfA._toStore("CostFunctionJo") \
2464 or selfA._toStore("APosterioriCovariance") \
2465 or selfA._toStore("InnovationAtCurrentAnalysis") \
2466 or selfA._toStore("SimulatedObservationAtCurrentAnalysis") \
2467 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2468 _HXa = numpy.asmatrix(numpy.ravel( H((Xa, Un)) )).T
2469 _Innovation = Ynpu - _HXa
2471 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
2472 # ---> avec analysis
2473 selfA.StoredVariables["Analysis"].store( Xa )
2474 if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
2475 selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( _HXa )
2476 if selfA._toStore("InnovationAtCurrentAnalysis"):
2477 selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
2478 # ---> avec current state
2479 if selfA._parameters["StoreInternalVariables"] \
2480 or selfA._toStore("CurrentState"):
2481 selfA.StoredVariables["CurrentState"].store( Xn )
2482 if selfA._toStore("ForecastState"):
2483 selfA.StoredVariables["ForecastState"].store( EMX )
2484 if selfA._toStore("BMA"):
2485 selfA.StoredVariables["BMA"].store( EMX - Xa )
2486 if selfA._toStore("InnovationAtCurrentState"):
2487 selfA.StoredVariables["InnovationAtCurrentState"].store( - HX_predicted + Ynpu )
2488 if selfA._toStore("SimulatedObservationAtCurrentState") \
2489 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2490 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HX_predicted )
2492 if selfA._parameters["StoreInternalVariables"] \
2493 or selfA._toStore("CostFunctionJ") \
2494 or selfA._toStore("CostFunctionJb") \
2495 or selfA._toStore("CostFunctionJo") \
2496 or selfA._toStore("CurrentOptimum") \
2497 or selfA._toStore("APosterioriCovariance"):
2498 Jb = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
2499 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
2501 selfA.StoredVariables["CostFunctionJb"].store( Jb )
2502 selfA.StoredVariables["CostFunctionJo"].store( Jo )
2503 selfA.StoredVariables["CostFunctionJ" ].store( J )
2505 if selfA._toStore("IndexOfOptimum") \
2506 or selfA._toStore("CurrentOptimum") \
2507 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
2508 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
2509 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
2510 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2511 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
2512 if selfA._toStore("IndexOfOptimum"):
2513 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
2514 if selfA._toStore("CurrentOptimum"):
2515 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
2516 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2517 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
2518 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
2519 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
2520 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
2521 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
2522 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
2523 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
2524 if selfA._toStore("APosterioriCovariance"):
2525 selfA.StoredVariables["APosterioriCovariance"].store( EnsembleErrorCovariance(Xn) )
2526 if selfA._parameters["EstimationOf"] == "Parameters" \
2527 and J < previousJMinimum:
2528 previousJMinimum = J
2530 if selfA._toStore("APosterioriCovariance"):
2531 covarianceXaMin = Pn
2533 # Stockage final supplémentaire de l'optimum en estimation de paramètres
2534 # ----------------------------------------------------------------------
2535 if selfA._parameters["EstimationOf"] == "Parameters":
2536 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
2537 selfA.StoredVariables["Analysis"].store( XaMin )
2538 if selfA._toStore("APosterioriCovariance"):
2539 selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
2540 if selfA._toStore("BMA"):
2541 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
2545 # ==============================================================================
2546 def std3dvar(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
2555 Hm = HO["Direct"].appliedTo
2556 Ha = HO["Adjoint"].appliedInXTo
2558 # Utilisation éventuelle d'un vecteur H(Xb) précalculé
2559 if HO["AppliedInX"] is not None and "HXb" in HO["AppliedInX"]:
2560 HXb = Hm( Xb, HO["AppliedInX"]["HXb"] )
2563 HXb = numpy.asmatrix(numpy.ravel( HXb )).T
2564 if Y.size != HXb.size:
2565 raise ValueError("The size %i of observations Y and %i of observed calculation H(X) are different, they have to be identical."%(Y.size,HXb.size))
2566 if max(Y.shape) != max(HXb.shape):
2567 raise ValueError("The shapes %s of observations Y and %s of observed calculation H(X) are different, they have to be identical."%(Y.shape,HXb.shape))
2569 if selfA._toStore("JacobianMatrixAtBackground"):
2570 HtMb = HO["Tangent"].asMatrix(ValueForMethodForm = Xb)
2571 HtMb = HtMb.reshape(Y.size,Xb.size) # ADAO & check shape
2572 selfA.StoredVariables["JacobianMatrixAtBackground"].store( HtMb )
2574 # Précalcul des inversions de B et R
2578 # Point de démarrage de l'optimisation
2579 Xini = selfA._parameters["InitializationPoint"]
2581 # Définition de la fonction-coût
2582 # ------------------------------
2583 def CostFunction(x):
2584 _X = numpy.asmatrix(numpy.ravel( x )).T
2585 if selfA._parameters["StoreInternalVariables"] or \
2586 selfA._toStore("CurrentState") or \
2587 selfA._toStore("CurrentOptimum"):
2588 selfA.StoredVariables["CurrentState"].store( _X )
2590 _HX = numpy.asmatrix(numpy.ravel( _HX )).T
2591 _Innovation = Y - _HX
2592 if selfA._toStore("SimulatedObservationAtCurrentState") or \
2593 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2594 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( _HX )
2595 if selfA._toStore("InnovationAtCurrentState"):
2596 selfA.StoredVariables["InnovationAtCurrentState"].store( _Innovation )
2598 Jb = float( 0.5 * (_X - Xb).T * BI * (_X - Xb) )
2599 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
2602 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["CostFunctionJ"]) )
2603 selfA.StoredVariables["CostFunctionJb"].store( Jb )
2604 selfA.StoredVariables["CostFunctionJo"].store( Jo )
2605 selfA.StoredVariables["CostFunctionJ" ].store( J )
2606 if selfA._toStore("IndexOfOptimum") or \
2607 selfA._toStore("CurrentOptimum") or \
2608 selfA._toStore("CostFunctionJAtCurrentOptimum") or \
2609 selfA._toStore("CostFunctionJbAtCurrentOptimum") or \
2610 selfA._toStore("CostFunctionJoAtCurrentOptimum") or \
2611 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2612 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
2613 if selfA._toStore("IndexOfOptimum"):
2614 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
2615 if selfA._toStore("CurrentOptimum"):
2616 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["CurrentState"][IndexMin] )
2617 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2618 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin] )
2619 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
2620 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
2621 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
2622 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
2623 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
2624 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
2627 def GradientOfCostFunction(x):
2628 _X = numpy.asmatrix(numpy.ravel( x )).T
2630 _HX = numpy.asmatrix(numpy.ravel( _HX )).T
2631 GradJb = BI * (_X - Xb)
2632 GradJo = - Ha( (_X, RI * (Y - _HX)) )
2633 GradJ = numpy.ravel( GradJb ) + numpy.ravel( GradJo )
2636 # Minimisation de la fonctionnelle
2637 # --------------------------------
2638 nbPreviousSteps = selfA.StoredVariables["CostFunctionJ"].stepnumber()
2640 if selfA._parameters["Minimizer"] == "LBFGSB":
2641 if "0.19" <= scipy.version.version <= "1.1.0":
2642 import lbfgsbhlt as optimiseur
2644 import scipy.optimize as optimiseur
2645 Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
2646 func = CostFunction,
2648 fprime = GradientOfCostFunction,
2650 bounds = selfA._parameters["Bounds"],
2651 maxfun = selfA._parameters["MaximumNumberOfSteps"]-1,
2652 factr = selfA._parameters["CostDecrementTolerance"]*1.e14,
2653 pgtol = selfA._parameters["ProjectedGradientTolerance"],
2654 iprint = selfA._parameters["optiprint"],
2656 nfeval = Informations['funcalls']
2657 rc = Informations['warnflag']
2658 elif selfA._parameters["Minimizer"] == "TNC":
2659 Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
2660 func = CostFunction,
2662 fprime = GradientOfCostFunction,
2664 bounds = selfA._parameters["Bounds"],
2665 maxfun = selfA._parameters["MaximumNumberOfSteps"],
2666 pgtol = selfA._parameters["ProjectedGradientTolerance"],
2667 ftol = selfA._parameters["CostDecrementTolerance"],
2668 messages = selfA._parameters["optmessages"],
2670 elif selfA._parameters["Minimizer"] == "CG":
2671 Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
2674 fprime = GradientOfCostFunction,
2676 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2677 gtol = selfA._parameters["GradientNormTolerance"],
2678 disp = selfA._parameters["optdisp"],
2681 elif selfA._parameters["Minimizer"] == "NCG":
2682 Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
2685 fprime = GradientOfCostFunction,
2687 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2688 avextol = selfA._parameters["CostDecrementTolerance"],
2689 disp = selfA._parameters["optdisp"],
2692 elif selfA._parameters["Minimizer"] == "BFGS":
2693 Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
2696 fprime = GradientOfCostFunction,
2698 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2699 gtol = selfA._parameters["GradientNormTolerance"],
2700 disp = selfA._parameters["optdisp"],
2704 raise ValueError("Error in Minimizer name: %s"%selfA._parameters["Minimizer"])
2706 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
2707 MinJ = selfA.StoredVariables["CostFunctionJ"][IndexMin]
2709 # Correction pour pallier a un bug de TNC sur le retour du Minimum
2710 # ----------------------------------------------------------------
2711 if selfA._parameters["StoreInternalVariables"] or selfA._toStore("CurrentState"):
2712 Minimum = selfA.StoredVariables["CurrentState"][IndexMin]
2714 # Obtention de l'analyse
2715 # ----------------------
2716 Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
2718 selfA.StoredVariables["Analysis"].store( Xa )
2720 if selfA._toStore("OMA") or \
2721 selfA._toStore("SigmaObs2") or \
2722 selfA._toStore("SimulationQuantiles") or \
2723 selfA._toStore("SimulatedObservationAtOptimum"):
2724 if selfA._toStore("SimulatedObservationAtCurrentState"):
2725 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin]
2726 elif selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2727 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"][-1]
2731 # Calcul de la covariance d'analyse
2732 # ---------------------------------
2733 if selfA._toStore("APosterioriCovariance") or \
2734 selfA._toStore("SimulationQuantiles") or \
2735 selfA._toStore("JacobianMatrixAtOptimum") or \
2736 selfA._toStore("KalmanGainAtOptimum"):
2737 HtM = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
2738 HtM = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
2739 if selfA._toStore("APosterioriCovariance") or \
2740 selfA._toStore("SimulationQuantiles") or \
2741 selfA._toStore("KalmanGainAtOptimum"):
2742 HaM = HO["Adjoint"].asMatrix(ValueForMethodForm = Xa)
2743 HaM = HaM.reshape(Xa.size,Y.size) # ADAO & check shape
2744 if selfA._toStore("APosterioriCovariance") or \
2745 selfA._toStore("SimulationQuantiles"):
2749 _ee = numpy.matrix(numpy.zeros(nb)).T
2751 _HtEE = numpy.dot(HtM,_ee)
2752 _HtEE = numpy.asmatrix(numpy.ravel( _HtEE )).T
2753 HessienneI.append( numpy.ravel( BI*_ee + HaM * (RI * _HtEE) ) )
2754 HessienneI = numpy.matrix( HessienneI )
2756 if min(A.shape) != max(A.shape):
2757 raise ValueError("The %s a posteriori covariance matrix A is of shape %s, despites it has to be a squared matrix. There is an error in the observation operator, please check it."%(selfA._name,str(A.shape)))
2758 if (numpy.diag(A) < 0).any():
2759 raise ValueError("The %s a posteriori covariance matrix A has at least one negative value on its diagonal. There is an error in the observation operator, please check it."%(selfA._name,))
2760 if logging.getLogger().level < logging.WARNING: # La verification n'a lieu qu'en debug
2762 L = numpy.linalg.cholesky( A )
2764 raise ValueError("The %s a posteriori covariance matrix A is not symmetric positive-definite. Please check your a priori covariances and your observation operator."%(selfA._name,))
2765 if selfA._toStore("APosterioriCovariance"):
2766 selfA.StoredVariables["APosterioriCovariance"].store( A )
2767 if selfA._toStore("JacobianMatrixAtOptimum"):
2768 selfA.StoredVariables["JacobianMatrixAtOptimum"].store( HtM )
2769 if selfA._toStore("KalmanGainAtOptimum"):
2770 if (Y.size <= Xb.size): KG = B * HaM * (R + numpy.dot(HtM, B * HaM)).I
2771 elif (Y.size > Xb.size): KG = (BI + numpy.dot(HaM, RI * HtM)).I * HaM * RI
2772 selfA.StoredVariables["KalmanGainAtOptimum"].store( KG )
2774 # Calculs et/ou stockages supplémentaires
2775 # ---------------------------------------
2776 if selfA._toStore("Innovation") or \
2777 selfA._toStore("SigmaObs2") or \
2778 selfA._toStore("MahalanobisConsistency") or \
2779 selfA._toStore("OMB"):
2781 if selfA._toStore("Innovation"):
2782 selfA.StoredVariables["Innovation"].store( numpy.ravel(d) )
2783 if selfA._toStore("BMA"):
2784 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
2785 if selfA._toStore("OMA"):
2786 selfA.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
2787 if selfA._toStore("OMB"):
2788 selfA.StoredVariables["OMB"].store( numpy.ravel(d) )
2789 if selfA._toStore("SigmaObs2"):
2790 TraceR = R.trace(Y.size)
2791 selfA.StoredVariables["SigmaObs2"].store( float( (d.T * (numpy.asmatrix(numpy.ravel(Y)).T-numpy.asmatrix(numpy.ravel(HXa)).T)) ) / TraceR )
2792 if selfA._toStore("MahalanobisConsistency"):
2793 selfA.StoredVariables["MahalanobisConsistency"].store( float( 2.*MinJ/d.size ) )
2794 if selfA._toStore("SimulationQuantiles"):
2795 QuantilesEstimations(selfA, A, Xa, HXa, Hm, HtM)
2796 if selfA._toStore("SimulatedObservationAtBackground"):
2797 selfA.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
2798 if selfA._toStore("SimulatedObservationAtOptimum"):
2799 selfA.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
2803 # ==============================================================================
2804 def std4dvar(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
2813 Hm = HO["Direct"].appliedControledFormTo
2814 Mm = EM["Direct"].appliedControledFormTo
2816 if CM is not None and "Tangent" in CM and U is not None:
2817 Cm = CM["Tangent"].asMatrix(Xb)
2823 if hasattr(U,"store") and 1<=_step<len(U) :
2824 _Un = numpy.asmatrix(numpy.ravel( U[_step] )).T
2825 elif hasattr(U,"store") and len(U)==1:
2826 _Un = numpy.asmatrix(numpy.ravel( U[0] )).T
2828 _Un = numpy.asmatrix(numpy.ravel( U )).T
2833 if Cm is not None and _un is not None: # Attention : si Cm est aussi dans M, doublon !
2834 _Cm = Cm.reshape(_xn.size,_un.size) # ADAO & check shape
2840 # Remarque : les observations sont exploitées à partir du pas de temps
2841 # numéro 1, et sont utilisées dans Yo comme rangées selon ces indices.
2842 # Donc le pas 0 n'est pas utilisé puisque la première étape commence
2843 # avec l'observation du pas 1.
2845 # Nombre de pas identique au nombre de pas d'observations
2846 if hasattr(Y,"stepnumber"):
2847 duration = Y.stepnumber()
2851 # Précalcul des inversions de B et R
2855 # Point de démarrage de l'optimisation
2856 Xini = selfA._parameters["InitializationPoint"]
2858 # Définition de la fonction-coût
2859 # ------------------------------
2860 selfA.DirectCalculation = [None,] # Le pas 0 n'est pas observé
2861 selfA.DirectInnovation = [None,] # Le pas 0 n'est pas observé
2862 def CostFunction(x):
2863 _X = numpy.asmatrix(numpy.ravel( x )).T
2864 if selfA._parameters["StoreInternalVariables"] or \
2865 selfA._toStore("CurrentState") or \
2866 selfA._toStore("CurrentOptimum"):
2867 selfA.StoredVariables["CurrentState"].store( _X )
2868 Jb = float( 0.5 * (_X - Xb).T * BI * (_X - Xb) )
2869 selfA.DirectCalculation = [None,]
2870 selfA.DirectInnovation = [None,]
2873 for step in range(0,duration-1):
2874 if hasattr(Y,"store"):
2875 _Ynpu = numpy.asmatrix(numpy.ravel( Y[step+1] )).T
2877 _Ynpu = numpy.asmatrix(numpy.ravel( Y )).T
2881 if selfA._parameters["EstimationOf"] == "State":
2882 _Xn = Mm( (_Xn, _Un) ) + CmUn(_Xn, _Un)
2883 elif selfA._parameters["EstimationOf"] == "Parameters":
2886 if selfA._parameters["Bounds"] is not None and selfA._parameters["ConstrainedBy"] == "EstimateProjection":
2887 _Xn = numpy.max(numpy.hstack((_Xn,numpy.asmatrix(selfA._parameters["Bounds"])[:,0])),axis=1)
2888 _Xn = numpy.min(numpy.hstack((_Xn,numpy.asmatrix(selfA._parameters["Bounds"])[:,1])),axis=1)
2890 # Etape de différence aux observations
2891 if selfA._parameters["EstimationOf"] == "State":
2892 _YmHMX = _Ynpu - numpy.asmatrix(numpy.ravel( Hm( (_Xn, None) ) )).T
2893 elif selfA._parameters["EstimationOf"] == "Parameters":
2894 _YmHMX = _Ynpu - numpy.asmatrix(numpy.ravel( Hm( (_Xn, _Un) ) )).T - CmUn(_Xn, _Un)
2896 # Stockage de l'état
2897 selfA.DirectCalculation.append( _Xn )
2898 selfA.DirectInnovation.append( _YmHMX )
2900 # Ajout dans la fonctionnelle d'observation
2901 Jo = Jo + 0.5 * float( _YmHMX.T * RI * _YmHMX )
2904 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["CostFunctionJ"]) )
2905 selfA.StoredVariables["CostFunctionJb"].store( Jb )
2906 selfA.StoredVariables["CostFunctionJo"].store( Jo )
2907 selfA.StoredVariables["CostFunctionJ" ].store( J )
2908 if selfA._toStore("IndexOfOptimum") or \
2909 selfA._toStore("CurrentOptimum") or \
2910 selfA._toStore("CostFunctionJAtCurrentOptimum") or \
2911 selfA._toStore("CostFunctionJbAtCurrentOptimum") or \
2912 selfA._toStore("CostFunctionJoAtCurrentOptimum"):
2913 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
2914 if selfA._toStore("IndexOfOptimum"):
2915 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
2916 if selfA._toStore("CurrentOptimum"):
2917 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["CurrentState"][IndexMin] )
2918 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
2919 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
2920 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
2921 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
2922 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
2923 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
2926 def GradientOfCostFunction(x):
2927 _X = numpy.asmatrix(numpy.ravel( x )).T
2928 GradJb = BI * (_X - Xb)
2930 for step in range(duration-1,0,-1):
2931 # Étape de récupération du dernier stockage de l'évolution
2932 _Xn = selfA.DirectCalculation.pop()
2933 # Étape de récupération du dernier stockage de l'innovation
2934 _YmHMX = selfA.DirectInnovation.pop()
2935 # Calcul des adjoints
2936 Ha = HO["Adjoint"].asMatrix(ValueForMethodForm = _Xn)
2937 Ha = Ha.reshape(_Xn.size,_YmHMX.size) # ADAO & check shape
2938 Ma = EM["Adjoint"].asMatrix(ValueForMethodForm = _Xn)
2939 Ma = Ma.reshape(_Xn.size,_Xn.size) # ADAO & check shape
2940 # Calcul du gradient par état adjoint
2941 GradJo = GradJo + Ha * RI * _YmHMX # Équivaut pour Ha linéaire à : Ha( (_Xn, RI * _YmHMX) )
2942 GradJo = Ma * GradJo # Équivaut pour Ma linéaire à : Ma( (_Xn, GradJo) )
2943 GradJ = numpy.ravel( GradJb ) - numpy.ravel( GradJo )
2946 # Minimisation de la fonctionnelle
2947 # --------------------------------
2948 nbPreviousSteps = selfA.StoredVariables["CostFunctionJ"].stepnumber()
2950 if selfA._parameters["Minimizer"] == "LBFGSB":
2951 if "0.19" <= scipy.version.version <= "1.1.0":
2952 import lbfgsbhlt as optimiseur
2954 import scipy.optimize as optimiseur
2955 Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
2956 func = CostFunction,
2958 fprime = GradientOfCostFunction,
2960 bounds = selfA._parameters["Bounds"],
2961 maxfun = selfA._parameters["MaximumNumberOfSteps"]-1,
2962 factr = selfA._parameters["CostDecrementTolerance"]*1.e14,
2963 pgtol = selfA._parameters["ProjectedGradientTolerance"],
2964 iprint = selfA._parameters["optiprint"],
2966 nfeval = Informations['funcalls']
2967 rc = Informations['warnflag']
2968 elif selfA._parameters["Minimizer"] == "TNC":
2969 Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
2970 func = CostFunction,
2972 fprime = GradientOfCostFunction,
2974 bounds = selfA._parameters["Bounds"],
2975 maxfun = selfA._parameters["MaximumNumberOfSteps"],
2976 pgtol = selfA._parameters["ProjectedGradientTolerance"],
2977 ftol = selfA._parameters["CostDecrementTolerance"],
2978 messages = selfA._parameters["optmessages"],
2980 elif selfA._parameters["Minimizer"] == "CG":
2981 Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
2984 fprime = GradientOfCostFunction,
2986 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2987 gtol = selfA._parameters["GradientNormTolerance"],
2988 disp = selfA._parameters["optdisp"],
2991 elif selfA._parameters["Minimizer"] == "NCG":
2992 Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
2995 fprime = GradientOfCostFunction,
2997 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2998 avextol = selfA._parameters["CostDecrementTolerance"],
2999 disp = selfA._parameters["optdisp"],
3002 elif selfA._parameters["Minimizer"] == "BFGS":
3003 Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
3006 fprime = GradientOfCostFunction,
3008 maxiter = selfA._parameters["MaximumNumberOfSteps"],
3009 gtol = selfA._parameters["GradientNormTolerance"],
3010 disp = selfA._parameters["optdisp"],
3014 raise ValueError("Error in Minimizer name: %s"%selfA._parameters["Minimizer"])
3016 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
3017 MinJ = selfA.StoredVariables["CostFunctionJ"][IndexMin]
3019 # Correction pour pallier a un bug de TNC sur le retour du Minimum
3020 # ----------------------------------------------------------------
3021 if selfA._parameters["StoreInternalVariables"] or selfA._toStore("CurrentState"):
3022 Minimum = selfA.StoredVariables["CurrentState"][IndexMin]
3024 # Obtention de l'analyse
3025 # ----------------------
3026 Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
3028 selfA.StoredVariables["Analysis"].store( Xa )
3030 # Calculs et/ou stockages supplémentaires
3031 # ---------------------------------------
3032 if selfA._toStore("BMA"):
3033 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
3037 # ==============================================================================
3038 def van3dvar(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
3040 3DVAR variational analysis with no inversion of B
3047 Hm = HO["Direct"].appliedTo
3048 Ha = HO["Adjoint"].appliedInXTo
3050 # Précalcul des inversions de B et R
3054 # Point de démarrage de l'optimisation
3055 Xini = numpy.zeros(Xb.shape)
3057 # Définition de la fonction-coût
3058 # ------------------------------
3059 def CostFunction(v):
3060 _V = numpy.asmatrix(numpy.ravel( v )).T
3062 if selfA._parameters["StoreInternalVariables"] or \
3063 selfA._toStore("CurrentState") or \
3064 selfA._toStore("CurrentOptimum"):
3065 selfA.StoredVariables["CurrentState"].store( _X )
3067 _HX = numpy.asmatrix(numpy.ravel( _HX )).T
3068 _Innovation = Y - _HX
3069 if selfA._toStore("SimulatedObservationAtCurrentState") or \
3070 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3071 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( _HX )
3072 if selfA._toStore("InnovationAtCurrentState"):
3073 selfA.StoredVariables["InnovationAtCurrentState"].store( _Innovation )
3075 Jb = float( 0.5 * _V.T * BT * _V )
3076 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
3079 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["CostFunctionJ"]) )
3080 selfA.StoredVariables["CostFunctionJb"].store( Jb )
3081 selfA.StoredVariables["CostFunctionJo"].store( Jo )
3082 selfA.StoredVariables["CostFunctionJ" ].store( J )
3083 if selfA._toStore("IndexOfOptimum") or \
3084 selfA._toStore("CurrentOptimum") or \
3085 selfA._toStore("CostFunctionJAtCurrentOptimum") or \
3086 selfA._toStore("CostFunctionJbAtCurrentOptimum") or \
3087 selfA._toStore("CostFunctionJoAtCurrentOptimum") or \
3088 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3089 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
3090 if selfA._toStore("IndexOfOptimum"):
3091 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
3092 if selfA._toStore("CurrentOptimum"):
3093 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["CurrentState"][IndexMin] )
3094 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3095 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin] )
3096 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
3097 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
3098 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
3099 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
3100 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
3101 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
3104 def GradientOfCostFunction(v):
3105 _V = numpy.asmatrix(numpy.ravel( v )).T
3108 _HX = numpy.asmatrix(numpy.ravel( _HX )).T
3110 GradJo = - Ha( (_X, RI * (Y - _HX)) )
3111 GradJ = numpy.ravel( GradJb ) + numpy.ravel( GradJo )
3114 # Minimisation de la fonctionnelle
3115 # --------------------------------
3116 nbPreviousSteps = selfA.StoredVariables["CostFunctionJ"].stepnumber()
3118 if selfA._parameters["Minimizer"] == "LBFGSB":
3119 if "0.19" <= scipy.version.version <= "1.1.0":
3120 import lbfgsbhlt as optimiseur
3122 import scipy.optimize as optimiseur
3123 Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
3124 func = CostFunction,
3126 fprime = GradientOfCostFunction,
3128 bounds = selfA._parameters["Bounds"],
3129 maxfun = selfA._parameters["MaximumNumberOfSteps"]-1,
3130 factr = selfA._parameters["CostDecrementTolerance"]*1.e14,
3131 pgtol = selfA._parameters["ProjectedGradientTolerance"],
3132 iprint = selfA._parameters["optiprint"],
3134 nfeval = Informations['funcalls']
3135 rc = Informations['warnflag']
3136 elif selfA._parameters["Minimizer"] == "TNC":
3137 Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
3138 func = CostFunction,
3140 fprime = GradientOfCostFunction,
3142 bounds = selfA._parameters["Bounds"],
3143 maxfun = selfA._parameters["MaximumNumberOfSteps"],
3144 pgtol = selfA._parameters["ProjectedGradientTolerance"],
3145 ftol = selfA._parameters["CostDecrementTolerance"],
3146 messages = selfA._parameters["optmessages"],
3148 elif selfA._parameters["Minimizer"] == "CG":
3149 Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
3152 fprime = GradientOfCostFunction,
3154 maxiter = selfA._parameters["MaximumNumberOfSteps"],
3155 gtol = selfA._parameters["GradientNormTolerance"],
3156 disp = selfA._parameters["optdisp"],
3159 elif selfA._parameters["Minimizer"] == "NCG":
3160 Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
3163 fprime = GradientOfCostFunction,
3165 maxiter = selfA._parameters["MaximumNumberOfSteps"],
3166 avextol = selfA._parameters["CostDecrementTolerance"],
3167 disp = selfA._parameters["optdisp"],
3170 elif selfA._parameters["Minimizer"] == "BFGS":
3171 Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
3174 fprime = GradientOfCostFunction,
3176 maxiter = selfA._parameters["MaximumNumberOfSteps"],
3177 gtol = selfA._parameters["GradientNormTolerance"],
3178 disp = selfA._parameters["optdisp"],
3182 raise ValueError("Error in Minimizer name: %s"%selfA._parameters["Minimizer"])
3184 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
3185 MinJ = selfA.StoredVariables["CostFunctionJ"][IndexMin]
3187 # Correction pour pallier a un bug de TNC sur le retour du Minimum
3188 # ----------------------------------------------------------------
3189 if selfA._parameters["StoreInternalVariables"] or selfA._toStore("CurrentState"):
3190 Minimum = selfA.StoredVariables["CurrentState"][IndexMin]
3191 Minimum = numpy.asmatrix(numpy.ravel( Minimum )).T
3193 Minimum = Xb + B * numpy.asmatrix(numpy.ravel( Minimum )).T
3195 # Obtention de l'analyse
3196 # ----------------------
3199 selfA.StoredVariables["Analysis"].store( Xa )
3201 if selfA._toStore("OMA") or \
3202 selfA._toStore("SigmaObs2") or \
3203 selfA._toStore("SimulationQuantiles") or \
3204 selfA._toStore("SimulatedObservationAtOptimum"):
3205 if selfA._toStore("SimulatedObservationAtCurrentState"):
3206 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin]
3207 elif selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3208 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"][-1]
3212 # Calcul de la covariance d'analyse
3213 # ---------------------------------
3214 if selfA._toStore("APosterioriCovariance") or \
3215 selfA._toStore("SimulationQuantiles") or \
3216 selfA._toStore("JacobianMatrixAtOptimum") or \
3217 selfA._toStore("KalmanGainAtOptimum"):
3218 HtM = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
3219 HtM = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
3220 if selfA._toStore("APosterioriCovariance") or \
3221 selfA._toStore("SimulationQuantiles") or \
3222 selfA._toStore("KalmanGainAtOptimum"):
3223 HaM = HO["Adjoint"].asMatrix(ValueForMethodForm = Xa)
3224 HaM = HaM.reshape(Xa.size,Y.size) # ADAO & check shape
3225 if selfA._toStore("APosterioriCovariance") or \
3226 selfA._toStore("SimulationQuantiles"):
3231 _ee = numpy.matrix(numpy.zeros(nb)).T
3233 _HtEE = numpy.dot(HtM,_ee)
3234 _HtEE = numpy.asmatrix(numpy.ravel( _HtEE )).T
3235 HessienneI.append( numpy.ravel( BI*_ee + HaM * (RI * _HtEE) ) )
3236 HessienneI = numpy.matrix( HessienneI )
3238 if min(A.shape) != max(A.shape):
3239 raise ValueError("The %s a posteriori covariance matrix A is of shape %s, despites it has to be a squared matrix. There is an error in the observation operator, please check it."%(selfA._name,str(A.shape)))
3240 if (numpy.diag(A) < 0).any():
3241 raise ValueError("The %s a posteriori covariance matrix A has at least one negative value on its diagonal. There is an error in the observation operator, please check it."%(selfA._name,))
3242 if logging.getLogger().level < logging.WARNING: # La verification n'a lieu qu'en debug
3244 L = numpy.linalg.cholesky( A )
3246 raise ValueError("The %s a posteriori covariance matrix A is not symmetric positive-definite. Please check your a priori covariances and your observation operator."%(selfA._name,))
3247 if selfA._toStore("APosterioriCovariance"):
3248 selfA.StoredVariables["APosterioriCovariance"].store( A )
3249 if selfA._toStore("JacobianMatrixAtOptimum"):
3250 selfA.StoredVariables["JacobianMatrixAtOptimum"].store( HtM )
3251 if selfA._toStore("KalmanGainAtOptimum"):
3252 if (Y.size <= Xb.size): KG = B * HaM * (R + numpy.dot(HtM, B * HaM)).I
3253 elif (Y.size > Xb.size): KG = (BI + numpy.dot(HaM, RI * HtM)).I * HaM * RI
3254 selfA.StoredVariables["KalmanGainAtOptimum"].store( KG )
3256 # Calculs et/ou stockages supplémentaires
3257 # ---------------------------------------
3258 if selfA._toStore("Innovation") or \
3259 selfA._toStore("SigmaObs2") or \
3260 selfA._toStore("MahalanobisConsistency") or \
3261 selfA._toStore("OMB"):
3263 if selfA._toStore("Innovation"):
3264 selfA.StoredVariables["Innovation"].store( numpy.ravel(d) )
3265 if selfA._toStore("BMA"):
3266 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
3267 if selfA._toStore("OMA"):
3268 selfA.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
3269 if selfA._toStore("OMB"):
3270 selfA.StoredVariables["OMB"].store( numpy.ravel(d) )
3271 if selfA._toStore("SigmaObs2"):
3272 TraceR = R.trace(Y.size)
3273 selfA.StoredVariables["SigmaObs2"].store( float( (d.T * (numpy.asmatrix(numpy.ravel(Y)).T-numpy.asmatrix(numpy.ravel(HXa)).T)) ) / TraceR )
3274 if selfA._toStore("MahalanobisConsistency"):
3275 selfA.StoredVariables["MahalanobisConsistency"].store( float( 2.*MinJ/d.size ) )
3276 if selfA._toStore("SimulationQuantiles"):
3277 QuantilesEstimations(selfA, A, Xa, HXa, Hm, HtM)
3278 if selfA._toStore("SimulatedObservationAtBackground"):
3279 selfA.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
3280 if selfA._toStore("SimulatedObservationAtOptimum"):
3281 selfA.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
3285 # ==============================================================================
3286 if __name__ == "__main__":
3287 print('\n AUTODIAGNOSTIC\n')