Exemplo n.º 1
0
#( 5 : Parameter expansion 収束が遅い時)
# from bayespy.inference.vmp import transformations
# rotX = transformations.RotateGaussianARD(X)
# rotC = transformations.RotateGaussianARD(C, alpha)
# R = transformations.RotationOptimizer(rotC, rotX, D)
# R.rotate()
# alpha.initialize_from_prior()
# C.initialize_from_prior()
# X.initialize_from_parameters(np.random.randn(1, 100, D), 10)
# tau.initialize_from_prior()
# Q = VB(Y, C, X, alpha, tau)
# Q.callback = R.rotate
# Q.update(repeat=1000, tol=1e-6)

#  -----Examining the results-----
# Plotting the results
bpplt.pyplot.figure()
bpplt.pdf(Q['tau'], np.linspace(60, 140, num=100))

V = Gaussian([3, 5], [[4, 2], [2, 5]])
bpplt.pyplot.figure()
bpplt.contour(V, np.linspace(1, 5, num=100), np.linspace(3, 7, num=100))

bpplt.pyplot.figure()
bpplt.hinton(C)

bpplt.pyplot.figure()
bpplt.plot(X, axis=-2)

bpplt.pyplot.show()
Exemplo n.º 2
0
import numpy as np

np.random.seed(1)
data = np.random.normal(5, 10, size=(10, ))
from bayespy.nodes import GaussianARD, Gamma

mu = GaussianARD(0, 1e-6)
tau = Gamma(1e-6, 1e-6)
y = GaussianARD(mu, tau, plates=(10, ))
y.observe(data)
from bayespy.inference import VB

Q = VB(mu, tau, y)
Q.update(repeat=20)
import bayespy.plot as bpplt

bpplt.pyplot.subplot(2, 1, 1)
bpplt.pdf(mu, np.linspace(-10, 20, num=100), color='k', name=r'\mu')
bpplt.pyplot.subplot(2, 1, 2)
bpplt.pdf(tau, np.linspace(1e-6, 0.08, num=100), color='k', name=r'\tau')
bpplt.pyplot.tight_layout()
bpplt.pyplot.show()
Exemplo n.º 3
0
def test_pdf_plot():
    data = _setup_linear_regression()
    bpplt.pdf(data['tau'], np.linspace(1e-6,1,100), color='k')
    bpplt.pyplot.axvline(data['s']**(-2), color='r')
Exemplo n.º 4
0
# In[4]:

from bayespy.inference import VB
Q = VB(mu, tau, y)


# The inference algorithm can be run as long as wanted (max. 20 iterations in this case):

# In[5]:

Q.update(repeat=20)


# Now the algorithm converged after four iterations, before the requested 20 iterations. 
# 
# VB approximates the true posterior $p(\mu,\tau|\mathbf{y})$ with a distribution which factorizes with respect to the nodes: $q(\mu)q(\tau)$. The resulting approximate posterior distributions $q(\mu)$ and $q(\tau)$ can be examined, for instance, by plotting the marginal probability density functions:

# In[6]:

import bayespy.plot as bpplt
# The following two two lines are just for enabling matplotlib plotting in notebooks
get_ipython().magic('matplotlib inline')
bpplt.pyplot.plot([])
bpplt.pyplot.subplot(2, 1, 1)
bpplt.pdf(mu, np.linspace(-10, 20, num=100), color='k', name=r'\mu')
bpplt.pyplot.subplot(2, 1, 2)
bpplt.pdf(tau, np.linspace(1e-6, 0.08, num=100), color='k', name=r'\tau');


# This example was a very simple introduction to using BayesPy.  The model can be much more complex and each phase contains more options to give the user more control over the inference.  The following sections give more details about the phases.
Exemplo n.º 5
0
def test_pdf_plot():
    data = _setup_linear_regression()
    bpplt.pdf(data['tau'], np.linspace(1e-6, 1, 100), color='k')
    bpplt.pyplot.axvline(data['s']**(-2), color='r')