当前位置:网站首页>[quantitative investment] discrete Fourier transform to calculate array period

[quantitative investment] discrete Fourier transform to calculate array period

2022-06-24 08:52:00 Coding leaves

        It's been a long time since the content related to quantitative analysis was updated , This section describes how to solve the possible periodicity in a set of data by Fourier transform , Subsequently, the results of this section will be applied in the quantification program . The calculation method in this paper is not necessarily correct , Welcome to make more corrections , And communicate in the comment area .

1 Discrete Fourier transform

        The formula of discrete Fourier transform is as follows :

 

        among N Is the length of the original data ,f(n) For raw data , That is, the time-varying sequence .F(u) Is the result of Fourier transform , You can see F(u+1) and F(u) The difference between each frequency component of 1/N Frequency intervals . therefore ,f(n) After Fourier transform F(u) In the frequency domain The frequency interval on the is 1/N.

        in addition , When u=0 when ,e The value of the index part is 1. therefore ,F(0) yes f(n) Average value .

2 Solve array period

        The steps of using Fourier transform to solve the array period :

        (1) utilize 1 The formula in , Solve the result of Fourier transform F(u).

        (2) solve (1) Amplitude of Fourier transform in .

        (3) solve (2) Peak value of Fourier amplitude in , The larger the amplitude, the more obvious the periodicity .

        (4) Suppose the peak occurs at u=k It's about , So according to 1 Middle analysis , The corresponding frequency is k/N, Then the corresponding period is N/k.

3 The sample program

import numpy as np
from scipy.signal import find_peaks
import matplotlib.pyplot as plt


x = np.array([ 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1])
N = x.shape[0]
print(N)
f = np.fft.fftn(x)  # Transform all ,f[0] Accumulate results for all values 
f = f[:N//2]

peaks, p = find_peaks(abs(f), height=0.1)
print(peaks)

# print(f)
# plt.plot(abs(x), '.')
plt.plot(abs(f), '.')
print(abs(f))
print(f[6])
real =  f[13].real
im = f[13].imag
theta = np.arctan(im/(real+1e-6))
pos = np.angle(f[13])/2/np.pi*5
print(np.angle(f[13]))
print(theta, pos)
# plt.show()

4 Solution result

 

         T =5.

        Period after solution , It is best to verify , To avoid errors . The calculation method in this paper is not necessarily correct , Welcome to make more corrections , And communicate in the comment area .

More 3D 、 Please pay attention to two-dimensional perception algorithm and financial quantitative analysis algorithm “ Lele perception school ” WeChat official account , And will continue to update .

原网站

版权声明
本文为[Coding leaves]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240628079064.html