Contents

Related

Time-series analysis

Peaks and troughs

Let’s start by creating a random walk.

import numpy as np
import pandas as pd

N = 10000
step_set = [-1, 0, 1]
origin = np.zeros((1, 1))
step_shape = (N, 1)
steps = np.random.choice(a=step_set, size=step_shape)
path = np.concatenate([origin, steps]).cumsum(0)
df = pd.DataFrame(path,
               columns =['y'])
df.head()
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

figure(figsize=(14, 7), dpi=80)
plt.plot(df['y'])
plt.show()
from scipy.signal import find_peaks

subset = df.head(100)

peaks = find_peaks(subset["y"])
troughs = find_peaks(-subset["y"])
peaks
ax = subset.plot(y="y", figsize=(14, 7))
subset.reset_index().iloc[peaks[0]].plot.scatter(x="index", y="y", ax=ax, color="green")
subset.reset_index().iloc[troughs[0]].plot.scatter(x="index", y="y", ax=ax, color="red")

Autocorrelation

Pandas provides an autocorrelation1 plot function.

figure(figsize=(14, 7), dpi=80)
pd.plotting.autocorrelation_plot(df["y"])

Differencing

Calculating the difference between $x_t$ and $x_{t-1}$.

stationary = df['y'].diff()
figure(figsize=(14, 7), dpi=80)
plt.plot(stationary)
plt.show()