金融时间序列的离散小波分解(Python)

摘要:import numpy as npimport pandas as pdimport yfinance as yfimport pywtimport matplotlib.pyplot as pltimport ossp500 = yf.download('

import numpy as npimport pandas as pdimport yfinance as yfimport pywtimport matplotlib.pyplot as pltimport ossp500 = yf.download('^GSPC', start='2020-01-01', end='2024-08-31')returns = np.log(sp500['Close']).diff.dropnawavelet = 'db8' level = 5coeffs = pywt.wavedec(returns, wavelet, mode='per', level=level)plt.style.use('dark_background')def add_textbox(ax, text, fontsize, fontweight='normal'):ax.text(0.02, 0.95, text, transform=ax.transAxes, fontsize=fontsize, fontweight=fontweight,verticalalignment='top', bbox=dict(boxstyle='round', facecolor='black', edgecolor='white', alpha=0.7))fig1, (ax1, ax2) = plt.subplots(2, 1, figsize=(14, 10), sharex=True)fig1.suptitle('S&P 500 Log Returns and Long-term Trend', fontsize=20, fontweight='bold', y=0.98)ax1.plot(returns.index, returns, color='cyan', lw=1.5)add_textbox(ax1, 'Original Signal', 14, 'bold')ax1.set_ylabel('Log Returns', fontsize=12)# A5reconstructed_approx = pywt.waverec([coeffs[0]] + [np.zeros_like(c) for c in coeffs[1:]], wavelet, mode='per')ax2.plot(returns.index, reconstructed_approx[:len(returns)], color='red', lw=1.5)add_textbox(ax2, 'Approximation (A5)', 14, 'bold')ax2.set_ylabel('A5', fontsize=12)ax2.set_xlabel('Date', fontsize=12)for ax in (ax1, ax2):ax.tick_params(axis='x', rotation=45)ax.grid(True, color='gray', linestyle='--', linewidth=0.5)plt.tight_layout(rect=[0, 0.02, 1, 0.96])returns_trend_path = os.path.join(current_dir, 'sp500_returns_and_trend.png')plt.savefig(returns_trend_path, dpi=300, bbox_inches='tight')plt.showplt.closefig2, axs = plt.subplots(level, 1, figsize=(14, 4*level), sharex=True)fig2.suptitle('Wavelet Decomposition: Detail Levels', fontsize=20, fontweight='bold', y=0.98)colors = plt.cm.coolwarm(np.linspace(0, 1, level))for i in range(level):detail_coeffs = [np.zeros_like(c) if j != i+1 else coeffs[j] for j, c in enumerate(coeffs)]reconstructed_detail = pywt.waverec(detail_coeffs, wavelet, mode='per')axs[i].plot(returns.index, reconstructed_detail[:len(returns)], color=colors[i], lw=1.5)add_textbox(axs[i], f'Detail Level {level-i}', 14, 'bold')axs[i].set_ylabel(f'D{level-i}', fontsize=12)axs[-1].set_xlabel('Date', fontsize=12)for ax in axs:ax.tick_params(axis='x', rotation=45)ax.grid(True, color='gray', linestyle='--', linewidth=0.5)plt.tight_layout(rect=[0, 0.02, 1, 0.96])wavelet_details_path = os.path.join(current_dir, 'sp500_wavelet_details.png')plt.savefig(wavelet_details_path, dpi=300, bbox_inches='tight')plt.showplt.close知乎学术咨询:https://www.zhihu.com/consult/people/792359672131756032?isMe=1担任《Mechanical System and Signal Processing》《中国电机工程学报》等期刊审稿专家,擅长领域:信号滤波/降噪,机器学习/深度学习,时间序列预分析/预测,设备故障诊断/缺陷检测/异常检测。分割线分割线分割线

Python环境下基于最大离散重叠小波变换和支持向量回归的金融时间序列预测

import numpy as npimport pandas as pdimport copyimport matplotlib.pyplot as pltfrom sklearn.model_selection import train_test_splitfrom sklearn import svmfrom sklearn.metrics import mean_squared_errorfrom numpy.lib.stride_tricks import sliding_window_viewfrom modwt import modwt, modwtmra,imodwt

完整代码:

使用时间序列分析方法预测比特币价格(密集神经网络、卷积网络、LSTM网络等方法,Python,ipynb文件)

压缩包文件包括Jupyter Notebook文件(Python编写)和数据。

完整代码:

基于深度学习的股票市场价格(时间序列)预测(Python,ipynb文件)

基于注意力机制长短期记忆(LSTM)网络的通用电气股票价格预测(Python,Keras)

Python环境下基于VMD-Attention-LSTM模型收盘价预测深度学习模型

import numpy as npimport pandas as pdfrom sklearn.preprocessing import MinMaxScalerimport tensorflow as tfimport models.vmd_attention_lstm as mvimport utilt.VMD as vmdzi

来源:彬琪教育

相关推荐