Ejemplo 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()
Ejemplo 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()
Ejemplo n.º 3
0
def test_compute_counts():
    """
    FUNCTION: compute_counts, in general.py.
    """
    """Create data required to test function"""
    data = read_samples("lawn_samples.txt")
    sz = np.array([2, 2, 2, 2])

    """Execute function"""
    count = general.compute_counts(data, sz)

    """Assert the function executed correctly"""
    assert_array_equal(count, np.array([[[[7, 0], [0, 1]], [[1, 14], [0, 1]]], [[[4, 0], [3, 14]], [[0, 1], [0, 4]]]]))
Ejemplo 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))
Ejemplo n.º 5
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_compute_counts():
    """
    FUNCTION: compute_counts, in general.py.
    """
    """Create data required to test function"""
    data = read_samples('lawn_samples.txt')
    sz = np.array([2, 2, 2, 2])
    """Execute function"""
    count = general.compute_counts(data, sz)
    """Assert the function executed correctly"""
    assert_array_equal(count, np.array([[[[7, 0], \
                                          [0, 1]],\
                                         [[1, 14], \
                                          [0, 1]]],
                                        [[[4, 0], \
                                          [3, 14]],\
                                         [[0, 1], \
                                          [0, 4]]]]))