def run():
    # Create some data
    N = 500
    D = 2
    # Initial state
    x0 = np.array([0.5, -0.5])
    # Dynamics (time varying)
    A0 = np.array([[.9, -.4], [.4, .9]])
    A1 = np.array([[.98, -.1], [.1, .98]])
    l = np.linspace(0, 1, N - 1).reshape((-1, 1, 1))
    A = (1 - l) * A0 + l * A1
    # Innovation covariance matrix
    V = np.identity(D)
    # Observation noise covariance matrix
    C = np.tile(np.identity(D), (N, 1, 1))
    ## C0 = 10*np.array([[1, 0], [0, 1]])
    ## C1 = 0.01*np.array([[1, 0], [0, 1]])
    ## C = (1-l)**2*C0 + l**2*C1

    X = np.empty((N, D))
    Y = np.empty((N, D))

    # Simulate data
    x = x0
    X[0, :] = x
    Y[0, :] = x + np.random.multivariate_normal(np.zeros(D), C[0, :, :])
    for n in range(N - 1):
        x = np.dot(A[n, :, :], x) + np.random.multivariate_normal(
            np.zeros(D), V)
        X[n + 1, :] = x
        Y[n + 1, :] = x + np.random.multivariate_normal(
            np.zeros(D), C[n + 1, :, :])

    # Invert observation noise covariance to observation precision matrices
    U = np.empty((N, D, D))
    UY = np.empty((N, D))
    for n in range(N):
        U[n, :, :] = np.linalg.inv(C[n, :, :])
        UY[n, :] = np.linalg.solve(C[n, :, :], Y[n, :])

    # Construct VB model
    Xh = GaussianMarkovChain(np.zeros(D), np.identity(D), A, np.ones(D), n=N)
    Yh = Gaussian(Xh.as_gaussian(), np.identity(D), plates=(N, ))
    Yh.observe(Y)
    Xh.update()

    xh = Xh.u[0]
    varxh = utils.diagonal(Xh.u[1]) - xh**2
    #err = 2 * np.sqrt(varxh)
    plt.figure(1)
    plt.clf()
    for d in range(D):
        plt.subplot(D, 1, d)
        bpplt.errorplot(xh[:, d], error=2 * np.sqrt(varxh[:, d]))
        plt.plot(X[:, d], 'r-')
        plt.plot(Y[:, d], '.')
def run():
    # Create some data
    N = 500
    D = 2
    # Initial state
    x0 = np.array([0.5, -0.5])
    # Dynamics (time varying)
    A0 = np.array([[.9, -.4], [.4, .9]])
    A1 = np.array([[.98, -.1], [.1, .98]])
    l = np.linspace(0, 1, N-1).reshape((-1,1,1))
    A = (1-l)*A0 + l*A1
    # Innovation covariance matrix
    V = np.identity(D)
    # Observation noise covariance matrix
    C = np.tile(np.identity(D), (N, 1, 1))
    ## C0 = 10*np.array([[1, 0], [0, 1]])
    ## C1 = 0.01*np.array([[1, 0], [0, 1]])
    ## C = (1-l)**2*C0 + l**2*C1

    X = np.empty((N,D))
    Y = np.empty((N,D))

    # Simulate data
    x = x0
    X[0,:] = x
    Y[0,:] = x + np.random.multivariate_normal(np.zeros(D), C[0,:,:])
    for n in range(N-1):
        x = np.dot(A[n,:,:],x) + np.random.multivariate_normal(np.zeros(D), V)
        X[n+1,:] = x
        Y[n+1,:] = x + np.random.multivariate_normal(np.zeros(D), C[n+1,:,:])

    # Invert observation noise covariance to observation precision matrices
    U = np.empty((N,D,D))
    UY = np.empty((N,D))
    for n in range(N):
        U[n,:,:] = np.linalg.inv(C[n,:,:])
        UY[n,:] = np.linalg.solve(C[n,:,:], Y[n,:])

    # Construct VB model
    Xh = GaussianMarkovChain(np.zeros(D), np.identity(D), A, np.ones(D), n=N)
    Yh = Gaussian(Xh.as_gaussian(), np.identity(D), plates=(N,))
    Yh.observe(Y)
    Xh.update()

    xh = Xh.u[0]
    varxh = utils.diagonal(Xh.u[1]) - xh**2
    #err = 2 * np.sqrt(varxh)
    plt.figure(1)
    plt.clf()
    for d in range(D):
        plt.subplot(D,1,d)
        bpplt.errorplot(xh[:,d], error=2*np.sqrt(varxh[:,d]))
        plt.plot(X[:,d], 'r-')
        plt.plot(Y[:,d], '.')
Example #3
0
def run(maxiter=100):

    seed = 496#np.random.randint(1000)
    print("seed = ", seed)
    np.random.seed(seed)

    # Simulate some data
    D = 3
    M = 6
    N = 200
    c = np.random.randn(M,D)
    w = 0.3
    a = np.array([[np.cos(w), -np.sin(w), 0], 
                  [np.sin(w), np.cos(w),  0], 
                  [0,         0,          1]])
    x = np.empty((N,D))
    f = np.empty((M,N))
    y = np.empty((M,N))
    x[0] = 10*np.random.randn(D)
    f[:,0] = np.dot(c,x[0])
    y[:,0] = f[:,0] + 3*np.random.randn(M)
    for n in range(N-1):
        x[n+1] = np.dot(a,x[n]) + np.random.randn(D)
        f[:,n+1] = np.dot(c,x[n+1])
        y[:,n+1] = f[:,n+1] + 3*np.random.randn(M)

    # Create the model
    (Y, CX, X, tau, C, gamma, A, alpha) = linear_state_space_model(D, N, M)
    
    # Add missing values randomly
    mask = random.mask(M, N, p=0.3)
    # Add missing values to a period of time
    mask[:,30:80] = False
    y[~mask] = np.nan # BayesPy doesn't require this. Just for plotting.
    # Observe the data
    Y.observe(y, mask=mask)
    

    # Initialize nodes (must use some randomness for C)
    C.initialize_from_random()

    # Run inference
    Q = VB(Y, X, C, gamma, A, alpha, tau)

    #
    # Run inference with rotations.
    #
    rotA = transformations.RotateGaussianARD(A, alpha)
    rotX = transformations.RotateGaussianMarkovChain(X, A, rotA)
    rotC = transformations.RotateGaussianARD(C, gamma)
    R = transformations.RotationOptimizer(rotX, rotC, D)

    #maxiter = 84
    for ind in range(maxiter):
        Q.update()
        #print('C term', C.lower_bound_contribution())
        R.rotate(maxiter=10, 
                 check_gradient=True,
                 verbose=False,
                 check_bound=Q.compute_lowerbound,
        #check_bound=None,
                 check_bound_terms=Q.compute_lowerbound_terms)
        #check_bound_terms=None)

    X_vb = X.u[0]
    varX_vb = utils.diagonal(X.u[1] - X_vb[...,np.newaxis,:] * X_vb[...,:,np.newaxis])

    u_CX = CX.get_moments()
    CX_vb = u_CX[0]
    varCX_vb = u_CX[1] - CX_vb**2

    # Show results
    plt.figure(3)
    plt.clf()
    for m in range(M):
        plt.subplot(M,1,m+1)
        plt.plot(y[m,:], 'r.')
        plt.plot(f[m,:], 'b-')
        bpplt.errorplot(y=CX_vb[m,:],
                        error=2*np.sqrt(varCX_vb[m,:]))

    plt.figure()
    Q.plot_iteration_by_nodes()
Example #4
0
def run(maxiter=100):

    seed = 496  #np.random.randint(1000)
    print("seed = ", seed)
    np.random.seed(seed)

    # Simulate some data
    D = 3
    M = 6
    N = 200
    c = np.random.randn(M, D)
    w = 0.3
    a = np.array([[np.cos(w), -np.sin(w), 0], [np.sin(w),
                                               np.cos(w), 0], [0, 0, 1]])
    x = np.empty((N, D))
    f = np.empty((M, N))
    y = np.empty((M, N))
    x[0] = 10 * np.random.randn(D)
    f[:, 0] = np.dot(c, x[0])
    y[:, 0] = f[:, 0] + 3 * np.random.randn(M)
    for n in range(N - 1):
        x[n + 1] = np.dot(a, x[n]) + np.random.randn(D)
        f[:, n + 1] = np.dot(c, x[n + 1])
        y[:, n + 1] = f[:, n + 1] + 3 * np.random.randn(M)

    # Create the model
    (Y, CX, X, tau, C, gamma, A, alpha) = linear_state_space_model(D, N, M)

    # Add missing values randomly
    mask = random.mask(M, N, p=0.3)
    # Add missing values to a period of time
    mask[:, 30:80] = False
    y[~mask] = np.nan  # BayesPy doesn't require this. Just for plotting.
    # Observe the data
    Y.observe(y, mask=mask)

    # Initialize nodes (must use some randomness for C)
    C.initialize_from_random()

    # Run inference
    Q = VB(Y, X, C, gamma, A, alpha, tau)

    #
    # Run inference with rotations.
    #
    rotA = transformations.RotateGaussianARD(A, alpha)
    rotX = transformations.RotateGaussianMarkovChain(X, A, rotA)
    rotC = transformations.RotateGaussianARD(C, gamma)
    R = transformations.RotationOptimizer(rotX, rotC, D)

    #maxiter = 84
    for ind in range(maxiter):
        Q.update()
        #print('C term', C.lower_bound_contribution())
        R.rotate(
            maxiter=10,
            check_gradient=True,
            verbose=False,
            check_bound=Q.compute_lowerbound,
            #check_bound=None,
            check_bound_terms=Q.compute_lowerbound_terms)
        #check_bound_terms=None)

    X_vb = X.u[0]
    varX_vb = utils.diagonal(X.u[1] - X_vb[..., np.newaxis, :] *
                             X_vb[..., :, np.newaxis])

    u_CX = CX.get_moments()
    CX_vb = u_CX[0]
    varCX_vb = u_CX[1] - CX_vb**2

    # Show results
    plt.figure(3)
    plt.clf()
    for m in range(M):
        plt.subplot(M, 1, m + 1)
        plt.plot(y[m, :], 'r.')
        plt.plot(f[m, :], 'b-')
        bpplt.errorplot(y=CX_vb[m, :], error=2 * np.sqrt(varCX_vb[m, :]))

    plt.figure()
    Q.plot_iteration_by_nodes()