Beispiel #1
0
def run_benchmark(alpha, algo, seed):
    # create scenario
    N, rho, noise_var = 1000, 0.05, 1e-2
    M = int(alpha * N)
    A = GaussianEnsemble(M=M, N=N).generate()
    t0 = time()
    model = (GaussBernoulliPrior(size=N, rho=rho) @ V("x") @ LinearChannel(A)
             @ V("z") @ GaussianChannel(var=noise_var) @ O("y")).to_model()
    t1 = time()
    record = {"svd_time": t1 - t0}  # svd precomputation time
    scenario = BayesOptimalScenario(model, x_ids=["x"])
    scenario.setup(seed)
    y = scenario.observations["y"]
    # run algo
    t0 = time()
    if algo == "SE":
        x_data = scenario.run_se(max_iter=1000, damping=0.1)
        record["mse"] = x_data["x"]["v"]
        record["n_iter"] = x_data["n_iter"]
    if algo == "EP":
        x_data = scenario.run_ep(max_iter=1000, damping=0.1)
        x_pred = x_data["x"]["r"]
        record["n_iter"] = x_data["n_iter"]
    if algo == "LassoCV":
        lasso = LassoCV(cv=5)
        lasso.fit(A, y)
        x_pred = lasso.coef_
        record["param_scikit"] = lasso.alpha_
        record["n_iter"] = lasso.n_iter_
    if algo == "Lasso":
        optim = pd.read_csv("optimal_param_lasso.csv")
        param_scaled = np.interp(alpha, optim["alpha"], optim["param_scaled"])
        param_scikit = noise_var * param_scaled / (M * rho)
        lasso = Lasso(alpha=param_scikit)
        lasso.fit(A, y)
        x_pred = lasso.coef_
        record["param_scikit"] = param_scikit
        record["n_iter"] = lasso.n_iter_
    if algo == "pymc3":
        with pm.Model():
            ber = pm.Bernoulli("ber", p=rho, shape=N)
            nor = pm.Normal("nor", mu=0, sd=1, shape=N)
            x = pm.Deterministic("x", ber * nor)
            likelihood = pm.Normal("y",
                                   mu=pm.math.dot(A, x),
                                   sigma=np.sqrt(noise_var),
                                   observed=y)
            trace = pm.sample(draws=1000, chains=1, return_inferencedata=False)
        x_pred = trace.get_values('x').mean(axis=0)
    t1 = time()
    record["time"] = t1 - t0
    if algo != "SE":
        record["mse"] = mean_squared_error(x_pred, scenario.x_true["x"])
    return record
def run_EP(alpha, rho, seed):
    model = glm_generative(N=2000,
                           alpha=alpha,
                           ensemble_type="gaussian",
                           prior_type="gauss_bernoulli",
                           output_type="abs",
                           prior_rho=rho,
                           prior_mean=0)
    scenario = BayesOptimalScenario(model, x_ids=["x"])
    scenario.setup(seed)
    x_data = scenario.run_ep(max_iter=200, damping=0.2)
    x_pred = x_data["x"]["r"]
    mse = sign_symmetric_mse(x_pred, scenario.x_true["x"])
    return dict(source="EP", v=mse)
Beispiel #3
0
def run_EP(alpha, rho, seed):
    model = glm_generative(N=2000,
                           alpha=alpha,
                           ensemble_type="gaussian",
                           prior_type="gauss_bernoulli",
                           output_type="gaussian",
                           prior_rho=rho,
                           output_var=1e-10)
    scenario = BayesOptimalScenario(model, x_ids=["x"])
    scenario.setup(seed)
    x_data = scenario.run_ep(max_iter=200)
    x_pred = x_data["x"]["r"]
    mse = mean_squared_error(x_pred, scenario.x_true["x"])
    return dict(source="EP", v=mse)