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()
from scipy.signal import find_peaks

subset = df.head(100)

peaks = find_peaks(subset["y"])
troughs = find_peaks(-subset["y"])
peaks
<AxesSubplot:xlabel='index', ylabel='y'>

Autocorrelation

Pandas provides an autocorrelation1 plot function.

figure(figsize=(14, 7), dpi=80)
pd.plotting.autocorrelation_plot(df["y"])
<AxesSubplot:xlabel='Lag', ylabel='Autocorrelation'>

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()

  1. https://pandas.pydata.org/docs/reference/api/pandas.plotting.autocorrelation%5Fplot.html ↩︎