Generate "transmission errors" in time series¶
These are errors that affects time index, for example: repeating values x(t) several times, dropping value x(t), or switching x(t) with x(t+1), etc.
In [1]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
from badgers.generators.time_series.transmission_errors import RandomTimeSwitchGenerator, RandomRepeatGenerator, RandomDropGenerator, LocalRegionsRandomDropGenerator, LocalRegionsRandomRepeatGenerator
import matplotlib.patches as patches
import numpy as np
import matplotlib.pyplot as plt
from badgers.generators.time_series.transmission_errors import RandomTimeSwitchGenerator, RandomRepeatGenerator, RandomDropGenerator, LocalRegionsRandomDropGenerator, LocalRegionsRandomRepeatGenerator
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!
fig, ax = plt.subplots()
ax.plot(X.values)
ax.set_title('original data')
plt.tight_layout()
fig, ax = plt.subplots()
ax.plot(X.values)
ax.set_title('original data')
plt.tight_layout()
Generating random switches in time¶
Where x(t) and x(t+1) are switched
In [6]:
Copied!
generator = RandomTimeSwitchGenerator(random_generator=rng)
Xt, y = generator.generate(X=X.copy(), y=None, n_switches=20)
generator = RandomTimeSwitchGenerator(random_generator=rng)
Xt, y = generator.generate(X=X.copy(), y=None, n_switches=20)
In [7]:
Copied!
fig, ax = plt.subplots()
ax.plot(X.values, alpha=0.5, ls='-', marker='.', label='original')
ax.plot(Xt.values, alpha=0.5, ls='-', marker='.', label='with random switches')
ax.set_xlabel('time units (arrows show where the switch occured)')
for i in generator.switch_indices_:
ax.arrow(i,0,0,50, head_width=2, head_length=10)
plt.legend()
plt.tight_layout()
fig, ax = plt.subplots()
ax.plot(X.values, alpha=0.5, ls='-', marker='.', label='original')
ax.plot(Xt.values, alpha=0.5, ls='-', marker='.', label='with random switches')
ax.set_xlabel('time units (arrows show where the switch occured)')
for i in generator.switch_indices_:
ax.arrow(i,0,0,50, head_width=2, head_length=10)
plt.legend()
plt.tight_layout()
Randomly repeating values (equivalent to delays)¶
Where x(t) is repeated several times
In [8]:
Copied!
generator = RandomRepeatGenerator(random_generator=rng)
Xt, y = generator.generate(X=X.copy(), y=None, n_repeats=5, min_nb_repeats=3, max_nb_repeats=10)
generator = RandomRepeatGenerator(random_generator=rng)
Xt, y = generator.generate(X=X.copy(), y=None, n_repeats=5, min_nb_repeats=3, max_nb_repeats=10)
In [9]:
Copied!
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(X.values, color='C0')
axes[1].plot(Xt.values, color='C1')
axes[0].set_title('Original')
axes[1].set_title('Transformed')
plt.tight_layout()
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(X.values, color='C0')
axes[1].plot(Xt.values, color='C1')
axes[0].set_title('Original')
axes[1].set_title('Transformed')
plt.tight_layout()
Repeating values in certain regions of time (time intervals)¶
In [10]:
Copied!
generator = LocalRegionsRandomRepeatGenerator(random_generator=rng)
Xt, y = generator.generate(X=X.copy(), y=None, n_repeats=10, min_nb_repeats=1, max_nb_repeats=4, n_regions=3, min_width_regions=5, max_width_regions=15)
generator = LocalRegionsRandomRepeatGenerator(random_generator=rng)
Xt, y = generator.generate(X=X.copy(), y=None, n_repeats=10, min_nb_repeats=1, max_nb_repeats=4, n_regions=3, min_width_regions=5, max_width_regions=15)
In [11]:
Copied!
fig, axes = plt.subplots(3, sharex=True, sharey=False, figsize=(6,8))
axes[0].plot(X.values, color='C0')
axes[1].plot(generator.repeats_probabilities_, color='C2')
axes[2].plot(Xt.values, color='C1')
axes[0].set_title('Original (arrows show where points were repeated)')
axes[1].set_title('Repeat probability')
axes[2].set_title('Transformed')
for i, l in generator.repeats_:
axes[0].arrow(i,0,0,50, head_width=2, head_length=10)
plt.tight_layout()
fig, axes = plt.subplots(3, sharex=True, sharey=False, figsize=(6,8))
axes[0].plot(X.values, color='C0')
axes[1].plot(generator.repeats_probabilities_, color='C2')
axes[2].plot(Xt.values, color='C1')
axes[0].set_title('Original (arrows show where points were repeated)')
axes[1].set_title('Repeat probability')
axes[2].set_title('Transformed')
for i, l in generator.repeats_:
axes[0].arrow(i,0,0,50, head_width=2, head_length=10)
plt.tight_layout()
Randomly dropping values¶
This is different from missingness as in missingness the time index in not affected
In [12]:
Copied!
generator = RandomDropGenerator(random_generator=rng)
Xt, y = generator.generate(X=X.copy(), y=None, n_drops=10)
generator = RandomDropGenerator(random_generator=rng)
Xt, y = generator.generate(X=X.copy(), y=None, n_drops=10)
In [13]:
Copied!
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(X.values, color='C0')
axes[1].plot(Xt.values, color='C1')
axes[0].set_title('Original (arrows show where points were dropped)')
axes[1].set_title('Transformed')
for i in generator.drops_indices_:
axes[0].arrow(i,0,0,50, head_width=2, head_length=10)
plt.tight_layout()
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(X.values, color='C0')
axes[1].plot(Xt.values, color='C1')
axes[0].set_title('Original (arrows show where points were dropped)')
axes[1].set_title('Transformed')
for i in generator.drops_indices_:
axes[0].arrow(i,0,0,50, head_width=2, head_length=10)
plt.tight_layout()
Dropping values in certain regions of time (certain time intervals)¶
In [14]:
Copied!
generator = LocalRegionsRandomDropGenerator(random_generator=rng)
Xt, y = generator.generate(X=X.copy(), y=None, n_drops=10, n_regions=3, min_width_regions=5, max_width_regions=15)
generator = LocalRegionsRandomDropGenerator(random_generator=rng)
Xt, y = generator.generate(X=X.copy(), y=None, n_drops=10, n_regions=3, min_width_regions=5, max_width_regions=15)
In [15]:
Copied!
fig, axes = plt.subplots(3, sharex=True, sharey=False, figsize=(6,8))
axes[0].plot(X.values, color='C0')
axes[1].plot(generator.drops_probabilities_, color='C2')
axes[2].plot(Xt.values, color='C1')
axes[0].set_title('Original (arrows show where points were dropped)')
axes[1].set_title('Drop probability')
axes[2].set_title('Transformed')
for i in generator.drops_indices_:
axes[0].arrow(i,0,0,50, head_width=2, head_length=10)
plt.tight_layout()
fig, axes = plt.subplots(3, sharex=True, sharey=False, figsize=(6,8))
axes[0].plot(X.values, color='C0')
axes[1].plot(generator.drops_probabilities_, color='C2')
axes[2].plot(Xt.values, color='C1')
axes[0].set_title('Original (arrows show where points were dropped)')
axes[1].set_title('Drop probability')
axes[2].set_title('Transformed')
for i in generator.drops_indices_:
axes[0].arrow(i,0,0,50, head_width=2, head_length=10)
plt.tight_layout()
In [ ]:
Copied!