当前位置:网站首页>[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 .
边栏推荐
猜你喜欢

Distributed | how to make "secret calls" with dble

leetcode——错误的集合

ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis

OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
![[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle](/img/db/e581087e550a2e460f12047685c48f.png)
[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle

MBA-day25 最值问题-应用题

À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL

Mysql数据(Liunx环境)定时备份

“论解不了数独所以选择做个数独游戏这件事”

Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
随机推荐
用VNC Viewer的方式远程连接无需显示屏的树莓派
WebRTC系列-网络传输之5选择最优connection切换
110. 平衡二叉树-递归法
Change SSH port number
工具类
Pymysql inserts data into MySQL and reports an error for no reason
何时使用RDD和DataFrame/DataSet
什么是图神经网络?图神经网络有什么用?
【LeetCode】541. 反转字符串 II
双指针模拟
Picture tools
Database to query the quantity of books lent in this month. If it is higher than 10, it will display "more than 10 books lent in this month". Otherwise, it will display "less than 10 books lent in thi
liunx服务器 telnet 带用户名 端口登陆方法
GradScaler MaxClipGradScaler
所说的Get post:请求的区别,你真的知道了吗??????
快慢指针系列
随笔-反思
为什么ping不通,而traceroute却可以通
Earthly container image construction tool -- the road to dream
MySQL 因字符集问题插入中文数据时提示代码 :1366