Skip to content
All library documents

Why Fourier Fits of Stock Prices Bend at the Sample Boundary

Article Quant Q&A · Author: Michael Joyner

Summary

The document examines a beginner’s attempt to use a fast Fourier transform (FFT) on Apple’s adjusted closing prices, including plots reconstructed with only a few frequency components. The resulting curves fall sharply near the end of the sample, prompting a question about how to prevent that behavior and whether the transform could serve as a machine-learning input.

One answer explains that a finite Fourier fit treats the observed series as one period of a repeating signal. When the first and last prices differ, the implied wraparound discontinuity strains the fit and can produce oscillations associated with the Gibbs phenomenon. The other answer discusses the finite sample’s boundary effects and suggests duplicating observations as a possible workaround. The exchange cautions that stock prices are not naturally periodic, so fitting their levels this way has little descriptive or forecasting value. It also notes that a plotted inverse transform is not the usual frequency-domain view of an FFT. The suggested duplication is not validated, and the discussion does not establish that FFT features improve a predictive model.

Key ideas

  • A Fourier reconstruction treats the sampled series as repeating beyond its boundaries.
  • A mismatch between the first and last prices creates a discontinuity at the implied wraparound.
  • Boundary discontinuities can distort the fit and produce Gibbs-like oscillations.
  • Stock price levels are not inherently periodic, limiting the interpretive and forecasting value of this fit.
  • Duplicating observations is suggested as a boundary workaround, but the exchange provides no validation of it.

Tags

Full text
# Beginner FFT (Fourier) transforms on closing prices for Apple


# Beginner FFT (Fourier) transforms on closing prices for Apple












I don't know math very well, but I have been programming for many years.

I would like to use FFT as a parameter to a ML model. The FFT is diving down sharply. I tried many stocks and its the same.

Please help :-)

```
import pandas as pd
import io
import requests
import datetime
import matplotlib.pyplot as plt
import numpy as np

############################################################
# API KEY IS FREE FOR AAPL - PLEASE FEEL FREE TO USE PUBLICLY
url = "https://eodhistoricaldata.com/api/eod/AAPL.US?api_token=OeAFFmMliFG5orCUuwAKQ8l4WWFQ67YX&from=2016-01-01"
############################################################

s = requests.get(url).content
df = pd.read_csv(io.StringIO(s.decode('utf-8')),header=0)

df = df[:-1] # drop last row
# df.drop(df.index[:7000])
df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')
df.drop('Close', axis=1, inplace=True) # Drop unadjusted close
df.rename(columns={'Adjusted_close': 'Close'},inplace=True)

close_fft = np.fft.fft(np.asarray(df['Close'].tolist()))
fft_df = pd.DataFrame({'fft':close_fft})
fft_df['absolute'] = fft_df['fft'].apply(lambda x: np.abs(x))
fft_df['angle'] = fft_df['fft'].apply(lambda x: np.angle(x))
plt.figure(figsize=(14, 7), dpi=100)
fft_list = np.asarray(fft_df['fft'].tolist())
for num_ in [3,6,25]:
    fft_list_m10= np.copy(fft_list); fft_list_m10[num_:-num_]=0
    plt.plot(np.fft.ifft(fft_list_m10), label='Fourier transform with {} components'.format(num_))
plt.plot( df['Close'])
plt.xlabel('Days')
plt.ylabel('USD')
plt.title('Figure 3: Apple (close) stock prices & Fourier transforms')
# plt.legend()
plt.show()
```

Is there anything I can do to make the FFTs not drop in the last 30 bars and be where they are supposed to be?

## Answer by nbbo2 (score 2)

https://quant.stackexchange.com/a/71554

The blue line that you have fit starts at 140 and also ends at 140 because when you fit a Fourier Series the signal is assumed to be periodic (repeated again and again) and continuous. 140 is a compromise between the the low values of the signal on the left (about 90) and the high values of the signal on the right (about 190). In the middle portion the blue line is close to the signal as desired. Where the signal is discontinuous as it "wraps around" the fit is strained and shows the Gibbs Phoenomenon that Vitomir referred to (high frequency oscillations on either side of the point of discontinuity). Obviously the stock price of Apple is not periodic, it is not going to return to a previous value. It does not make sense to fit it with sines and cosines as done here, this has no forecasting or descriptive value.

## Answer by user61085 (score 0)

https://quant.stackexchange.com/a/69840

The problem is that FFT (theory) assumes an infinite number of samples before and after the point of inspection. Since you are at the end of your sample data set the FFT treats that as zero value from then to the right. try to duplicate the data set (for the FFT sake). Also, the FFT output normally looks different (Time vs Frequency), so I am not sure what you are showing here.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.