代替前言:当经典机器学习放弃时
加密货币市场是传统预测方法走向死亡的地方。LSTM模型开始对比特币的波动性感到紧张,ARIMA模型对以太坊的急剧跳跃产生歇斯底里,而经典神经网络在看到狗狗币图表时干脆放弃。然后扩散模型登上舞台——这项最初教计算机画猫的技术,现在试图预测比特币何时决定再来一个"黑色星期一"。
有趣的是,诞生了Stable Diffusion和DALL-E的架构现在被积极应用于金融时间序列分析。你知道吗?效果相当不错。特别是当经典方法开始因极端加密货币波动性而产生幻觉时。
为什么扩散模型能与时间序列一起工作?
扩散模型是一类生成模型,通过学习从噪声中恢复原始数据的过程,通过顺序"去噪"过程。基本思想很简单:我们取真实数据,逐渐向其添加高斯噪声,直到得到纯噪声,然后教神经网络逆转这个过程。
在金融时间序列的背景下,这意味着模型学会从字面意义上分离信号和噪声。加密货币市场以其极端噪声而闻名——随机的埃隆·马斯克推文、恐慌性抛售、FOMO购买。扩散模型可以学会"看到"所有这些混乱中的结构性模式。
数学上,这个过程看起来像这样:
- 前向过程:
- 反向过程:
其中 是噪声调度, 是神经网络参数。
具体库和现成解决方案
1. Diffusion-TS:时间序列的通用士兵
GitHub:Y-debug-sys/Diffusion-TS
这是用于时间序列扩散模型的旗舰库,在ICLR 2024上发表。主要优势是它既可以有条件地工作(预测),也可以无条件地工作(生成)。
import torch
from diffusion_ts import DiffusionTS
import pandas as pd
btc_data = pd.read_csv('btc_prices.csv')
prices = torch.tensor(btc_data['close'].values).float()
model = DiffusionTS(
input_dim=1,
hidden_dim=64,
num_layers=4,
max_sequence_length=100,
num_diffusion_steps=1000
)
model.fit(prices, epochs=100)
forecast = model.predict(prices[-100:], forecast_horizon=24)
该模型使用编码器-解码器transformer,具有分离的时间表示,其中分解有助于捕获时间序列的语义含义。
2. TSDiff:亚马逊应对加密货币混乱的方法
GitHub:amazon-science/unconditional-time-series-diffusion
亚马逊研究提出了TSDiff——一个无条件扩散模型,可以通过自引导机制进行预测工作。特殊性在于该模型不需要额外的网络进行条件化。
from tsdiff import TSDiff
import numpy as np
crypto_data = load_cryptocurrency_data(['BTC', 'ETH', 'LTC'])
tsdiff = TSDiff(
input_size=crypto_data.shape[-1],
hidden_size=128,
num_layers=6,
diffusion_steps=1000,
beta_schedule='cosine'
)
tsdiff.train(crypto_data, num_epochs=200)
synthetic_crypto = tsdiff.sample(num_samples=1000, length=365)
forecast = tsdiff.forecast_with_guidance(
context=crypto_data[-30:], # 最近30天
forecast_length=7, # 一周预测
guidance_scale=2.0
)
3. FinDiff:表格金融数据遇见扩散
论文:FinDiff专门设计用于生成合成金融表格数据。适合创建多样化的市场场景。
import torch
from findiff import FinancialDiffusion
market_data = pd.read_csv('crypto_market_features.csv')
financial_features = [
'price', 'volume', 'market_cap', 'volatility',
'rsi', 'macd', 'bollinger_bands'
]
findiff = FinancialDiffusion(
categorical_columns=['exchange', 'crypto_type'],
numerical_columns=financial_features,
embedding_dim=32,
hidden_dim=256
)
findiff.fit(market_data[financial_features])
synthetic_scenarios = findiff.generate(n_samples=10000)
stress_test_data = findiff.generate_conditional(
conditions={'volatility': '>0.8'} # 高波动性
)
4. 使用pytorch-forecasting快速实现
对于那些想要快速尝试扩散模型与成熟架构结合的人:
import lightning.pytorch as pl
from pytorch_forecasting import TimeSeriesDataSet, TemporalFusionTransformer
from diffusion_wrapper import DiffusionTFT # 假设包装器
crypto_df = pd.read_csv('hourly_crypto_data.csv')
training = TimeSeriesDataSet(
crypto_df,
time_idx="hour",
target="btc_price",
group_ids=["crypto_pair"],
max_encoder_length=168, # 一周前
max_prediction_length=24, # 一天后
time_varying_unknown_reals=["price", "volume", "volatility"],
time_varying_known_reals=["hour_of_day", "day_of_week"],
)
diffusion_tft = DiffusionTFT.from_dataset(
training,
hidden_size=64,
attention_head_size=4,
diffusion_steps=100,
noise_schedule='linear'
)
trainer = pl.Trainer(max_epochs=50, accelerator="gpu")
trainer.fit(diffusion_tft, train_dataloaders=training.to_dataloader(train=True))
实际结果:扩散 vs 经典
研究显示有趣的结果。在论文"通过路径依赖蒙特卡洛模拟预测加密货币价格"中,作者使用默顿跳跃扩散模型——随机过程和机器学习的混合。结果?该模型能够捕获加密货币市场特有的渐进价格变化和急剧跳跃。
另一项研究显示,ADE-TFT(高级深度学习增强时间融合变换器)与扩散组件在MAPE、MSE和RMSE指标上显著优于经典方法。8隐藏层配置的结果特别令人印象深刻。
扩散模型在金融中的阴暗面
但让我们诚实一点。扩散模型不是银弹。它们有严重的问题:
1. 计算贪婪性
在加密货币数据上训练扩散模型需要严重的计算资源。如果你的模型进行1000步扩散,那么要获得一个预测需要1000次通过神经网络。这不非常适合高频交易。
2. 黑天鹅问题
加密货币市场以极端事件而闻名——一天内暴跌50%,中国禁止加密货币,主要交易所被黑客攻击。在历史数据上训练的扩散模型对这些事件的预测很差。
3. 制度依赖性
加密货币市场有各种行为制度——牛市、熊市、横盘整理。扩散模型可能在一个制度中工作出色,而在另一个制度中完全失败。
优化和加速:如何不在GPU上破产
扩散的Token合并
GitHub:dbolya/tomesd
Token合并库允许通过合并冗余token将扩散模型加速1.24倍而不损失质量:
import tomesd
from diffusion_model import CryptoDiffusion
model = CryptoDiffusion(...)
tomesd.apply_patch(model, ratio=0.7) # 移除30%的token
forecast = model.predict(btc_data)
缓存自适应Token合并
GitHub:omidiu/ca_tome
CA-ToMe结合空间和时间优化,这对时间序列特别重要:
from ca_tome import apply_ca_tome
apply_ca_tome(
model,
threshold=0.7,
caching_steps=[0, 10, 20, 30, 40] # 每10步缓存一次
)
实际例子:比特币的完整管道
这是如何使用扩散模型进行比特币预测的现实例子:
import torch
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from diffusion_ts import DiffusionTS
class CryptoDiffusionPipeline:
def __init__(self, sequence_length=100, forecast_horizon=24):
self.sequence_length = sequence_length
self.forecast_horizon = forecast_horizon
self.scaler = MinMaxScaler()
self.model = None
def prepare_data(self, crypto_data):
"""考虑加密货币特征的数据准备"""
crypto_data['returns'] = crypto_data['close'].pct_change()
crypto_data['volatility'] = crypto_data['returns'].rolling(24).std()
crypto_data['rsi'] = self.compute_rsi(crypto_data['close'])
features = ['close', 'volume', 'volatility', 'rsi']
scaled_data = self.scaler.fit_transform(crypto_data[features])
return scaled_data
def train_model(self, data):
"""训练扩散模型"""
self.model = DiffusionTS(
input_dim=data.shape[1],
hidden_dim=128,
num_layers=6,
diffusion_steps=1000,
noise_schedule='cosine',
loss_type='l2'
)
X, y = self.create_sequences(data)
self.model.fit(
X, y,
epochs=200,
batch_size=32,
learning_rate=1e-4,
validation_split=0.2
)
def forecast(self, recent_data):
"""带置信区间的预测"""
predictions = []
for _ in range(100): # 蒙特卡洛采样
pred = self.model.sample_forecast(
context=recent_data[-self.sequence_length:],
horizon=self.forecast_horizon
)
predictions.append(pred)
predictions = np.array(predictions)
mean_pred = np.mean(predictions, axis=0)
std_pred = np.std(predictions, axis=0)
return {
'forecast': mean_pred,
'confidence_95': mean_pred + 1.96 * std_pred,
'confidence_5': mean_pred - 1.96 * std_pred
}
pipeline = CryptoDiffusionPipeline()
btc_data = pd.read_csv('btc_hourly.csv')
prepared_data = pipeline.prepare_data(btc_data)
pipeline.train_model(prepared_data)
forecast_result = pipeline.forecast(prepared_data)
print(f"比特币未来24小时预测:{forecast_result['forecast'][-1]:.2f}")
何时应该使用扩散模型?
值得使用如果:
- 你有大量历史数据(至少一年的小时数据)
- 你能负担长时间训练(GPU上几天-几周)
- 需要合成场景生成用于回测
- 处理多变量时间序列
- 预测不确定性估计很重要
不值得使用如果:
- 需要实时快速预测
- 处理短时间序列
- 计算资源有限
- 模型可解释性很关键
扩散模型在加密分析中的未来
金融中的扩散模型就像2010年的加密货币。技术原始,资源密集,但潜力巨大。我们已经看到混合方法:DDPM + Transformer,扩散 + 强化学习,市场制度的条件扩散。
下一个突破预计在多模态扩散领域——不仅考虑价格,还考虑新闻、社交信号、链上指标的模型。想象一个扩散模型"看到"埃隆·马斯克推文与狗狗币运动之间的相关性。
结论:扩散作为进化,而非革命
扩散模型不会取代加密货币预测的经典方法。它们将补充它们。LSTM将保留用于快速预测,ARIMA用于平稳部分,而扩散将承担场景生成和极端波动性工作。
主要教训:在加密货币世界中,没有银弹。只有智能工具组合、深入的市场理解和健康的对任何"革命性"解决方案的怀疑。扩散模型是强大的工具,但记住:它们只是试图在混乱中找到模式。而混乱,众所周知,不太喜欢被预测。
附言:如果你的扩散模型在比特币预测上显示95%的准确性——检查代码两次。很可能某处有数据泄露 😉
MarketMaker.cc Team
量化研究与策略