Exemplo n.º 1
0
def scm_to_linear_scm(scm, ds, n_samples=100):
    linear_scm_model = {}

    for node, model in scm.assignment.items():
        print("Node: {}  Items: {}".format(node, model))
        parents = model.parents
        if parents:
            intercept, coefs = get_coefficients(ds, node, parents)
            linear_scm_model[node] = linear_model(parents,
                                                  coefs,
                                                  offset=intercept,
                                                  noise_scale=.1)
            # print("mu: {}, std: {}".format(mu, std))
            print("PARENTS: {}".format(parents))
            print("COEFFS: {}".format(coefs))

        else:
            mu = np.mean(ds[node])
            std = np.std(ds[node])

            structural_eq = lambda n_samples: np.random.normal(
                loc=mu, scale=std, size=n_samples)
            linear_scm_model[node] = structural_eq

    return StructuralCausalModel(linear_scm_model)
Exemplo n.º 2
0
front_door_example = CausalGraphicalModel(nodes=["x", "y", "z"],
                                          edges=[("x", "z"), ("z", "y")],
                                          latent_edges=[("x", "y")])

chain_csm = StructuralCausalModel({
    "a": lambda n_samples: np.ones(n_samples),
    "b": lambda a, n_samples: a + 2,
    "c": lambda b, n_samples: b * 2
})

big_csm = StructuralCausalModel({
    "a":
    lambda n_samples: np.random.normal(size=n_samples),
    "b":
    lambda n_samples: np.random.normal(size=n_samples),
    "x":
    logistic_model(["a", "b"], [-1, 1]),
    "c":
    linear_model(["x"], [1]),
    "y":
    linear_model(["c", "e"], [3, -1]),
    "d":
    linear_model(["b"], [-1]),
    "e":
    linear_model(["d"], [2]),
    "f":
    linear_model(["y"], [0.7]),
    "h":
    linear_model(["y", "a"], [1.3, 2.1])
})
Exemplo n.º 3
0
        plt.pause(1.)
        plt.clf()
    plt.show()


scm = StructuralCausalModel({
    "goal_x":
    lambda n_samples: np.random.normal(loc=2., scale=2.0, size=n_samples),
    "goal_y":
    lambda n_samples: np.random.normal(loc=2., scale=2.0, size=n_samples),
    "init_x":
    lambda n_samples: np.random.normal(loc=8., scale=2.0, size=n_samples),
    "init_y":
    lambda n_samples: np.random.normal(loc=8., scale=2.0, size=n_samples),
    "dist_x":
    linear_model(["goal_x", "init_x"], [1, -1], noise_scale=.1),
    "dist_y":
    linear_model(["goal_y", "init_y"], [1, -1], noise_scale=.1),
    "final_x":
    linear_model(["init_x", "dist_x"], [1, .2], noise_scale=.1),
    "final_y":
    linear_model(["init_y", "dist_y"], [1, .2], noise_scale=.1),
})

ds = scm.sample(n_samples=1000)

# ds.to_csv(DATA_FILE)
# print(list(scm.assignment.items()))
# print(ds["init_x"].values)
# print(get_coefficients(ds,"dist_x", ["goal_x", "goal_y", "init_x", "init_y"]))
Exemplo n.º 4
0
def online_learning_example(it):

    # Initialize weights to random value
    weight_gt = [2, -3]
    weight_final = np.random.randint(-10, 10, size=2)
    # weight_final = np.array([0, 2])

    ground_truth_scm = StructuralCausalModel({
        "init_x":
        lambda n_samples: np.random.normal(loc=8., scale=2.0, size=n_samples),
        "init_y":
        lambda n_samples: np.random.normal(loc=8., scale=2.0, size=n_samples),
        "push_x":
        lambda n_samples: np.random.normal(loc=0., scale=1.0, size=n_samples),
        "push_y":
        lambda n_samples: np.random.normal(loc=0., scale=1.0, size=n_samples),
        "final_x":
        linear_model(["init_x", "push_x"], weight_gt, noise_scale=.1),
        "final_y":
        linear_model(["init_y", "push_y"], weight_gt, noise_scale=.1),
    })

    df_gt = ground_truth_scm.sample(n_samples=it)
    sgd = SGD(.001, 1, init_weights=weight_final)
    for i in range(it):
        gt_init_x = [df_gt.init_x[i]]
        gt_init_y = [df_gt.init_y[i]]
        gt_push_x = [df_gt.push_x[i]]
        gt_push_y = [df_gt.push_y[i]]
        gt_final_x = [df_gt.final_x[i]]
        gt_final_y = [df_gt.final_y[i]]

        intervention_vars = {
            "init_x": gt_init_x,
            "init_y": gt_init_y,
            "push_x": gt_push_x,
            "push_y": gt_push_y
        }
        pred_scm = StructuralCausalModel({
            "init_x":
            lambda n_samples: np.random.normal(
                loc=8., scale=2.0, size=n_samples),
            "init_y":
            lambda n_samples: np.random.normal(
                loc=8., scale=2.0, size=n_samples),
            "push_x":
            lambda n_samples: np.random.normal(
                loc=0., scale=3.0, size=n_samples),
            "push_y":
            lambda n_samples: np.random.normal(
                loc=0., scale=3.0, size=n_samples),
            "final_x":
            linear_model(["init_x", "push_x"], weight_final, noise_scale=.1),
            "final_y":
            linear_model(["init_y", "push_y"], weight_final, noise_scale=.1),
        })

        pred_scm_do = do_multiple(list(intervention_vars), pred_scm)
        df_pred = pred_scm_do.sample(n_samples=1, set_values=intervention_vars)

        pred_final_x = df_pred.final_x
        pred_final_y = df_pred.final_y

        plt.plot(df_gt.init_x[i], df_gt.init_y[i], 'bo')
        text = "True_weights: {}\n Predicted weights {}".format(
            weight_gt, weight_final)
        plt.text(df_gt.init_x[i] * (1 + 0.01),
                 df_gt.init_y[i] * (1 + 0.01),
                 text,
                 fontsize=12)
        # plt.plot(df_gt.final_x, df_gt.final_y, 'go')
        plt.quiver(gt_init_x, gt_init_y, gt_final_x, gt_final_y, color="b")
        plt.quiver(gt_init_x, gt_init_y, pred_final_x, pred_final_y, color="r")

        weight_final, rmse_x = sgd.fit(gt_final_x, pred_final_x,
                                       [gt_init_x, gt_push_x])
        # weight_final_y, rmse_y = sgd.fit(gt_final_y, gt_final_y)

        plt.pause(1.)
        plt.clf()
    plt.show()
Exemplo n.º 5
0
simple_confounded_potential_outcomes = CausalGraphicalModel(
    ["x", "y_0", "y_1", "y", "z"], [("z", "x"), ("z", "y_0"), ("z", "y_1"),
                                    ("y_0", "y"), ("y_1", "y"), ("x", "y")])

chain_csm = StructuralCausalModel({
    "a": lambda n_samples: np.ones(n_samples),
    "b": lambda a, n_samples: a + 2,
    "c": lambda b, n_samples: b * 2
})

big_csm = StructuralCausalModel({
    "a":
    lambda n_samples: np.random.normal(size=n_samples),
    "b":
    lambda n_samples: np.random.normal(size=n_samples),
    "x":
    logistic_model(("a", "b"), [-1, 1]),
    "c":
    linear_model(("x", ), [1]),
    "y":
    linear_model(("c", "e"), [3, -1]),
    "d":
    linear_model(("b", ), [-1]),
    "e":
    linear_model(("d", ), [2]),
    "f":
    linear_model(("y"), [0.7]),
    "h":
    linear_model(("y", "a"), [1.3, 2.1])
})