我正在尝试使用PyMC3
将高斯过程回归函数拟合到一些基本的金融时间序列数据,以便预测接下来几天给定过去价格的“价格”。然而,当我试图从合适的GP中形成预测时,我遇到了问题。
请原谅我,如果其他地方已经涉及过;我已经做了很多挖掘和搜索,但是尽管使用PyMC3
进行了GP回归的各种相关教程,但我无法提出解决方案。
通过获取每日价格的自然对数,然后从detrend
应用statsmodels.tsa.tsatools
,时间序列已经转换为静止(在某种意义上),然后应用In: X[0]
Out: array([ 0.02824104, 0.02591076, 0.00990705, 0.00150235, 0.01375847,
0.02963981])
。
输入X变量是6维(过去6个价格),这是一个数据点的示例:
In: y[0]
Out: array([ 0.01260668])
关联的y变量是下一天的价格:
import pymc3 as pm
with pm.Model() as gp_fit:
# Prior beliefs in hyperparameter values (they're Gamma distributed as specified) for Matern 3/2 kernel
ρ = pm.Gamma('ρ', 1, 1)
η = pm.Gamma('η', 1, 1)
# The input is 6 dimensional hence Matern32(6, ρ)
K = η * pm.gp.cov.Matern32(6, ρ)
# Prior belief in mean of the Gaussian Process (it's zero)
M = pm.gp.mean.Zero()
# Prior noise belief (Standard Normal, 0 mean, unit variance)
σ = pm.Normal('σ', 0, 1)
# Fit to first 20 data points
y_obs = pm.gp.GP('y_obs', mean_func=M, cov_func=K, sigma=σ, observed={'X':X[:20], 'Y':y[:20]})
trace = pm.sample(2000, n_init=40000)
我适合GP的代码是:
Auto-assigning NUTS sampler...
Initializing NUTS using ADVI...
Average Loss = -2,456.9: 83%|████████▎ | 33167/40000 [00:38<00:06, 1133.80it/s]
Convergence archived at 33200
Interrupted at 33,200 [83%]: Average Loss = -1,467.9
100%|█████████▉| 2499/2500 [17:29<00:00, 15.29it/s] /home/bango/miniconda3/lib/python3.6/site-packages/pymc3/step_methods/hmc/nuts.py:448: UserWarning: Chain 0 reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
'reparameterize.' % self._chain_id)
/home/bango/miniconda3/lib/python3.6/site-packages/pymc3/step_methods/hmc/nuts.py:440: UserWarning: The acceptance probability in chain 0 does not match the target. It is 0.18543857929, but should be close to 0.8. Try to increase the number of tuning steps.
% (self._chain_id, mean_accept, target_accept))
100%|██████████| 2500/2500 [17:29<00:00, 2.38it/s]
它给出了输出(带有一些警告):
# Get the features of a single data point and rehape to (1,6)
X_test = X[20][None,:]
with gp_fit:
gp_samples = pm.gp.sample_gp(trace, y_obs, X_test, samples=50, random_seed=666, progressbar=False)
随后,我尝试通过以下方式从拟合的GP中获得预测的y值(给出一些新的观察结果):
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-29-9eaebf0e139a> in <module>()
1 with gp_fit:
----> 2 gp_samples = pm.gp.sample_gp(trace, y_obs, Z, samples=50, random_seed=666, progressbar=False)
~/miniconda3/lib/python3.6/site-packages/pymc3/gp/gp.py in sample_gp(trace, gp, X_values, samples, obs_noise, model, random_seed, progressbar)
139 gp_post = MvNormal.dist(m_post, S_post, shape=Z.shape[0])
140
--> 141 samples = [gp_post.random(point=trace[idx]) for idx in indices]
142
143 return np.array(samples)
~/miniconda3/lib/python3.6/site-packages/pymc3/gp/gp.py in <listcomp>(.0)
139 gp_post = MvNormal.dist(m_post, S_post, shape=Z.shape[0])
140
--> 141 samples = [gp_post.random(point=trace[idx]) for idx in indices]
142
143 return np.array(samples)
~/miniconda3/lib/python3.6/site-packages/pymc3/distributions/multivariate.py in random(self, point, size)
156 mu, cov = draw_values([self.mu, self.cov], point=point)
157 if mu.shape != cov[0].shape:
--> 158 raise ValueError("Shapes for mu an cov don't match")
159
160 try:
ValueError: Shapes for mu an cov don't match
但碰到:
$(document).ready(function() {
//location API
$("#myweather").load("window",function() {
$("#location").fadeOut(function() {
$.ajax({
async: false,
url: "https://geoip-db.com/jsonp",
jsonpCallback: "callback",
dataType: "jsonp",
success: function(loc) {
$("#location").html(loc.city)
var long = loc.longitude
var lat = loc.latitude
$("#result").load("location", function() {
$("#result").fadeIn(function() {
$.getJSON("https://api.darksky.net/forecast/15f8bf5641489ec32f66662221933c14/" + lat + long, function(forecast) {
consolelog(forecast);
});
});
});
}
});
}).fadeIn();
});
});
我已经挖了一段时间,但目前无法分开错误并纠正它。我不完全确定是什么mu和cov矩阵被引用。
非常感谢您对此的帮助,谢谢!