当前位置:网站首页>[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 .
边栏推荐
猜你喜欢
随机推荐
Solving linear equations with MATLAB ax=b
Liunx Mysql安装
liunx服务器 telnet 带用户名 端口登陆方法
Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)
双指针模拟
Why can ping fail while traceroute can
Solution: Nan occurs in loss during model training
[team management] 25 tips for testing team performance management
What is graph neural network? Figure what is the use of neural networks?
提高INSERT速度
表单图片上传在Chorme中无法查看请求体的二进制图片信息
1844. 将所有数字用字符替换
4275. Dijkstra序列
Matlab camera calibrator camera calibration
Pymysql inserts data into MySQL and reports an error for no reason
pymysql 向MySQL 插入数据无故报错
“不平凡的代理初始值设定不受支持”,出现的原因及解决方法
Increase insert speed
Picture tools
【使用 PicGo+腾讯云对象存储COS 作为图床】









