2014-03-04 21 views
4

估算後路預測說我有隨機的一組X,Y點:在迴歸

x = np.array(range(0,50)) 
y = np.random.uniform(low=0.0, high=40.0, size=200) 
y = map((lambda a: a[0] + a[1]), zip(x,y)) 
plt.scatter(x,y) 

enter image description here

假設我建模y如使用線性迴歸的x每個值的高斯,我怎樣才能估計posterior predictive,即p(y|x)爲每個(可能的)值x

有沒有直接的方式做到這一點pymcscikit-learn

+4

你知道怎麼用手做這個嗎? – User

回答

2

如果我明白你想要什麼,你可以使用gMC版本的PyMC(PyMC3)和glm子模塊來做到這一點。 例如

import numpy as np 
import pymc as pm 
import matplotlib.pyplot as plt 
from pymc import glm 

## Make some data 
x = np.array(range(0,50)) 
y = np.random.uniform(low=0.0, high=40.0, size=50) 
y = 2*x+y 
## plt.scatter(x,y) 

data = dict(x=x, y=y) 
with pm.Model() as model: 
    # specify glm and pass in data. The resulting linear model, its likelihood and 
    # and all its parameters are automatically added to our model. 
    pm.glm.glm('y ~ x', data) 
    step = pm.NUTS() # Instantiate MCMC sampling algorithm 
    trace = pm.sample(2000, step) 


##fig = pm.traceplot(trace, lines={'alpha': 1, 'beta': 2, 'sigma': .5});## traces 
fig = plt.figure() 
ax = fig.add_subplot(111) 
plt.scatter(x, y, label='data') 
glm.plot_posterior_predictive(trace, samples=50, eval=x, 
           label='posterior predictive regression lines') 

爲了得到這樣的posterior predictive

你會發現這些博客文章有意思:從那裏我把想法 12

編輯 爲了得到每個x的y值,嘗試從挖掘到glm源中獲得的這個值。

lm = lambda x, sample: sample['Intercept'] + sample['x'] * x ## linear model 
samples=50 ## Choose to be the same as in plot call 
trace_det = np.empty([samples, len(x)]) ## initialise 
for i, rand_loc in enumerate(np.random.randint(0, len(trace), samples)): 
    rand_sample = trace[rand_loc] 
    trace_det[i] = lm(x, rand_sample) 
y = trace_det.T 
y[0] 

道歉,如果它不是最優雅的 - 希望你可以按照邏輯。

+0

謝謝!我特別感興趣的是得到'y [0],y [1],... y [50]'的樣本(即每個'y [i]'的樣本向量)。你知道我能怎麼做到嗎? –

+0

要清楚,對於x的每個值,您都希望50 y值? – nstjhp

+0

是的 - 這是正確的(即我正在尋找的應該是確定性的痕跡)。 –