Exemplo n.º 1
0
    def learn_params_mle(self, samples):
        """
        Maximum liklihood estimation (MLE) parameter learing for a MRF.

        Parameters
        ----------
        samples: List
            A list of fully observed samples for the spanning the total domain
            of this MRF. Where samples[i][n] is the i'th sample for node n.
        """
        samples = np.array(samples)
        """For every clique"""
        for clq in self.cliques:
            """Obtain the evidence that is within the domain of this clique"""
            local_samples = samples[:, clq.domain]
            
            """If there is evidence for this clique"""
            if len(local_samples.tolist()) != 0:
                """Compute the counts of the samples"""
                counts = general.compute_counts(local_samples, clq.pot.sizes)

                """Reshape the counts into a potentials lookup table"""
                clq.unobserved_pot.T =\
                        general.mk_stochastic(np.array(counts, dtype=float))
                clq.pot.T = clq.unobserved_pot.T.copy()
Exemplo n.º 2
0
    def learn_params_mle(self, samples):
        """
        Maximum liklihood estimation (MLE) parameter learing for a MRF.

        Parameters
        ----------
        samples: List
            A list of fully observed samples for the spanning the total domain
            of this MRF. Where samples[i][n] is the i'th sample for node n.
        """
        samples = np.array(samples)
        """For every clique"""
        for clq in self.cliques:
            """Obtain the evidence that is within the domain of this clique"""
            local_samples = samples[:, clq.domain]
            
            """If there is evidence for this clique"""
            if len(local_samples.tolist()) != 0:
                """Compute the counts of the samples"""
                counts = general.compute_counts(local_samples, clq.pot.sizes)

                """Reshape the counts into a potentials lookup table"""
                clq.unobserved_pot.T =\
                        general.mk_stochastic(np.array(counts, dtype=float))
                clq.pot.T = clq.unobserved_pot.T.copy()
Exemplo n.º 3
0
    def learn_params_mle(self, samples):
        """
        Maximum liklihood estimation (MLE) parameter learing for a tabular
        CPD.

        Parameters
        ----------
        samples: List
            A list of fully observed samples for the spanning the total domain
            of this CPD. Where samples[i][n] is the i'th sample for node n.
        """
        """Compute the counts of the samples"""
        counts = general.compute_counts(samples, self.fam_size)
        """Reshape the counts into a CPT"""
        self.CPT = general.mk_stochastic(np.array(counts, dtype=float))
Exemplo n.º 4
0
    def learn_params_mle(self, samples):
        """
        Maximum liklihood estimation (MLE) parameter learing for a tabular
        CPD.

        Parameters
        ----------
        samples: List
            A list of fully observed samples for the spanning the total domain
            of this CPD. Where samples[i][n] is the i'th sample for node n.
        """
        """Compute the counts of the samples"""
        counts = general.compute_counts(samples, self.fam_size)

        """Reshape the counts into a CPT"""
        self.CPT = general.mk_stochastic(np.array(counts, dtype=float))
def test_mk_stochastic():
    """
    FUNCTION: mk_stochastic, in general.py
    """
    """Define input values"""
    mat = np.random.random((5, 5, 5, 5, 5))
    """Execute function"""
    mat = general.mk_stochastic(mat)
    """Assert that the sum over the last dimension is 1"""
    ans = []
    for i in range(0, 5):
        for j in range(0, 5):
            for k in range(0, 5):
                for l in range(0, 5):
                    ans.append(np.sum(mat[i, j, k, l, :]))

    assert not sum(np.round(np.array(ans), 3) != 1)
Exemplo n.º 6
0
def test_mk_stochastic():
    """
    FUNCTION: mk_stochastic, in general.py
    """
    """Define input values"""
    mat = np.random.random((5, 5, 5, 5, 5))

    """Execute function"""
    mat = general.mk_stochastic(mat)

    """Assert that the sum over the last dimension is 1"""
    ans = []
    for i in range(0, 5):
        for j in range(0, 5):
            for k in range(0, 5):
                for l in range(0, 5):
                    ans.append(np.sum(mat[i, j, k, l, :]))

    assert not sum(np.round(np.array(ans), 3) != 1)
Exemplo n.º 7
0
 def maximize_params(self):
     """
     Maximize the parameters from the expected sufficent statistics.
     """
     ess = np.array(self.ess).reshape(self.CPT.shape)
     self.CPT = general.mk_stochastic(ess)
Exemplo n.º 8
0
 def maximize_params(self):
     """
     Maximize the parameters from the expected sufficent statistics.
     """
     ess = np.array(self.ess).reshape(self.unobserved_pot.T.shape)
     self.unobserved_pot.T = general.mk_stochastic(ess)