Esempio n. 1
0
 def __init__(self, child: BernoulliArray, parent: GaussianArray):
     super().__init__()
     self._deterministic = True
     self.shape = child.shape()
     self.child = child
     self.parent = parent
     self.message_to_child = BernoulliArray.uniform(self.shape)
     self.message_to_parent = GaussianArray.uniform(self.shape)
Esempio n. 2
0
 def to_child(self):
     x = self.parent / self.message_to_parent
     # x = self.parent
     m, v = x.mean_and_variance()
     integral = sigmoid_integrals(m, v, [0])[0]
     proba = tf.where(self.child.proba() == 1., integral, 1. - integral)
     self.message_to_child = BernoulliArray.from_array(proba)
Esempio n. 3
0
 def to_result(self, x):
     # compute incoming message
     from_x = x / self.message_to_x
     # add noise
     from_x = GaussianArray.from_array(
         mean=from_x.mean(),
         variance=from_x.log_var() + self.variance.value()
     )
     # compute probability
     proba = 1. - tfp.distributions.Normal(*from_x.mean_and_variance()).cdf(0.0)
     return BernoulliArray.from_array(proba)
Esempio n. 4
0
 def to_child(self):
     # only used for prediction, does not affect VMP
     x = self.parent / self.message_to_parent
     proba = 1. - tfp.distributions.Normal(*x.mean_and_stddev()).cdf(0.0)
     self.message_to_child = BernoulliArray.from_array(proba)
Esempio n. 5
0
 def __init__(self, shape, variance):
     super().__init__()
     self._deterministic = False
     self.message_to_x = GaussianArray.uniform(shape)
     self.message_to_result = BernoulliArray.uniform(shape)
     self.variance = variance
Esempio n. 6
0
 def to_result(self, x):
     x = x / self.message_to_x
     # TODO: should that be sqrt?
     proba = 1. - tfp.distributions.Normal(*x.mean_and_variance()).cdf(0.0)
     return BernoulliArray.from_array(proba)
Esempio n. 7
0
 def __init__(self, shape):
     super().__init__()
     self.message_to_x = GaussianArray.uniform(shape)
     self.message_to_result = BernoulliArray.uniform(shape)
Esempio n. 8
0
    def __init__(self,
                 K,
                 A,
                 X_cts=None,
                 X_bin=None,
                 link_model="NoisyProbit",
                 bin_model="NoisyProbit",
                 initial={
                     "bias": None,
                     "weights": None,
                     "positions": None,
                     "heterogeneity": None
                 }):
        super().__init__()
        self._deterministic = False
        self._check_input(K, A, X_cts, X_bin)

        # initial values
        if initial["bias"] is None:
            initial["bias"] = tf.zeros((1, self.p))
        if initial["weights"] is None:
            initial["weights"] = tf.ones((self.K, self.p))
        if initial["positions"] is None:
            initial["positions"] = tf.random.normal((self.N, self.K), 0., 1.)
        if initial["heterogeneity"] is None:
            initial["heterogeneity"] = tf.random.normal((self.N, 1), 0., 1.)

        # prepare nodes
        self.positions = GaussianArray.uniform((self.N, self.K))
        self.heterogeneity = GaussianArray.uniform((self.N, 1))
        self.covariate_mean = GaussianArray.uniform((self.N, self.p))
        self.adjacency_mean = GaussianArray.uniform((self.N, self.N))
        self.covariate_continuous = GaussianArray.observed(X_cts)
        self.covariate_binary = BernoulliArray.observed(X_bin)
        self.links = BernoulliArray.observed(A)
        self._nodes = {
            "positions": self.positions,
            "heterogeneity": self.heterogeneity,
            "covariate_mean": self.covariate_mean,
            "adjacency_mean": self.adjacency_mean,
            "covariate_continuous": self.covariate_continuous,
            "covariate_binary": self.covariate_binary,
            "links": self.links
        }

        # prepare factors
        self.position_prior = Prior(child=self.positions,
                                    mean=0.,
                                    variance=1.,
                                    initial=initial["positions"],
                                    name="position_prior")
        self.heterogeneity_prior = Prior(child=self.heterogeneity,
                                         mean=-2.,
                                         variance=1.,
                                         initial=initial["heterogeneity"],
                                         name="heterogeneity_prior")
        self.inner_product_model = InnerProductModel(
            positions=self.positions,
            heterogeneity=self.heterogeneity,
            linear_predictor=self.adjacency_mean)
        if self.p > 0:
            self.mean_model = WeightedSum(parent=self.positions,
                                          child=self.covariate_mean,
                                          bias=initial["bias"],
                                          weight=initial["weights"])
            self.covariate_model = GLM(parent=self.covariate_mean,
                                       child_cts=self.covariate_continuous,
                                       child_bin=self.covariate_binary,
                                       variance_cts=tf.ones((1, self.p_cts)),
                                       variance_bin=tf.ones((1, self.p_bin)),
                                       bin_model=bin_model)
        else:
            self.mean_model = VMPFactor()
            self.covariate_model = VMPFactor()
        if link_model == "Logistic":
            self.adjacency_model = Logistic(parent=self.adjacency_mean,
                                            child=self.links)
        else:
            self.adjacency_model = NoisyProbit(parent=self.adjacency_mean,
                                               child=self.links,
                                               variance=1.)
        self._factors = {
            "position_prior": self.position_prior,
            "heterogeneity_prior": self.heterogeneity_prior,
            "mean_model": self.mean_model,
            "covariate_model": self.covariate_model,
            "inner_product_model": self.inner_product_model,
            "adjacency_model": self.adjacency_model
        }

        self.elbo = -np.inf