1
$\begingroup$

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

FFT for AAPL since 01-01-2016

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?

$\endgroup$
2
  • 1
    $\begingroup$ Seems to be Gibb´s phenomenon, maybe you can check how to fix that $\endgroup$
    – Vitomir
    Jul 22, 2019 at 10:55
  • $\begingroup$ It might help to model returns instead of prices. $\endgroup$
    – Bob Jansen
    Jul 22, 2019 at 11:52

2 Answers 2

2
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.