Esempio n. 1
0
 def first(self, e):
     self.mb = set(population_icp.markov_blanket(self.target, e))
     self.candidates = self.mb.copy()
     if self.mb == set():
         var = np.random.choice(utils.all_but(self.target, self.p))
     else:
         var = np.random.choice(list(self.mb))
         self.interventions.append(var)
     return var
Esempio n. 2
0
def markov_blanket(i, dist, tol=1e-10):
    """Return the Markov blanket of variable i wrt. the given
    distribution. HOW IT WORKS: In the population setting, the
    regression coefficients of all variables outside the Markov
    Blanket are 0. Taking into account numerical issues (that's what
    tol is for), use this to compute the Markov blanket.
    """
    (coefs, _) = dist.regress(i, utils.all_but(i, dist.p))
    return utils.nonzero(coefs, tol)
Esempio n. 3
0
 def first(self, e):
     mb = population_icp.markov_blanket(self.target, e)
     if len(
             mb
     ) == 0:  # If the estimate of the MB is empty resort to the random strategy
         self.mb = np.random.permutation(utils.all_but(self.target, self.p))
     else:
         self.mb = np.random.permutation(mb)
         self.i = 0
     return self.pick_intervention()
Esempio n. 4
0
def markov_blanket(sample, target, tol=1e-3, debug=False):
    """Use the Lasso estimator to return an estimate of the Markov Blanket"""
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        p = sample.shape[1]
        predictors = utils.all_but(target, p)
        X = sample[:, predictors]
        Y = sample[:, target]
        coefs = np.zeros(p)
        coefs[predictors] = linear_model.LassoCV(cv=10,
                                                 normalize=True,
                                                 max_iter=1000,
                                                 verbose=debug).fit(X, Y).coef_
        return utils.nonzero(coefs, tol)
Esempio n. 5
0
def intervention_targets(target, response, p, max_off_targets):
    """Return intervention targets. If no off target effects are allowed
    (i.e. max_off_targets = 0), the intervention is at the given
    target. Otherwise, a number (at random, up to max_off_targets) of
    additional targets are selected at random, excluding the response
    (i.e. Y)."""
    if max_off_targets == 0:
        return [target]
    else:
        max_off_targets = min(max_off_targets, p-2)
        num_off_targets = np.random.choice(max_off_targets+1)
        off_targets = list(np.random.choice(utils.all_but([target, response], p),
                                            size=num_off_targets,
                                            replace=False))
        return [target] + off_targets
Esempio n. 6
0
 def first(self, _):
     self.idx = np.random.permutation(utils.all_but(self.target, self.p))
     self.i = 0
     return self.random_intervention()
Esempio n. 7
0
 def first(self, sample):
     self.obs_sample = sample
     self.candidates = set(utils.all_but(self.target, self.p))
     var = self.pick_intervention()
     self.interventions.append(var)
     return var
Esempio n. 8
0
 def pick_intervention(self):
     return np.random.choice(utils.all_but(self.target, self.p))
Esempio n. 9
0
 def test_all_but(self):
     self.assertTrue([0, 1, 3, 4] == all_but(2,5))
     self.assertTrue([0, 3, 4] == all_but([1,2],5))