def svi_example(true_model, true_data):
    X, mask = true_data.X, true_data.mask

    # Fit a test model
    model = FactorAnalysis(
        D_obs, D_latent,
        # W=true_model.W, sigmasq=true_model.sigmasq
        )

    # Add the data in minibatches
    minibatchsize = 250
    for start in range(0, N, minibatchsize):
        end = min(start + minibatchsize, N)
        model.add_data(X[start:end], mask=mask[start:end])

    lps = []
    angles = []
    N_iters = 100
    delay = 10.0
    forgetting_rate = 0.75
    stepsize = (np.arange(N_iters) + delay)**(-forgetting_rate)
    for itr in progprint_xrange(N_iters):
        lps.append(model.meanfield_sgdstep(stepsize[itr]))
        E_W, _, _, _ = model.regression.mf_expectations
        angles.append(principal_angle(true_model.W, E_W))

    Z_inf = model.data_list[0].E_Z
    Z_true = true_data.Z[:Z_inf.shape[0]]
    plot_results(lps, angles, Z_true, Z_inf)
def svi_example(true_model, X, Z_true, mask):
    # Fit a test model
    model = FactorAnalysis(
        D_obs, D_latent,
        # W=true_model.W, sigmasq=true_model.sigmasq
        )

    # Add the data in minibatches
    N = X.shape[0]
    minibatchsize = 200
    prob = minibatchsize / float(N)

    lps = []
    angles = []
    N_iters = 100
    delay = 10.0
    forgetting_rate = 0.75
    stepsize = (np.arange(N_iters) + delay)**(-forgetting_rate)
    for itr in progprint_xrange(N_iters):
        minibatch = np.random.permutation(N)[:minibatchsize]
        X_mb, mask_mb = X[minibatch], mask[minibatch]
        lps.append(model.meanfield_sgdstep(X_mb, prob, stepsize[itr], masks=mask_mb))
        E_W, _, _, _ = model.regression.mf_expectations
        angles.append(principal_angle(true_model.W, E_W))

    # Compute the expected states for the first minibatch of data
    model.add_data(X, mask)
    statesobj = model.data_list.pop()
    statesobj.meanfieldupdate()
    Z_inf = statesobj.E_Z
    plot_results(lps, angles, Z_true, Z_inf)
def svi_example(true_model, true_data):
    X, mask = true_data.X, true_data.mask

    # Fit a test model
    model = FactorAnalysis(
        D_obs, D_latent,
        # W=true_model.W, sigmasq=true_model.sigmasq
        )

    # Add the data in minibatches
    N = X.shape[0]
    minibatchsize = 200
    prob = minibatchsize / float(N)

    lps = []
    angles = []
    N_iters = 100
    delay = 10.0
    forgetting_rate = 0.75
    stepsize = (np.arange(N_iters) + delay)**(-forgetting_rate)
    for itr in progprint_xrange(N_iters):
        minibatch = np.random.permutation(N)[:minibatchsize]
        X_mb, mask_mb = X[minibatch], mask[minibatch]
        lps.append(model.meanfield_sgdstep(X_mb, prob, stepsize[itr], masks=mask_mb))
        E_W, _, _, _ = model.regression.mf_expectations
        angles.append(principal_angle(true_model.W, E_W))

    # Compute the expected states for the first minibatch of data
    model.add_data(X[:minibatchsize], mask[:minibatchsize])
    statesobj = model.data_list.pop()
    statesobj.meanfieldupdate()
    Z_inf = statesobj.E_Z
    Z_true = true_data.Z[:minibatchsize]
    plot_results(lps, angles, Z_true, Z_inf)
def em_example(true_model, X, Z_true, mask):
    # Fit a test model
    model = FactorAnalysis(
        D_obs, D_latent,
        # W=true_model.W, sigmasq=true_model.sigmasq
        )
    inf_data = model.add_data(X, mask=mask)
    model.set_empirical_mean()

    lps = []
    angles = []
    N_iters = 100
    for _ in progprint_xrange(N_iters):
        model.EM_step()
        lps.append(model.log_likelihood())
        angles.append(principal_angle(true_model.W, model.W))

    plot_results(lps, angles, Z_true, inf_data.E_Z)
def gibbs_example(true_model, true_data):
    X, mask = true_data.X, true_data.mask

    # Fit a test model
    model = FactorAnalysis(
        D_obs, D_latent,
        # W=true_model.W, sigmasq=true_model.sigmasq
        )
    inf_data = model.add_data(X, mask=mask)

    lps = []
    angles = []
    N_iters = 100
    for _ in progprint_xrange(N_iters):
        model.resample_model()
        lps.append(model.log_likelihood())
        angles.append(principal_angle(true_model.W, model.W))

    plot_results(lps, angles, true_data.Z, inf_data.Z)
def meanfield_example(true_model, X, Z_true, mask):
    # Fit a test model
    model = FactorAnalysis(
        D_obs, D_latent,
        # W=true_model.W, sigmasq=true_model.sigmasq
        )
    inf_data = model.add_data(X, mask=mask)
    model.set_empirical_mean()

    lps = []
    angles = []
    N_iters = 100
    for _ in progprint_xrange(N_iters):
        model.meanfield_coordinate_descent_step()
        lps.append(model.expected_log_likelihood())
        E_W, _, _, _ = model.regression.mf_expectations
        angles.append(principal_angle(true_model.W, E_W))

    plot_results(lps, angles, Z_true, inf_data.Z)
def em_example(true_model, true_data):
    X, mask = true_data.X, true_data.mask

    # Fit a test model
    model = FactorAnalysis(
        D_obs, D_latent,
        # W=true_model.W, sigmasq=true_model.sigmasq
        )
    inf_data = model.add_data(X, mask=mask)

    lps = []
    angles = []
    N_iters = 100
    for _ in progprint_xrange(N_iters):
        model.EM_step()
        lps.append(model.log_likelihood())
        angles.append(principal_angle(true_model.W, model.W))

    plot_results(lps, angles, true_data.Z, inf_data.E_Z)
def gibbs_example(true_model, X, Z_true, mask):
    # Fit a test model
    model = FactorAnalysis(
        D_obs,
        D_latent,
        # W=true_model.W, sigmasq=true_model.sigmasq
    )
    inf_data = model.add_data(X, mask=mask)
    model.set_empirical_mean()

    lps = []
    angles = []
    N_iters = 100
    for _ in progprint_xrange(N_iters):
        model.resample_model()
        lps.append(model.log_likelihood())
        angles.append(principal_angle(true_model.W, model.W))

    plot_results(lps, angles, Z_true, inf_data.Z)
def meanfield_example(true_model, true_data):
    X, mask = true_data.X, true_data.mask

    # Fit a test model
    model = FactorAnalysis(
        D_obs, D_latent,
        # W=true_model.W, sigmasq=true_model.sigmasq
        )
    inf_data = model.add_data(X, mask=mask)

    lps = []
    angles = []
    N_iters = 100
    for _ in progprint_xrange(N_iters):
        model.meanfield_coordinate_descent_step()
        lps.append(model.expected_log_likelihood())
        E_W, _, _, _ = model.regression.mf_expectations
        angles.append(principal_angle(true_model.W, E_W))

    plot_results(lps, angles, true_data.Z, inf_data.Z)