ThinkDSP
This notebook contains code examples from Chapter 9: Differentiation and Integration
Copyright 2015 Allen Downey
As the first example, I'll look at daily closing stock prices for Facebook, from its IPO on 17 May 2012 to 8 December 2015 (note: the dataset includes only trading days )
Here's what the time series looks like (ignoring the actual gaps between trading days).
And here's the spectrum on a log-log scale.
The slope of the power spectrum is -1.86, which is similar to red noise (which should have a slope of -2).
We can use np.diff
to compute the difference between successive elements, which is the daily change.
And here's the spectrum of the daily changes:
Recall that the spectrum of white noise looks like white noise.
Here's the spectrum on a log-log scale.
The estimated slope is close to zero, which is consistent with white noise.
We can think the diff operation as convolution with a difference window, [1, -1].
And convolution with this window corresponds to multiplication by a filter.
The following function computes the filter that corresponds to the window.
And here's what the filter looks like for the difference window:
And the angles:
So we could also compute the daily changes by multiplying the spectrum of closing prices by the diff filter:
And then converting the spectrum to a wave.
Then we can confirm that we get the same result both ways (within floating point error).
Differentiation
This diff operation is an approximation of differentiation, and we can compute the filter for differentiation analytically: each complex component is multiplied by .
Now we can apply the derivative filter to the spectrum of closing prices:
The results are similar to what we got from np.diff
, with some differences due to (1) the difference window is only a coarse approximation of the derivative, especially at higher frequencies, and (2) the spectral derivative is based on the assumption that the signal is periodic, so the behavior at the beginning and end is different.
We can see the differences more clearly by zooming in on a slice:
The diffs and the spectral derivative are similar in many places, but sometimes substantially different.
Here's the difference between the derivative filter and the difference filter:
The difference filter does not amplify the highest frequencies as much, which is why the diffs are smoother than the derivative.
Integration
Now let's think about integration. We can compute the filter for integration analytically: each frequency component gets divided by .
I plot the result on a log-y scale so we can see it more clearly.
We can confirm that the integration filter is correct by applying it to the spectrum of the derivative we just computed:
And then converting back to a wave. The result is identical to the daily closing prices we started with, but shifted so the mean is 0.
The reason the mean is 0 is that the derivative clobbers the first element of the spectrum, which is the bias. Once the bias information is lost, integration can't restore it. So the result has an unspecified constant of integration.
Cumulative sum
In the same way that the diff operator approximates differentiation, the cumulative sum approximates integration.
I'll demonstrate with a Sawtooth signal.
Here's the spectrum before the cumulative sum:
The output wave is the cumulative sum of the input
And here's its spectrum
Now we compute the ratio of the output to the input:
In between the harmonics, the input componenents are small, so I set those ratios to NaN.
To get the cumsum filter, I compute the diff filter again and invert it.
Finally, we can compare the computed ratios to the filter. They match, confirming that the cumsum filter is the inverse of the diff filter.
Now we can compute the output wave using the convolution theorem, and compare the results:
They are the same, within floating point error.