Generate noise in time series¶
In [1]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
from badgers.generators.time_series.noise import LocalGaussianNoiseGenerator, GlobalGaussianNoiseGenerator
import matplotlib.patches as patches
import numpy as np
import matplotlib.pyplot as plt
from badgers.generators.time_series.noise import LocalGaussianNoiseGenerator, GlobalGaussianNoiseGenerator
import matplotlib.patches as patches
Setup random generator¶
In [2]:
Copied!
from numpy.random import default_rng
seed = 0
rng = default_rng(seed)
from numpy.random import default_rng
seed = 0
rng = default_rng(seed)
Import data (using sktime)¶
In [3]:
Copied!
from sktime.datasets import load_airline
from sktime.datasets import load_airline
In [4]:
Copied!
X = load_airline()
t = X.index.to_timestamp()
X = load_airline()
t = X.index.to_timestamp()
In [5]:
Copied!
plt.plot(t,X)
plt.plot(t,X)
Out[5]:
[<matplotlib.lines.Line2D at 0x21e3b5cd550>]
Adding Gaussian white noise on the full time series¶
In [6]:
Copied!
generator = GlobalGaussianNoiseGenerator(random_generator=rng)
generator = GlobalGaussianNoiseGenerator(random_generator=rng)
In [7]:
Copied!
Xt, _ = generator.generate(X.copy(), y=None, noise_std=0.25)
Xt, _ = generator.generate(X.copy(), y=None, noise_std=0.25)
In [8]:
Copied!
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(t, X.values)
axes[0].set_title('Original data')
axes[1].plot(t, Xt)
axes[1].set_title('Transformed data')
plt.tight_layout();
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(t, X.values)
axes[0].set_title('Original data')
axes[1].plot(t, Xt)
axes[1].set_title('Transformed data')
plt.tight_layout();
Adding Gaussian white noise on randomly chosen subsequences¶
In [9]:
Copied!
generator = LocalGaussianNoiseGenerator(random_generator=rng)
generator = LocalGaussianNoiseGenerator(random_generator=rng)
In [10]:
Copied!
Xt, _ = generator.generate(X.copy(), y=None, n_patterns=5, noise_std=0.25, min_width_pattern=8, max_width_patterns=12)
Xt, _ = generator.generate(X.copy(), y=None, n_patterns=5, noise_std=0.25, min_width_pattern=8, max_width_patterns=12)
In [11]:
Copied!
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(t, X.values)
axes[0].set_title('Original data')
axes[1].plot(t, Xt)
axes[1].set_title('Transformed data')
# show where the patterns are located
bottom = np.min(Xt)
height = np.max(Xt) - np.min(Xt)
for start, end in generator.patterns_indices_:
width = end-start
left = X.index[start]
rect = plt.Rectangle((left, bottom), width, height,
facecolor="red", alpha=0.1)
axes[1].add_patch(rect)
plt.tight_layout();
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(t, X.values)
axes[0].set_title('Original data')
axes[1].plot(t, Xt)
axes[1].set_title('Transformed data')
# show where the patterns are located
bottom = np.min(Xt)
height = np.max(Xt) - np.min(Xt)
for start, end in generator.patterns_indices_:
width = end-start
left = X.index[start]
rect = plt.Rectangle((left, bottom), width, height,
facecolor="red", alpha=0.1)
axes[1].add_patch(rect)
plt.tight_layout();
In [ ]:
Copied!