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.ravel( X ).reshape((-1,1))
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.ravel( dX )
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 _HX = numpy.ravel(self.__userFunction( numpy.ravel(X) ))
183 # ---------------------------------------------------------
184 def TangentMatrix(self, X ):
186 Calcul de l'opérateur tangent comme la Jacobienne par différences finies,
187 c'est-à-dire le gradient de H en X. On utilise des différences finies
188 directionnelles autour du point X. X est un numpy.ndarray.
190 Différences finies centrées (approximation d'ordre 2):
191 1/ Pour chaque composante i de X, on ajoute et on enlève la perturbation
192 dX[i] à la composante X[i], pour composer X_plus_dXi et X_moins_dXi, et
193 on calcule les réponses HX_plus_dXi = H( X_plus_dXi ) et HX_moins_dXi =
195 2/ On effectue les différences (HX_plus_dXi-HX_moins_dXi) et on divise par
197 3/ Chaque résultat, par composante, devient une colonne de la Jacobienne
199 Différences finies non centrées (approximation d'ordre 1):
200 1/ Pour chaque composante i de X, on ajoute la perturbation dX[i] à la
201 composante X[i] pour composer X_plus_dXi, et on calcule la réponse
202 HX_plus_dXi = H( X_plus_dXi )
203 2/ On calcule la valeur centrale HX = H(X)
204 3/ On effectue les différences (HX_plus_dXi-HX) et on divise par
206 4/ Chaque résultat, par composante, devient une colonne de la Jacobienne
209 logging.debug("FDA Début du calcul de la Jacobienne")
210 logging.debug("FDA Incrément de............: %s*X"%float(self.__increment))
211 logging.debug("FDA Approximation centrée...: %s"%(self.__centeredDF))
213 if X is None or len(X)==0:
214 raise ValueError("Nominal point X for approximate derivatives can not be None or void (given X: %s)."%(str(X),))
216 _X = numpy.ravel( X )
218 if self.__dX is None:
219 _dX = self.__increment * _X
221 _dX = numpy.ravel( self.__dX )
222 assert len(_X) == len(_dX), "Inconsistent dX increment length with respect to the X one"
223 assert _X.size == _dX.size, "Inconsistent dX increment size with respect to the X one"
225 if (_dX == 0.).any():
228 _dX = numpy.where( _dX == 0., float(self.__increment), _dX )
230 _dX = numpy.where( _dX == 0., moyenne, _dX )
232 __alreadyCalculated = False
234 __bidon, __alreadyCalculatedP = self.__doublon__(_X, self.__listJPCP, self.__listJPPN, None)
235 __bidon, __alreadyCalculatedI = self.__doublon__(_dX, self.__listJPCI, self.__listJPIN, None)
236 if __alreadyCalculatedP == __alreadyCalculatedI > -1:
237 __alreadyCalculated, __i = True, __alreadyCalculatedP
238 logging.debug("FDA Cas J déjà calculé, récupération du doublon %i"%__i)
240 if __alreadyCalculated:
241 logging.debug("FDA Calcul Jacobienne (par récupération du doublon %i)"%__i)
242 _Jacobienne = self.__listJPCR[__i]
244 logging.debug("FDA Calcul Jacobienne (explicite)")
245 if self.__centeredDF:
247 if self.__mpEnabled and not self.__mfEnabled:
249 "__userFunction__path" : self.__userFunction__path,
250 "__userFunction__modl" : self.__userFunction__modl,
251 "__userFunction__name" : self.__userFunction__name,
254 for i in range( len(_dX) ):
256 _X_plus_dXi = numpy.array( _X, dtype=float )
257 _X_plus_dXi[i] = _X[i] + _dXi
258 _X_moins_dXi = numpy.array( _X, dtype=float )
259 _X_moins_dXi[i] = _X[i] - _dXi
261 _jobs.append( (_X_plus_dXi, self.__extraArgs, funcrepr) )
262 _jobs.append( (_X_moins_dXi, self.__extraArgs, funcrepr) )
264 import multiprocessing
265 self.__pool = multiprocessing.Pool(self.__mpWorkers)
266 _HX_plusmoins_dX = self.__pool.map( ExecuteFunction, _jobs )
271 for i in range( len(_dX) ):
272 _Jacobienne.append( numpy.ravel( _HX_plusmoins_dX[2*i] - _HX_plusmoins_dX[2*i+1] ) / (2.*_dX[i]) )
274 elif self.__mfEnabled:
276 for i in range( len(_dX) ):
278 _X_plus_dXi = numpy.array( _X, dtype=float )
279 _X_plus_dXi[i] = _X[i] + _dXi
280 _X_moins_dXi = numpy.array( _X, dtype=float )
281 _X_moins_dXi[i] = _X[i] - _dXi
283 _xserie.append( _X_plus_dXi )
284 _xserie.append( _X_moins_dXi )
286 _HX_plusmoins_dX = self.DirectOperator( _xserie )
289 for i in range( len(_dX) ):
290 _Jacobienne.append( numpy.ravel( _HX_plusmoins_dX[2*i] - _HX_plusmoins_dX[2*i+1] ) / (2.*_dX[i]) )
294 for i in range( _dX.size ):
296 _X_plus_dXi = numpy.array( _X, dtype=float )
297 _X_plus_dXi[i] = _X[i] + _dXi
298 _X_moins_dXi = numpy.array( _X, dtype=float )
299 _X_moins_dXi[i] = _X[i] - _dXi
301 _HX_plus_dXi = self.DirectOperator( _X_plus_dXi )
302 _HX_moins_dXi = self.DirectOperator( _X_moins_dXi )
304 _Jacobienne.append( numpy.ravel( _HX_plus_dXi - _HX_moins_dXi ) / (2.*_dXi) )
308 if self.__mpEnabled and not self.__mfEnabled:
310 "__userFunction__path" : self.__userFunction__path,
311 "__userFunction__modl" : self.__userFunction__modl,
312 "__userFunction__name" : self.__userFunction__name,
315 _jobs.append( (_X, self.__extraArgs, funcrepr) )
316 for i in range( len(_dX) ):
317 _X_plus_dXi = numpy.array( _X, dtype=float )
318 _X_plus_dXi[i] = _X[i] + _dX[i]
320 _jobs.append( (_X_plus_dXi, self.__extraArgs, funcrepr) )
322 import multiprocessing
323 self.__pool = multiprocessing.Pool(self.__mpWorkers)
324 _HX_plus_dX = self.__pool.map( ExecuteFunction, _jobs )
328 _HX = _HX_plus_dX.pop(0)
331 for i in range( len(_dX) ):
332 _Jacobienne.append( numpy.ravel(( _HX_plus_dX[i] - _HX ) / _dX[i]) )
334 elif self.__mfEnabled:
337 for i in range( len(_dX) ):
338 _X_plus_dXi = numpy.array( _X, dtype=float )
339 _X_plus_dXi[i] = _X[i] + _dX[i]
341 _xserie.append( _X_plus_dXi )
343 _HX_plus_dX = self.DirectOperator( _xserie )
345 _HX = _HX_plus_dX.pop(0)
348 for i in range( len(_dX) ):
349 _Jacobienne.append( numpy.ravel(( _HX_plus_dX[i] - _HX ) / _dX[i]) )
353 _HX = self.DirectOperator( _X )
354 for i in range( _dX.size ):
356 _X_plus_dXi = numpy.array( _X, dtype=float )
357 _X_plus_dXi[i] = _X[i] + _dXi
359 _HX_plus_dXi = self.DirectOperator( _X_plus_dXi )
361 _Jacobienne.append( numpy.ravel(( _HX_plus_dXi - _HX ) / _dXi) )
364 _Jacobienne = numpy.transpose( numpy.vstack( _Jacobienne ) )
366 if self.__lenghtRJ < 0: self.__lenghtRJ = 2 * _X.size
367 while len(self.__listJPCP) > self.__lenghtRJ:
368 self.__listJPCP.pop(0)
369 self.__listJPCI.pop(0)
370 self.__listJPCR.pop(0)
371 self.__listJPPN.pop(0)
372 self.__listJPIN.pop(0)
373 self.__listJPCP.append( copy.copy(_X) )
374 self.__listJPCI.append( copy.copy(_dX) )
375 self.__listJPCR.append( copy.copy(_Jacobienne) )
376 self.__listJPPN.append( numpy.linalg.norm(_X) )
377 self.__listJPIN.append( numpy.linalg.norm(_Jacobienne) )
379 logging.debug("FDA Fin du calcul de la Jacobienne")
383 # ---------------------------------------------------------
384 def TangentOperator(self, paire, **extraArgs ):
386 Calcul du tangent à l'aide de la Jacobienne.
388 NB : les extraArgs sont là pour assurer la compatibilité d'appel, mais
389 ne doivent pas être données ici à la fonction utilisateur.
392 assert len(paire) == 1, "Incorrect lenght of arguments"
394 assert len(_paire) == 2, "Incorrect number of arguments"
396 assert len(paire) == 2, "Incorrect number of arguments"
399 _Jacobienne = self.TangentMatrix( X )
400 if dX is None or len(dX) == 0:
402 # Calcul de la forme matricielle si le second argument est None
403 # -------------------------------------------------------------
404 if self.__mfEnabled: return [_Jacobienne,]
405 else: return _Jacobienne
408 # Calcul de la valeur linéarisée de H en X appliqué à dX
409 # ------------------------------------------------------
410 _dX = numpy.ravel( dX )
411 _HtX = numpy.dot(_Jacobienne, _dX)
412 if self.__mfEnabled: return [_HtX,]
415 # ---------------------------------------------------------
416 def AdjointOperator(self, paire, **extraArgs ):
418 Calcul de l'adjoint à l'aide de la Jacobienne.
420 NB : les extraArgs sont là pour assurer la compatibilité d'appel, mais
421 ne doivent pas être données ici à la fonction utilisateur.
424 assert len(paire) == 1, "Incorrect lenght of arguments"
426 assert len(_paire) == 2, "Incorrect number of arguments"
428 assert len(paire) == 2, "Incorrect number of arguments"
431 _JacobienneT = self.TangentMatrix( X ).T
432 if Y is None or len(Y) == 0:
434 # Calcul de la forme matricielle si le second argument est None
435 # -------------------------------------------------------------
436 if self.__mfEnabled: return [_JacobienneT,]
437 else: return _JacobienneT
440 # Calcul de la valeur de l'adjoint en X appliqué à Y
441 # --------------------------------------------------
442 _Y = numpy.ravel( Y )
443 _HaY = numpy.dot(_JacobienneT, _Y)
444 if self.__mfEnabled: return [_HaY,]
447 # ==============================================================================
448 def EnsembleOfCenteredPerturbations( _bgcenter, _bgcovariance, _nbmembers ):
449 "Génération d'un ensemble de taille _nbmembers-1 d'états aléatoires centrés"
451 _bgcenter = numpy.ravel(_bgcenter)[:,None]
453 raise ValueError("Number of members has to be strictly more than 1 (given number: %s)."%(str(_nbmembers),))
455 if _bgcovariance is None:
456 BackgroundEnsemble = numpy.tile( _bgcenter, _nbmembers)
458 _Z = numpy.random.multivariate_normal(numpy.zeros(_bgcenter.size), _bgcovariance, size=_nbmembers).T
459 BackgroundEnsemble = numpy.tile( _bgcenter, _nbmembers) + _Z
461 return BackgroundEnsemble
463 # ==============================================================================
464 def EnsembleOfBackgroundPerturbations( _bgcenter, _bgcovariance, _nbmembers, _withSVD = True):
465 "Génération d'un ensemble de taille _nbmembers-1 d'états aléatoires centrés"
466 def __CenteredRandomAnomalies(Zr, N):
468 Génère une matrice de N anomalies aléatoires centrées sur Zr selon les
469 notes manuscrites de MB et conforme au code de PS avec eps = -1
472 Q = numpy.identity(N-1)-numpy.ones((N-1,N-1))/numpy.sqrt(N)/(numpy.sqrt(N)-eps)
473 Q = numpy.concatenate((Q, [eps*numpy.ones(N-1)/numpy.sqrt(N)]), axis=0)
474 R, _ = numpy.linalg.qr(numpy.random.normal(size = (N-1,N-1)))
479 _bgcenter = numpy.ravel(_bgcenter).reshape((-1,1))
481 raise ValueError("Number of members has to be strictly more than 1 (given number: %s)."%(str(_nbmembers),))
482 if _bgcovariance is None:
483 BackgroundEnsemble = numpy.tile( _bgcenter, _nbmembers)
486 U, s, V = numpy.linalg.svd(_bgcovariance, full_matrices=False)
487 _nbctl = _bgcenter.size
488 if _nbmembers > _nbctl:
489 _Z = numpy.concatenate((numpy.dot(
490 numpy.diag(numpy.sqrt(s[:_nbctl])), V[:_nbctl]),
491 numpy.random.multivariate_normal(numpy.zeros(_nbctl),_bgcovariance,_nbmembers-1-_nbctl)), axis = 0)
493 _Z = numpy.dot(numpy.diag(numpy.sqrt(s[:_nbmembers-1])), V[:_nbmembers-1])
494 _Zca = __CenteredRandomAnomalies(_Z, _nbmembers)
495 BackgroundEnsemble = _bgcenter + _Zca
497 if max(abs(_bgcovariance.flatten())) > 0:
498 _nbctl = _bgcenter.size
499 _Z = numpy.random.multivariate_normal(numpy.zeros(_nbctl),_bgcovariance,_nbmembers-1)
500 _Zca = __CenteredRandomAnomalies(_Z, _nbmembers)
501 BackgroundEnsemble = _bgcenter + _Zca
503 BackgroundEnsemble = numpy.tile( _bgcenter, _nbmembers)
505 return BackgroundEnsemble
507 # ==============================================================================
508 def EnsembleMean( __Ensemble ):
509 "Renvoie la moyenne empirique d'un ensemble"
510 return numpy.asarray(__Ensemble).mean(axis=1, dtype=mfp).astype('float').reshape((-1,1))
512 # ==============================================================================
513 def EnsembleOfAnomalies( __Ensemble, __OptMean = None, __Normalisation = 1.):
514 "Renvoie les anomalies centrées à partir d'un ensemble"
515 if __OptMean is None:
516 __Em = EnsembleMean( __Ensemble )
518 __Em = numpy.ravel( __OptMean ).reshape((-1,1))
520 return __Normalisation * (numpy.asarray( __Ensemble ) - __Em)
522 # ==============================================================================
523 def EnsembleErrorCovariance( __Ensemble, __quick = False ):
524 "Renvoie l'estimation empirique de la covariance d'ensemble"
526 # Covariance rapide mais rarement définie positive
527 __Covariance = numpy.cov( __Ensemble )
529 # Résultat souvent identique à numpy.cov, mais plus robuste
530 __n, __m = numpy.asarray( __Ensemble ).shape
531 __Anomalies = EnsembleOfAnomalies( __Ensemble )
532 # Estimation empirique
533 __Covariance = ( __Anomalies @ __Anomalies.T ) / (__m-1)
535 __Covariance = ( __Covariance + __Covariance.T ) * 0.5
536 # Assure la positivité
537 __epsilon = mpr*numpy.trace( __Covariance )
538 __Covariance = __Covariance + __epsilon * numpy.identity(__n)
542 # ==============================================================================
543 def EnsemblePerturbationWithGivenCovariance( __Ensemble, __Covariance, __Seed=None ):
544 "Ajout d'une perturbation à chaque membre d'un ensemble selon une covariance prescrite"
545 if hasattr(__Covariance,"assparsematrix"):
546 if (abs(__Ensemble).mean() > mpr) and (abs(__Covariance.assparsematrix())/abs(__Ensemble).mean() < mpr).all():
547 # Traitement d'une covariance nulle ou presque
549 if (abs(__Ensemble).mean() <= mpr) and (abs(__Covariance.assparsematrix()) < mpr).all():
550 # Traitement d'une covariance nulle ou presque
553 if (abs(__Ensemble).mean() > mpr) and (abs(__Covariance)/abs(__Ensemble).mean() < mpr).all():
554 # Traitement d'une covariance nulle ou presque
556 if (abs(__Ensemble).mean() <= mpr) and (abs(__Covariance) < mpr).all():
557 # Traitement d'une covariance nulle ou presque
560 __n, __m = __Ensemble.shape
561 if __Seed is not None: numpy.random.seed(__Seed)
563 if hasattr(__Covariance,"isscalar") and __Covariance.isscalar():
564 # Traitement d'une covariance multiple de l'identité
566 __std = numpy.sqrt(__Covariance.assparsematrix())
567 __Ensemble += numpy.random.normal(__zero, __std, size=(__m,__n)).T
569 elif hasattr(__Covariance,"isvector") and __Covariance.isvector():
570 # Traitement d'une covariance diagonale avec variances non identiques
571 __zero = numpy.zeros(__n)
572 __std = numpy.sqrt(__Covariance.assparsematrix())
573 __Ensemble += numpy.asarray([numpy.random.normal(__zero, __std) for i in range(__m)]).T
575 elif hasattr(__Covariance,"ismatrix") and __Covariance.ismatrix():
576 # Traitement d'une covariance pleine
577 __Ensemble += numpy.random.multivariate_normal(numpy.zeros(__n), __Covariance.asfullmatrix(__n), size=__m).T
579 elif isinstance(__Covariance, numpy.ndarray):
580 # Traitement d'une covariance numpy pleine, sachant qu'on arrive ici en dernier
581 __Ensemble += numpy.random.multivariate_normal(numpy.zeros(__n), __Covariance, size=__m).T
584 raise ValueError("Error in ensemble perturbation with inadequate covariance specification")
588 # ==============================================================================
589 def CovarianceInflation(
591 InflationType = None,
592 InflationFactor = None,
593 BackgroundCov = None,
596 Inflation applicable soit sur Pb ou Pa, soit sur les ensembles EXb ou EXa
598 Synthèse : Hunt 2007, section 2.3.5
600 if InflationFactor is None:
603 InflationFactor = float(InflationFactor)
605 if InflationType in ["MultiplicativeOnAnalysisCovariance", "MultiplicativeOnBackgroundCovariance"]:
606 if InflationFactor < 1.:
607 raise ValueError("Inflation factor for multiplicative inflation has to be greater or equal than 1.")
608 if InflationFactor < 1.+mpr:
610 OutputCovOrEns = InflationFactor**2 * InputCovOrEns
612 elif InflationType in ["MultiplicativeOnAnalysisAnomalies", "MultiplicativeOnBackgroundAnomalies"]:
613 if InflationFactor < 1.:
614 raise ValueError("Inflation factor for multiplicative inflation has to be greater or equal than 1.")
615 if InflationFactor < 1.+mpr:
617 InputCovOrEnsMean = InputCovOrEns.mean(axis=1, dtype=mfp).astype('float')
618 OutputCovOrEns = InputCovOrEnsMean[:,numpy.newaxis] \
619 + InflationFactor * (InputCovOrEns - InputCovOrEnsMean[:,numpy.newaxis])
621 elif InflationType in ["AdditiveOnAnalysisCovariance", "AdditiveOnBackgroundCovariance"]:
622 if InflationFactor < 0.:
623 raise ValueError("Inflation factor for additive inflation has to be greater or equal than 0.")
624 if InflationFactor < mpr:
626 __n, __m = numpy.asarray(InputCovOrEns).shape
628 raise ValueError("Additive inflation can only be applied to squared (covariance) matrix.")
629 OutputCovOrEns = (1. - InflationFactor) * InputCovOrEns + InflationFactor * numpy.identity(__n)
631 elif InflationType == "HybridOnBackgroundCovariance":
632 if InflationFactor < 0.:
633 raise ValueError("Inflation factor for hybrid inflation has to be greater or equal than 0.")
634 if InflationFactor < mpr:
636 __n, __m = numpy.asarray(InputCovOrEns).shape
638 raise ValueError("Additive inflation can only be applied to squared (covariance) matrix.")
639 if BackgroundCov is None:
640 raise ValueError("Background covariance matrix B has to be given for hybrid inflation.")
641 if InputCovOrEns.shape != BackgroundCov.shape:
642 raise ValueError("Ensemble covariance matrix has to be of same size than background covariance matrix B.")
643 OutputCovOrEns = (1. - InflationFactor) * InputCovOrEns + InflationFactor * BackgroundCov
645 elif InflationType == "Relaxation":
646 raise NotImplementedError("InflationType Relaxation")
649 raise ValueError("Error in inflation type, '%s' is not a valid keyword."%InflationType)
651 return OutputCovOrEns
653 # ==============================================================================
654 def HessienneEstimation(nb, HaM, HtM, BI, RI):
655 "Estimation de la Hessienne"
658 for i in range(int(nb)):
659 _ee = numpy.zeros((nb,1))
661 _HtEE = numpy.dot(HtM,_ee).reshape((-1,1))
662 HessienneI.append( numpy.ravel( BI * _ee + HaM * (RI * _HtEE) ) )
664 A = numpy.linalg.inv(numpy.array( HessienneI ))
666 if min(A.shape) != max(A.shape):
667 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)))
668 if (numpy.diag(A) < 0).any():
669 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,))
670 if logging.getLogger().level < logging.WARNING: # La verification n'a lieu qu'en debug
672 L = numpy.linalg.cholesky( A )
674 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,))
678 # ==============================================================================
679 def QuantilesEstimations(selfA, A, Xa, HXa = None, Hm = None, HtM = None):
680 "Estimation des quantiles a posteriori (selfA est modifié)"
681 nbsamples = selfA._parameters["NumberOfSamplesForQuantiles"]
683 # Traitement des bornes
684 if "StateBoundsForQuantiles" in selfA._parameters:
685 LBounds = selfA._parameters["StateBoundsForQuantiles"] # Prioritaire
686 elif "Bounds" in selfA._parameters:
687 LBounds = selfA._parameters["Bounds"] # Défaut raisonnable
690 if LBounds is not None:
691 LBounds = ForceNumericBounds( LBounds )
692 _Xa = numpy.ravel(Xa)
694 # Échantillonnage des états
697 for i in range(nbsamples):
698 if selfA._parameters["SimulationForQuantiles"] == "Linear" and HtM is not None and HXa is not None:
699 dXr = (numpy.random.multivariate_normal(_Xa,A) - _Xa).reshape((-1,1))
700 if LBounds is not None: # "EstimateProjection" par défaut
701 dXr = numpy.max(numpy.hstack((dXr,LBounds[:,0].reshape((-1,1))) - Xa),axis=1)
702 dXr = numpy.min(numpy.hstack((dXr,LBounds[:,1].reshape((-1,1))) - Xa),axis=1)
704 Yr = HXa.reshape((-1,1)) + dYr
705 if selfA._toStore("SampledStateForQuantiles"): Xr = _Xa + numpy.ravel(dXr)
706 elif selfA._parameters["SimulationForQuantiles"] == "NonLinear" and Hm is not None:
707 Xr = numpy.random.multivariate_normal(_Xa,A)
708 if LBounds is not None: # "EstimateProjection" par défaut
709 Xr = numpy.max(numpy.hstack((Xr.reshape((-1,1)),LBounds[:,0].reshape((-1,1)))),axis=1)
710 Xr = numpy.min(numpy.hstack((Xr.reshape((-1,1)),LBounds[:,1].reshape((-1,1)))),axis=1)
713 raise ValueError("Quantile simulations has only to be Linear or NonLinear.")
716 YfQ = Yr.reshape((-1,1))
717 if selfA._toStore("SampledStateForQuantiles"): EXr = Xr.reshape((-1,1))
719 YfQ = numpy.hstack((YfQ,Yr.reshape((-1,1))))
720 if selfA._toStore("SampledStateForQuantiles"): EXr = numpy.hstack((EXr,Xr.reshape((-1,1))))
722 # Extraction des quantiles
725 for quantile in selfA._parameters["Quantiles"]:
726 if not (0. <= float(quantile) <= 1.): continue
727 indice = int(nbsamples * float(quantile) - 1./nbsamples)
728 if YQ is None: YQ = YfQ[:,indice].reshape((-1,1))
729 else: YQ = numpy.hstack((YQ,YfQ[:,indice].reshape((-1,1))))
730 if YQ is not None: # Liste non vide de quantiles
731 selfA.StoredVariables["SimulationQuantiles"].store( YQ )
732 if selfA._toStore("SampledStateForQuantiles"):
733 selfA.StoredVariables["SampledStateForQuantiles"].store( EXr )
737 # ==============================================================================
738 def ForceNumericBounds( __Bounds ):
739 "Force les bornes à être des valeurs numériques, sauf si globalement None"
740 # Conserve une valeur par défaut à None s'il n'y a pas de bornes
741 if __Bounds is None: return None
742 # Converti toutes les bornes individuelles None à +/- l'infini
743 __Bounds = numpy.asarray( __Bounds, dtype=float )
744 if len(__Bounds.shape) != 2 or min(__Bounds.shape) <= 0 or __Bounds.shape[1] != 2:
745 raise ValueError("Incorrectly shaped bounds data")
746 __Bounds[numpy.isnan(__Bounds[:,0]),0] = -sys.float_info.max
747 __Bounds[numpy.isnan(__Bounds[:,1]),1] = sys.float_info.max
750 # ==============================================================================
751 def RecentredBounds( __Bounds, __Center):
752 "Recentre les bornes autour de 0, sauf si globalement None"
753 # Conserve une valeur par défaut à None s'il n'y a pas de bornes
754 if __Bounds is None: return None
755 # Recentre les valeurs numériques de bornes
756 return ForceNumericBounds( __Bounds ) - numpy.ravel( __Center ).transpose((-1,1))
758 # ==============================================================================
759 def ApplyBounds( __Vector, __Bounds, __newClip = True):
760 "Applique des bornes numériques à un point"
761 # Conserve une valeur par défaut s'il n'y a pas de bornes
762 if __Bounds is None: return __Vector
764 if not isinstance(__Vector, numpy.ndarray): # Is an array
765 raise ValueError("Incorrect array definition of vector data")
766 if not isinstance(__Bounds, numpy.ndarray): # Is an array
767 raise ValueError("Incorrect array definition of bounds data")
768 if 2*__Vector.size != __Bounds.size: # Is a 2 column array of vector lenght
769 raise ValueError("Incorrect bounds number to be applied for this vector")
770 if len(__Bounds.shape) != 2 or min(__Bounds.shape) <= 0 or __Bounds.shape[1] != 2:
771 raise ValueError("Incorrectly shaped bounds data")
774 __Vector = __Vector.clip(
775 __Bounds[:,0].reshape(__Vector.shape),
776 __Bounds[:,1].reshape(__Vector.shape),
779 __Vector = numpy.max(numpy.hstack((__Vector.reshape((-1,1)),numpy.asmatrix(__Bounds)[:,0])),axis=1)
780 __Vector = numpy.min(numpy.hstack((__Vector.reshape((-1,1)),numpy.asmatrix(__Bounds)[:,1])),axis=1)
781 __Vector = numpy.asarray(__Vector)
785 # ==============================================================================
786 def c2ukf(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
788 Constrained Unscented Kalman Filter
790 if selfA._parameters["EstimationOf"] == "Parameters":
791 selfA._parameters["StoreInternalVariables"] = True
792 selfA._parameters["Bounds"] = ForceNumericBounds( selfA._parameters["Bounds"] )
795 Alpha = selfA._parameters["Alpha"]
796 Beta = selfA._parameters["Beta"]
797 if selfA._parameters["Kappa"] == 0:
798 if selfA._parameters["EstimationOf"] == "State":
800 elif selfA._parameters["EstimationOf"] == "Parameters":
803 Kappa = selfA._parameters["Kappa"]
804 Lambda = float( Alpha**2 ) * ( L + Kappa ) - L
805 Gamma = math.sqrt( L + Lambda )
810 Ww.append( 1. / (2.*(L + Lambda)) )
812 Wm = numpy.array( Ww )
813 Wm[0] = Lambda / (L + Lambda)
814 Wc = numpy.array( Ww )
815 Wc[0] = Lambda / (L + Lambda) + (1. - Alpha**2 + Beta)
818 Hm = HO["Direct"].appliedControledFormTo
820 if selfA._parameters["EstimationOf"] == "State":
821 Mm = EM["Direct"].appliedControledFormTo
823 if CM is not None and "Tangent" in CM and U is not None:
824 Cm = CM["Tangent"].asMatrix(Xb)
828 # Durée d'observation et tailles
829 if hasattr(Y,"stepnumber"):
830 duration = Y.stepnumber()
831 __p = numpy.cumprod(Y.shape())[-1]
834 __p = numpy.array(Y).size
836 # Précalcul des inversions de B et R
837 if selfA._parameters["StoreInternalVariables"] \
838 or selfA._toStore("CostFunctionJ") \
839 or selfA._toStore("CostFunctionJb") \
840 or selfA._toStore("CostFunctionJo") \
841 or selfA._toStore("CurrentOptimum") \
842 or selfA._toStore("APosterioriCovariance"):
848 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
850 if hasattr(B,"asfullmatrix"):
851 Pn = B.asfullmatrix(__n)
854 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
855 selfA.StoredVariables["Analysis"].store( Xb )
856 if selfA._toStore("APosterioriCovariance"):
857 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
858 elif selfA._parameters["nextStep"]:
859 Xn = selfA._getInternalState("Xn")
860 Pn = selfA._getInternalState("Pn")
862 if selfA._parameters["EstimationOf"] == "Parameters":
864 previousJMinimum = numpy.finfo(float).max
866 for step in range(duration-1):
867 if hasattr(Y,"store"):
868 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
870 Ynpu = numpy.ravel( Y ).reshape((__p,1))
873 if hasattr(U,"store") and len(U)>1:
874 Un = numpy.ravel( U[step] ).reshape((-1,1))
875 elif hasattr(U,"store") and len(U)==1:
876 Un = numpy.ravel( U[0] ).reshape((-1,1))
878 Un = numpy.ravel( U ).reshape((-1,1))
882 Pndemi = numpy.real(scipy.linalg.sqrtm(Pn))
883 Xnp = numpy.hstack([Xn, Xn+Gamma*Pndemi, Xn-Gamma*Pndemi])
886 if selfA._parameters["Bounds"] is not None and selfA._parameters["ConstrainedBy"] == "EstimateProjection":
887 for point in range(nbSpts):
888 Xnp[:,point] = ApplyBounds( Xnp[:,point], selfA._parameters["Bounds"] )
891 for point in range(nbSpts):
892 if selfA._parameters["EstimationOf"] == "State":
893 XEtnnpi = numpy.asarray( Mm( (Xnp[:,point], Un) ) ).reshape((-1,1))
894 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
895 Cm = Cm.reshape(Xn.size,Un.size) # ADAO & check shape
896 XEtnnpi = XEtnnpi + Cm * Un
897 if selfA._parameters["Bounds"] is not None and selfA._parameters["ConstrainedBy"] == "EstimateProjection":
898 XEtnnpi = ApplyBounds( XEtnnpi, selfA._parameters["Bounds"] )
899 elif selfA._parameters["EstimationOf"] == "Parameters":
900 # --- > Par principe, M = Id, Q = 0
901 XEtnnpi = Xnp[:,point]
902 XEtnnp.append( numpy.ravel(XEtnnpi).reshape((-1,1)) )
903 XEtnnp = numpy.concatenate( XEtnnp, axis=1 )
905 Xncm = ( XEtnnp * Wm ).sum(axis=1)
907 if selfA._parameters["Bounds"] is not None and selfA._parameters["ConstrainedBy"] == "EstimateProjection":
908 Xncm = ApplyBounds( Xncm, selfA._parameters["Bounds"] )
910 if selfA._parameters["EstimationOf"] == "State": Pnm = Q
911 elif selfA._parameters["EstimationOf"] == "Parameters": Pnm = 0.
912 for point in range(nbSpts):
913 Pnm += Wc[i] * ((XEtnnp[:,point]-Xncm).reshape((-1,1)) * (XEtnnp[:,point]-Xncm))
915 if selfA._parameters["EstimationOf"] == "Parameters" and selfA._parameters["Bounds"] is not None:
916 Pnmdemi = selfA._parameters["Reconditioner"] * numpy.real(scipy.linalg.sqrtm(Pnm))
918 Pnmdemi = numpy.real(scipy.linalg.sqrtm(Pnm))
920 Xnnp = numpy.hstack([Xncm.reshape((-1,1)), Xncm.reshape((-1,1))+Gamma*Pnmdemi, Xncm.reshape((-1,1))-Gamma*Pnmdemi])
922 if selfA._parameters["Bounds"] is not None and selfA._parameters["ConstrainedBy"] == "EstimateProjection":
923 for point in range(nbSpts):
924 Xnnp[:,point] = ApplyBounds( Xnnp[:,point], selfA._parameters["Bounds"] )
927 for point in range(nbSpts):
928 if selfA._parameters["EstimationOf"] == "State":
929 Ynnpi = Hm( (Xnnp[:,point], None) )
930 elif selfA._parameters["EstimationOf"] == "Parameters":
931 Ynnpi = Hm( (Xnnp[:,point], Un) )
932 Ynnp.append( numpy.ravel(Ynnpi).reshape((-1,1)) )
933 Ynnp = numpy.concatenate( Ynnp, axis=1 )
935 Yncm = ( Ynnp * Wm ).sum(axis=1)
939 for point in range(nbSpts):
940 Pyyn += Wc[i] * ((Ynnp[:,point]-Yncm).reshape((-1,1)) * (Ynnp[:,point]-Yncm))
941 Pxyn += Wc[i] * ((Xnnp[:,point]-Xncm).reshape((-1,1)) * (Ynnp[:,point]-Yncm))
943 _Innovation = Ynpu - Yncm.reshape((-1,1))
944 if selfA._parameters["EstimationOf"] == "Parameters":
945 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans H, doublon !
946 _Innovation = _Innovation - Cm * Un
949 Xn = Xncm.reshape((-1,1)) + Kn * _Innovation
950 Pn = Pnm - Kn * Pyyn * Kn.T
952 if selfA._parameters["Bounds"] is not None and selfA._parameters["ConstrainedBy"] == "EstimateProjection":
953 Xn = ApplyBounds( Xn, selfA._parameters["Bounds"] )
956 #--------------------------
957 selfA._setInternalState("Xn", Xn)
958 selfA._setInternalState("Pn", Pn)
959 #--------------------------
961 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
963 selfA.StoredVariables["Analysis"].store( Xa )
964 if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
965 selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( Hm((Xa, Un)) )
966 if selfA._toStore("InnovationAtCurrentAnalysis"):
967 selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
968 # ---> avec current state
969 if selfA._parameters["StoreInternalVariables"] \
970 or selfA._toStore("CurrentState"):
971 selfA.StoredVariables["CurrentState"].store( Xn )
972 if selfA._toStore("ForecastState"):
973 selfA.StoredVariables["ForecastState"].store( Xncm )
974 if selfA._toStore("ForecastCovariance"):
975 selfA.StoredVariables["ForecastCovariance"].store( Pnm )
976 if selfA._toStore("BMA"):
977 selfA.StoredVariables["BMA"].store( Xncm - Xa )
978 if selfA._toStore("InnovationAtCurrentState"):
979 selfA.StoredVariables["InnovationAtCurrentState"].store( _Innovation )
980 if selfA._toStore("SimulatedObservationAtCurrentState") \
981 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
982 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( Yncm )
984 if selfA._parameters["StoreInternalVariables"] \
985 or selfA._toStore("CostFunctionJ") \
986 or selfA._toStore("CostFunctionJb") \
987 or selfA._toStore("CostFunctionJo") \
988 or selfA._toStore("CurrentOptimum") \
989 or selfA._toStore("APosterioriCovariance"):
990 Jb = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
991 Jo = float( 0.5 * _Innovation.T * (RI * _Innovation) )
993 selfA.StoredVariables["CostFunctionJb"].store( Jb )
994 selfA.StoredVariables["CostFunctionJo"].store( Jo )
995 selfA.StoredVariables["CostFunctionJ" ].store( J )
997 if selfA._toStore("IndexOfOptimum") \
998 or selfA._toStore("CurrentOptimum") \
999 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
1000 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
1001 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
1002 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1003 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
1004 if selfA._toStore("IndexOfOptimum"):
1005 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
1006 if selfA._toStore("CurrentOptimum"):
1007 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
1008 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1009 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
1010 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
1011 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
1012 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
1013 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
1014 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
1015 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
1016 if selfA._toStore("APosterioriCovariance"):
1017 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
1018 if selfA._parameters["EstimationOf"] == "Parameters" \
1019 and J < previousJMinimum:
1020 previousJMinimum = J
1022 if selfA._toStore("APosterioriCovariance"):
1023 covarianceXaMin = selfA.StoredVariables["APosterioriCovariance"][-1]
1025 # Stockage final supplémentaire de l'optimum en estimation de paramètres
1026 # ----------------------------------------------------------------------
1027 if selfA._parameters["EstimationOf"] == "Parameters":
1028 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1029 selfA.StoredVariables["Analysis"].store( XaMin )
1030 if selfA._toStore("APosterioriCovariance"):
1031 selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
1032 if selfA._toStore("BMA"):
1033 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
1037 # ==============================================================================
1038 def cekf(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
1040 Contrained Extended Kalman Filter
1042 if selfA._parameters["EstimationOf"] == "Parameters":
1043 selfA._parameters["StoreInternalVariables"] = True
1044 selfA._parameters["Bounds"] = ForceNumericBounds( selfA._parameters["Bounds"] )
1047 H = HO["Direct"].appliedControledFormTo
1049 if selfA._parameters["EstimationOf"] == "State":
1050 M = EM["Direct"].appliedControledFormTo
1052 if CM is not None and "Tangent" in CM and U is not None:
1053 Cm = CM["Tangent"].asMatrix(Xb)
1057 # Durée d'observation et tailles
1058 if hasattr(Y,"stepnumber"):
1059 duration = Y.stepnumber()
1060 __p = numpy.cumprod(Y.shape())[-1]
1063 __p = numpy.array(Y).size
1065 # Précalcul des inversions de B et R
1066 if selfA._parameters["StoreInternalVariables"] \
1067 or selfA._toStore("CostFunctionJ") \
1068 or selfA._toStore("CostFunctionJb") \
1069 or selfA._toStore("CostFunctionJo") \
1070 or selfA._toStore("CurrentOptimum") \
1071 or selfA._toStore("APosterioriCovariance"):
1077 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
1080 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1081 selfA.StoredVariables["Analysis"].store( Xb )
1082 if selfA._toStore("APosterioriCovariance"):
1083 if hasattr(B,"asfullmatrix"):
1084 selfA.StoredVariables["APosterioriCovariance"].store( B.asfullmatrix(__n) )
1086 selfA.StoredVariables["APosterioriCovariance"].store( B )
1087 selfA._setInternalState("seed", numpy.random.get_state())
1088 elif selfA._parameters["nextStep"]:
1089 Xn = selfA._getInternalState("Xn")
1090 Pn = selfA._getInternalState("Pn")
1092 if selfA._parameters["EstimationOf"] == "Parameters":
1094 previousJMinimum = numpy.finfo(float).max
1096 for step in range(duration-1):
1097 if hasattr(Y,"store"):
1098 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
1100 Ynpu = numpy.ravel( Y ).reshape((__p,1))
1102 Ht = HO["Tangent"].asMatrix(ValueForMethodForm = Xn)
1103 Ht = Ht.reshape(Ynpu.size,Xn.size) # ADAO & check shape
1104 Ha = HO["Adjoint"].asMatrix(ValueForMethodForm = Xn)
1105 Ha = Ha.reshape(Xn.size,Ynpu.size) # ADAO & check shape
1107 if selfA._parameters["EstimationOf"] == "State":
1108 Mt = EM["Tangent"].asMatrix(ValueForMethodForm = Xn)
1109 Mt = Mt.reshape(Xn.size,Xn.size) # ADAO & check shape
1110 Ma = EM["Adjoint"].asMatrix(ValueForMethodForm = Xn)
1111 Ma = Ma.reshape(Xn.size,Xn.size) # ADAO & check shape
1114 if hasattr(U,"store") and len(U)>1:
1115 Un = numpy.ravel( U[step] ).reshape((-1,1))
1116 elif hasattr(U,"store") and len(U)==1:
1117 Un = numpy.ravel( U[0] ).reshape((-1,1))
1119 Un = numpy.ravel( U ).reshape((-1,1))
1123 if selfA._parameters["Bounds"] is not None and selfA._parameters["ConstrainedBy"] == "EstimateProjection":
1124 Xn = ApplyBounds( Xn, selfA._parameters["Bounds"] )
1126 if selfA._parameters["EstimationOf"] == "State": # Forecast + Q and observation of forecast
1127 Xn_predicted = numpy.ravel( M( (Xn, Un) ) ).reshape((__n,1))
1128 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
1129 Cm = Cm.reshape(__n,Un.size) # ADAO & check shape
1130 Xn_predicted = Xn_predicted + Cm * Un
1131 Pn_predicted = Q + Mt * (Pn * Ma)
1132 elif selfA._parameters["EstimationOf"] == "Parameters": # Observation of forecast
1133 # --- > Par principe, M = Id, Q = 0
1137 if selfA._parameters["Bounds"] is not None and selfA._parameters["ConstrainedBy"] == "EstimateProjection":
1138 Xn_predicted = ApplyBounds( Xn_predicted, selfA._parameters["Bounds"] )
1140 if selfA._parameters["EstimationOf"] == "State":
1141 HX_predicted = numpy.ravel( H( (Xn_predicted, None) ) ).reshape((__p,1))
1142 _Innovation = Ynpu - HX_predicted
1143 elif selfA._parameters["EstimationOf"] == "Parameters":
1144 HX_predicted = numpy.ravel( H( (Xn_predicted, Un) ) ).reshape((__p,1))
1145 _Innovation = Ynpu - HX_predicted
1146 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans H, doublon !
1147 _Innovation = _Innovation - Cm * Un
1149 Kn = Pn_predicted * Ha * numpy.linalg.inv(R + numpy.dot(Ht, Pn_predicted * Ha))
1150 Xn = Xn_predicted + Kn * _Innovation
1151 Pn = Pn_predicted - Kn * Ht * Pn_predicted
1153 if selfA._parameters["Bounds"] is not None and selfA._parameters["ConstrainedBy"] == "EstimateProjection":
1154 Xn = ApplyBounds( Xn, selfA._parameters["Bounds"] )
1157 #--------------------------
1158 selfA._setInternalState("Xn", Xn)
1159 selfA._setInternalState("Pn", Pn)
1160 #--------------------------
1162 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1163 # ---> avec analysis
1164 selfA.StoredVariables["Analysis"].store( Xa )
1165 if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
1166 selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( H((Xa, Un)) )
1167 if selfA._toStore("InnovationAtCurrentAnalysis"):
1168 selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
1169 # ---> avec current state
1170 if selfA._parameters["StoreInternalVariables"] \
1171 or selfA._toStore("CurrentState"):
1172 selfA.StoredVariables["CurrentState"].store( Xn )
1173 if selfA._toStore("ForecastState"):
1174 selfA.StoredVariables["ForecastState"].store( Xn_predicted )
1175 if selfA._toStore("ForecastCovariance"):
1176 selfA.StoredVariables["ForecastCovariance"].store( Pn_predicted )
1177 if selfA._toStore("BMA"):
1178 selfA.StoredVariables["BMA"].store( Xn_predicted - Xa )
1179 if selfA._toStore("InnovationAtCurrentState"):
1180 selfA.StoredVariables["InnovationAtCurrentState"].store( _Innovation )
1181 if selfA._toStore("SimulatedObservationAtCurrentState") \
1182 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1183 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HX_predicted )
1185 if selfA._parameters["StoreInternalVariables"] \
1186 or selfA._toStore("CostFunctionJ") \
1187 or selfA._toStore("CostFunctionJb") \
1188 or selfA._toStore("CostFunctionJo") \
1189 or selfA._toStore("CurrentOptimum") \
1190 or selfA._toStore("APosterioriCovariance"):
1191 Jb = float( 0.5 * (Xa - Xb).T @ (BI @ (Xa - Xb)) )
1192 Jo = float( 0.5 * _Innovation.T @ (RI @ _Innovation) )
1194 selfA.StoredVariables["CostFunctionJb"].store( Jb )
1195 selfA.StoredVariables["CostFunctionJo"].store( Jo )
1196 selfA.StoredVariables["CostFunctionJ" ].store( J )
1198 if selfA._toStore("IndexOfOptimum") \
1199 or selfA._toStore("CurrentOptimum") \
1200 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
1201 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
1202 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
1203 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1204 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
1205 if selfA._toStore("IndexOfOptimum"):
1206 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
1207 if selfA._toStore("CurrentOptimum"):
1208 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
1209 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1210 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
1211 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
1212 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
1213 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
1214 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
1215 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
1216 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
1217 if selfA._toStore("APosterioriCovariance"):
1218 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
1219 if selfA._parameters["EstimationOf"] == "Parameters" \
1220 and J < previousJMinimum:
1221 previousJMinimum = J
1223 if selfA._toStore("APosterioriCovariance"):
1224 covarianceXaMin = selfA.StoredVariables["APosterioriCovariance"][-1]
1226 # Stockage final supplémentaire de l'optimum en estimation de paramètres
1227 # ----------------------------------------------------------------------
1228 if selfA._parameters["EstimationOf"] == "Parameters":
1229 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1230 selfA.StoredVariables["Analysis"].store( XaMin )
1231 if selfA._toStore("APosterioriCovariance"):
1232 selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
1233 if selfA._toStore("BMA"):
1234 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
1238 # ==============================================================================
1239 def enks(selfA, Xb, Y, U, HO, EM, CM, R, B, Q, VariantM="EnKS16-KalmanFilterFormula"):
1245 H = HO["Direct"].appliedControledFormTo
1247 if selfA._parameters["EstimationOf"] == "State":
1248 M = EM["Direct"].appliedControledFormTo
1250 if CM is not None and "Tangent" in CM and U is not None:
1251 Cm = CM["Tangent"].asMatrix(Xb)
1255 # Précalcul des inversions de B et R
1258 # Durée d'observation et tailles
1259 LagL = selfA._parameters["SmootherLagL"]
1260 if (not hasattr(Y,"store")) or (not hasattr(Y,"stepnumber")):
1261 raise ValueError("Fixed-lag smoother requires a series of observation")
1262 if Y.stepnumber() < LagL:
1263 raise ValueError("Fixed-lag smoother requires a series of observation greater then the lag L")
1264 duration = Y.stepnumber()
1265 __p = numpy.cumprod(Y.shape())[-1]
1267 __m = selfA._parameters["NumberOfMembers"]
1269 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
1270 selfA.StoredVariables["Analysis"].store( Xb )
1271 if selfA._toStore("APosterioriCovariance"):
1272 if hasattr(B,"asfullmatrix"):
1273 selfA.StoredVariables["APosterioriCovariance"].store( B.asfullmatrix(__n) )
1275 selfA.StoredVariables["APosterioriCovariance"].store( B )
1277 # Calcul direct initial (on privilégie la mémorisation au recalcul)
1278 __seed = numpy.random.get_state()
1279 selfB = copy.deepcopy(selfA)
1280 selfB._parameters["StoreSupplementaryCalculations"] = ["CurrentEnsembleState"]
1281 if VariantM == "EnKS16-KalmanFilterFormula":
1282 etkf(selfB, Xb, Y, U, HO, EM, CM, R, B, Q, VariantM = "KalmanFilterFormula")
1284 raise ValueError("VariantM has to be chosen in the authorized methods list.")
1286 EL = selfB.StoredVariables["CurrentEnsembleState"][LagL-1]
1288 EL = EnsembleOfBackgroundPerturbations( Xb, None, __m ) # Cf. etkf
1289 selfA._parameters["SetSeed"] = numpy.random.set_state(__seed)
1291 for step in range(LagL,duration-1):
1293 sEL = selfB.StoredVariables["CurrentEnsembleState"][step+1-LagL:step+1]
1296 if hasattr(Y,"store"):
1297 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
1299 Ynpu = numpy.ravel( Y ).reshape((__p,1))
1302 if hasattr(U,"store") and len(U)>1:
1303 Un = numpy.ravel( U[step] ).reshape((-1,1))
1304 elif hasattr(U,"store") and len(U)==1:
1305 Un = numpy.ravel( U[0] ).reshape((-1,1))
1307 Un = numpy.ravel( U ).reshape((-1,1))
1311 #--------------------------
1312 if VariantM == "EnKS16-KalmanFilterFormula":
1313 if selfA._parameters["EstimationOf"] == "State": # Forecast
1314 EL = M( [(EL[:,i], Un) for i in range(__m)],
1316 returnSerieAsArrayMatrix = True )
1317 EL = EnsemblePerturbationWithGivenCovariance( EL, Q )
1318 EZ = H( [(EL[:,i], Un) for i in range(__m)],
1320 returnSerieAsArrayMatrix = True )
1321 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
1322 Cm = Cm.reshape(__n,Un.size) # ADAO & check shape
1324 elif selfA._parameters["EstimationOf"] == "Parameters":
1325 # --- > Par principe, M = Id, Q = 0
1326 EZ = H( [(EL[:,i], Un) for i in range(__m)],
1328 returnSerieAsArrayMatrix = True )
1330 vEm = EL.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
1331 vZm = EZ.mean(axis=1, dtype=mfp).astype('float').reshape((__p,1))
1333 mS = RIdemi @ EnsembleOfAnomalies( EZ, vZm, 1./math.sqrt(__m-1) )
1334 mS = mS.reshape((-1,__m)) # Pour dimension 1
1335 delta = RIdemi @ ( Ynpu - vZm )
1336 mT = numpy.linalg.inv( numpy.identity(__m) + mS.T @ mS )
1337 vw = mT @ mS.T @ delta
1339 Tdemi = numpy.real(scipy.linalg.sqrtm(mT))
1340 mU = numpy.identity(__m)
1341 wTU = (vw.reshape((__m,1)) + math.sqrt(__m-1) * Tdemi @ mU)
1343 EX = EnsembleOfAnomalies( EL, vEm, 1./math.sqrt(__m-1) )
1347 for irl in range(LagL): # Lissage des L précédentes analysis
1348 vEm = sEL[irl].mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
1349 EX = EnsembleOfAnomalies( sEL[irl], vEm, 1./math.sqrt(__m-1) )
1350 sEL[irl] = vEm + EX @ wTU
1352 # Conservation de l'analyse retrospective d'ordre 0 avant rotation
1353 Xa = sEL[0].mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
1354 if selfA._toStore("APosterioriCovariance"):
1357 for irl in range(LagL):
1358 sEL[irl] = sEL[irl+1]
1360 #--------------------------
1362 raise ValueError("VariantM has to be chosen in the authorized methods list.")
1364 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1365 # ---> avec analysis
1366 selfA.StoredVariables["Analysis"].store( Xa )
1367 if selfA._toStore("APosterioriCovariance"):
1368 selfA.StoredVariables["APosterioriCovariance"].store( EnsembleErrorCovariance(EXn) )
1370 # Stockage des dernières analyses incomplètement remises à jour
1371 for irl in range(LagL):
1372 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1373 Xa = sEL[irl].mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
1374 selfA.StoredVariables["Analysis"].store( Xa )
1378 # ==============================================================================
1379 def etkf(selfA, Xb, Y, U, HO, EM, CM, R, B, Q, VariantM="KalmanFilterFormula"):
1381 Ensemble-Transform EnKF
1383 if selfA._parameters["EstimationOf"] == "Parameters":
1384 selfA._parameters["StoreInternalVariables"] = True
1387 H = HO["Direct"].appliedControledFormTo
1389 if selfA._parameters["EstimationOf"] == "State":
1390 M = EM["Direct"].appliedControledFormTo
1392 if CM is not None and "Tangent" in CM and U is not None:
1393 Cm = CM["Tangent"].asMatrix(Xb)
1397 # Durée d'observation et tailles
1398 if hasattr(Y,"stepnumber"):
1399 duration = Y.stepnumber()
1400 __p = numpy.cumprod(Y.shape())[-1]
1403 __p = numpy.array(Y).size
1405 # Précalcul des inversions de B et R
1406 if selfA._parameters["StoreInternalVariables"] \
1407 or selfA._toStore("CostFunctionJ") \
1408 or selfA._toStore("CostFunctionJb") \
1409 or selfA._toStore("CostFunctionJo") \
1410 or selfA._toStore("CurrentOptimum") \
1411 or selfA._toStore("APosterioriCovariance"):
1414 elif VariantM != "KalmanFilterFormula":
1416 if VariantM == "KalmanFilterFormula":
1420 __m = selfA._parameters["NumberOfMembers"]
1422 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
1423 Xn = EnsembleOfBackgroundPerturbations( Xb, None, __m )
1424 selfA.StoredVariables["Analysis"].store( Xb )
1425 if selfA._toStore("APosterioriCovariance"):
1426 if hasattr(B,"asfullmatrix"):
1427 selfA.StoredVariables["APosterioriCovariance"].store( B.asfullmatrix(__n) )
1429 selfA.StoredVariables["APosterioriCovariance"].store( B )
1430 selfA._setInternalState("seed", numpy.random.get_state())
1431 elif selfA._parameters["nextStep"]:
1432 Xn = selfA._getInternalState("Xn")
1434 previousJMinimum = numpy.finfo(float).max
1436 for step in range(duration-1):
1437 numpy.random.set_state(selfA._getInternalState("seed"))
1438 if hasattr(Y,"store"):
1439 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
1441 Ynpu = numpy.ravel( Y ).reshape((__p,1))
1444 if hasattr(U,"store") and len(U)>1:
1445 Un = numpy.ravel( U[step] ).reshape((-1,1))
1446 elif hasattr(U,"store") and len(U)==1:
1447 Un = numpy.ravel( U[0] ).reshape((-1,1))
1449 Un = numpy.ravel( U ).reshape((-1,1))
1453 if selfA._parameters["InflationType"] == "MultiplicativeOnBackgroundAnomalies":
1454 Xn = CovarianceInflation( Xn,
1455 selfA._parameters["InflationType"],
1456 selfA._parameters["InflationFactor"],
1459 if selfA._parameters["EstimationOf"] == "State": # Forecast + Q and observation of forecast
1460 EMX = M( [(Xn[:,i], Un) for i in range(__m)],
1462 returnSerieAsArrayMatrix = True )
1463 Xn_predicted = EnsemblePerturbationWithGivenCovariance( EMX, Q )
1464 HX_predicted = H( [(Xn_predicted[:,i], Un) for i in range(__m)],
1466 returnSerieAsArrayMatrix = True )
1467 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
1468 Cm = Cm.reshape(__n,Un.size) # ADAO & check shape
1469 Xn_predicted = Xn_predicted + Cm * Un
1470 elif selfA._parameters["EstimationOf"] == "Parameters": # Observation of forecast
1471 # --- > Par principe, M = Id, Q = 0
1472 Xn_predicted = EMX = Xn
1473 HX_predicted = H( [(Xn_predicted[:,i], Un) for i in range(__m)],
1475 returnSerieAsArrayMatrix = True )
1477 # Mean of forecast and observation of forecast
1478 Xfm = Xn_predicted.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
1479 Hfm = HX_predicted.mean(axis=1, dtype=mfp).astype('float').reshape((__p,1))
1482 EaX = EnsembleOfAnomalies( Xn_predicted, Xfm )
1483 EaHX = EnsembleOfAnomalies( HX_predicted, Hfm)
1485 #--------------------------
1486 if VariantM == "KalmanFilterFormula":
1487 mS = RIdemi * EaHX / math.sqrt(__m-1)
1488 mS = mS.reshape((-1,__m)) # Pour dimension 1
1489 delta = RIdemi * ( Ynpu - Hfm )
1490 mT = numpy.linalg.inv( numpy.identity(__m) + mS.T @ mS )
1491 vw = mT @ mS.T @ delta
1493 Tdemi = numpy.real(scipy.linalg.sqrtm(mT))
1494 mU = numpy.identity(__m)
1496 EaX = EaX / math.sqrt(__m-1)
1497 Xn = Xfm + EaX @ ( vw.reshape((__m,1)) + math.sqrt(__m-1) * Tdemi @ mU )
1498 #--------------------------
1499 elif VariantM == "Variational":
1500 HXfm = H((Xfm[:,None], Un)) # Eventuellement Hfm
1501 def CostFunction(w):
1502 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1503 _Jo = 0.5 * _A.T @ (RI * _A)
1504 _Jb = 0.5 * (__m-1) * w.T @ w
1507 def GradientOfCostFunction(w):
1508 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1509 _GardJo = - EaHX.T @ (RI * _A)
1510 _GradJb = (__m-1) * w.reshape((__m,1))
1511 _GradJ = _GardJo + _GradJb
1512 return numpy.ravel(_GradJ)
1513 vw = scipy.optimize.fmin_cg(
1515 x0 = numpy.zeros(__m),
1516 fprime = GradientOfCostFunction,
1521 Hto = EaHX.T @ (RI * EaHX).reshape((-1,__m))
1522 Htb = (__m-1) * numpy.identity(__m)
1525 Pta = numpy.linalg.inv( Hta )
1526 EWa = numpy.real(scipy.linalg.sqrtm((__m-1)*Pta)) # Partie imaginaire ~= 10^-18
1528 Xn = Xfm + EaX @ (vw[:,None] + EWa)
1529 #--------------------------
1530 elif VariantM == "FiniteSize11": # Jauge Boc2011
1531 HXfm = H((Xfm[:,None], Un)) # Eventuellement Hfm
1532 def CostFunction(w):
1533 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1534 _Jo = 0.5 * _A.T @ (RI * _A)
1535 _Jb = 0.5 * __m * math.log(1 + 1/__m + w.T @ w)
1538 def GradientOfCostFunction(w):
1539 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1540 _GardJo = - EaHX.T @ (RI * _A)
1541 _GradJb = __m * w.reshape((__m,1)) / (1 + 1/__m + w.T @ w)
1542 _GradJ = _GardJo + _GradJb
1543 return numpy.ravel(_GradJ)
1544 vw = scipy.optimize.fmin_cg(
1546 x0 = numpy.zeros(__m),
1547 fprime = GradientOfCostFunction,
1552 Hto = EaHX.T @ (RI * EaHX).reshape((-1,__m))
1554 ( (1 + 1/__m + vw.T @ vw) * numpy.identity(__m) - 2 * vw @ vw.T ) \
1555 / (1 + 1/__m + vw.T @ vw)**2
1558 Pta = numpy.linalg.inv( Hta )
1559 EWa = numpy.real(scipy.linalg.sqrtm((__m-1)*Pta)) # Partie imaginaire ~= 10^-18
1561 Xn = Xfm + EaX @ (vw.reshape((__m,1)) + EWa)
1562 #--------------------------
1563 elif VariantM == "FiniteSize15": # Jauge Boc2015
1564 HXfm = H((Xfm[:,None], Un)) # Eventuellement Hfm
1565 def CostFunction(w):
1566 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1567 _Jo = 0.5 * _A.T * RI * _A
1568 _Jb = 0.5 * (__m+1) * math.log(1 + 1/__m + w.T @ w)
1571 def GradientOfCostFunction(w):
1572 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1573 _GardJo = - EaHX.T @ (RI * _A)
1574 _GradJb = (__m+1) * w.reshape((__m,1)) / (1 + 1/__m + w.T @ w)
1575 _GradJ = _GardJo + _GradJb
1576 return numpy.ravel(_GradJ)
1577 vw = scipy.optimize.fmin_cg(
1579 x0 = numpy.zeros(__m),
1580 fprime = GradientOfCostFunction,
1585 Hto = EaHX.T @ (RI * EaHX).reshape((-1,__m))
1587 ( (1 + 1/__m + vw.T @ vw) * numpy.identity(__m) - 2 * vw @ vw.T ) \
1588 / (1 + 1/__m + vw.T @ vw)**2
1591 Pta = numpy.linalg.inv( Hta )
1592 EWa = numpy.real(scipy.linalg.sqrtm((__m-1)*Pta)) # Partie imaginaire ~= 10^-18
1594 Xn = Xfm + EaX @ (vw.reshape((__m,1)) + EWa)
1595 #--------------------------
1596 elif VariantM == "FiniteSize16": # Jauge Boc2016
1597 HXfm = H((Xfm[:,None], Un)) # Eventuellement Hfm
1598 def CostFunction(w):
1599 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1600 _Jo = 0.5 * _A.T @ (RI * _A)
1601 _Jb = 0.5 * (__m+1) * math.log(1 + 1/__m + w.T @ w / (__m-1))
1604 def GradientOfCostFunction(w):
1605 _A = Ynpu - HXfm.reshape((__p,1)) - (EaHX @ w).reshape((__p,1))
1606 _GardJo = - EaHX.T @ (RI * _A)
1607 _GradJb = ((__m+1) / (__m-1)) * w.reshape((__m,1)) / (1 + 1/__m + w.T @ w / (__m-1))
1608 _GradJ = _GardJo + _GradJb
1609 return numpy.ravel(_GradJ)
1610 vw = scipy.optimize.fmin_cg(
1612 x0 = numpy.zeros(__m),
1613 fprime = GradientOfCostFunction,
1618 Hto = EaHX.T @ (RI * EaHX).reshape((-1,__m))
1619 Htb = ((__m+1) / (__m-1)) * \
1620 ( (1 + 1/__m + vw.T @ vw / (__m-1)) * numpy.identity(__m) - 2 * vw @ vw.T / (__m-1) ) \
1621 / (1 + 1/__m + vw.T @ vw / (__m-1))**2
1624 Pta = numpy.linalg.inv( Hta )
1625 EWa = numpy.real(scipy.linalg.sqrtm((__m-1)*Pta)) # Partie imaginaire ~= 10^-18
1627 Xn = Xfm + EaX @ (vw[:,None] + EWa)
1628 #--------------------------
1630 raise ValueError("VariantM has to be chosen in the authorized methods list.")
1632 if selfA._parameters["InflationType"] == "MultiplicativeOnAnalysisAnomalies":
1633 Xn = CovarianceInflation( Xn,
1634 selfA._parameters["InflationType"],
1635 selfA._parameters["InflationFactor"],
1638 Xa = Xn.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
1639 #--------------------------
1640 selfA._setInternalState("Xn", Xn)
1641 selfA._setInternalState("seed", numpy.random.get_state())
1642 #--------------------------
1644 if selfA._parameters["StoreInternalVariables"] \
1645 or selfA._toStore("CostFunctionJ") \
1646 or selfA._toStore("CostFunctionJb") \
1647 or selfA._toStore("CostFunctionJo") \
1648 or selfA._toStore("APosterioriCovariance") \
1649 or selfA._toStore("InnovationAtCurrentAnalysis") \
1650 or selfA._toStore("SimulatedObservationAtCurrentAnalysis") \
1651 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1652 _HXa = numpy.asmatrix(numpy.ravel( H((Xa, Un)) )).T
1653 _Innovation = Ynpu - _HXa
1655 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1656 # ---> avec analysis
1657 selfA.StoredVariables["Analysis"].store( Xa )
1658 if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
1659 selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( _HXa )
1660 if selfA._toStore("InnovationAtCurrentAnalysis"):
1661 selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
1662 # ---> avec current state
1663 if selfA._parameters["StoreInternalVariables"] \
1664 or selfA._toStore("CurrentState"):
1665 selfA.StoredVariables["CurrentState"].store( Xn )
1666 if selfA._toStore("ForecastState"):
1667 selfA.StoredVariables["ForecastState"].store( EMX )
1668 if selfA._toStore("ForecastCovariance"):
1669 selfA.StoredVariables["ForecastCovariance"].store( EnsembleErrorCovariance(EMX) )
1670 if selfA._toStore("BMA"):
1671 selfA.StoredVariables["BMA"].store( EMX - Xa )
1672 if selfA._toStore("InnovationAtCurrentState"):
1673 selfA.StoredVariables["InnovationAtCurrentState"].store( - HX_predicted + Ynpu )
1674 if selfA._toStore("SimulatedObservationAtCurrentState") \
1675 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1676 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HX_predicted )
1678 if selfA._parameters["StoreInternalVariables"] \
1679 or selfA._toStore("CostFunctionJ") \
1680 or selfA._toStore("CostFunctionJb") \
1681 or selfA._toStore("CostFunctionJo") \
1682 or selfA._toStore("CurrentOptimum") \
1683 or selfA._toStore("APosterioriCovariance"):
1684 Jb = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
1685 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
1687 selfA.StoredVariables["CostFunctionJb"].store( Jb )
1688 selfA.StoredVariables["CostFunctionJo"].store( Jo )
1689 selfA.StoredVariables["CostFunctionJ" ].store( J )
1691 if selfA._toStore("IndexOfOptimum") \
1692 or selfA._toStore("CurrentOptimum") \
1693 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
1694 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
1695 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
1696 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1697 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
1698 if selfA._toStore("IndexOfOptimum"):
1699 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
1700 if selfA._toStore("CurrentOptimum"):
1701 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
1702 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1703 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
1704 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
1705 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
1706 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
1707 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
1708 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
1709 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
1710 if selfA._toStore("APosterioriCovariance"):
1711 selfA.StoredVariables["APosterioriCovariance"].store( EnsembleErrorCovariance(Xn) )
1712 if selfA._parameters["EstimationOf"] == "Parameters" \
1713 and J < previousJMinimum:
1714 previousJMinimum = J
1716 if selfA._toStore("APosterioriCovariance"):
1717 covarianceXaMin = selfA.StoredVariables["APosterioriCovariance"][-1]
1718 # ---> Pour les smoothers
1719 if selfA._toStore("CurrentEnsembleState"):
1720 selfA.StoredVariables["CurrentEnsembleState"].store( Xn )
1722 # Stockage final supplémentaire de l'optimum en estimation de paramètres
1723 # ----------------------------------------------------------------------
1724 if selfA._parameters["EstimationOf"] == "Parameters":
1725 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1726 selfA.StoredVariables["Analysis"].store( XaMin )
1727 if selfA._toStore("APosterioriCovariance"):
1728 selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
1729 if selfA._toStore("BMA"):
1730 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
1734 # ==============================================================================
1735 def exkf(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
1737 Extended Kalman Filter
1739 if selfA._parameters["EstimationOf"] == "Parameters":
1740 selfA._parameters["StoreInternalVariables"] = True
1743 H = HO["Direct"].appliedControledFormTo
1745 if selfA._parameters["EstimationOf"] == "State":
1746 M = EM["Direct"].appliedControledFormTo
1748 if CM is not None and "Tangent" in CM and U is not None:
1749 Cm = CM["Tangent"].asMatrix(Xb)
1753 # Durée d'observation et tailles
1754 if hasattr(Y,"stepnumber"):
1755 duration = Y.stepnumber()
1756 __p = numpy.cumprod(Y.shape())[-1]
1759 __p = numpy.array(Y).size
1761 # Précalcul des inversions de B et R
1762 if selfA._parameters["StoreInternalVariables"] \
1763 or selfA._toStore("CostFunctionJ") \
1764 or selfA._toStore("CostFunctionJb") \
1765 or selfA._toStore("CostFunctionJo") \
1766 or selfA._toStore("CurrentOptimum") \
1767 or selfA._toStore("APosterioriCovariance"):
1773 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
1776 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1777 selfA.StoredVariables["Analysis"].store( Xb )
1778 if selfA._toStore("APosterioriCovariance"):
1779 if hasattr(B,"asfullmatrix"):
1780 selfA.StoredVariables["APosterioriCovariance"].store( B.asfullmatrix(__n) )
1782 selfA.StoredVariables["APosterioriCovariance"].store( B )
1783 selfA._setInternalState("seed", numpy.random.get_state())
1784 elif selfA._parameters["nextStep"]:
1785 Xn = selfA._getInternalState("Xn")
1786 Pn = selfA._getInternalState("Pn")
1788 if selfA._parameters["EstimationOf"] == "Parameters":
1790 previousJMinimum = numpy.finfo(float).max
1792 for step in range(duration-1):
1793 if hasattr(Y,"store"):
1794 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
1796 Ynpu = numpy.ravel( Y ).reshape((__p,1))
1798 Ht = HO["Tangent"].asMatrix(ValueForMethodForm = Xn)
1799 Ht = Ht.reshape(Ynpu.size,Xn.size) # ADAO & check shape
1800 Ha = HO["Adjoint"].asMatrix(ValueForMethodForm = Xn)
1801 Ha = Ha.reshape(Xn.size,Ynpu.size) # ADAO & check shape
1803 if selfA._parameters["EstimationOf"] == "State":
1804 Mt = EM["Tangent"].asMatrix(ValueForMethodForm = Xn)
1805 Mt = Mt.reshape(Xn.size,Xn.size) # ADAO & check shape
1806 Ma = EM["Adjoint"].asMatrix(ValueForMethodForm = Xn)
1807 Ma = Ma.reshape(Xn.size,Xn.size) # ADAO & check shape
1810 if hasattr(U,"store") and len(U)>1:
1811 Un = numpy.ravel( U[step] ).reshape((-1,1))
1812 elif hasattr(U,"store") and len(U)==1:
1813 Un = numpy.ravel( U[0] ).reshape((-1,1))
1815 Un = numpy.ravel( U ).reshape((-1,1))
1819 if selfA._parameters["EstimationOf"] == "State": # Forecast + Q and observation of forecast
1820 Xn_predicted = numpy.ravel( M( (Xn, Un) ) ).reshape((__n,1))
1821 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
1822 Cm = Cm.reshape(__n,Un.size) # ADAO & check shape
1823 Xn_predicted = Xn_predicted + Cm * Un
1824 Pn_predicted = Q + Mt * (Pn * Ma)
1825 elif selfA._parameters["EstimationOf"] == "Parameters": # Observation of forecast
1826 # --- > Par principe, M = Id, Q = 0
1830 if selfA._parameters["EstimationOf"] == "State":
1831 HX_predicted = numpy.ravel( H( (Xn_predicted, None) ) ).reshape((__p,1))
1832 _Innovation = Ynpu - HX_predicted
1833 elif selfA._parameters["EstimationOf"] == "Parameters":
1834 HX_predicted = numpy.ravel( H( (Xn_predicted, Un) ) ).reshape((__p,1))
1835 _Innovation = Ynpu - HX_predicted
1836 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans H, doublon !
1837 _Innovation = _Innovation - Cm * Un
1839 Kn = Pn_predicted * Ha * numpy.linalg.inv(R + numpy.dot(Ht, Pn_predicted * Ha))
1840 Xn = Xn_predicted + Kn * _Innovation
1841 Pn = Pn_predicted - Kn * Ht * Pn_predicted
1844 #--------------------------
1845 selfA._setInternalState("Xn", Xn)
1846 selfA._setInternalState("Pn", Pn)
1847 #--------------------------
1849 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1850 # ---> avec analysis
1851 selfA.StoredVariables["Analysis"].store( Xa )
1852 if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
1853 selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( H((Xa, Un)) )
1854 if selfA._toStore("InnovationAtCurrentAnalysis"):
1855 selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
1856 # ---> avec current state
1857 if selfA._parameters["StoreInternalVariables"] \
1858 or selfA._toStore("CurrentState"):
1859 selfA.StoredVariables["CurrentState"].store( Xn )
1860 if selfA._toStore("ForecastState"):
1861 selfA.StoredVariables["ForecastState"].store( Xn_predicted )
1862 if selfA._toStore("ForecastCovariance"):
1863 selfA.StoredVariables["ForecastCovariance"].store( Pn_predicted )
1864 if selfA._toStore("BMA"):
1865 selfA.StoredVariables["BMA"].store( Xn_predicted - Xa )
1866 if selfA._toStore("InnovationAtCurrentState"):
1867 selfA.StoredVariables["InnovationAtCurrentState"].store( _Innovation )
1868 if selfA._toStore("SimulatedObservationAtCurrentState") \
1869 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1870 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HX_predicted )
1872 if selfA._parameters["StoreInternalVariables"] \
1873 or selfA._toStore("CostFunctionJ") \
1874 or selfA._toStore("CostFunctionJb") \
1875 or selfA._toStore("CostFunctionJo") \
1876 or selfA._toStore("CurrentOptimum") \
1877 or selfA._toStore("APosterioriCovariance"):
1878 Jb = float( 0.5 * (Xa - Xb).T @ (BI @ (Xa - Xb)) )
1879 Jo = float( 0.5 * _Innovation.T @ (RI @ _Innovation) )
1881 selfA.StoredVariables["CostFunctionJb"].store( Jb )
1882 selfA.StoredVariables["CostFunctionJo"].store( Jo )
1883 selfA.StoredVariables["CostFunctionJ" ].store( J )
1885 if selfA._toStore("IndexOfOptimum") \
1886 or selfA._toStore("CurrentOptimum") \
1887 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
1888 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
1889 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
1890 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1891 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
1892 if selfA._toStore("IndexOfOptimum"):
1893 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
1894 if selfA._toStore("CurrentOptimum"):
1895 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
1896 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
1897 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
1898 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
1899 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
1900 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
1901 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
1902 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
1903 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
1904 if selfA._toStore("APosterioriCovariance"):
1905 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
1906 if selfA._parameters["EstimationOf"] == "Parameters" \
1907 and J < previousJMinimum:
1908 previousJMinimum = J
1910 if selfA._toStore("APosterioriCovariance"):
1911 covarianceXaMin = selfA.StoredVariables["APosterioriCovariance"][-1]
1913 # Stockage final supplémentaire de l'optimum en estimation de paramètres
1914 # ----------------------------------------------------------------------
1915 if selfA._parameters["EstimationOf"] == "Parameters":
1916 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
1917 selfA.StoredVariables["Analysis"].store( XaMin )
1918 if selfA._toStore("APosterioriCovariance"):
1919 selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
1920 if selfA._toStore("BMA"):
1921 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
1925 # ==============================================================================
1926 def ienkf(selfA, Xb, Y, U, HO, EM, CM, R, B, Q, VariantM="IEnKF12",
1927 BnotT=False, _epsilon=1.e-3, _e=1.e-7, _jmax=15000):
1931 if selfA._parameters["EstimationOf"] == "Parameters":
1932 selfA._parameters["StoreInternalVariables"] = True
1935 H = HO["Direct"].appliedControledFormTo
1937 if selfA._parameters["EstimationOf"] == "State":
1938 M = EM["Direct"].appliedControledFormTo
1940 if CM is not None and "Tangent" in CM and U is not None:
1941 Cm = CM["Tangent"].asMatrix(Xb)
1945 # Durée d'observation et tailles
1946 if hasattr(Y,"stepnumber"):
1947 duration = Y.stepnumber()
1948 __p = numpy.cumprod(Y.shape())[-1]
1951 __p = numpy.array(Y).size
1953 # Précalcul des inversions de B et R
1954 if selfA._parameters["StoreInternalVariables"] \
1955 or selfA._toStore("CostFunctionJ") \
1956 or selfA._toStore("CostFunctionJb") \
1957 or selfA._toStore("CostFunctionJo") \
1958 or selfA._toStore("CurrentOptimum") \
1959 or selfA._toStore("APosterioriCovariance"):
1964 __m = selfA._parameters["NumberOfMembers"]
1966 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
1967 if hasattr(B,"asfullmatrix"): Pn = B.asfullmatrix(__n)
1969 Xn = EnsembleOfBackgroundPerturbations( Xb, Pn, __m )
1970 selfA.StoredVariables["Analysis"].store( Xb )
1971 if selfA._toStore("APosterioriCovariance"):
1972 if hasattr(B,"asfullmatrix"):
1973 selfA.StoredVariables["APosterioriCovariance"].store( B.asfullmatrix(__n) )
1975 selfA.StoredVariables["APosterioriCovariance"].store( B )
1976 selfA._setInternalState("seed", numpy.random.get_state())
1977 elif selfA._parameters["nextStep"]:
1978 Xn = selfA._getInternalState("Xn")
1980 previousJMinimum = numpy.finfo(float).max
1982 for step in range(duration-1):
1983 numpy.random.set_state(selfA._getInternalState("seed"))
1984 if hasattr(Y,"store"):
1985 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
1987 Ynpu = numpy.ravel( Y ).reshape((__p,1))
1990 if hasattr(U,"store") and len(U)>1:
1991 Un = numpy.asmatrix(numpy.ravel( U[step] )).T
1992 elif hasattr(U,"store") and len(U)==1:
1993 Un = numpy.asmatrix(numpy.ravel( U[0] )).T
1995 Un = numpy.asmatrix(numpy.ravel( U )).T
1999 if selfA._parameters["InflationType"] == "MultiplicativeOnBackgroundAnomalies":
2000 Xn = CovarianceInflation( Xn,
2001 selfA._parameters["InflationType"],
2002 selfA._parameters["InflationFactor"],
2005 #--------------------------
2006 if VariantM == "IEnKF12":
2007 Xfm = numpy.ravel(Xn.mean(axis=1, dtype=mfp).astype('float'))
2008 EaX = EnsembleOfAnomalies( Xn ) / math.sqrt(__m-1)
2012 Ta = numpy.identity(__m)
2013 vw = numpy.zeros(__m)
2014 while numpy.linalg.norm(Deltaw) >= _e and __j <= _jmax:
2015 vx1 = (Xfm + EaX @ vw).reshape((__n,1))
2018 E1 = vx1 + _epsilon * EaX
2020 E1 = vx1 + math.sqrt(__m-1) * EaX @ Ta
2022 if selfA._parameters["EstimationOf"] == "State": # Forecast + Q
2023 E2 = M( [(E1[:,i,numpy.newaxis], Un) for i in range(__m)],
2025 returnSerieAsArrayMatrix = True )
2026 elif selfA._parameters["EstimationOf"] == "Parameters":
2027 # --- > Par principe, M = Id
2029 vx2 = E2.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
2030 vy1 = H((vx2, Un)).reshape((__p,1))
2032 HE2 = H( [(E2[:,i,numpy.newaxis], Un) for i in range(__m)],
2034 returnSerieAsArrayMatrix = True )
2035 vy2 = HE2.mean(axis=1, dtype=mfp).astype('float').reshape((__p,1))
2038 EaY = (HE2 - vy2) / _epsilon
2040 EaY = ( (HE2 - vy2) @ numpy.linalg.inv(Ta) ) / math.sqrt(__m-1)
2042 GradJ = numpy.ravel(vw[:,None] - EaY.transpose() @ (RI * ( Ynpu - vy1 )))
2043 mH = numpy.identity(__m) + EaY.transpose() @ (RI * EaY).reshape((-1,__m))
2044 Deltaw = - numpy.linalg.solve(mH,GradJ)
2049 Ta = numpy.real(scipy.linalg.sqrtm(numpy.linalg.inv( mH )))
2053 A2 = EnsembleOfAnomalies( E2 )
2056 Ta = numpy.real(scipy.linalg.sqrtm(numpy.linalg.inv( mH )))
2057 A2 = math.sqrt(__m-1) * A2 @ Ta / _epsilon
2060 #--------------------------
2062 raise ValueError("VariantM has to be chosen in the authorized methods list.")
2064 if selfA._parameters["InflationType"] == "MultiplicativeOnAnalysisAnomalies":
2065 Xn = CovarianceInflation( Xn,
2066 selfA._parameters["InflationType"],
2067 selfA._parameters["InflationFactor"],
2070 Xa = Xn.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
2071 #--------------------------
2072 selfA._setInternalState("Xn", Xn)
2073 selfA._setInternalState("seed", numpy.random.get_state())
2074 #--------------------------
2076 if selfA._parameters["StoreInternalVariables"] \
2077 or selfA._toStore("CostFunctionJ") \
2078 or selfA._toStore("CostFunctionJb") \
2079 or selfA._toStore("CostFunctionJo") \
2080 or selfA._toStore("APosterioriCovariance") \
2081 or selfA._toStore("InnovationAtCurrentAnalysis") \
2082 or selfA._toStore("SimulatedObservationAtCurrentAnalysis") \
2083 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2084 _HXa = numpy.asmatrix(numpy.ravel( H((Xa, Un)) )).T
2085 _Innovation = Ynpu - _HXa
2087 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
2088 # ---> avec analysis
2089 selfA.StoredVariables["Analysis"].store( Xa )
2090 if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
2091 selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( _HXa )
2092 if selfA._toStore("InnovationAtCurrentAnalysis"):
2093 selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
2094 # ---> avec current state
2095 if selfA._parameters["StoreInternalVariables"] \
2096 or selfA._toStore("CurrentState"):
2097 selfA.StoredVariables["CurrentState"].store( Xn )
2098 if selfA._toStore("ForecastState"):
2099 selfA.StoredVariables["ForecastState"].store( E2 )
2100 if selfA._toStore("ForecastCovariance"):
2101 selfA.StoredVariables["ForecastCovariance"].store( EnsembleErrorCovariance(E2) )
2102 if selfA._toStore("BMA"):
2103 selfA.StoredVariables["BMA"].store( E2 - Xa )
2104 if selfA._toStore("InnovationAtCurrentState"):
2105 selfA.StoredVariables["InnovationAtCurrentState"].store( - HE2 + Ynpu )
2106 if selfA._toStore("SimulatedObservationAtCurrentState") \
2107 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2108 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HE2 )
2110 if selfA._parameters["StoreInternalVariables"] \
2111 or selfA._toStore("CostFunctionJ") \
2112 or selfA._toStore("CostFunctionJb") \
2113 or selfA._toStore("CostFunctionJo") \
2114 or selfA._toStore("CurrentOptimum") \
2115 or selfA._toStore("APosterioriCovariance"):
2116 Jb = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
2117 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
2119 selfA.StoredVariables["CostFunctionJb"].store( Jb )
2120 selfA.StoredVariables["CostFunctionJo"].store( Jo )
2121 selfA.StoredVariables["CostFunctionJ" ].store( J )
2123 if selfA._toStore("IndexOfOptimum") \
2124 or selfA._toStore("CurrentOptimum") \
2125 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
2126 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
2127 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
2128 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2129 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
2130 if selfA._toStore("IndexOfOptimum"):
2131 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
2132 if selfA._toStore("CurrentOptimum"):
2133 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
2134 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2135 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
2136 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
2137 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
2138 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
2139 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
2140 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
2141 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
2142 if selfA._toStore("APosterioriCovariance"):
2143 selfA.StoredVariables["APosterioriCovariance"].store( EnsembleErrorCovariance(Xn) )
2144 if selfA._parameters["EstimationOf"] == "Parameters" \
2145 and J < previousJMinimum:
2146 previousJMinimum = J
2148 if selfA._toStore("APosterioriCovariance"):
2149 covarianceXaMin = selfA.StoredVariables["APosterioriCovariance"][-1]
2150 # ---> Pour les smoothers
2151 if selfA._toStore("CurrentEnsembleState"):
2152 selfA.StoredVariables["CurrentEnsembleState"].store( Xn )
2154 # Stockage final supplémentaire de l'optimum en estimation de paramètres
2155 # ----------------------------------------------------------------------
2156 if selfA._parameters["EstimationOf"] == "Parameters":
2157 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
2158 selfA.StoredVariables["Analysis"].store( XaMin )
2159 if selfA._toStore("APosterioriCovariance"):
2160 selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
2161 if selfA._toStore("BMA"):
2162 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
2166 # ==============================================================================
2167 def incr3dvar(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
2175 # Opérateur non-linéaire pour la boucle externe
2176 Hm = HO["Direct"].appliedTo
2178 # Précalcul des inversions de B et R
2182 # Point de démarrage de l'optimisation
2183 Xini = selfA._parameters["InitializationPoint"]
2185 HXb = numpy.asmatrix(numpy.ravel( Hm( Xb ) )).T
2186 Innovation = Y - HXb
2193 Xr = Xini.reshape((-1,1))
2194 while abs(DeltaJ) >= selfA._parameters["CostDecrementTolerance"] and iOuter <= selfA._parameters["MaximumNumberOfSteps"]:
2198 Ht = HO["Tangent"].asMatrix(Xr)
2199 Ht = Ht.reshape(Y.size,Xr.size) # ADAO & check shape
2201 # Définition de la fonction-coût
2202 # ------------------------------
2203 def CostFunction(dx):
2204 _dX = numpy.asmatrix(numpy.ravel( dx )).T
2205 if selfA._parameters["StoreInternalVariables"] or \
2206 selfA._toStore("CurrentState") or \
2207 selfA._toStore("CurrentOptimum"):
2208 selfA.StoredVariables["CurrentState"].store( Xb + _dX )
2210 _HdX = numpy.asmatrix(numpy.ravel( _HdX )).T
2211 _dInnovation = Innovation - _HdX
2212 if selfA._toStore("SimulatedObservationAtCurrentState") or \
2213 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2214 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HXb + _HdX )
2215 if selfA._toStore("InnovationAtCurrentState"):
2216 selfA.StoredVariables["InnovationAtCurrentState"].store( _dInnovation )
2218 Jb = float( 0.5 * _dX.T * BI * _dX )
2219 Jo = float( 0.5 * _dInnovation.T * RI * _dInnovation )
2222 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["CostFunctionJ"]) )
2223 selfA.StoredVariables["CostFunctionJb"].store( Jb )
2224 selfA.StoredVariables["CostFunctionJo"].store( Jo )
2225 selfA.StoredVariables["CostFunctionJ" ].store( J )
2226 if selfA._toStore("IndexOfOptimum") or \
2227 selfA._toStore("CurrentOptimum") or \
2228 selfA._toStore("CostFunctionJAtCurrentOptimum") or \
2229 selfA._toStore("CostFunctionJbAtCurrentOptimum") or \
2230 selfA._toStore("CostFunctionJoAtCurrentOptimum") or \
2231 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2232 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
2233 if selfA._toStore("IndexOfOptimum"):
2234 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
2235 if selfA._toStore("CurrentOptimum"):
2236 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["CurrentState"][IndexMin] )
2237 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2238 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin] )
2239 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
2240 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
2241 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
2242 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
2243 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
2244 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
2247 def GradientOfCostFunction(dx):
2248 _dX = numpy.asmatrix(numpy.ravel( dx )).T
2250 _HdX = numpy.asmatrix(numpy.ravel( _HdX )).T
2251 _dInnovation = Innovation - _HdX
2253 GradJo = - Ht.T @ (RI * _dInnovation)
2254 GradJ = numpy.ravel( GradJb ) + numpy.ravel( GradJo )
2257 # Minimisation de la fonctionnelle
2258 # --------------------------------
2259 nbPreviousSteps = selfA.StoredVariables["CostFunctionJ"].stepnumber()
2261 if selfA._parameters["Minimizer"] == "LBFGSB":
2262 # Minimum, J_optimal, Informations = scipy.optimize.fmin_l_bfgs_b(
2263 if "0.19" <= scipy.version.version <= "1.1.0":
2264 import lbfgsbhlt as optimiseur
2266 import scipy.optimize as optimiseur
2267 Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
2268 func = CostFunction,
2269 x0 = numpy.zeros(Xini.size),
2270 fprime = GradientOfCostFunction,
2272 bounds = RecentredBounds(selfA._parameters["Bounds"], Xb),
2273 maxfun = selfA._parameters["MaximumNumberOfSteps"]-1,
2274 factr = selfA._parameters["CostDecrementTolerance"]*1.e14,
2275 pgtol = selfA._parameters["ProjectedGradientTolerance"],
2276 iprint = selfA._parameters["optiprint"],
2278 nfeval = Informations['funcalls']
2279 rc = Informations['warnflag']
2280 elif selfA._parameters["Minimizer"] == "TNC":
2281 Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
2282 func = CostFunction,
2283 x0 = numpy.zeros(Xini.size),
2284 fprime = GradientOfCostFunction,
2286 bounds = RecentredBounds(selfA._parameters["Bounds"], Xb),
2287 maxfun = selfA._parameters["MaximumNumberOfSteps"],
2288 pgtol = selfA._parameters["ProjectedGradientTolerance"],
2289 ftol = selfA._parameters["CostDecrementTolerance"],
2290 messages = selfA._parameters["optmessages"],
2292 elif selfA._parameters["Minimizer"] == "CG":
2293 Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
2295 x0 = numpy.zeros(Xini.size),
2296 fprime = GradientOfCostFunction,
2298 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2299 gtol = selfA._parameters["GradientNormTolerance"],
2300 disp = selfA._parameters["optdisp"],
2303 elif selfA._parameters["Minimizer"] == "NCG":
2304 Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
2306 x0 = numpy.zeros(Xini.size),
2307 fprime = GradientOfCostFunction,
2309 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2310 avextol = selfA._parameters["CostDecrementTolerance"],
2311 disp = selfA._parameters["optdisp"],
2314 elif selfA._parameters["Minimizer"] == "BFGS":
2315 Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
2317 x0 = numpy.zeros(Xini.size),
2318 fprime = GradientOfCostFunction,
2320 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2321 gtol = selfA._parameters["GradientNormTolerance"],
2322 disp = selfA._parameters["optdisp"],
2326 raise ValueError("Error in Minimizer name: %s"%selfA._parameters["Minimizer"])
2328 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
2329 MinJ = selfA.StoredVariables["CostFunctionJ"][IndexMin]
2331 if selfA._parameters["StoreInternalVariables"] or selfA._toStore("CurrentState"):
2332 Minimum = selfA.StoredVariables["CurrentState"][IndexMin]
2334 Minimum = Xb + Minimum.reshape((-1,1))
2337 DeltaJ = selfA.StoredVariables["CostFunctionJ" ][-1] - J
2338 iOuter = selfA.StoredVariables["CurrentIterationNumber"][-1]
2341 #--------------------------
2343 selfA.StoredVariables["Analysis"].store( Xa )
2345 if selfA._toStore("OMA") or \
2346 selfA._toStore("SigmaObs2") or \
2347 selfA._toStore("SimulationQuantiles") or \
2348 selfA._toStore("SimulatedObservationAtOptimum"):
2349 if selfA._toStore("SimulatedObservationAtCurrentState"):
2350 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin]
2351 elif selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2352 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"][-1]
2356 if selfA._toStore("APosterioriCovariance") or \
2357 selfA._toStore("SimulationQuantiles") or \
2358 selfA._toStore("JacobianMatrixAtOptimum") or \
2359 selfA._toStore("KalmanGainAtOptimum"):
2360 HtM = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
2361 HtM = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
2362 if selfA._toStore("APosterioriCovariance") or \
2363 selfA._toStore("SimulationQuantiles") or \
2364 selfA._toStore("KalmanGainAtOptimum"):
2365 HaM = HO["Adjoint"].asMatrix(ValueForMethodForm = Xa)
2366 HaM = HaM.reshape(Xa.size,Y.size) # ADAO & check shape
2367 if selfA._toStore("APosterioriCovariance") or \
2368 selfA._toStore("SimulationQuantiles"):
2369 A = HessienneEstimation(Xa.size, HaM, HtM, BI, RI)
2370 if selfA._toStore("APosterioriCovariance"):
2371 selfA.StoredVariables["APosterioriCovariance"].store( A )
2372 if selfA._toStore("JacobianMatrixAtOptimum"):
2373 selfA.StoredVariables["JacobianMatrixAtOptimum"].store( HtM )
2374 if selfA._toStore("KalmanGainAtOptimum"):
2375 if (Y.size <= Xb.size): KG = B * HaM * (R + numpy.dot(HtM, B * HaM)).I
2376 elif (Y.size > Xb.size): KG = (BI + numpy.dot(HaM, RI * HtM)).I * HaM * RI
2377 selfA.StoredVariables["KalmanGainAtOptimum"].store( KG )
2379 # Calculs et/ou stockages supplémentaires
2380 # ---------------------------------------
2381 if selfA._toStore("Innovation") or \
2382 selfA._toStore("SigmaObs2") or \
2383 selfA._toStore("MahalanobisConsistency") or \
2384 selfA._toStore("OMB"):
2386 if selfA._toStore("Innovation"):
2387 selfA.StoredVariables["Innovation"].store( d )
2388 if selfA._toStore("BMA"):
2389 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
2390 if selfA._toStore("OMA"):
2391 selfA.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
2392 if selfA._toStore("OMB"):
2393 selfA.StoredVariables["OMB"].store( d )
2394 if selfA._toStore("SigmaObs2"):
2395 TraceR = R.trace(Y.size)
2396 selfA.StoredVariables["SigmaObs2"].store( float( (d.T @ (numpy.ravel(Y)-numpy.ravel(HXa))) ) / TraceR )
2397 if selfA._toStore("MahalanobisConsistency"):
2398 selfA.StoredVariables["MahalanobisConsistency"].store( float( 2.*MinJ/d.size ) )
2399 if selfA._toStore("SimulationQuantiles"):
2400 QuantilesEstimations(selfA, A, Xa, HXa, Hm, HtM)
2401 if selfA._toStore("SimulatedObservationAtBackground"):
2402 selfA.StoredVariables["SimulatedObservationAtBackground"].store( HXb )
2403 if selfA._toStore("SimulatedObservationAtOptimum"):
2404 selfA.StoredVariables["SimulatedObservationAtOptimum"].store( HXa )
2408 # ==============================================================================
2409 def mlef(selfA, Xb, Y, U, HO, EM, CM, R, B, Q, VariantM="MLEF13",
2410 BnotT=False, _epsilon=1.e-3, _e=1.e-7, _jmax=15000):
2412 Maximum Likelihood Ensemble Filter
2414 if selfA._parameters["EstimationOf"] == "Parameters":
2415 selfA._parameters["StoreInternalVariables"] = True
2418 H = HO["Direct"].appliedControledFormTo
2420 if selfA._parameters["EstimationOf"] == "State":
2421 M = EM["Direct"].appliedControledFormTo
2423 if CM is not None and "Tangent" in CM and U is not None:
2424 Cm = CM["Tangent"].asMatrix(Xb)
2428 # Durée d'observation et tailles
2429 if hasattr(Y,"stepnumber"):
2430 duration = Y.stepnumber()
2431 __p = numpy.cumprod(Y.shape())[-1]
2434 __p = numpy.array(Y).size
2436 # Précalcul des inversions de B et R
2437 if selfA._parameters["StoreInternalVariables"] \
2438 or selfA._toStore("CostFunctionJ") \
2439 or selfA._toStore("CostFunctionJb") \
2440 or selfA._toStore("CostFunctionJo") \
2441 or selfA._toStore("CurrentOptimum") \
2442 or selfA._toStore("APosterioriCovariance"):
2447 __m = selfA._parameters["NumberOfMembers"]
2449 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
2450 Xn = EnsembleOfBackgroundPerturbations( Xb, None, __m )
2451 selfA.StoredVariables["Analysis"].store( Xb )
2452 if selfA._toStore("APosterioriCovariance"):
2453 if hasattr(B,"asfullmatrix"):
2454 selfA.StoredVariables["APosterioriCovariance"].store( B.asfullmatrix(__n) )
2456 selfA.StoredVariables["APosterioriCovariance"].store( B )
2457 selfA._setInternalState("seed", numpy.random.get_state())
2458 elif selfA._parameters["nextStep"]:
2459 Xn = selfA._getInternalState("Xn")
2461 previousJMinimum = numpy.finfo(float).max
2463 for step in range(duration-1):
2464 numpy.random.set_state(selfA._getInternalState("seed"))
2465 if hasattr(Y,"store"):
2466 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
2468 Ynpu = numpy.ravel( Y ).reshape((__p,1))
2471 if hasattr(U,"store") and len(U)>1:
2472 Un = numpy.asmatrix(numpy.ravel( U[step] )).T
2473 elif hasattr(U,"store") and len(U)==1:
2474 Un = numpy.asmatrix(numpy.ravel( U[0] )).T
2476 Un = numpy.asmatrix(numpy.ravel( U )).T
2480 if selfA._parameters["InflationType"] == "MultiplicativeOnBackgroundAnomalies":
2481 Xn = CovarianceInflation( Xn,
2482 selfA._parameters["InflationType"],
2483 selfA._parameters["InflationFactor"],
2486 if selfA._parameters["EstimationOf"] == "State": # Forecast + Q and observation of forecast
2487 EMX = M( [(Xn[:,i], Un) for i in range(__m)],
2489 returnSerieAsArrayMatrix = True )
2490 Xn_predicted = EnsemblePerturbationWithGivenCovariance( EMX, Q )
2491 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
2492 Cm = Cm.reshape(__n,Un.size) # ADAO & check shape
2493 Xn_predicted = Xn_predicted + Cm * Un
2494 elif selfA._parameters["EstimationOf"] == "Parameters": # Observation of forecast
2495 # --- > Par principe, M = Id, Q = 0
2496 Xn_predicted = EMX = Xn
2498 #--------------------------
2499 if VariantM == "MLEF13":
2500 Xfm = numpy.ravel(Xn_predicted.mean(axis=1, dtype=mfp).astype('float'))
2501 EaX = EnsembleOfAnomalies( Xn_predicted, Xfm, 1./math.sqrt(__m-1) )
2502 Ua = numpy.identity(__m)
2506 Ta = numpy.identity(__m)
2507 vw = numpy.zeros(__m)
2508 while numpy.linalg.norm(Deltaw) >= _e and __j <= _jmax:
2509 vx1 = (Xfm + EaX @ vw).reshape((__n,1))
2512 E1 = vx1 + _epsilon * EaX
2514 E1 = vx1 + math.sqrt(__m-1) * EaX @ Ta
2516 HE2 = H( [(E1[:,i,numpy.newaxis], Un) for i in range(__m)],
2518 returnSerieAsArrayMatrix = True )
2519 vy2 = HE2.mean(axis=1, dtype=mfp).astype('float').reshape((__p,1))
2522 EaY = (HE2 - vy2) / _epsilon
2524 EaY = ( (HE2 - vy2) @ numpy.linalg.inv(Ta) ) / math.sqrt(__m-1)
2526 GradJ = numpy.ravel(vw[:,None] - EaY.transpose() @ (RI * ( Ynpu - vy2 )))
2527 mH = numpy.identity(__m) + EaY.transpose() @ (RI * EaY).reshape((-1,__m))
2528 Deltaw = - numpy.linalg.solve(mH,GradJ)
2533 Ta = numpy.real(scipy.linalg.sqrtm(numpy.linalg.inv( mH )))
2538 Ta = numpy.real(scipy.linalg.sqrtm(numpy.linalg.inv( mH )))
2540 Xn = vx1 + math.sqrt(__m-1) * EaX @ Ta @ Ua
2541 #--------------------------
2543 raise ValueError("VariantM has to be chosen in the authorized methods list.")
2545 if selfA._parameters["InflationType"] == "MultiplicativeOnAnalysisAnomalies":
2546 Xn = CovarianceInflation( Xn,
2547 selfA._parameters["InflationType"],
2548 selfA._parameters["InflationFactor"],
2551 Xa = Xn.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
2552 #--------------------------
2553 selfA._setInternalState("Xn", Xn)
2554 selfA._setInternalState("seed", numpy.random.get_state())
2555 #--------------------------
2557 if selfA._parameters["StoreInternalVariables"] \
2558 or selfA._toStore("CostFunctionJ") \
2559 or selfA._toStore("CostFunctionJb") \
2560 or selfA._toStore("CostFunctionJo") \
2561 or selfA._toStore("APosterioriCovariance") \
2562 or selfA._toStore("InnovationAtCurrentAnalysis") \
2563 or selfA._toStore("SimulatedObservationAtCurrentAnalysis") \
2564 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2565 _HXa = numpy.asmatrix(numpy.ravel( H((Xa, Un)) )).T
2566 _Innovation = Ynpu - _HXa
2568 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
2569 # ---> avec analysis
2570 selfA.StoredVariables["Analysis"].store( Xa )
2571 if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
2572 selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( _HXa )
2573 if selfA._toStore("InnovationAtCurrentAnalysis"):
2574 selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
2575 # ---> avec current state
2576 if selfA._parameters["StoreInternalVariables"] \
2577 or selfA._toStore("CurrentState"):
2578 selfA.StoredVariables["CurrentState"].store( Xn )
2579 if selfA._toStore("ForecastState"):
2580 selfA.StoredVariables["ForecastState"].store( EMX )
2581 if selfA._toStore("ForecastCovariance"):
2582 selfA.StoredVariables["ForecastCovariance"].store( EnsembleErrorCovariance(EMX) )
2583 if selfA._toStore("BMA"):
2584 selfA.StoredVariables["BMA"].store( EMX - Xa )
2585 if selfA._toStore("InnovationAtCurrentState"):
2586 selfA.StoredVariables["InnovationAtCurrentState"].store( - HE2 + Ynpu )
2587 if selfA._toStore("SimulatedObservationAtCurrentState") \
2588 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2589 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HE2 )
2591 if selfA._parameters["StoreInternalVariables"] \
2592 or selfA._toStore("CostFunctionJ") \
2593 or selfA._toStore("CostFunctionJb") \
2594 or selfA._toStore("CostFunctionJo") \
2595 or selfA._toStore("CurrentOptimum") \
2596 or selfA._toStore("APosterioriCovariance"):
2597 Jb = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
2598 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
2600 selfA.StoredVariables["CostFunctionJb"].store( Jb )
2601 selfA.StoredVariables["CostFunctionJo"].store( Jo )
2602 selfA.StoredVariables["CostFunctionJ" ].store( J )
2604 if selfA._toStore("IndexOfOptimum") \
2605 or selfA._toStore("CurrentOptimum") \
2606 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
2607 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
2608 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
2609 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2610 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
2611 if selfA._toStore("IndexOfOptimum"):
2612 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
2613 if selfA._toStore("CurrentOptimum"):
2614 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
2615 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2616 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
2617 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
2618 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
2619 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
2620 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
2621 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
2622 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
2623 if selfA._toStore("APosterioriCovariance"):
2624 selfA.StoredVariables["APosterioriCovariance"].store( EnsembleErrorCovariance(Xn) )
2625 if selfA._parameters["EstimationOf"] == "Parameters" \
2626 and J < previousJMinimum:
2627 previousJMinimum = J
2629 if selfA._toStore("APosterioriCovariance"):
2630 covarianceXaMin = selfA.StoredVariables["APosterioriCovariance"][-1]
2631 # ---> Pour les smoothers
2632 if selfA._toStore("CurrentEnsembleState"):
2633 selfA.StoredVariables["CurrentEnsembleState"].store( Xn )
2635 # Stockage final supplémentaire de l'optimum en estimation de paramètres
2636 # ----------------------------------------------------------------------
2637 if selfA._parameters["EstimationOf"] == "Parameters":
2638 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
2639 selfA.StoredVariables["Analysis"].store( XaMin )
2640 if selfA._toStore("APosterioriCovariance"):
2641 selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
2642 if selfA._toStore("BMA"):
2643 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
2647 # ==============================================================================
2659 Implémentation informatique de l'algorithme MMQR, basée sur la publication :
2660 David R. Hunter, Kenneth Lange, "Quantile Regression via an MM Algorithm",
2661 Journal of Computational and Graphical Statistics, 9, 1, pp.60-77, 2000.
2664 # Recuperation des donnees et informations initiales
2665 # --------------------------------------------------
2666 variables = numpy.ravel( x0 )
2667 mesures = numpy.ravel( y )
2668 increment = sys.float_info[0]
2671 quantile = float(quantile)
2673 # Calcul des parametres du MM
2674 # ---------------------------
2675 tn = float(toler) / n
2676 e0 = -tn / math.log(tn)
2677 epsilon = (e0-tn)/(1+math.log(e0))
2679 # Calculs d'initialisation
2680 # ------------------------
2681 residus = mesures - numpy.ravel( func( variables ) )
2682 poids = 1./(epsilon+numpy.abs(residus))
2683 veps = 1. - 2. * quantile - residus * poids
2684 lastsurrogate = -numpy.sum(residus*veps) - (1.-2.*quantile)*numpy.sum(residus)
2687 # Recherche iterative
2688 # -------------------
2689 while (increment > toler) and (iteration < maxfun) :
2692 Derivees = numpy.array(fprime(variables))
2693 Derivees = Derivees.reshape(n,p) # Necessaire pour remettre en place la matrice si elle passe par des tuyaux YACS
2694 DeriveesT = Derivees.transpose()
2695 M = numpy.dot( DeriveesT , (numpy.array(numpy.matrix(p*[poids,]).T)*Derivees) )
2696 SM = numpy.transpose(numpy.dot( DeriveesT , veps ))
2697 step = - numpy.linalg.lstsq( M, SM, rcond=-1 )[0]
2699 variables = variables + step
2700 if bounds is not None:
2701 # Attention : boucle infinie à éviter si un intervalle est trop petit
2702 while( (variables < numpy.ravel(numpy.asmatrix(bounds)[:,0])).any() or (variables > numpy.ravel(numpy.asmatrix(bounds)[:,1])).any() ):
2704 variables = variables - step
2705 residus = mesures - numpy.ravel( func(variables) )
2706 surrogate = numpy.sum(residus**2 * poids) + (4.*quantile-2.) * numpy.sum(residus)
2708 while ( (surrogate > lastsurrogate) and ( max(list(numpy.abs(step))) > 1.e-16 ) ) :
2710 variables = variables - step
2711 residus = mesures - numpy.ravel( func(variables) )
2712 surrogate = numpy.sum(residus**2 * poids) + (4.*quantile-2.) * numpy.sum(residus)
2714 increment = lastsurrogate-surrogate
2715 poids = 1./(epsilon+numpy.abs(residus))
2716 veps = 1. - 2. * quantile - residus * poids
2717 lastsurrogate = -numpy.sum(residus * veps) - (1.-2.*quantile)*numpy.sum(residus)
2721 Ecart = quantile * numpy.sum(residus) - numpy.sum( residus[residus<0] )
2723 return variables, Ecart, [n,p,iteration,increment,0]
2725 # ==============================================================================
2726 def multi3dvar(selfA, Xb, Y, U, HO, EM, CM, R, B, Q, oneCycle):
2728 3DVAR multi-pas et multi-méthodes
2732 if selfA._parameters["EstimationOf"] == "State":
2733 M = EM["Direct"].appliedTo
2735 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
2736 Xn = numpy.ravel(Xb).reshape((-1,1))
2737 selfA.StoredVariables["Analysis"].store( Xn )
2738 if selfA._toStore("APosterioriCovariance"):
2739 if hasattr(B,"asfullmatrix"):
2740 selfA.StoredVariables["APosterioriCovariance"].store( B.asfullmatrix(Xn.size) )
2742 selfA.StoredVariables["APosterioriCovariance"].store( B )
2743 if selfA._toStore("ForecastState"):
2744 selfA.StoredVariables["ForecastState"].store( Xn )
2745 elif selfA._parameters["nextStep"]:
2746 Xn = selfA._getInternalState("Xn")
2748 Xn = numpy.ravel(Xb).reshape((-1,1))
2750 if hasattr(Y,"stepnumber"):
2751 duration = Y.stepnumber()
2756 for step in range(duration-1):
2757 if hasattr(Y,"store"):
2758 Ynpu = numpy.ravel( Y[step+1] ).reshape((-1,1))
2760 Ynpu = numpy.ravel( Y ).reshape((-1,1))
2762 if selfA._parameters["EstimationOf"] == "State": # Forecast
2763 Xn_predicted = M( Xn )
2764 if selfA._toStore("ForecastState"):
2765 selfA.StoredVariables["ForecastState"].store( Xn_predicted )
2766 elif selfA._parameters["EstimationOf"] == "Parameters": # No forecast
2767 # --- > Par principe, M = Id, Q = 0
2769 Xn_predicted = numpy.ravel(Xn_predicted).reshape((-1,1))
2771 oneCycle(selfA, Xn_predicted, Ynpu, U, HO, None, None, R, B, None)
2773 Xn = selfA.StoredVariables["Analysis"][-1]
2774 #--------------------------
2775 selfA._setInternalState("Xn", Xn)
2779 # ==============================================================================
2780 def psas3dvar(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
2789 Hm = HO["Direct"].appliedTo
2791 # Utilisation éventuelle d'un vecteur H(Xb) précalculé
2792 if HO["AppliedInX"] is not None and "HXb" in HO["AppliedInX"]:
2793 HXb = Hm( Xb, HO["AppliedInX"]["HXb"] )
2796 HXb = numpy.asmatrix(numpy.ravel( HXb )).T
2797 if Y.size != HXb.size:
2798 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))
2799 if max(Y.shape) != max(HXb.shape):
2800 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))
2802 if selfA._toStore("JacobianMatrixAtBackground"):
2803 HtMb = HO["Tangent"].asMatrix(ValueForMethodForm = Xb)
2804 HtMb = HtMb.reshape(Y.size,Xb.size) # ADAO & check shape
2805 selfA.StoredVariables["JacobianMatrixAtBackground"].store( HtMb )
2807 Ht = HO["Tangent"].asMatrix(Xb)
2809 HBHTpR = R + Ht * BHT
2810 Innovation = Y - HXb
2812 # Point de démarrage de l'optimisation
2813 Xini = numpy.zeros(Xb.shape)
2815 # Définition de la fonction-coût
2816 # ------------------------------
2817 def CostFunction(w):
2818 _W = w.reshape((-1,1))
2819 if selfA._parameters["StoreInternalVariables"] or \
2820 selfA._toStore("CurrentState") or \
2821 selfA._toStore("CurrentOptimum"):
2822 selfA.StoredVariables["CurrentState"].store( Xb + BHT @ _W )
2823 if selfA._toStore("SimulatedObservationAtCurrentState") or \
2824 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2825 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( Hm( Xb + BHT @ _W ) )
2826 if selfA._toStore("InnovationAtCurrentState"):
2827 selfA.StoredVariables["InnovationAtCurrentState"].store( Innovation )
2829 Jb = float( 0.5 * _W.T @ (HBHTpR @ _W) )
2830 Jo = float( - _W.T @ Innovation )
2833 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["CostFunctionJ"]) )
2834 selfA.StoredVariables["CostFunctionJb"].store( Jb )
2835 selfA.StoredVariables["CostFunctionJo"].store( Jo )
2836 selfA.StoredVariables["CostFunctionJ" ].store( J )
2837 if selfA._toStore("IndexOfOptimum") or \
2838 selfA._toStore("CurrentOptimum") or \
2839 selfA._toStore("CostFunctionJAtCurrentOptimum") or \
2840 selfA._toStore("CostFunctionJbAtCurrentOptimum") or \
2841 selfA._toStore("CostFunctionJoAtCurrentOptimum") or \
2842 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2843 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
2844 if selfA._toStore("IndexOfOptimum"):
2845 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
2846 if selfA._toStore("CurrentOptimum"):
2847 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["CurrentState"][IndexMin] )
2848 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2849 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin] )
2850 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
2851 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
2852 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
2853 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
2854 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
2855 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
2858 def GradientOfCostFunction(w):
2859 _W = w.reshape((-1,1))
2860 GradJb = HBHTpR @ _W
2861 GradJo = - Innovation
2862 GradJ = numpy.ravel( GradJb ) + numpy.ravel( GradJo )
2865 # Minimisation de la fonctionnelle
2866 # --------------------------------
2867 nbPreviousSteps = selfA.StoredVariables["CostFunctionJ"].stepnumber()
2869 if selfA._parameters["Minimizer"] == "LBFGSB":
2870 if "0.19" <= scipy.version.version <= "1.1.0":
2871 import lbfgsbhlt as optimiseur
2873 import scipy.optimize as optimiseur
2874 Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
2875 func = CostFunction,
2877 fprime = GradientOfCostFunction,
2879 bounds = RecentredBounds(selfA._parameters["Bounds"], Xb),
2880 maxfun = selfA._parameters["MaximumNumberOfSteps"]-1,
2881 factr = selfA._parameters["CostDecrementTolerance"]*1.e14,
2882 pgtol = selfA._parameters["ProjectedGradientTolerance"],
2883 iprint = selfA._parameters["optiprint"],
2885 nfeval = Informations['funcalls']
2886 rc = Informations['warnflag']
2887 elif selfA._parameters["Minimizer"] == "TNC":
2888 Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
2889 func = CostFunction,
2891 fprime = GradientOfCostFunction,
2893 bounds = RecentredBounds(selfA._parameters["Bounds"], Xb),
2894 maxfun = selfA._parameters["MaximumNumberOfSteps"],
2895 pgtol = selfA._parameters["ProjectedGradientTolerance"],
2896 ftol = selfA._parameters["CostDecrementTolerance"],
2897 messages = selfA._parameters["optmessages"],
2899 elif selfA._parameters["Minimizer"] == "CG":
2900 Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
2903 fprime = GradientOfCostFunction,
2905 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2906 gtol = selfA._parameters["GradientNormTolerance"],
2907 disp = selfA._parameters["optdisp"],
2910 elif selfA._parameters["Minimizer"] == "NCG":
2911 Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
2914 fprime = GradientOfCostFunction,
2916 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2917 avextol = selfA._parameters["CostDecrementTolerance"],
2918 disp = selfA._parameters["optdisp"],
2921 elif selfA._parameters["Minimizer"] == "BFGS":
2922 Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
2925 fprime = GradientOfCostFunction,
2927 maxiter = selfA._parameters["MaximumNumberOfSteps"],
2928 gtol = selfA._parameters["GradientNormTolerance"],
2929 disp = selfA._parameters["optdisp"],
2933 raise ValueError("Error in Minimizer name: %s"%selfA._parameters["Minimizer"])
2935 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
2936 MinJ = selfA.StoredVariables["CostFunctionJ"][IndexMin]
2938 # Correction pour pallier a un bug de TNC sur le retour du Minimum
2939 # ----------------------------------------------------------------
2940 if selfA._parameters["StoreInternalVariables"] or selfA._toStore("CurrentState"):
2941 Minimum = selfA.StoredVariables["CurrentState"][IndexMin]
2943 Minimum = Xb + BHT @ Minimum.reshape((-1,1))
2946 #--------------------------
2948 selfA.StoredVariables["Analysis"].store( Xa )
2950 if selfA._toStore("OMA") or \
2951 selfA._toStore("SigmaObs2") or \
2952 selfA._toStore("SimulationQuantiles") or \
2953 selfA._toStore("SimulatedObservationAtOptimum"):
2954 if selfA._toStore("SimulatedObservationAtCurrentState"):
2955 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin]
2956 elif selfA._toStore("SimulatedObservationAtCurrentOptimum"):
2957 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"][-1]
2961 if selfA._toStore("APosterioriCovariance") or \
2962 selfA._toStore("SimulationQuantiles") or \
2963 selfA._toStore("JacobianMatrixAtOptimum") or \
2964 selfA._toStore("KalmanGainAtOptimum"):
2965 HtM = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
2966 HtM = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
2967 if selfA._toStore("APosterioriCovariance") or \
2968 selfA._toStore("SimulationQuantiles") or \
2969 selfA._toStore("KalmanGainAtOptimum"):
2970 HaM = HO["Adjoint"].asMatrix(ValueForMethodForm = Xa)
2971 HaM = HaM.reshape(Xa.size,Y.size) # ADAO & check shape
2972 if selfA._toStore("APosterioriCovariance") or \
2973 selfA._toStore("SimulationQuantiles"):
2976 A = HessienneEstimation(Xa.size, HaM, HtM, BI, RI)
2977 if selfA._toStore("APosterioriCovariance"):
2978 selfA.StoredVariables["APosterioriCovariance"].store( A )
2979 if selfA._toStore("JacobianMatrixAtOptimum"):
2980 selfA.StoredVariables["JacobianMatrixAtOptimum"].store( HtM )
2981 if selfA._toStore("KalmanGainAtOptimum"):
2982 if (Y.size <= Xb.size): KG = B * HaM * (R + numpy.dot(HtM, B * HaM)).I
2983 elif (Y.size > Xb.size): KG = (BI + numpy.dot(HaM, RI * HtM)).I * HaM * RI
2984 selfA.StoredVariables["KalmanGainAtOptimum"].store( KG )
2986 # Calculs et/ou stockages supplémentaires
2987 # ---------------------------------------
2988 if selfA._toStore("Innovation") or \
2989 selfA._toStore("SigmaObs2") or \
2990 selfA._toStore("MahalanobisConsistency") or \
2991 selfA._toStore("OMB"):
2993 if selfA._toStore("Innovation"):
2994 selfA.StoredVariables["Innovation"].store( d )
2995 if selfA._toStore("BMA"):
2996 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
2997 if selfA._toStore("OMA"):
2998 selfA.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
2999 if selfA._toStore("OMB"):
3000 selfA.StoredVariables["OMB"].store( d )
3001 if selfA._toStore("SigmaObs2"):
3002 TraceR = R.trace(Y.size)
3003 selfA.StoredVariables["SigmaObs2"].store( float( (d.T @ (numpy.ravel(Y)-numpy.ravel(HXa))) ) / TraceR )
3004 if selfA._toStore("MahalanobisConsistency"):
3005 selfA.StoredVariables["MahalanobisConsistency"].store( float( 2.*MinJ/d.size ) )
3006 if selfA._toStore("SimulationQuantiles"):
3007 QuantilesEstimations(selfA, A, Xa, HXa, Hm, HtM)
3008 if selfA._toStore("SimulatedObservationAtBackground"):
3009 selfA.StoredVariables["SimulatedObservationAtBackground"].store( HXb )
3010 if selfA._toStore("SimulatedObservationAtOptimum"):
3011 selfA.StoredVariables["SimulatedObservationAtOptimum"].store( HXa )
3015 # ==============================================================================
3016 def senkf(selfA, Xb, Y, U, HO, EM, CM, R, B, Q, VariantM="KalmanFilterFormula16"):
3020 if selfA._parameters["EstimationOf"] == "Parameters":
3021 selfA._parameters["StoreInternalVariables"] = True
3024 H = HO["Direct"].appliedControledFormTo
3026 if selfA._parameters["EstimationOf"] == "State":
3027 M = EM["Direct"].appliedControledFormTo
3029 if CM is not None and "Tangent" in CM and U is not None:
3030 Cm = CM["Tangent"].asMatrix(Xb)
3034 # Durée d'observation et tailles
3035 if hasattr(Y,"stepnumber"):
3036 duration = Y.stepnumber()
3037 __p = numpy.cumprod(Y.shape())[-1]
3040 __p = numpy.array(Y).size
3042 # Précalcul des inversions de B et R
3043 if selfA._parameters["StoreInternalVariables"] \
3044 or selfA._toStore("CostFunctionJ") \
3045 or selfA._toStore("CostFunctionJb") \
3046 or selfA._toStore("CostFunctionJo") \
3047 or selfA._toStore("CurrentOptimum") \
3048 or selfA._toStore("APosterioriCovariance"):
3053 __m = selfA._parameters["NumberOfMembers"]
3055 if hasattr(R,"asfullmatrix"): Rn = R.asfullmatrix(__p)
3058 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
3059 if hasattr(B,"asfullmatrix"): Pn = B.asfullmatrix(__n)
3061 Xn = EnsembleOfBackgroundPerturbations( Xb, Pn, __m )
3062 selfA.StoredVariables["Analysis"].store( Xb )
3063 if selfA._toStore("APosterioriCovariance"):
3064 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
3065 selfA._setInternalState("seed", numpy.random.get_state())
3066 elif selfA._parameters["nextStep"]:
3067 Xn = selfA._getInternalState("Xn")
3069 previousJMinimum = numpy.finfo(float).max
3071 for step in range(duration-1):
3072 numpy.random.set_state(selfA._getInternalState("seed"))
3073 if hasattr(Y,"store"):
3074 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
3076 Ynpu = numpy.ravel( Y ).reshape((__p,1))
3079 if hasattr(U,"store") and len(U)>1:
3080 Un = numpy.asmatrix(numpy.ravel( U[step] )).T
3081 elif hasattr(U,"store") and len(U)==1:
3082 Un = numpy.asmatrix(numpy.ravel( U[0] )).T
3084 Un = numpy.asmatrix(numpy.ravel( U )).T
3088 if selfA._parameters["InflationType"] == "MultiplicativeOnBackgroundAnomalies":
3089 Xn = CovarianceInflation( Xn,
3090 selfA._parameters["InflationType"],
3091 selfA._parameters["InflationFactor"],
3094 if selfA._parameters["EstimationOf"] == "State": # Forecast + Q and observation of forecast
3095 EMX = M( [(Xn[:,i], Un) for i in range(__m)],
3097 returnSerieAsArrayMatrix = True )
3098 Xn_predicted = EnsemblePerturbationWithGivenCovariance( EMX, Q )
3099 HX_predicted = H( [(Xn_predicted[:,i], Un) for i in range(__m)],
3101 returnSerieAsArrayMatrix = True )
3102 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
3103 Cm = Cm.reshape(__n,Un.size) # ADAO & check shape
3104 Xn_predicted = Xn_predicted + Cm * Un
3105 elif selfA._parameters["EstimationOf"] == "Parameters": # Observation of forecast
3106 # --- > Par principe, M = Id, Q = 0
3107 Xn_predicted = EMX = Xn
3108 HX_predicted = H( [(Xn_predicted[:,i], Un) for i in range(__m)],
3110 returnSerieAsArrayMatrix = True )
3112 # Mean of forecast and observation of forecast
3113 Xfm = Xn_predicted.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
3114 Hfm = HX_predicted.mean(axis=1, dtype=mfp).astype('float').reshape((__p,1))
3116 #--------------------------
3117 if VariantM == "KalmanFilterFormula05":
3118 PfHT, HPfHT = 0., 0.
3119 for i in range(__m):
3120 Exfi = Xn_predicted[:,i].reshape((__n,1)) - Xfm
3121 Eyfi = HX_predicted[:,i].reshape((__p,1)) - Hfm
3122 PfHT += Exfi * Eyfi.T
3123 HPfHT += Eyfi * Eyfi.T
3124 PfHT = (1./(__m-1)) * PfHT
3125 HPfHT = (1./(__m-1)) * HPfHT
3126 Kn = PfHT * ( R + HPfHT ).I
3129 for i in range(__m):
3130 ri = numpy.random.multivariate_normal(numpy.zeros(__p), Rn)
3131 Xn[:,i] = numpy.ravel(Xn_predicted[:,i]) + Kn @ (numpy.ravel(Ynpu) + ri - HX_predicted[:,i])
3132 #--------------------------
3133 elif VariantM == "KalmanFilterFormula16":
3134 EpY = EnsembleOfCenteredPerturbations(Ynpu, Rn, __m)
3135 EpYm = EpY.mean(axis=1, dtype=mfp).astype('float').reshape((__p,1))
3137 EaX = EnsembleOfAnomalies( Xn_predicted ) / math.sqrt(__m-1)
3138 EaY = (HX_predicted - Hfm - EpY + EpYm) / math.sqrt(__m-1)
3140 Kn = EaX @ EaY.T @ numpy.linalg.inv( EaY @ EaY.T)
3142 for i in range(__m):
3143 Xn[:,i] = numpy.ravel(Xn_predicted[:,i]) + Kn @ (numpy.ravel(EpY[:,i]) - HX_predicted[:,i])
3144 #--------------------------
3146 raise ValueError("VariantM has to be chosen in the authorized methods list.")
3148 if selfA._parameters["InflationType"] == "MultiplicativeOnAnalysisAnomalies":
3149 Xn = CovarianceInflation( Xn,
3150 selfA._parameters["InflationType"],
3151 selfA._parameters["InflationFactor"],
3154 Xa = Xn.mean(axis=1, dtype=mfp).astype('float').reshape((__n,1))
3155 #--------------------------
3156 selfA._setInternalState("Xn", Xn)
3157 selfA._setInternalState("seed", numpy.random.get_state())
3158 #--------------------------
3160 if selfA._parameters["StoreInternalVariables"] \
3161 or selfA._toStore("CostFunctionJ") \
3162 or selfA._toStore("CostFunctionJb") \
3163 or selfA._toStore("CostFunctionJo") \
3164 or selfA._toStore("APosterioriCovariance") \
3165 or selfA._toStore("InnovationAtCurrentAnalysis") \
3166 or selfA._toStore("SimulatedObservationAtCurrentAnalysis") \
3167 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3168 _HXa = numpy.asmatrix(numpy.ravel( H((Xa, Un)) )).T
3169 _Innovation = Ynpu - _HXa
3171 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
3172 # ---> avec analysis
3173 selfA.StoredVariables["Analysis"].store( Xa )
3174 if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
3175 selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( _HXa )
3176 if selfA._toStore("InnovationAtCurrentAnalysis"):
3177 selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
3178 # ---> avec current state
3179 if selfA._parameters["StoreInternalVariables"] \
3180 or selfA._toStore("CurrentState"):
3181 selfA.StoredVariables["CurrentState"].store( Xn )
3182 if selfA._toStore("ForecastState"):
3183 selfA.StoredVariables["ForecastState"].store( EMX )
3184 if selfA._toStore("ForecastCovariance"):
3185 selfA.StoredVariables["ForecastCovariance"].store( EnsembleErrorCovariance(EMX) )
3186 if selfA._toStore("BMA"):
3187 selfA.StoredVariables["BMA"].store( EMX - Xa )
3188 if selfA._toStore("InnovationAtCurrentState"):
3189 selfA.StoredVariables["InnovationAtCurrentState"].store( - HX_predicted + Ynpu )
3190 if selfA._toStore("SimulatedObservationAtCurrentState") \
3191 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3192 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HX_predicted )
3194 if selfA._parameters["StoreInternalVariables"] \
3195 or selfA._toStore("CostFunctionJ") \
3196 or selfA._toStore("CostFunctionJb") \
3197 or selfA._toStore("CostFunctionJo") \
3198 or selfA._toStore("CurrentOptimum") \
3199 or selfA._toStore("APosterioriCovariance"):
3200 Jb = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
3201 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
3203 selfA.StoredVariables["CostFunctionJb"].store( Jb )
3204 selfA.StoredVariables["CostFunctionJo"].store( Jo )
3205 selfA.StoredVariables["CostFunctionJ" ].store( J )
3207 if selfA._toStore("IndexOfOptimum") \
3208 or selfA._toStore("CurrentOptimum") \
3209 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
3210 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
3211 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
3212 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3213 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
3214 if selfA._toStore("IndexOfOptimum"):
3215 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
3216 if selfA._toStore("CurrentOptimum"):
3217 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
3218 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3219 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
3220 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
3221 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
3222 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
3223 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
3224 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
3225 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
3226 if selfA._toStore("APosterioriCovariance"):
3227 selfA.StoredVariables["APosterioriCovariance"].store( EnsembleErrorCovariance(Xn) )
3228 if selfA._parameters["EstimationOf"] == "Parameters" \
3229 and J < previousJMinimum:
3230 previousJMinimum = J
3232 if selfA._toStore("APosterioriCovariance"):
3233 covarianceXaMin = selfA.StoredVariables["APosterioriCovariance"][-1]
3234 # ---> Pour les smoothers
3235 if selfA._toStore("CurrentEnsembleState"):
3236 selfA.StoredVariables["CurrentEnsembleState"].store( Xn )
3238 # Stockage final supplémentaire de l'optimum en estimation de paramètres
3239 # ----------------------------------------------------------------------
3240 if selfA._parameters["EstimationOf"] == "Parameters":
3241 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
3242 selfA.StoredVariables["Analysis"].store( XaMin )
3243 if selfA._toStore("APosterioriCovariance"):
3244 selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
3245 if selfA._toStore("BMA"):
3246 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
3250 # ==============================================================================
3251 def std3dvar(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
3260 Hm = HO["Direct"].appliedTo
3261 Ha = HO["Adjoint"].appliedInXTo
3263 # Utilisation éventuelle d'un vecteur H(Xb) précalculé
3264 if HO["AppliedInX"] is not None and "HXb" in HO["AppliedInX"]:
3265 HXb = Hm( Xb, HO["AppliedInX"]["HXb"] )
3268 HXb = HXb.reshape((-1,1))
3269 if Y.size != HXb.size:
3270 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))
3271 if max(Y.shape) != max(HXb.shape):
3272 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))
3274 if selfA._toStore("JacobianMatrixAtBackground"):
3275 HtMb = HO["Tangent"].asMatrix(ValueForMethodForm = Xb)
3276 HtMb = HtMb.reshape(Y.size,Xb.size) # ADAO & check shape
3277 selfA.StoredVariables["JacobianMatrixAtBackground"].store( HtMb )
3279 # Précalcul des inversions de B et R
3283 # Point de démarrage de l'optimisation
3284 Xini = selfA._parameters["InitializationPoint"]
3286 # Définition de la fonction-coût
3287 # ------------------------------
3288 def CostFunction(x):
3289 _X = numpy.ravel( x ).reshape((-1,1))
3290 if selfA._parameters["StoreInternalVariables"] or \
3291 selfA._toStore("CurrentState") or \
3292 selfA._toStore("CurrentOptimum"):
3293 selfA.StoredVariables["CurrentState"].store( _X )
3294 _HX = Hm( _X ).reshape((-1,1))
3295 _Innovation = Y - _HX
3296 if selfA._toStore("SimulatedObservationAtCurrentState") or \
3297 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3298 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( _HX )
3299 if selfA._toStore("InnovationAtCurrentState"):
3300 selfA.StoredVariables["InnovationAtCurrentState"].store( _Innovation )
3302 Jb = float( 0.5 * (_X - Xb).T * (BI * (_X - Xb)) )
3303 Jo = float( 0.5 * _Innovation.T * (RI * _Innovation) )
3306 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["CostFunctionJ"]) )
3307 selfA.StoredVariables["CostFunctionJb"].store( Jb )
3308 selfA.StoredVariables["CostFunctionJo"].store( Jo )
3309 selfA.StoredVariables["CostFunctionJ" ].store( J )
3310 if selfA._toStore("IndexOfOptimum") or \
3311 selfA._toStore("CurrentOptimum") or \
3312 selfA._toStore("CostFunctionJAtCurrentOptimum") or \
3313 selfA._toStore("CostFunctionJbAtCurrentOptimum") or \
3314 selfA._toStore("CostFunctionJoAtCurrentOptimum") or \
3315 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3316 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
3317 if selfA._toStore("IndexOfOptimum"):
3318 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
3319 if selfA._toStore("CurrentOptimum"):
3320 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["CurrentState"][IndexMin] )
3321 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3322 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin] )
3323 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
3324 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
3325 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
3326 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
3327 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
3328 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
3331 def GradientOfCostFunction(x):
3332 _X = x.reshape((-1,1))
3333 _HX = Hm( _X ).reshape((-1,1))
3334 GradJb = BI * (_X - Xb)
3335 GradJo = - Ha( (_X, RI * (Y - _HX)) )
3336 GradJ = numpy.ravel( GradJb ) + numpy.ravel( GradJo )
3339 # Minimisation de la fonctionnelle
3340 # --------------------------------
3341 nbPreviousSteps = selfA.StoredVariables["CostFunctionJ"].stepnumber()
3343 if selfA._parameters["Minimizer"] == "LBFGSB":
3344 if "0.19" <= scipy.version.version <= "1.1.0":
3345 import lbfgsbhlt as optimiseur
3347 import scipy.optimize as optimiseur
3348 Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
3349 func = CostFunction,
3351 fprime = GradientOfCostFunction,
3353 bounds = selfA._parameters["Bounds"],
3354 maxfun = selfA._parameters["MaximumNumberOfSteps"]-1,
3355 factr = selfA._parameters["CostDecrementTolerance"]*1.e14,
3356 pgtol = selfA._parameters["ProjectedGradientTolerance"],
3357 iprint = selfA._parameters["optiprint"],
3359 nfeval = Informations['funcalls']
3360 rc = Informations['warnflag']
3361 elif selfA._parameters["Minimizer"] == "TNC":
3362 Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
3363 func = CostFunction,
3365 fprime = GradientOfCostFunction,
3367 bounds = selfA._parameters["Bounds"],
3368 maxfun = selfA._parameters["MaximumNumberOfSteps"],
3369 pgtol = selfA._parameters["ProjectedGradientTolerance"],
3370 ftol = selfA._parameters["CostDecrementTolerance"],
3371 messages = selfA._parameters["optmessages"],
3373 elif selfA._parameters["Minimizer"] == "CG":
3374 Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
3377 fprime = GradientOfCostFunction,
3379 maxiter = selfA._parameters["MaximumNumberOfSteps"],
3380 gtol = selfA._parameters["GradientNormTolerance"],
3381 disp = selfA._parameters["optdisp"],
3384 elif selfA._parameters["Minimizer"] == "NCG":
3385 Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
3388 fprime = GradientOfCostFunction,
3390 maxiter = selfA._parameters["MaximumNumberOfSteps"],
3391 avextol = selfA._parameters["CostDecrementTolerance"],
3392 disp = selfA._parameters["optdisp"],
3395 elif selfA._parameters["Minimizer"] == "BFGS":
3396 Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
3399 fprime = GradientOfCostFunction,
3401 maxiter = selfA._parameters["MaximumNumberOfSteps"],
3402 gtol = selfA._parameters["GradientNormTolerance"],
3403 disp = selfA._parameters["optdisp"],
3407 raise ValueError("Error in Minimizer name: %s"%selfA._parameters["Minimizer"])
3409 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
3410 MinJ = selfA.StoredVariables["CostFunctionJ"][IndexMin]
3412 # Correction pour pallier a un bug de TNC sur le retour du Minimum
3413 # ----------------------------------------------------------------
3414 if selfA._parameters["StoreInternalVariables"] or selfA._toStore("CurrentState"):
3415 Minimum = selfA.StoredVariables["CurrentState"][IndexMin]
3418 #--------------------------
3420 selfA.StoredVariables["Analysis"].store( Xa )
3422 if selfA._toStore("OMA") or \
3423 selfA._toStore("SigmaObs2") or \
3424 selfA._toStore("SimulationQuantiles") or \
3425 selfA._toStore("SimulatedObservationAtOptimum"):
3426 if selfA._toStore("SimulatedObservationAtCurrentState"):
3427 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin]
3428 elif selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3429 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"][-1]
3433 if selfA._toStore("APosterioriCovariance") or \
3434 selfA._toStore("SimulationQuantiles") or \
3435 selfA._toStore("JacobianMatrixAtOptimum") or \
3436 selfA._toStore("KalmanGainAtOptimum"):
3437 HtM = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
3438 HtM = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
3439 if selfA._toStore("APosterioriCovariance") or \
3440 selfA._toStore("SimulationQuantiles") or \
3441 selfA._toStore("KalmanGainAtOptimum"):
3442 HaM = HO["Adjoint"].asMatrix(ValueForMethodForm = Xa)
3443 HaM = HaM.reshape(Xa.size,Y.size) # ADAO & check shape
3444 if selfA._toStore("APosterioriCovariance") or \
3445 selfA._toStore("SimulationQuantiles"):
3446 A = HessienneEstimation(Xa.size, HaM, HtM, BI, RI)
3447 if selfA._toStore("APosterioriCovariance"):
3448 selfA.StoredVariables["APosterioriCovariance"].store( A )
3449 if selfA._toStore("JacobianMatrixAtOptimum"):
3450 selfA.StoredVariables["JacobianMatrixAtOptimum"].store( HtM )
3451 if selfA._toStore("KalmanGainAtOptimum"):
3452 if (Y.size <= Xb.size): KG = B * HaM * (R + numpy.dot(HtM, B * HaM)).I
3453 elif (Y.size > Xb.size): KG = (BI + numpy.dot(HaM, RI * HtM)).I * HaM * RI
3454 selfA.StoredVariables["KalmanGainAtOptimum"].store( KG )
3456 # Calculs et/ou stockages supplémentaires
3457 # ---------------------------------------
3458 if selfA._toStore("Innovation") or \
3459 selfA._toStore("SigmaObs2") or \
3460 selfA._toStore("MahalanobisConsistency") or \
3461 selfA._toStore("OMB"):
3463 if selfA._toStore("Innovation"):
3464 selfA.StoredVariables["Innovation"].store( d )
3465 if selfA._toStore("BMA"):
3466 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
3467 if selfA._toStore("OMA"):
3468 selfA.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
3469 if selfA._toStore("OMB"):
3470 selfA.StoredVariables["OMB"].store( d )
3471 if selfA._toStore("SigmaObs2"):
3472 TraceR = R.trace(Y.size)
3473 selfA.StoredVariables["SigmaObs2"].store( float( (d.T @ (numpy.ravel(Y)-numpy.ravel(HXa))) ) / TraceR )
3474 if selfA._toStore("MahalanobisConsistency"):
3475 selfA.StoredVariables["MahalanobisConsistency"].store( float( 2.*MinJ/d.size ) )
3476 if selfA._toStore("SimulationQuantiles"):
3477 QuantilesEstimations(selfA, A, Xa, HXa, Hm, HtM)
3478 if selfA._toStore("SimulatedObservationAtBackground"):
3479 selfA.StoredVariables["SimulatedObservationAtBackground"].store( HXb )
3480 if selfA._toStore("SimulatedObservationAtOptimum"):
3481 selfA.StoredVariables["SimulatedObservationAtOptimum"].store( HXa )
3485 # ==============================================================================
3486 def std4dvar(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
3495 Hm = HO["Direct"].appliedControledFormTo
3496 Mm = EM["Direct"].appliedControledFormTo
3498 if CM is not None and "Tangent" in CM and U is not None:
3499 Cm = CM["Tangent"].asMatrix(Xb)
3505 if hasattr(U,"store") and 1<=_step<len(U) :
3506 _Un = numpy.asmatrix(numpy.ravel( U[_step] )).T
3507 elif hasattr(U,"store") and len(U)==1:
3508 _Un = numpy.asmatrix(numpy.ravel( U[0] )).T
3510 _Un = numpy.asmatrix(numpy.ravel( U )).T
3515 if Cm is not None and _un is not None: # Attention : si Cm est aussi dans M, doublon !
3516 _Cm = Cm.reshape(_xn.size,_un.size) # ADAO & check shape
3522 # Remarque : les observations sont exploitées à partir du pas de temps
3523 # numéro 1, et sont utilisées dans Yo comme rangées selon ces indices.
3524 # Donc le pas 0 n'est pas utilisé puisque la première étape commence
3525 # avec l'observation du pas 1.
3527 # Nombre de pas identique au nombre de pas d'observations
3528 if hasattr(Y,"stepnumber"):
3529 duration = Y.stepnumber()
3533 # Précalcul des inversions de B et R
3537 # Point de démarrage de l'optimisation
3538 Xini = selfA._parameters["InitializationPoint"]
3540 # Définition de la fonction-coût
3541 # ------------------------------
3542 selfA.DirectCalculation = [None,] # Le pas 0 n'est pas observé
3543 selfA.DirectInnovation = [None,] # Le pas 0 n'est pas observé
3544 def CostFunction(x):
3545 _X = numpy.asmatrix(numpy.ravel( x )).T
3546 if selfA._parameters["StoreInternalVariables"] or \
3547 selfA._toStore("CurrentState") or \
3548 selfA._toStore("CurrentOptimum"):
3549 selfA.StoredVariables["CurrentState"].store( _X )
3550 Jb = float( 0.5 * (_X - Xb).T * BI * (_X - Xb) )
3551 selfA.DirectCalculation = [None,]
3552 selfA.DirectInnovation = [None,]
3555 for step in range(0,duration-1):
3556 if hasattr(Y,"store"):
3557 _Ynpu = numpy.asmatrix(numpy.ravel( Y[step+1] )).T
3559 _Ynpu = numpy.asmatrix(numpy.ravel( Y )).T
3563 if selfA._parameters["EstimationOf"] == "State":
3564 _Xn = Mm( (_Xn, _Un) ) + CmUn(_Xn, _Un)
3565 elif selfA._parameters["EstimationOf"] == "Parameters":
3568 if selfA._parameters["Bounds"] is not None and selfA._parameters["ConstrainedBy"] == "EstimateProjection":
3569 _Xn = ApplyBounds( _Xn, ForceNumericBounds(selfA._parameters["Bounds"]) )
3571 # Etape de différence aux observations
3572 if selfA._parameters["EstimationOf"] == "State":
3573 _YmHMX = _Ynpu - numpy.asmatrix(numpy.ravel( Hm( (_Xn, None) ) )).T
3574 elif selfA._parameters["EstimationOf"] == "Parameters":
3575 _YmHMX = _Ynpu - numpy.asmatrix(numpy.ravel( Hm( (_Xn, _Un) ) )).T - CmUn(_Xn, _Un)
3577 # Stockage de l'état
3578 selfA.DirectCalculation.append( _Xn )
3579 selfA.DirectInnovation.append( _YmHMX )
3581 # Ajout dans la fonctionnelle d'observation
3582 Jo = Jo + 0.5 * float( _YmHMX.T * RI * _YmHMX )
3585 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["CostFunctionJ"]) )
3586 selfA.StoredVariables["CostFunctionJb"].store( Jb )
3587 selfA.StoredVariables["CostFunctionJo"].store( Jo )
3588 selfA.StoredVariables["CostFunctionJ" ].store( J )
3589 if selfA._toStore("IndexOfOptimum") or \
3590 selfA._toStore("CurrentOptimum") or \
3591 selfA._toStore("CostFunctionJAtCurrentOptimum") or \
3592 selfA._toStore("CostFunctionJbAtCurrentOptimum") or \
3593 selfA._toStore("CostFunctionJoAtCurrentOptimum"):
3594 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
3595 if selfA._toStore("IndexOfOptimum"):
3596 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
3597 if selfA._toStore("CurrentOptimum"):
3598 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["CurrentState"][IndexMin] )
3599 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
3600 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
3601 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
3602 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
3603 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
3604 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
3607 def GradientOfCostFunction(x):
3608 _X = numpy.asmatrix(numpy.ravel( x )).T
3609 GradJb = BI * (_X - Xb)
3611 for step in range(duration-1,0,-1):
3612 # Étape de récupération du dernier stockage de l'évolution
3613 _Xn = selfA.DirectCalculation.pop()
3614 # Étape de récupération du dernier stockage de l'innovation
3615 _YmHMX = selfA.DirectInnovation.pop()
3616 # Calcul des adjoints
3617 Ha = HO["Adjoint"].asMatrix(ValueForMethodForm = _Xn)
3618 Ha = Ha.reshape(_Xn.size,_YmHMX.size) # ADAO & check shape
3619 Ma = EM["Adjoint"].asMatrix(ValueForMethodForm = _Xn)
3620 Ma = Ma.reshape(_Xn.size,_Xn.size) # ADAO & check shape
3621 # Calcul du gradient par état adjoint
3622 GradJo = GradJo + Ha * (RI * _YmHMX) # Équivaut pour Ha linéaire à : Ha( (_Xn, RI * _YmHMX) )
3623 GradJo = Ma * GradJo # Équivaut pour Ma linéaire à : Ma( (_Xn, GradJo) )
3624 GradJ = numpy.ravel( GradJb ) - numpy.ravel( GradJo )
3627 # Minimisation de la fonctionnelle
3628 # --------------------------------
3629 nbPreviousSteps = selfA.StoredVariables["CostFunctionJ"].stepnumber()
3631 if selfA._parameters["Minimizer"] == "LBFGSB":
3632 if "0.19" <= scipy.version.version <= "1.1.0":
3633 import lbfgsbhlt as optimiseur
3635 import scipy.optimize as optimiseur
3636 Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
3637 func = CostFunction,
3639 fprime = GradientOfCostFunction,
3641 bounds = selfA._parameters["Bounds"],
3642 maxfun = selfA._parameters["MaximumNumberOfSteps"]-1,
3643 factr = selfA._parameters["CostDecrementTolerance"]*1.e14,
3644 pgtol = selfA._parameters["ProjectedGradientTolerance"],
3645 iprint = selfA._parameters["optiprint"],
3647 nfeval = Informations['funcalls']
3648 rc = Informations['warnflag']
3649 elif selfA._parameters["Minimizer"] == "TNC":
3650 Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
3651 func = CostFunction,
3653 fprime = GradientOfCostFunction,
3655 bounds = selfA._parameters["Bounds"],
3656 maxfun = selfA._parameters["MaximumNumberOfSteps"],
3657 pgtol = selfA._parameters["ProjectedGradientTolerance"],
3658 ftol = selfA._parameters["CostDecrementTolerance"],
3659 messages = selfA._parameters["optmessages"],
3661 elif selfA._parameters["Minimizer"] == "CG":
3662 Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
3665 fprime = GradientOfCostFunction,
3667 maxiter = selfA._parameters["MaximumNumberOfSteps"],
3668 gtol = selfA._parameters["GradientNormTolerance"],
3669 disp = selfA._parameters["optdisp"],
3672 elif selfA._parameters["Minimizer"] == "NCG":
3673 Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
3676 fprime = GradientOfCostFunction,
3678 maxiter = selfA._parameters["MaximumNumberOfSteps"],
3679 avextol = selfA._parameters["CostDecrementTolerance"],
3680 disp = selfA._parameters["optdisp"],
3683 elif selfA._parameters["Minimizer"] == "BFGS":
3684 Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
3687 fprime = GradientOfCostFunction,
3689 maxiter = selfA._parameters["MaximumNumberOfSteps"],
3690 gtol = selfA._parameters["GradientNormTolerance"],
3691 disp = selfA._parameters["optdisp"],
3695 raise ValueError("Error in Minimizer name: %s"%selfA._parameters["Minimizer"])
3697 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
3698 MinJ = selfA.StoredVariables["CostFunctionJ"][IndexMin]
3700 # Correction pour pallier a un bug de TNC sur le retour du Minimum
3701 # ----------------------------------------------------------------
3702 if selfA._parameters["StoreInternalVariables"] or selfA._toStore("CurrentState"):
3703 Minimum = selfA.StoredVariables["CurrentState"][IndexMin]
3705 # Obtention de l'analyse
3706 # ----------------------
3709 selfA.StoredVariables["Analysis"].store( Xa )
3711 # Calculs et/ou stockages supplémentaires
3712 # ---------------------------------------
3713 if selfA._toStore("BMA"):
3714 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
3718 # ==============================================================================
3719 def stdkf(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
3721 Standard Kalman Filter
3723 if selfA._parameters["EstimationOf"] == "Parameters":
3724 selfA._parameters["StoreInternalVariables"] = True
3728 Ht = HO["Tangent"].asMatrix(Xb)
3729 Ha = HO["Adjoint"].asMatrix(Xb)
3731 if selfA._parameters["EstimationOf"] == "State":
3732 Mt = EM["Tangent"].asMatrix(Xb)
3733 Ma = EM["Adjoint"].asMatrix(Xb)
3735 if CM is not None and "Tangent" in CM and U is not None:
3736 Cm = CM["Tangent"].asMatrix(Xb)
3740 # Durée d'observation et tailles
3741 if hasattr(Y,"stepnumber"):
3742 duration = Y.stepnumber()
3743 __p = numpy.cumprod(Y.shape())[-1]
3746 __p = numpy.array(Y).size
3748 # Précalcul des inversions de B et R
3749 if selfA._parameters["StoreInternalVariables"] \
3750 or selfA._toStore("CostFunctionJ") \
3751 or selfA._toStore("CostFunctionJb") \
3752 or selfA._toStore("CostFunctionJo") \
3753 or selfA._toStore("CurrentOptimum") \
3754 or selfA._toStore("APosterioriCovariance"):
3760 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
3763 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
3764 selfA.StoredVariables["Analysis"].store( Xb )
3765 if selfA._toStore("APosterioriCovariance"):
3766 if hasattr(B,"asfullmatrix"):
3767 selfA.StoredVariables["APosterioriCovariance"].store( B.asfullmatrix(__n) )
3769 selfA.StoredVariables["APosterioriCovariance"].store( B )
3770 selfA._setInternalState("seed", numpy.random.get_state())
3771 elif selfA._parameters["nextStep"]:
3772 Xn = selfA._getInternalState("Xn")
3773 Pn = selfA._getInternalState("Pn")
3775 if selfA._parameters["EstimationOf"] == "Parameters":
3777 previousJMinimum = numpy.finfo(float).max
3779 for step in range(duration-1):
3780 if hasattr(Y,"store"):
3781 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
3783 Ynpu = numpy.ravel( Y ).reshape((__p,1))
3786 if hasattr(U,"store") and len(U)>1:
3787 Un = numpy.asmatrix(numpy.ravel( U[step] )).T
3788 elif hasattr(U,"store") and len(U)==1:
3789 Un = numpy.asmatrix(numpy.ravel( U[0] )).T
3791 Un = numpy.asmatrix(numpy.ravel( U )).T
3795 if selfA._parameters["EstimationOf"] == "State": # Forecast + Q and observation of forecast
3796 Xn_predicted = Mt * Xn
3797 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
3798 Cm = Cm.reshape(__n,Un.size) # ADAO & check shape
3799 Xn_predicted = Xn_predicted + Cm * Un
3800 Pn_predicted = Q + Mt * (Pn * Ma)
3801 elif selfA._parameters["EstimationOf"] == "Parameters": # Observation of forecast
3802 # --- > Par principe, M = Id, Q = 0
3806 if selfA._parameters["EstimationOf"] == "State":
3807 HX_predicted = Ht * Xn_predicted
3808 _Innovation = Ynpu - HX_predicted
3809 elif selfA._parameters["EstimationOf"] == "Parameters":
3810 HX_predicted = Ht * Xn_predicted
3811 _Innovation = Ynpu - HX_predicted
3812 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans H, doublon !
3813 _Innovation = _Innovation - Cm * Un
3815 Kn = Pn_predicted * Ha * numpy.linalg.inv(R + numpy.dot(Ht, Pn_predicted * Ha))
3816 Xn = Xn_predicted + Kn * _Innovation
3817 Pn = Pn_predicted - Kn * Ht * Pn_predicted
3820 #--------------------------
3821 selfA._setInternalState("Xn", Xn)
3822 selfA._setInternalState("Pn", Pn)
3823 #--------------------------
3825 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
3826 # ---> avec analysis
3827 selfA.StoredVariables["Analysis"].store( Xa )
3828 if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
3829 selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( Ht * Xa )
3830 if selfA._toStore("InnovationAtCurrentAnalysis"):
3831 selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
3832 # ---> avec current state
3833 if selfA._parameters["StoreInternalVariables"] \
3834 or selfA._toStore("CurrentState"):
3835 selfA.StoredVariables["CurrentState"].store( Xn )
3836 if selfA._toStore("ForecastState"):
3837 selfA.StoredVariables["ForecastState"].store( Xn_predicted )
3838 if selfA._toStore("ForecastCovariance"):
3839 selfA.StoredVariables["ForecastCovariance"].store( Pn_predicted )
3840 if selfA._toStore("BMA"):
3841 selfA.StoredVariables["BMA"].store( Xn_predicted - Xa )
3842 if selfA._toStore("InnovationAtCurrentState"):
3843 selfA.StoredVariables["InnovationAtCurrentState"].store( _Innovation )
3844 if selfA._toStore("SimulatedObservationAtCurrentState") \
3845 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3846 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( HX_predicted )
3848 if selfA._parameters["StoreInternalVariables"] \
3849 or selfA._toStore("CostFunctionJ") \
3850 or selfA._toStore("CostFunctionJb") \
3851 or selfA._toStore("CostFunctionJo") \
3852 or selfA._toStore("CurrentOptimum") \
3853 or selfA._toStore("APosterioriCovariance"):
3854 Jb = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
3855 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
3857 selfA.StoredVariables["CostFunctionJb"].store( Jb )
3858 selfA.StoredVariables["CostFunctionJo"].store( Jo )
3859 selfA.StoredVariables["CostFunctionJ" ].store( J )
3861 if selfA._toStore("IndexOfOptimum") \
3862 or selfA._toStore("CurrentOptimum") \
3863 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
3864 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
3865 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
3866 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3867 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
3868 if selfA._toStore("IndexOfOptimum"):
3869 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
3870 if selfA._toStore("CurrentOptimum"):
3871 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
3872 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
3873 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
3874 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
3875 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
3876 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
3877 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
3878 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
3879 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
3880 if selfA._toStore("APosterioriCovariance"):
3881 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
3882 if selfA._parameters["EstimationOf"] == "Parameters" \
3883 and J < previousJMinimum:
3884 previousJMinimum = J
3886 if selfA._toStore("APosterioriCovariance"):
3887 covarianceXaMin = selfA.StoredVariables["APosterioriCovariance"][-1]
3889 # Stockage final supplémentaire de l'optimum en estimation de paramètres
3890 # ----------------------------------------------------------------------
3891 if selfA._parameters["EstimationOf"] == "Parameters":
3892 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
3893 selfA.StoredVariables["Analysis"].store( XaMin )
3894 if selfA._toStore("APosterioriCovariance"):
3895 selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
3896 if selfA._toStore("BMA"):
3897 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
3901 # ==============================================================================
3902 def uskf(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
3904 Unscented Kalman Filter
3906 if selfA._parameters["EstimationOf"] == "Parameters":
3907 selfA._parameters["StoreInternalVariables"] = True
3910 Alpha = selfA._parameters["Alpha"]
3911 Beta = selfA._parameters["Beta"]
3912 if selfA._parameters["Kappa"] == 0:
3913 if selfA._parameters["EstimationOf"] == "State":
3915 elif selfA._parameters["EstimationOf"] == "Parameters":
3918 Kappa = selfA._parameters["Kappa"]
3919 Lambda = float( Alpha**2 ) * ( L + Kappa ) - L
3920 Gamma = math.sqrt( L + Lambda )
3924 for i in range(2*L):
3925 Ww.append( 1. / (2.*(L + Lambda)) )
3927 Wm = numpy.array( Ww )
3928 Wm[0] = Lambda / (L + Lambda)
3929 Wc = numpy.array( Ww )
3930 Wc[0] = Lambda / (L + Lambda) + (1. - Alpha**2 + Beta)
3933 Hm = HO["Direct"].appliedControledFormTo
3935 if selfA._parameters["EstimationOf"] == "State":
3936 Mm = EM["Direct"].appliedControledFormTo
3938 if CM is not None and "Tangent" in CM and U is not None:
3939 Cm = CM["Tangent"].asMatrix(Xb)
3943 # Durée d'observation et tailles
3944 if hasattr(Y,"stepnumber"):
3945 duration = Y.stepnumber()
3946 __p = numpy.cumprod(Y.shape())[-1]
3949 __p = numpy.array(Y).size
3951 # Précalcul des inversions de B et R
3952 if selfA._parameters["StoreInternalVariables"] \
3953 or selfA._toStore("CostFunctionJ") \
3954 or selfA._toStore("CostFunctionJb") \
3955 or selfA._toStore("CostFunctionJo") \
3956 or selfA._toStore("CurrentOptimum") \
3957 or selfA._toStore("APosterioriCovariance"):
3963 if len(selfA.StoredVariables["Analysis"])==0 or not selfA._parameters["nextStep"]:
3965 if hasattr(B,"asfullmatrix"):
3966 Pn = B.asfullmatrix(__n)
3969 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
3970 selfA.StoredVariables["Analysis"].store( Xb )
3971 if selfA._toStore("APosterioriCovariance"):
3972 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
3973 elif selfA._parameters["nextStep"]:
3974 Xn = selfA._getInternalState("Xn")
3975 Pn = selfA._getInternalState("Pn")
3977 if selfA._parameters["EstimationOf"] == "Parameters":
3979 previousJMinimum = numpy.finfo(float).max
3981 for step in range(duration-1):
3982 if hasattr(Y,"store"):
3983 Ynpu = numpy.ravel( Y[step+1] ).reshape((__p,1))
3985 Ynpu = numpy.ravel( Y ).reshape((__p,1))
3988 if hasattr(U,"store") and len(U)>1:
3989 Un = numpy.ravel( U[step] ).reshape((-1,1))
3990 elif hasattr(U,"store") and len(U)==1:
3991 Un = numpy.ravel( U[0] ).reshape((-1,1))
3993 Un = numpy.ravel( U ).reshape((-1,1))
3997 Pndemi = numpy.real(scipy.linalg.sqrtm(Pn))
3998 Xnp = numpy.hstack([Xn, Xn+Gamma*Pndemi, Xn-Gamma*Pndemi])
3999 nbSpts = 2*Xn.size+1
4002 for point in range(nbSpts):
4003 if selfA._parameters["EstimationOf"] == "State":
4004 XEtnnpi = numpy.asarray( Mm( (Xnp[:,point], Un) ) ).reshape((-1,1))
4005 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
4006 Cm = Cm.reshape(Xn.size,Un.size) # ADAO & check shape
4007 XEtnnpi = XEtnnpi + Cm * Un
4008 elif selfA._parameters["EstimationOf"] == "Parameters":
4009 # --- > Par principe, M = Id, Q = 0
4010 XEtnnpi = Xnp[:,point]
4011 XEtnnp.append( numpy.ravel(XEtnnpi).reshape((-1,1)) )
4012 XEtnnp = numpy.concatenate( XEtnnp, axis=1 )
4014 Xncm = ( XEtnnp * Wm ).sum(axis=1)
4016 if selfA._parameters["EstimationOf"] == "State": Pnm = Q
4017 elif selfA._parameters["EstimationOf"] == "Parameters": Pnm = 0.
4018 for point in range(nbSpts):
4019 Pnm += Wc[i] * ((XEtnnp[:,point]-Xncm).reshape((-1,1)) * (XEtnnp[:,point]-Xncm))
4021 Pnmdemi = numpy.real(scipy.linalg.sqrtm(Pnm))
4023 Xnnp = numpy.hstack([Xncm.reshape((-1,1)), Xncm.reshape((-1,1))+Gamma*Pnmdemi, Xncm.reshape((-1,1))-Gamma*Pnmdemi])
4026 for point in range(nbSpts):
4027 if selfA._parameters["EstimationOf"] == "State":
4028 Ynnpi = Hm( (Xnnp[:,point], None) )
4029 elif selfA._parameters["EstimationOf"] == "Parameters":
4030 Ynnpi = Hm( (Xnnp[:,point], Un) )
4031 Ynnp.append( numpy.ravel(Ynnpi).reshape((-1,1)) )
4032 Ynnp = numpy.concatenate( Ynnp, axis=1 )
4034 Yncm = ( Ynnp * Wm ).sum(axis=1)
4038 for point in range(nbSpts):
4039 Pyyn += Wc[i] * ((Ynnp[:,point]-Yncm).reshape((-1,1)) * (Ynnp[:,point]-Yncm))
4040 Pxyn += Wc[i] * ((Xnnp[:,point]-Xncm).reshape((-1,1)) * (Ynnp[:,point]-Yncm))
4042 _Innovation = Ynpu - Yncm.reshape((-1,1))
4043 if selfA._parameters["EstimationOf"] == "Parameters":
4044 if Cm is not None and Un is not None: # Attention : si Cm est aussi dans H, doublon !
4045 _Innovation = _Innovation - Cm * Un
4048 Xn = Xncm.reshape((-1,1)) + Kn * _Innovation
4049 Pn = Pnm - Kn * Pyyn * Kn.T
4052 #--------------------------
4053 selfA._setInternalState("Xn", Xn)
4054 selfA._setInternalState("Pn", Pn)
4055 #--------------------------
4057 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
4058 # ---> avec analysis
4059 selfA.StoredVariables["Analysis"].store( Xa )
4060 if selfA._toStore("SimulatedObservationAtCurrentAnalysis"):
4061 selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"].store( Hm((Xa, Un)) )
4062 if selfA._toStore("InnovationAtCurrentAnalysis"):
4063 selfA.StoredVariables["InnovationAtCurrentAnalysis"].store( _Innovation )
4064 # ---> avec current state
4065 if selfA._parameters["StoreInternalVariables"] \
4066 or selfA._toStore("CurrentState"):
4067 selfA.StoredVariables["CurrentState"].store( Xn )
4068 if selfA._toStore("ForecastState"):
4069 selfA.StoredVariables["ForecastState"].store( Xncm )
4070 if selfA._toStore("ForecastCovariance"):
4071 selfA.StoredVariables["ForecastCovariance"].store( Pnm )
4072 if selfA._toStore("BMA"):
4073 selfA.StoredVariables["BMA"].store( Xncm - Xa )
4074 if selfA._toStore("InnovationAtCurrentState"):
4075 selfA.StoredVariables["InnovationAtCurrentState"].store( _Innovation )
4076 if selfA._toStore("SimulatedObservationAtCurrentState") \
4077 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
4078 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( Yncm )
4080 if selfA._parameters["StoreInternalVariables"] \
4081 or selfA._toStore("CostFunctionJ") \
4082 or selfA._toStore("CostFunctionJb") \
4083 or selfA._toStore("CostFunctionJo") \
4084 or selfA._toStore("CurrentOptimum") \
4085 or selfA._toStore("APosterioriCovariance"):
4086 Jb = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
4087 Jo = float( 0.5 * _Innovation.T * (RI * _Innovation) )
4089 selfA.StoredVariables["CostFunctionJb"].store( Jb )
4090 selfA.StoredVariables["CostFunctionJo"].store( Jo )
4091 selfA.StoredVariables["CostFunctionJ" ].store( J )
4093 if selfA._toStore("IndexOfOptimum") \
4094 or selfA._toStore("CurrentOptimum") \
4095 or selfA._toStore("CostFunctionJAtCurrentOptimum") \
4096 or selfA._toStore("CostFunctionJbAtCurrentOptimum") \
4097 or selfA._toStore("CostFunctionJoAtCurrentOptimum") \
4098 or selfA._toStore("SimulatedObservationAtCurrentOptimum"):
4099 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
4100 if selfA._toStore("IndexOfOptimum"):
4101 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
4102 if selfA._toStore("CurrentOptimum"):
4103 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["Analysis"][IndexMin] )
4104 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
4105 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentAnalysis"][IndexMin] )
4106 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
4107 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
4108 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
4109 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
4110 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
4111 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
4112 if selfA._toStore("APosterioriCovariance"):
4113 selfA.StoredVariables["APosterioriCovariance"].store( Pn )
4114 if selfA._parameters["EstimationOf"] == "Parameters" \
4115 and J < previousJMinimum:
4116 previousJMinimum = J
4118 if selfA._toStore("APosterioriCovariance"):
4119 covarianceXaMin = selfA.StoredVariables["APosterioriCovariance"][-1]
4121 # Stockage final supplémentaire de l'optimum en estimation de paramètres
4122 # ----------------------------------------------------------------------
4123 if selfA._parameters["EstimationOf"] == "Parameters":
4124 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["Analysis"]) )
4125 selfA.StoredVariables["Analysis"].store( XaMin )
4126 if selfA._toStore("APosterioriCovariance"):
4127 selfA.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
4128 if selfA._toStore("BMA"):
4129 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
4133 # ==============================================================================
4134 def van3dvar(selfA, Xb, Y, U, HO, EM, CM, R, B, Q):
4136 3DVAR variational analysis with no inversion of B
4143 Hm = HO["Direct"].appliedTo
4144 Ha = HO["Adjoint"].appliedInXTo
4146 # Précalcul des inversions de B et R
4150 # Point de démarrage de l'optimisation
4151 Xini = numpy.zeros(Xb.shape)
4153 # Définition de la fonction-coût
4154 # ------------------------------
4155 def CostFunction(v):
4156 _V = numpy.asmatrix(numpy.ravel( v )).T
4158 if selfA._parameters["StoreInternalVariables"] or \
4159 selfA._toStore("CurrentState") or \
4160 selfA._toStore("CurrentOptimum"):
4161 selfA.StoredVariables["CurrentState"].store( _X )
4163 _HX = numpy.asmatrix(numpy.ravel( _HX )).T
4164 _Innovation = Y - _HX
4165 if selfA._toStore("SimulatedObservationAtCurrentState") or \
4166 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
4167 selfA.StoredVariables["SimulatedObservationAtCurrentState"].store( _HX )
4168 if selfA._toStore("InnovationAtCurrentState"):
4169 selfA.StoredVariables["InnovationAtCurrentState"].store( _Innovation )
4171 Jb = float( 0.5 * _V.T * BT * _V )
4172 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
4175 selfA.StoredVariables["CurrentIterationNumber"].store( len(selfA.StoredVariables["CostFunctionJ"]) )
4176 selfA.StoredVariables["CostFunctionJb"].store( Jb )
4177 selfA.StoredVariables["CostFunctionJo"].store( Jo )
4178 selfA.StoredVariables["CostFunctionJ" ].store( J )
4179 if selfA._toStore("IndexOfOptimum") or \
4180 selfA._toStore("CurrentOptimum") or \
4181 selfA._toStore("CostFunctionJAtCurrentOptimum") or \
4182 selfA._toStore("CostFunctionJbAtCurrentOptimum") or \
4183 selfA._toStore("CostFunctionJoAtCurrentOptimum") or \
4184 selfA._toStore("SimulatedObservationAtCurrentOptimum"):
4185 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
4186 if selfA._toStore("IndexOfOptimum"):
4187 selfA.StoredVariables["IndexOfOptimum"].store( IndexMin )
4188 if selfA._toStore("CurrentOptimum"):
4189 selfA.StoredVariables["CurrentOptimum"].store( selfA.StoredVariables["CurrentState"][IndexMin] )
4190 if selfA._toStore("SimulatedObservationAtCurrentOptimum"):
4191 selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin] )
4192 if selfA._toStore("CostFunctionJbAtCurrentOptimum"):
4193 selfA.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJb"][IndexMin] )
4194 if selfA._toStore("CostFunctionJoAtCurrentOptimum"):
4195 selfA.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( selfA.StoredVariables["CostFunctionJo"][IndexMin] )
4196 if selfA._toStore("CostFunctionJAtCurrentOptimum"):
4197 selfA.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( selfA.StoredVariables["CostFunctionJ" ][IndexMin] )
4200 def GradientOfCostFunction(v):
4201 _V = v.reshape((-1,1))
4202 _X = Xb + (B @ _V).reshape((-1,1))
4203 _HX = Hm( _X ).reshape((-1,1))
4205 GradJo = - Ha( (_X, RI * (Y - _HX)) )
4206 GradJ = numpy.ravel( GradJb ) + numpy.ravel( GradJo )
4209 # Minimisation de la fonctionnelle
4210 # --------------------------------
4211 nbPreviousSteps = selfA.StoredVariables["CostFunctionJ"].stepnumber()
4213 if selfA._parameters["Minimizer"] == "LBFGSB":
4214 if "0.19" <= scipy.version.version <= "1.1.0":
4215 import lbfgsbhlt as optimiseur
4217 import scipy.optimize as optimiseur
4218 Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
4219 func = CostFunction,
4221 fprime = GradientOfCostFunction,
4223 bounds = RecentredBounds(selfA._parameters["Bounds"], Xb),
4224 maxfun = selfA._parameters["MaximumNumberOfSteps"]-1,
4225 factr = selfA._parameters["CostDecrementTolerance"]*1.e14,
4226 pgtol = selfA._parameters["ProjectedGradientTolerance"],
4227 iprint = selfA._parameters["optiprint"],
4229 nfeval = Informations['funcalls']
4230 rc = Informations['warnflag']
4231 elif selfA._parameters["Minimizer"] == "TNC":
4232 Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
4233 func = CostFunction,
4235 fprime = GradientOfCostFunction,
4237 bounds = RecentredBounds(selfA._parameters["Bounds"], Xb),
4238 maxfun = selfA._parameters["MaximumNumberOfSteps"],
4239 pgtol = selfA._parameters["ProjectedGradientTolerance"],
4240 ftol = selfA._parameters["CostDecrementTolerance"],
4241 messages = selfA._parameters["optmessages"],
4243 elif selfA._parameters["Minimizer"] == "CG":
4244 Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
4247 fprime = GradientOfCostFunction,
4249 maxiter = selfA._parameters["MaximumNumberOfSteps"],
4250 gtol = selfA._parameters["GradientNormTolerance"],
4251 disp = selfA._parameters["optdisp"],
4254 elif selfA._parameters["Minimizer"] == "NCG":
4255 Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
4258 fprime = GradientOfCostFunction,
4260 maxiter = selfA._parameters["MaximumNumberOfSteps"],
4261 avextol = selfA._parameters["CostDecrementTolerance"],
4262 disp = selfA._parameters["optdisp"],
4265 elif selfA._parameters["Minimizer"] == "BFGS":
4266 Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
4269 fprime = GradientOfCostFunction,
4271 maxiter = selfA._parameters["MaximumNumberOfSteps"],
4272 gtol = selfA._parameters["GradientNormTolerance"],
4273 disp = selfA._parameters["optdisp"],
4277 raise ValueError("Error in Minimizer name: %s"%selfA._parameters["Minimizer"])
4279 IndexMin = numpy.argmin( selfA.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
4280 MinJ = selfA.StoredVariables["CostFunctionJ"][IndexMin]
4282 # Correction pour pallier a un bug de TNC sur le retour du Minimum
4283 # ----------------------------------------------------------------
4284 if selfA._parameters["StoreInternalVariables"] or selfA._toStore("CurrentState"):
4285 Minimum = selfA.StoredVariables["CurrentState"][IndexMin]
4287 Minimum = Xb + B * Minimum.reshape((-1,1)) # Pas @
4290 #--------------------------
4292 selfA.StoredVariables["Analysis"].store( Xa )
4294 if selfA._toStore("OMA") or \
4295 selfA._toStore("SigmaObs2") or \
4296 selfA._toStore("SimulationQuantiles") or \
4297 selfA._toStore("SimulatedObservationAtOptimum"):
4298 if selfA._toStore("SimulatedObservationAtCurrentState"):
4299 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin]
4300 elif selfA._toStore("SimulatedObservationAtCurrentOptimum"):
4301 HXa = selfA.StoredVariables["SimulatedObservationAtCurrentOptimum"][-1]
4305 if selfA._toStore("APosterioriCovariance") or \
4306 selfA._toStore("SimulationQuantiles") or \
4307 selfA._toStore("JacobianMatrixAtOptimum") or \
4308 selfA._toStore("KalmanGainAtOptimum"):
4309 HtM = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
4310 HtM = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
4311 if selfA._toStore("APosterioriCovariance") or \
4312 selfA._toStore("SimulationQuantiles") or \
4313 selfA._toStore("KalmanGainAtOptimum"):
4314 HaM = HO["Adjoint"].asMatrix(ValueForMethodForm = Xa)
4315 HaM = HaM.reshape(Xa.size,Y.size) # ADAO & check shape
4316 if selfA._toStore("APosterioriCovariance") or \
4317 selfA._toStore("SimulationQuantiles"):
4319 A = HessienneEstimation(Xa.size, HaM, HtM, BI, RI)
4320 if selfA._toStore("APosterioriCovariance"):
4321 selfA.StoredVariables["APosterioriCovariance"].store( A )
4322 if selfA._toStore("JacobianMatrixAtOptimum"):
4323 selfA.StoredVariables["JacobianMatrixAtOptimum"].store( HtM )
4324 if selfA._toStore("KalmanGainAtOptimum"):
4325 if (Y.size <= Xb.size): KG = B * HaM * (R + numpy.dot(HtM, B * HaM)).I
4326 elif (Y.size > Xb.size): KG = (BI + numpy.dot(HaM, RI * HtM)).I * HaM * RI
4327 selfA.StoredVariables["KalmanGainAtOptimum"].store( KG )
4329 # Calculs et/ou stockages supplémentaires
4330 # ---------------------------------------
4331 if selfA._toStore("Innovation") or \
4332 selfA._toStore("SigmaObs2") or \
4333 selfA._toStore("MahalanobisConsistency") or \
4334 selfA._toStore("OMB"):
4336 if selfA._toStore("Innovation"):
4337 selfA.StoredVariables["Innovation"].store( d )
4338 if selfA._toStore("BMA"):
4339 selfA.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
4340 if selfA._toStore("OMA"):
4341 selfA.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
4342 if selfA._toStore("OMB"):
4343 selfA.StoredVariables["OMB"].store( d )
4344 if selfA._toStore("SigmaObs2"):
4345 TraceR = R.trace(Y.size)
4346 selfA.StoredVariables["SigmaObs2"].store( float( (d.T @ (numpy.ravel(Y)-numpy.ravel(HXa))) ) / TraceR )
4347 if selfA._toStore("MahalanobisConsistency"):
4348 selfA.StoredVariables["MahalanobisConsistency"].store( float( 2.*MinJ/d.size ) )
4349 if selfA._toStore("SimulationQuantiles"):
4350 QuantilesEstimations(selfA, A, Xa, HXa, Hm, HtM)
4351 if selfA._toStore("SimulatedObservationAtBackground"):
4352 selfA.StoredVariables["SimulatedObservationAtBackground"].store( HXb )
4353 if selfA._toStore("SimulatedObservationAtOptimum"):
4354 selfA.StoredVariables["SimulatedObservationAtOptimum"].store( HXa )
4358 # ==============================================================================
4359 if __name__ == "__main__":
4360 print('\n AUTODIAGNOSTIC\n')