def test_c2_embed(): # Test d2.c2_embed() t = np.linspace(0, 10 * 2 * np.pi, 5000) y = np.array([np.sin(t), np.cos(t)]).T r = utils.gprange(0.01, 1, 1000) desired = d2.c2(y, r=r)[1] dim = [2] tau = 125 x = y[:, 0] assert_allclose(desired, d2.c2_embed(x, dim=dim, tau=tau, r=r)[0][1], atol=1e-3)
import matplotlib.pyplot as plt import numpy as np from nolitsa import surrogates, d2, noise, delay x = noise.sma(np.random.normal(size=(2 ** 12)), hwin=100) ends = surrogates.mismatch(x)[0] x = x[ends[0]:ends[1]] act = np.argmax(delay.acorr(x) < 1 / np.e) mle = np.empty(19) # Compute 19 IAAFT surrogates and compute the correlation sum. for k in range(19): y = surrogates.iaaft(x)[0] r, c = d2.c2_embed(y, dim=[7], tau=act, window=act)[0] # Compute the Takens MLE. r_mle, mle_surr = d2.ttmle(r, c) i = np.argmax(r_mle > 0.5 * np.std(y)) mle[k] = mle_surr[i] plt.loglog(r, c, color='#BC8F8F') r, c = d2.c2_embed(x, dim=[7], tau=act, window=act)[0] # Compute the Takens MLE. r_mle, true_mle = d2.ttmle(r, c) i = np.argmax(r_mle > 0.5 * np.std(x)) true_mle = true_mle[i]
Expected: D2 = 2 / (alpha - 1) = 2.0 Of course, this value is not due to the existence of any invariant measure. What is being measured here is the fractal dimension of the Brownian trail. The scaling region would vanish if we impose a nonzero Theiler window, telling us that the underlying system is not low dimensional. """ import numpy as np import matplotlib.pyplot as plt from nolitsa import d2, data, utils np.random.seed(101) x = utils.rescale(data.falpha(alpha=2.0, length=(2 ** 14))[:10 * 1000]) dim = np.arange(1, 10 + 1) tau = 500 plt.title('Local $D_2$ vs $r$ for Brown noise') plt.xlabel(r'Distance $r$') plt.ylabel(r'Local $D_2$') for r, c in d2.c2_embed(x, tau=tau, dim=dim, window=0, r=utils.gprange(0.001, 1.0, 100)): plt.semilogx(r[2:-2], d2.d2(r, c, hwin=2), color='#4682B4') plt.semilogx(utils.gprange(0.001, 1.0, 100), 2.0 * np.ones(100), color='#000000') plt.show()
import numpy as np import matplotlib.pyplot as plt from nolitsa import d2, utils phi = np.linspace(2 * np.pi, 52 * np.pi, 1000) x = phi * np.cos(phi) x = utils.rescale(x) dim = np.arange(1, 10 + 1) tau = 10 r = utils.gprange(0.01, 1.0, 100) plt.figure(1) plt.title('Correlation sum $C(r)$ without any Theiler window') plt.xlabel(r'Distance $r$') plt.ylabel(r'Correlation sum $C(r)$') for r, c in d2.c2_embed(x, tau=tau, dim=dim, window=0, r=r): plt.loglog(r, c) plt.figure(2) plt.title('Correlation sum $C(r)$ with a Theiler window of 100') plt.xlabel(r'Distance $r$') plt.ylabel(r'Correlation sum $C(r)$') for r, c in d2.c2_embed(x, tau=tau, dim=dim, window=100, r=r): plt.loglog(r, c) plt.show()
Although there is a proper scaling region with D2 between 1.2 and 1.5, it is higher than the expected value of 1.0, perhaps due to the additive noise. """ import numpy as np import matplotlib.pyplot as plt from nolitsa import d2, utils t = np.linspace(0, 100 * np.pi, 5000) x = np.sin(t) + np.sin(2 * t) + np.sin(3 * t) + np.sin(5 * t) x = utils.corrupt(x, np.random.normal(size=5000), snr=1000) # Time delay. tau = 25 window = 100 # Embedding dimension. dim = np.arange(1, 10) plt.title('Local $D_2$ vs $r$ for a noisy closed curve') plt.xlabel(r'Distance $r$') plt.ylabel(r'Local $D_2$') for r, c in d2.c2_embed(x, tau=tau, dim=dim, window=window): plt.semilogx(r[3:-3], d2.d2(r, c), color='#4682B4') plt.plot(r[3:-3], np.ones(len(r) - 6), color='#000000') plt.show()
np.random.seed(882) n = np.random.normal(size=(N), loc=0, scale=1.0) a = 0.998 x[0] = n[0] for i in range(1, N): x[i] = a * x[i - 1] + n[i] # Delay is the autocorrelation time. tau = 400 dim = np.arange(1, 10 + 1) plt.figure(1) plt.title(r'Local $D_2$ vs $r$ for AR(1) time series with $W = 0$') plt.xlabel(r'Distance $r$') plt.ylabel(r'Local $D_2$') for r, c in d2.c2_embed(x, tau=tau, dim=dim, window=0): plt.semilogx(r[3:-3], d2.d2(r, c)) plt.figure(2) plt.title(r'Local $D_2$ vs $r$ for AR(1) time series with $W = 400$') plt.xlabel(r'Distance $r$') plt.ylabel(r'Local $D_2$') for r, c in d2.c2_embed(x, tau=tau, dim=dim, window=400): plt.semilogx(r[3:-3], d2.d2(r, c)) plt.show()
def _corr_dim(self, signal, ac_zero): r, C_r = c2_embed(signal, [self.Q], ac_zero)[0] return d2(r[:self.Q], C_r[:self.Q])[0]
the null hypothesis of a linear correlated stochastic process. """ import matplotlib.pyplot as plt import numpy as np from nolitsa import surrogates, d2, data x = data.lorenz(x0=[-13.5, -16.0, 31.0], length=(2**12))[1][:, 0] x = x[422:3547] mle = np.empty(19) # Compute 19 IAAFT surrogates and compute the correlation sum. for k in range(19): y = surrogates.iaaft(x)[0] r, c = d2.c2_embed(y, dim=[5], tau=5, window=100)[0] # Compute the Takens MLE. r_mle, mle_surr = d2.ttmle(r, c, zero=False) i = np.argmax(r_mle > 0.5 * np.std(y)) mle[k] = mle_surr[i] plt.loglog(r, c, color='#BC8F8F') r, c = d2.c2_embed(x, dim=[5], tau=5, window=100)[0] # Compute the Takens MLE. r_mle, true_mle = d2.ttmle(r, c, zero=False) i = np.argmax(r_mle > 0.5 * np.std(x)) true_mle = true_mle[i]