示例#1
0
# -*- coding: utf-8 -*-

"""IAAFT surrogates for correlated noise.

The properties of linearly correlated noise can be captured quite
accurately by IAAFT surrogates.  Thus, they cannot easily fool
a dimension estimator (here we use Takens's maximum likelihood estimator
for the correlation dimension) if surrogate analysis is performed
additionally.
"""

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]
示例#2
0
def draw_attractor() -> None:
    tspan = np.linspace(0, tEnd, dSize)
    ys = odeint(sys, startPoint, tspan, rtol=0.0000000001, atol=0.0000000001)
    xx, yy = np.meshgrid(ys[:, 0], ys[:, 0])
    I = (abs(xx - yy) - epsillon) > 0

    tspan = np.linspace(0, tEnd, tSize)
    yso = ys
    ys = odeint(sys, startPoint, tspan)

    lag = np.arange(250)
    x = ys[:, 0]
    r = delay.acorr(x, maxtau=250)
    i = delay.dmi(x, maxtau=250)

    i_delay = localmin(noise.sma(i, hwin=1)) + 1
    r_delay = np.argmax(r < 1.0 / np.e)

    print(r'Minima of delayed mutual information = %s' % i_delay)
    print(r'Autocorrelation time = %d' % r_delay)

    dim = np.arange(1, 15 + 1)
    tau_here = (localmin(noise.sma(delay.dmi(x, maxtau=250), hwin=1)) + 1)[0]
    tau_here = np.argmax(delay.acorr(yso[:, 0], maxtau=250) < 1.0 / np.e)
    f = dimension.fnn(yso[:, 0],
                      tau=tau_here,
                      dim=dim,
                      window=0,
                      metric='euclidean')

    fig = plt.figure(1)
    ax = plt.axes(projection='3d')
    ax.plot3D(ys[:, 0], ys[:, 1], ys[:, 2], alpha=0.75)
    plt.figure(2)
    plt.imshow(I, cmap=plt.cm.gray, origin='lower')
    plt.figure(3)
    plt.subplot(211)
    plt.plot(tspan, ys[:, 0])
    plt.subplot(212)
    plt.plot(tspan, ys[:, 1])
    plt.figure(4)
    plt.subplot(121)
    plt.plot(ys[:, 0], ys[:, 1])
    plt.subplot(122)
    plt.plot(ys[:, 0], ys[:, 2])
    plt.figure(5)
    # ~ plt.subplot(121)
    # ~ plt.title(r'Time delay = %d' % r_delay)
    # ~ plt.xlabel(r'$x(t)$')
    # ~ plt.ylabel(r'$x(t + \tau)$')
    # ~ plt.plot(ys[:-r_delay,0], ys[r_delay:,0])
    # ~ plt.subplot(122)
    plt.title(r'Time delay = %d' % i_delay[0])
    plt.xlabel(r'$x(t)$')
    plt.ylabel(r'$x(t + \tau)$')
    plt.plot(ys[:-i_delay[0], 0], ys[i_delay[0]:, 0])
    plt.figure(6)
    plt.ylabel(r'Delayed mutual information')
    plt.plot(lag, i, i_delay, i[i_delay], 'o')
    plt.figure(7)
    plt.plot(dim, f[0], dim, f[1], dim, f[2])
    plt.xlabel(r'Embedding dimension $d$')
    plt.ylabel(r'FNN (%)')
    plt.figure(8)
    ax = plt.axes(projection='3d')
    ax.plot3D(ys[:-2 * i_delay[0], 0],
              ys[i_delay[0]:-i_delay[0], 0],
              ys[2 * i_delay[0]:, 0],
              alpha=0.75)
    print(ys[-1])
    plt.show()
示例#3
0
deterministic time series (from the Henon map).

As we can see, SMA performs quite badly and distorts the structure in
the time series considerably (even with a very small averaging window).
However, nonlinear reduction works well (within limits).
"""

import numpy as np
import matplotlib.pyplot as plt
from nolitsa import data, noise, utils

x = data.henon()[:, 0]
x = utils.corrupt(x, np.random.normal(size=(10 * 1000)), snr=500)

y1 = noise.nored(x, dim=7, tau=1, r=0.10, repeat=5)
y2 = noise.sma(x, hwin=1)

plt.figure(1)
plt.title('Time series from the Henon map with an SNR of 500')
plt.xlabel('$x_1$')
plt.ylabel('$x_2$')
plt.plot(x[:-1], x[1:], '.')

plt.figure(2)
plt.title('After doing an SMA over 3 bins')
plt.xlabel('$x_1$')
plt.ylabel('$x_2$')
plt.plot(y2[:-1], y2[1:], '.')

plt.figure(3)
plt.title('After using the simple nonlinear filter')
示例#4
0
    i : array
        Array containing location of all local minima.
    """
    return (np.diff(np.sign(np.diff(x))) > 0).nonzero()[0] + 1


x = data.lorenz()[1][:, 0]

# Compute autocorrelation and delayed mutual information.
lag = np.arange(100)
r = delay.acorr(x, maxtau=100)
i = delay.dmi(x, maxtau=100)

# While looking for local minima in the DMI curve, it's useful to do an
# SMA to remove "kinky" minima.
i_delay = localmin(noise.sma(i, hwin=1)) + 1
r_delay = np.argmax(r < 1.0 / np.e)

print(r'Minima of delayed mutual information = %s' % i_delay)
print(r'Autocorrelation time = %d' % r_delay)

plt.figure(1)

plt.subplot(211)
plt.title(r'Delay estimation for Lorenz attractor')
plt.ylabel(r'Delayed mutual information')
plt.plot(lag, i, i_delay, i[i_delay], 'o')

plt.subplot(212)
plt.xlabel(r'Time delay $\tau$')
plt.ylabel(r'Autocorrelation')
示例#5
0
d ~ log(3000) / log(10) ~ 4.0.  Ordinarily the second test would have
helped here and an increase in FNN should occur.  But here, the strong
temporal correlations between the points in the series prevent it from
working.

Of course, once we impose a Theiler window equal to the autocorrelation
time of the series, the second test reports large amounts of FNNs.
"""

from nolitsa import dimension, noise, delay
import matplotlib.pyplot as plt
import numpy as np

# Generate data.
np.random.seed(17)
x = noise.sma(np.random.random(3000), hwin=30)

tau = np.argmax(delay.acorr(x) < 1 / np.e)

# FNN without Theiler window.
dim = np.arange(1, 10 + 1)
f1, f2, f3 = dimension.fnn(x, tau=tau, dim=dim, window=0, metric='cityblock')

plt.figure(1)
plt.title(r'FNN for correlated random numbers (no Theiler window)')
plt.xlabel(r'Embedding dimension $d$')
plt.ylabel(r'FNN (%)')
plt.plot(dim, 100 * f1, 'bo--', label=r'Test I')
plt.plot(dim, 100 * f2, 'g^--', label=r'Test II')
plt.plot(dim, 100 * f3, 'rs-', label=r'Test I + II')
plt.legend()