0

Here's an example of a trend I have tried smoothing using exponential smoothing:

import numpy as np
import matplotlib.pyplot as plt

y1 = np.random.randn(100)
y2 = np.random.randn(100)+2
y3 = 2*np.random.randn(100)+2
y4 = np.random.randn(100)
y = np.append(np.append(np.append(y1,y2),y3),y4)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(y)

def exp_smoothing(y, alpha):
    s=np.zeros(len(y))
    s[0] = y[0]
    for i in range(1,len(y)):
        s[i] = alpha*y[i]+(1-alpha)*s[i-1]
    return np.array(s)

ax.plot(exp_smoothing(y,.05), c='r')
plt.show()

This gives the following output:

enter image description here

The red line gives a good idea about the change in mean that's occurring, but not about the change in variance.

I would like to vary the line's thickness, so that thicker parts indicate sections of the original trend with high variance, and thinner parts indicate sections of the original trend with low variance.

The only way I can think of this is by plotting not one line, but many smaller lines, with the width of each set using set_lw (see documentation).

Is there a better way to go about this?

ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65

0 Answers0