def demo(M=6, N=200, D=3, maxiter=100, debug=False, seed=42, rotate=True, precompute=False, plot=True, monitor=True): """ Run the demo for linear state-space model. """ # Use deterministic random numbers if seed is not None: np.random.seed(seed) # Get data (y, f) = simulate_data(M, N) # 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. # Run inference Q = infer(y, D, mask=mask, rotate=rotate, debug=debug, monitor=monitor, maxiter=maxiter) if plot: # Show results plt.figure() bpplt.timeseries_normal(Q['F'], scale=2) bpplt.timeseries(f, linestyle='-', color='b') bpplt.timeseries(y, linestyle='None', color='r', marker='.') plt.show()
def demo(N=1000, maxiter=100, D=3, K=2, seed=42, plot=True, debug=False, rotate=True, monitor=True): """ Run the demo for linear state-space model with switching dynamics. """ # Use deterministic random numbers if seed is not None: np.random.seed(seed) # Generate data (Y, F) = simulate_data(N) # Plot observations if plot: plt.figure() bpplt.timeseries(F, linestyle='-', color='b') bpplt.timeseries(Y, linestyle='None', color='r', marker='x') # Apply the linear state-space model with switching dynamics Q = infer(Y, D, K, debug=debug, maxiter=maxiter, monitor=monitor, rotate=rotate, update_hyper=5) # Show results if plot: Q.plot() plt.show() return
def demo(N=1000, maxiter=100, D=3, K=2, seed=42, plot=True, debug=False, rotate=True, monitor=True): """ Run the demo for linear state-space model with switching dynamics. """ # Use deterministic random numbers if seed is not None: np.random.seed(seed) # Generate data (Y, F) = simulate_data(N) # Plot observations if plot: plt.figure() bpplt.timeseries(F, linestyle='-', color='b') bpplt.timeseries(Y, linestyle='None', color='r', marker='x') # Apply the linear state-space model with switching dynamics Q = infer(Y, D, K, debug=debug, maxiter=maxiter, monitor=monitor, rotate=rotate, update_hyper=5) # Show results if plot: Q.plot() return
def demo(M=6, N=200, D=3, maxiter=100, debug=False, seed=42, rotate=True, precompute=False, plot=True, monitor=True): """ Run the demo for linear state-space model. """ # Use deterministic random numbers if seed is not None: np.random.seed(seed) # Get data (y, f) = simulate_data(M, N) # 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. # Run inference Q = infer(y, D, mask=mask, rotate=rotate, debug=debug, monitor=monitor, maxiter=maxiter) if plot: # Show results plt.figure() bpplt.timeseries_normal(Q['F'], scale=2) bpplt.timeseries(f, linestyle='-', color='b') bpplt.timeseries(y, linestyle='None', color='r', marker='.')
def run(M=10, N=100, D_y=3, D=5, seed=42, rotate=False, maxiter=100, debug=False, plot=True): if seed is not None: np.random.seed(seed) # Generate data w = np.random.normal(0, 1, size=(M, 1, D_y)) x = np.random.normal(0, 1, size=(1, N, D_y)) f = misc.sum_product(w, x, axes_to_sum=[-1]) y = f + np.random.normal(0, 0.2, size=(M, N)) # Construct model (Y, F, W, X, tau, alpha) = model(M, N, D) # Data with missing values mask = random.mask(M, N, p=0.5) # randomly missing y[~mask] = np.nan Y.observe(y, mask=mask) # Construct inference machine Q = VB(Y, W, X, tau, alpha) # Initialize some nodes randomly X.initialize_from_random() W.initialize_from_random() # Run inference algorithm if rotate: # Use rotations to speed up learning rotW = transformations.RotateGaussianARD(W, alpha) rotX = transformations.RotateGaussianARD(X) R = transformations.RotationOptimizer(rotW, rotX, D) for ind in range(maxiter): Q.update() if debug: R.rotate(check_bound=True, check_gradient=True) else: R.rotate() else: # Use standard VB-EM alone Q.update(repeat=maxiter) # Plot results if plot: plt.figure() bpplt.timeseries_normal(F, scale=2) bpplt.timeseries(f, color='g', linestyle='-') bpplt.timeseries(y, color='r', linestyle='None', marker='+')
def run(M=10, N=100, D_y=3, D=5, seed=42, rotate=False, maxiter=100, debug=False): if seed is not None: np.random.seed(seed) # Generate data w = np.random.normal(0, 1, size=(M,1,D_y)) x = np.random.normal(0, 1, size=(1,N,D_y)) f = misc.sum_product(w, x, axes_to_sum=[-1]) y = f + np.random.normal(0, 0.2, size=(M,N)) # Construct model (Y, F, W, X, tau, alpha) = model(M, N, D) # Data with missing values mask = random.mask(M, N, p=0.5) # randomly missing y[~mask] = np.nan Y.observe(y, mask=mask) # Construct inference machine Q = VB(Y, W, X, tau, alpha) # Initialize some nodes randomly X.initialize_from_random() W.initialize_from_random() # Run inference algorithm if rotate: # Use rotations to speed up learning rotW = transformations.RotateGaussianARD(W, alpha) rotX = transformations.RotateGaussianARD(X) R = transformations.RotationOptimizer(rotW, rotX, D) for ind in range(maxiter): Q.update() if debug: R.rotate(check_bound=True, check_gradient=True) else: R.rotate() else: # Use standard VB-EM alone Q.update(repeat=maxiter) # Plot results plt.figure() bpplt.timeseries_normal(F, scale=2) bpplt.timeseries(f, color='g', linestyle='-') bpplt.timeseries(y, color='r', linestyle='None', marker='+') plt.show()
def run(M=10, N=100, D_y=3, D=5, seed=42, rotate=False, maxiter=1000, debug=False, plot=True): if seed is not None: np.random.seed(seed) # Generate data w = np.random.normal(0, 1, size=(M, 1, D_y)) x = np.random.normal(0, 1, size=(1, N, D_y)) f = misc.sum_product(w, x, axes_to_sum=[-1]) y = f + np.random.normal(0, 0.1, size=(M, N)) # Construct model Q = model(M, N, D) # Data with missing values mask = random.mask(M, N, p=0.5) # randomly missing y[~mask] = np.nan Q['Y'].observe(y, mask=mask) # Run inference algorithm if rotate: # Use rotations to speed up learning rotW = transformations.RotateGaussianARD(Q['W'], Q['alpha']) rotX = transformations.RotateGaussianARD(Q['X']) R = transformations.RotationOptimizer(rotW, rotX, D) if debug: Q.callback = lambda: R.rotate(check_bound=True, check_gradient=True) else: Q.callback = R.rotate # Use standard VB-EM alone Q.update(repeat=maxiter) # Plot results if plot: plt.figure() bpplt.timeseries_normal(Q['F'], scale=2) bpplt.timeseries(f, color='g', linestyle='-') bpplt.timeseries(y, color='r', linestyle='None', marker='+')
def run(M=10, N=100, D_y=3, D=5, seed=42, rotate=False, maxiter=1000, debug=False, plot=True): if seed is not None: np.random.seed(seed) # Generate data w = np.random.normal(0, 1, size=(M,1,D_y)) x = np.random.normal(0, 1, size=(1,N,D_y)) f = misc.sum_product(w, x, axes_to_sum=[-1]) y = f + np.random.normal(0, 0.1, size=(M,N)) # Construct model Q = model(M, N, D) # Data with missing values mask = random.mask(M, N, p=0.5) # randomly missing y[~mask] = np.nan Q['Y'].observe(y, mask=mask) # Run inference algorithm if rotate: # Use rotations to speed up learning rotW = transformations.RotateGaussianARD(Q['W'], Q['alpha']) rotX = transformations.RotateGaussianARD(Q['X']) R = transformations.RotationOptimizer(rotW, rotX, D) if debug: Q.callback = lambda : R.rotate(check_bound=True, check_gradient=True) else: Q.callback = R.rotate # Use standard VB-EM alone Q.update(repeat=maxiter) # Plot results if plot: plt.figure() bpplt.timeseries_normal(Q['F'], scale=2) bpplt.timeseries(f, color='g', linestyle='-') bpplt.timeseries(y, color='r', linestyle='None', marker='+')
def demo(N=1000, D=5, K=4, seed=42, maxiter=200, rotate=True, debug=False, precompute=False, plot=True): # Seed for random number generator if seed is not None: np.random.seed(seed) # Create data (y, f) = simulate_data(N) # Create some gaps mask_gaps = misc.trues(N) for m in range(100, N, 140): start = m end = min(m+15, N-1) mask_gaps[start:end] = False # Randomly missing values mask_random = np.logical_or(random.mask(N, p=0.8), np.logical_not(mask_gaps)) # Remove the observations mask = np.logical_and(mask_gaps, mask_random) y[~mask] = np.nan # BayesPy doesn't require NaNs, they're just for plotting. # Add row axes y = y[None,...] f = f[None,...] mask = mask[None,...] mask_gaps = mask_gaps[None,...] mask_random = mask_random[None,...] # Run the method Q = infer(y, D, K, mask=mask, maxiter=maxiter, rotate=rotate, debug=debug, precompute=precompute, update_hyper=10, start_rotating_weights=20, monitor=True) if plot: # Plot observations plt.figure() bpplt.timeseries_normal(Q['F'], scale=2) bpplt.timeseries(f, linestyle='-', color='b') bpplt.timeseries(y, linestyle='None', color='r', marker='.') plt.ylim([-2, 2]) # Plot latent space Q.plot('X') # Plot mixing weight space Q.plot('S') # Compute RMSE rmse_random = misc.rmse(Q['Y'].get_moments()[0][~mask_random], f[~mask_random]) rmse_gaps = misc.rmse(Q['Y'].get_moments()[0][~mask_gaps], f[~mask_gaps]) print("RMSE for randomly missing values: %f" % rmse_random) print("RMSE for gap values: %f" % rmse_gaps) plt.show()
def demo(N=1000, D=5, K=4, seed=42, maxiter=200, rotate=True, debug=False, precompute=False, plot=True): # Seed for random number generator if seed is not None: np.random.seed(seed) # Create data (y, f) = simulate_data(N) # Create some gaps mask_gaps = misc.trues(N) for m in range(100, N, 140): start = m end = min(m + 15, N - 1) mask_gaps[start:end] = False # Randomly missing values mask_random = np.logical_or(random.mask(N, p=0.8), np.logical_not(mask_gaps)) # Remove the observations mask = np.logical_and(mask_gaps, mask_random) y[~mask] = np.nan # BayesPy doesn't require NaNs, they're just for plotting. # Add row axes y = y[None, ...] f = f[None, ...] mask = mask[None, ...] mask_gaps = mask_gaps[None, ...] mask_random = mask_random[None, ...] # Run the method Q = infer(y, D, K, mask=mask, maxiter=maxiter, rotate=rotate, debug=debug, precompute=precompute, update_hyper=10, start_rotating_weights=20, monitor=True) if plot: # Plot observations plt.figure() bpplt.timeseries_normal(Q['F'], scale=2) bpplt.timeseries(f, linestyle='-', color='b') bpplt.timeseries(y, linestyle='None', color='r', marker='.') plt.ylim([-2, 2]) # Plot latent space Q.plot('X') # Plot mixing weight space Q.plot('S') # Compute RMSE rmse_random = misc.rmse(Q['Y'].get_moments()[0][~mask_random], f[~mask_random]) rmse_gaps = misc.rmse(Q['Y'].get_moments()[0][~mask_gaps], f[~mask_gaps]) print("RMSE for randomly missing values: %f" % rmse_random) print("RMSE for gap values: %f" % rmse_gaps)