Python 使用傅立叶分析进行时间序列预测
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4479463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using fourier analysis for time series prediction
提问by NetSmoothMF
For data that is known to have seasonal, or daily patterns I'd like to use fourier analysis be used to make predictions. After running fft on time series data, I obtain coefficients. How can I use these coefficients for prediction?
对于已知具有季节性或每日模式的数据,我想使用傅立叶分析进行预测。在对时间序列数据运行 fft 后,我获得了系数。如何使用这些系数进行预测?
I believe FFT assumes all data it receives constitute one period, then, if I simply regenerate data using ifft, I am also regenerating the continuation of my function, so can I use these values for future values?
我相信 FFT 假设它接收到的所有数据都构成一个时期,那么,如果我只是使用 ifft 重新生成数据,我也在重新生成我的函数的延续,那么我可以将这些值用于未来的值吗?
Simply put: I run fft for t=0,1,2,..10 then using ifft on coef, can I use regenerated time series for t=11,12,..20 ?
简单地说:我对 t=0,1,2,..10 运行 fft 然后在 coef 上使用 ifft,我可以对 t=11,12,..20 使用重新生成的时间序列吗?
采纳答案by Steve Tjoa
It sounds like you want a combination of extrapolation anddenoising.
听起来您想要外推和去噪的组合。
You say you want to repeat the observed data over multiple periods. Well, then just repeat the observed data. No need for Fourier analysis.
你说你想在多个时期重复观察到的数据。好吧,那么只需重复观察到的数据即可。不需要傅立叶分析。
But you also want to find "patterns". I assume that means finding the dominant frequency components in the observed data. Then yes, take the Fourier transform, preserve the largest coefficients, and eliminate the rest.
但你也想找到“模式”。我认为这意味着在观察到的数据中找到主要的频率分量。那么是的,进行傅立叶变换,保留最大的系数,并消除其余的。
X = scipy.fft(x)
Y = scipy.zeros(len(X))
Y[important frequencies] = X[important frequencies]
As for periodic repetition: Let z = [x, x], i.e., two periods of the signal x. Then Z[2k] = X[k]for all kin {0, 1, ..., N-1}, and zeros otherwise.
至于周期重复:设z = [x, x],即信号的两个周期x。然后Z[2k] = X[k]对于k{0, 1, ..., N-1} 中的所有,否则为零。
Z = scipy.zeros(2*len(X))
Z[::2] = X
回答by duffymo
When you run an FFT on time series data, you transform it into the frequency domain. The coefficients multiply the terms in the series (sines and cosines or complex exponentials), each with a different frequency.
当您对时间序列数据运行 FFT 时,您会将其转换为频域。系数乘以系列中的项(正弦和余弦或复指数),每个项具有不同的频率。
Extrapolation is always a dangerous thing, but you're welcome to try it. You're using past information to predict the future when you do this: "Predict tomorrow's weather by looking at today." Just be aware of the risks.
推断总是一件危险的事情,但欢迎您尝试。当您执行以下操作时,您正在使用过去的信息来预测未来:“通过查看今天来预测明天的天气”。请注意风险。
I'd recommend reading "Black Swan".
推荐阅读《黑天鹅》。
回答by tartakynov
I'm aware that this question may be not actual for you anymore, but for others that are looking for answers I wrote a very simple example of fourier extrapolation in Python https://gist.github.com/tartakynov/83f3cd8f44208a1856ce
我知道这个问题对你来说可能不再现实,但对于其他正在寻找答案的人,我在 Python 中写了一个非常简单的傅立叶外推示例https://gist.github.com/tartakynov/83f3cd8f44208a1856ce
Before you run the script make sure that you have all dependencies installed (numpy, matplotlib). Feel free to experiment with it.
P.S. Locally Stationary Wavelet may be better than fourier extrapolation. LSW is commonly used in predicting time series. The main disadvantage of fourier extrapolation is that it just repeats your series with period N, where N - length of your time series.
在运行脚本之前,请确保已安装所有依赖项(numpy、matplotlib)。随意尝试一下。
PS 局部平稳小波可能比傅立叶外推更好。LSW 通常用于预测时间序列。傅立叶外推法的主要缺点是它只是用周期 N 重复您的系列,其中 N - 时间系列的长度。
回答by Pablo
you can use the library that @tartakynov posted and, to not repeat exactly the same time series in the forcast (overfitting), you can add a new parameter to the function called n_paramand fix a lower bound hfor the amplitudes of the frequencies.
您可以使用@tartakynov 发布的库,并且为了不在预测中重复完全相同的时间序列(过度拟合),您可以向调用的函数添加一个新参数n_param并修复h频率幅度的下限。
def fourierExtrapolation(x, n_predict,n_param):
usually you will find that, in a signal, there are some frequencies that have significantly higher amplitude than others, so, if you select this frequencies you will be able to isolate the periodic nature of the signal
通常您会发现,在信号中,有些频率的幅度明显高于其他频率,因此,如果您选择这些频率,您将能够隔离信号的周期性
you can add this two lines who are determinated by certain number n_param
您可以添加由特定数字确定的这两行 n_param
h=np.sort(x_freqdom)[-n_param]
x_freqdom=[ x_freqdom[i] if np.absolute(x_freqdom[i])>=h else 0 for i in range(len(x_freqdom)) ]
just adding this you will be able to forecast nice and smooth
只需添加此功能,您就可以准确而流畅地进行预测
another useful article about FFt: forecast FFt in R
关于 FFt 的另一篇有用的文章: 在 R 中预测 FFt

