Example #1
0
    return -alpha * x


def sigma(x):
    return np.sqrt(2 / beta) * np.ones((1, 1, x.shape[1]))


#%% generate data
m = 1000
X = Omega.rand(m)
Y = b(X)
Z = sigma(X)

# define kernel
#k = kernels.polynomialKernel(7)
k = kernels.gaussianKernel(0.5)

#%% apply kernel generator EDMD
epsilon = 0.1
S = np.einsum('ijk,ljk->ilk', Z, Z)  # sigma \cdot sigma^T

G_00 = kernels.gramian(X, k)
G_10 = np.zeros((m, m))
for i in range(m):
    for j in range(m):
        G_10[i, j] = Y[:, i].T @ k.diff(X[:, i], X[:, j]) + 0.5 * np.sum(
            S[:, :, i] * k.ddiff(X[:, i], X[:, j]), axis=(0, 1))

A, _, _, _ = np.linalg.lstsq(G_00, G_10, rcond=1e-12)
d, V = algorithms.sortEig(A, evs=m, which='LM')
W = kernels.gramian2(Omega.midpointGrid(), X, k) @ V
Example #2
0
def c2(x):
    return -h**2 / (2 * m0)


#%% analytically computed eigenfunctions fors = 4
psi = (lambda x: sech(x)**4, lambda x: sech(x)**3 * np.tanh(x),
       lambda x: sech(x)**2 *
       (7 * np.tanh(x)**2 - 1), lambda x: sech(x) * np.tanh(x) *
       (7 * np.tanh(x)**2 - 3))

#%% generate data
m = 100
X = Omega.rand(m)

#%% define kernel
k = kernels.gaussianKernel(0.3)

#%% apply kernel generator EDMD
evs = 4
G_00 = kernels.gramian(X, k)
G_10 = np.zeros((m, m))
for i in range(m):
    for j in range(m):
        G_10[i, j] = c0(X[:, i]) * k(X[:, i], X[:, j]) + np.sum(
            c2(X[:, i]) * k.ddiff(X[:, i], X[:, j]), axis=(0, 1))

A, _, _, _ = np.linalg.lstsq(G_00, G_10, rcond=1e-12)
d, V = algorithms.sortEig(A, evs=evs, which='SR')

c = Omega.midpointGrid()
W = kernels.gramian2(c, X, k) @ V
Example #3
0
import d3s.kernels as kernels
import d3s.algorithms as algorithms

plt.ion()

#%% Bickley jet -----------------------------------------------------------------------------------

#%% load variables from mat file into main scope
data = sp.io.loadmat('data/bickley.mat', squeeze_me=True)
for s in data.keys():
    if s[:2] == '__' and s[-2:] == '__': continue
    exec('%s = data["%s"]' % (s, s))

#%% apply kernel CCA to detect coherent sets
sigma = 1
k = kernels.gaussianKernel(sigma)

evs = 9  # number of eigenfunctions to be computed
d, V = algorithms.kcca(X, Y, k, evs, epsilon=1e-3)

#%% plot eigenvectors
for i in range(evs):
    plt.figure(figsize=(3, 2))
    plt.scatter(X[0, :], X[1, :], c=V[:, i])
plt.show()

#%% k-means of eigenvectors
c, l = sp.cluster.vq.kmeans2(np.real(V[:, :5]), 5)
plt.figure(figsize=(3, 2))
plt.scatter(X[0, :], X[1, :], c=l)
plt.show()
Example #4
0
bounds = np.array([[-2, 2]])
boxes = np.array([100])
Omega = domain.discretization(bounds, boxes)

#%% define system
alpha = 1
beta = 4

f = systems.OrnsteinUhlenbeck(1e-3, 1000)

#%% generate data
m = 2000
X = f.getTrajectory(np.zeros((1, 1)), m)

#%% kernel density estimation
k = kernels.gaussianKernel(0.1)
kde = kernels.densityEstimate(X, k, beta)

c = Omega.midpointGrid()
n = Omega.numBoxes()

rho = kde.rho(c)
Vk = kde.V(c)
gradVk = kde.gradV(c)

#%% plot results
V = 1 / 2 * alpha * c**2  # true potential
mu = np.sqrt(2 / np.pi) * np.exp(-beta * V)  # true invariant density

plt.figure()
plt.plot(c.flatten(), rho.flatten(), 'r')