new functions mrd init_X update

This commit is contained in:
Max Zwiessele 2013-04-15 10:57:27 +01:00
parent cc32825a4a
commit c63beddcf0
4 changed files with 115 additions and 47 deletions

View file

@ -6,7 +6,6 @@ import pylab as pb
from matplotlib import pyplot as plt, pyplot
import GPy
from GPy.models.mrd import MRD
default_seed = np.random.seed(123344)
@ -102,10 +101,10 @@ def oil_100():
def mrd_simulation(plot_sim=False):
# num = 2
ard1 = np.array([1., 1, 0, 0], dtype=float)
ard2 = np.array([0., 1, 1, 0], dtype=float)
ard1[ard1 == 0] = 1E-10
ard2[ard2 == 0] = 1E-10
# ard1 = np.array([1., 1, 0, 0], dtype=float)
# ard2 = np.array([0., 1, 1, 0], dtype=float)
# ard1[ard1 == 0] = 1E-10
# ard2[ard2 == 0] = 1E-10
# ard1i = 1. / ard1
# ard2i = 1. / ard2
@ -119,46 +118,74 @@ def mrd_simulation(plot_sim=False):
# Y2 -= Y2.mean(0)
# make_params = lambda ard: np.hstack([[1], ard, [1, .3]])
D1, D2, N, M, Q = 5, 10, 150, 15, 3
x = np.linspace(0, 4 * np.pi, N)[:, None]
D1, D2, D3, N, M, Q = 5, 5, 5, 150, 18, 5
x = np.linspace(0, 2 * np.pi, N)[:, None]
s1 = np.vectorize(lambda x: np.sin(x))
s2 = np.vectorize(lambda x: np.cos(x))
s3 = np.vectorize(lambda x: np.cos(4 * x))
sS = np.vectorize(lambda x: np.sin(2 * x))
S1 = np.hstack([s1(x), sS(x)]) + .1 * np.random.randn(N, 2)
S2 = np.hstack([s2(x), sS(x)]) + .1 * np.random.randn(N, 2)
s1 = s1(x)
s2 = s2(x)
s3 = s3(x)
sS = sS(x)
s1 /= np.abs(s1).max()
s2 /= np.abs(s2).max()
s3 /= np.abs(s3).max()
sS /= np.abs(sS).max()
S1 = np.hstack([s1, sS])
S2 = np.hstack([s2, sS])
S3 = np.hstack([s3, sS])
Y1 = S1.dot(np.random.randn(S1.shape[1], D1))
Y2 = S2.dot(np.random.randn(S2.shape[1], D2))
Y3 = S3.dot(np.random.randn(S3.shape[1], D3))
Y1 += .041 * np.random.randn(*Y1.shape)
Y2 += .041 * np.random.randn(*Y2.shape)
Y3 += .041 * np.random.randn(*Y3.shape)
Y1 -= Y1.mean(0)
Y2 -= Y2.mean(0)
Y3 -= Y3.mean(0)
Y1 /= Y1.std(0)
Y2 /= Y2.std(0)
Y3 /= Y3.std(0)
Slist = [s1, s2, s3, sS]
Ylist = [Y1, Y2, Y3]
if plot_sim:
import pylab
fig = pylab.figure("MRD Simulation")
ax = fig.add_subplot(2, 2, 1)
ax.imshow(S1)
ax.set_title("S1")
ax = fig.add_subplot(2, 2, 2)
ax.imshow(S2)
ax.set_title("S2")
ax = fig.add_subplot(2, 2, 3)
ax.imshow(Y1)
ax.set_title("Y1")
ax = fig.add_subplot(2, 2, 4)
ax.imshow(Y2)
ax.set_title("Y2")
import itertools
fig = pylab.figure("MRD Simulation", figsize=(12, 12))
fig.clf()
ax = fig.add_subplot(2, 1, 1)
labls = sorted(filter(lambda x: x.startswith("s"), locals()))
for S, lab in itertools.izip(Slist, labls):
ax.plot(x, S, label=lab)
ax.legend()
for i, Y in enumerate(Ylist):
ax = fig.add_subplot(2, len(Ylist), len(Slist) + i)
ax.imshow(Y)
ax.set_title("Y{}".format(i + 1))
pylab.draw()
pylab.tight_layout()
k = GPy.kern.rbf(Q, ARD=True) + GPy.kern.bias(Q) + GPy.kern.white(Q)
m = MRD(Y1, Y2, Q=Q, M=M, kernel=k, init="concat", _debug=False)
from GPy.models import mrd
from GPy import kern
reload(mrd); reload(kern)
k = kern.rbf(Q, ARD=True) + kern.bias(Q) + kern.white(Q)
m = mrd.MRD(*Ylist, Q=Q, M=M, kernel=k, init="concat", _debug=False)
m.ensure_default_constraints()
cstr = "noise|white"
m.unconstrain(cstr); m.constrain_bounded(cstr, 1e-3, 1.)
# cstr = "noise|white|variance"
# m.unconstrain(cstr); m.constrain_bounded(cstr, 1e-6, 1.)
m.auto_scale_factor = True
# fig = pyplot.figure("expected", figsize=(8, 3))
# ax = fig.add_subplot(121)