def test_operator_dyn(): """Simple test using 1D linear advection.""" Nx = 6 tseq = modelling.Chronology(dt=1, dko=5, T=10) Fm = Fmat(Nx, c=-1, dx=1, dt=tseq.dt) def step(x, t, dt): assert dt == tseq.dt return x @ Fm.T Dyn = { "M": Nx, "model": step, "linear": lambda x, t, dt: Fm, "noise": 0, } Dyn_op = modelling.Operator(**Dyn) # Square wave x = np.array([1, 1, 1, 0, 0, 0]) x1 = Dyn_op(x, tseq.T - 1, tseq.dt) assert (x1 == np.array([0, 1, 1, 1, 0, 0])).all() x2 = Dyn_op(x1, tseq.T - 3, 1) assert (x2 == np.array([0, 0, 1, 1, 1, 0])).all() x3 = Dyn_op(x2, tseq.T - 4, 1) assert (x3 == np.array([0, 0, 0, 1, 1, 1])).all() x4 = Dyn_op(x3, tseq.T - 5, 1) assert (x4 == np.array([1, 0, 0, 0, 1, 1])).all()
'for experiment initialization. Generating...') NQ = 20000 # Must have NQ > (2*wnumQ+1) A = sinusoidal_sample(Nx, wnumQ, NQ) A = 1 / 10 * (A - A.mean(0)) / np.sqrt(NQ) Q = A.T @ A U, s, _ = tsvd(Q) L = U * np.sqrt(s) np.savez(sample_filename, Left=L) X0 = modelling.GaussRV(C=modelling.CovMat(np.sqrt(5) * L, 'Left')) ################### # Forward model # ################### damp = 0.98 Fm = Fmat(Nx, -1, 1, tseq.dt) def step(x, t, dt): assert dt == tseq.dt return x @ Fm.T Dyn = { 'M': Nx, 'model': lambda x, t, dt: damp * step(x, t, dt), 'linear': lambda x, t, dt: damp * Fm, 'noise': modelling.GaussRV(C=modelling.CovMat(L, 'Left')), } HMM = modelling.HiddenMarkovModel(Dyn, Obs, tseq, X0, LP=LPs(jj))
import numpy as np import dapper.mods as modelling from dapper.mods.LA import Fmat, sinusoidal_sample from dapper.mods.Lorenz96 import LPs Nx = 1000 Ny = 4 jj = modelling.linspace_int(Nx, Ny) tseq = modelling.Chronology(dt=1, dkObs=5, T=300, BurnIn=-1, Tplot=100) # WITHOUT explicit matrix (assumes dt == dx/c): # step = lambda x,t,dt: np.roll(x,1,axis=x.ndim-1) # WITH: Fm = Fmat(Nx, c=-1, dx=1, dt=tseq.dt) def step(x, t, dt): assert dt == tseq.dt return x @ Fm.T Dyn = { 'M': Nx, 'model': step, 'linear': lambda x, t, dt: Fm, 'noise': 0, } # In the animation, it can sometimes/somewhat occur