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

A tip to read on Medium for free

It is enough to read this article about ETL. Three minutes will let you understand what ETL is

4274. 后缀表达式

【量化投资】离散傅里叶变换求数组周期

数据中台:中台架构及概述

What is graph neural network? Figure what is the use of neural networks?

开源之夏中选名单已公示,基础软件领域成为今年的热门申请

【E325: ATTENTION】vim编辑时报错

2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。

Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)
随机推荐
表单图片上传在Chorme中无法查看请求体的二进制图片信息
提高INSERT速度
数据中台:中台架构及概述
leetcode 1642. Furthest building you can reach
xtrabackup做数据备份
【牛客】HJ1 字符串最后一个单词的长度
JS to find and update the specified value in the object through the key
Determination of monocular and binocular 3D coordinates
Pymysql inserts data into MySQL and reports an error for no reason
110. 平衡二叉树-递归法
SLAM14讲中Sophus包的安装问题
Prompt code when MySQL inserts Chinese data due to character set problems: 1366
2138. 将字符串拆分为若干长度为 k 的组
Telnet port login method with user name for liunx server
GradScaler MaxClipGradScaler
“不平凡的代理初始值设定不受支持”,出现的原因及解决方法
Lombok use
一文讲透,商业智能BI未来发展趋势如何
Background management of uniapp hot update
mysql写的代码数据 增删查改等等