📚 The CoCalc Library - books, templates and other resources
cocalc-examples / data-science-ipython-notebooks / deep-learning / theano-tutorial / rnn_tutorial / synthetic.py
132928 viewsLicense: OTHER
import collections1import numpy as np234def mackey_glass(sample_len=1000, tau=17, seed=None, n_samples = 1):5'''6mackey_glass(sample_len=1000, tau=17, seed = None, n_samples = 1) -> input7Generate the Mackey Glass time-series. Parameters are:8- sample_len: length of the time-series in timesteps. Default is 1000.9- tau: delay of the MG - system. Commonly used values are tau=17 (mild10chaos) and tau=30 (moderate chaos). Default is 17.11- seed: to seed the random generator, can be used to generate the same12timeseries at each invocation.13- n_samples : number of samples to generate14'''15delta_t = 1016history_len = tau * delta_t17# Initial conditions for the history of the system18timeseries = 1.21920if seed is not None:21np.random.seed(seed)2223samples = []2425for _ in range(n_samples):26history = collections.deque(1.2 * np.ones(history_len) + 0.2 * \27(np.random.rand(history_len) - 0.5))28# Preallocate the array for the time-series29inp = np.zeros((sample_len,1))3031for timestep in range(sample_len):32for _ in range(delta_t):33xtau = history.popleft()34history.append(timeseries)35timeseries = history[-1] + (0.2 * xtau / (1.0 + xtau ** 10) - \360.1 * history[-1]) / delta_t37inp[timestep] = timeseries3839# Squash timeseries through tanh40inp = np.tanh(inp - 1)41samples.append(inp)42return samples434445def mso(sample_len=1000, n_samples = 1):46'''47mso(sample_len=1000, n_samples = 1) -> input48Generate the Multiple Sinewave Oscillator time-series, a sum of two sines49with incommensurable periods. Parameters are:50- sample_len: length of the time-series in timesteps51- n_samples: number of samples to generate52'''53signals = []54for _ in range(n_samples):55phase = np.random.rand()56x = np.atleast_2d(np.arange(sample_len)).T57signals.append(np.sin(0.2 * x + phase) + np.sin(0.311 * x + phase))58return signals596061def lorentz(sample_len=1000, sigma=10, rho=28, beta=8 / 3, step=0.01):62"""This function generates a Lorentz time series of length sample_len,63with standard parameters sigma, rho and beta.64"""6566x = np.zeros([sample_len])67y = np.zeros([sample_len])68z = np.zeros([sample_len])6970# Initial conditions taken from 'Chaos and Time Series Analysis', J. Sprott71x[0] = 0;72y[0] = -0.01;73z[0] = 9;7475for t in range(sample_len - 1):76x[t + 1] = x[t] + sigma * (y[t] - x[t]) * step77y[t + 1] = y[t] + (x[t] * (rho - z[t]) - y[t]) * step78z[t + 1] = z[t] + (x[t] * y[t] - beta * z[t]) * step7980x.shape += (1,)81y.shape += (1,)82z.shape += (1,)8384return np.concatenate((x, y, z), axis=1)858687