Ejemplo n.º 1
0
    def test_set_resps(self):
        data = np.array([[1, 3], [1, 6]])
        mean1 = np.array([1, 5])
        C_det1 = 0.75
        C_inv1 = np.array([[4, -2], [-2, 4]]) / 3
        mean2 = np.array([2, 2])
        C_det2 = 1
        C_inv2 = np.array([[1, 0], [0, 1]])
        mixture = PCAMixture(data, 2, 2)
        model1 = PCAModel(mixture, 2)
        model2 = PCAModel(mixture, 2)
        mixture.models = [model1, model2]
        model1.mean = mean1
        model1.C_det = C_det1
        model1.C_inv = C_inv1
        model2.mean = mean2
        model2.C_det = C_det2
        model2.C_inv = C_inv2

        mixture.set_resps()

        resps1 = mixture.resps[0]
        resps2 = mixture.resps[1]

        self.assertAlmostEqual(resps1[0], 0.1790458078)
        self.assertAlmostEqual(resps1[1], 0.99965691)

        self.assertAlmostEqual(resps2[0], 0.8209541918)
        self.assertAlmostEqual(resps2[1], 3.43090081e-04)
Ejemplo n.º 2
0
    def test_set_S(self):
        data = np.array([[1, 3], [1, 6]])
        mixture = PCAMixture(data, 1, 2)
        model = PCAModel(mixture, 2)
        model_resps = np.array([1, 2])
        mixture.resps = [model_resps]
        model.set_mean(model_resps)
        mixture.mixing_coeffs = [3 / 2]

        mixing_coeff = mixture.mixing_coeffs[0]
        model_resps = mixture.resps[0]
        model.set_S(mixing_coeff, model_resps)

        self.assertTrue(np.array_equal(model.S, np.array([[0, 0], [0, 2]])))
Ejemplo n.º 3
0
    def test_calc_mixing_coeff(self):
        data = np.random.multivariate_normal([0, 0], [[2, 1], [1, 1]], 3)
        mixture = PCAMixture(data, 1, 2)
        model = PCAModel(mixture, 2)

        resps = np.array([1, 2, 3])
        self.assertEqual(model.calc_mixing_coeff(resps), 2)
Ejemplo n.º 4
0
    def test_set_mean(self):
        data = np.array([[1, 3], [1, 6]])
        mixture = PCAMixture(data, 1, 2)
        model = PCAModel(mixture, 2)

        model_resps = np.array([1, 2])
        model.set_mean(model_resps)
        self.assertTrue(np.array_equal(model.mean, np.array([1, 5])))
Ejemplo n.º 5
0
    def test_set_mixing_coefficients(self):
        data = np.array([[1, 3], [1, 6]])
        mean1 = np.array([1, 5])
        C_det1 = 0.75
        C_inv1 = np.array([[4, -2], [-2, 4]]) / 3
        mean2 = np.array([2, 2])
        C_det2 = 1
        C_inv2 = np.array([[1, 0], [0, 1]])
        mixture = PCAMixture(data, 2, 2)
        model1 = PCAModel(mixture, 2)
        model2 = PCAModel(mixture, 2)
        mixture.models = [model1, model2]
        model1.mean = mean1
        model1.C_det = C_det1
        model1.C_inv = C_inv1
        model2.mean = mean2
        model2.C_det = C_det2
        model2.C_inv = C_inv2

        mixture.set_resps()

        mixture.set_mixing_coefficients()

        mixing_coeffs = mixture.mixing_coeffs
        self.assertAlmostEqual(mixing_coeffs[0], 0.5893513589)
        self.assertAlmostEqual(mixing_coeffs[1], 0.41064864094)
Ejemplo n.º 6
0
 def test_update_M_inv(self):
     data = np.array([[1, 3], [1, 6]])
     mixture = PCAMixture(data, 1, 2)
     model = PCAModel(mixture, 2)
     model.var = 3
     model.W = np.array([[1, 2], [2, 1], [1, 3]])
     model.update_M_inv()
     M_inv = np.array([[17, -7], [-7, 9]]) / 104
     res = np.isclose(model.M_inv, M_inv)
     self.assertTrue(res.all())
Ejemplo n.º 7
0
    def test_set_C_det(self):
        data = np.array([[1, 3], [1, 6]])
        var = 2
        W = np.array([[2, 3], [1, 0], [2, 1]])
        mixture = PCAMixture(data, 1, 2)
        model = PCAModel(mixture, 2)
        model.var = var
        model.W = W
        model.set_C_det()
        C_det = 136

        self.assertAlmostEqual(model.C_det, C_det)
Ejemplo n.º 8
0
 def test_calc_prob_data(self):
     data = np.array([[1, 3], [1, 6]])
     mean = np.array([1, 5])
     C_det = 0.75
     C_inv = np.array([[4, -2], [-2, 4]]) / 3
     mixture = PCAMixture(data, 1, 2)
     model = PCAModel(mixture, 2)
     model.mean = mean
     model.C_det = C_det
     model.C_inv = C_inv
     prob = model.calc_prob_data(data[0])
     correct_prob = 0.0127694115
     self.assertAlmostEqual(prob, correct_prob)
Ejemplo n.º 9
0
    def test_set_var(self):
        S = np.array([[0, 0], [0, 2]])

        data = np.array([[1, 3], [1, 6]])
        mixture = PCAMixture(data, 1, 2)
        model = PCAModel(mixture, 2)
        model.S = S
        W_old = np.array([[1, 1], [3, 2]])
        M_inv_old = np.array([[17, -7], [-7, 9]]) / 104
        model.W = np.array([[0, 0], [104 / 87, 208 / 261]])

        model.set_var(W_old, M_inv_old)

        self.assertAlmostEqual(model.var, 52 / 87)
Ejemplo n.º 10
0
    def test_set_C_inv(self):
        data = np.array([[1, 3], [1, 6]])
        M_inv = np.array([[17, -7], [-7, 9]]) / 104
        W = np.array([[0, 0], [104 / 87, 208 / 261]])
        var = 1 + 2 / 87
        mixture = PCAMixture(data, 1, 2)
        model = PCAModel(mixture, 2)
        model.M_inv = M_inv
        model.W = W
        model.var = var

        model.set_C_inv()

        C_inv = np.array([[1, 0], [0, 19067 / 22707]]) / var

        res = np.isclose(model.C_inv, C_inv)
        self.assertTrue(res.all())
Ejemplo n.º 11
0
    def test_set_W(self):
        S = np.array([[0, 0], [0, 2]])
        M_inv = np.array([[17, -7], [-7, 9]]) / 104
        W = np.array([[1, 1], [3, 2]])

        data = np.array([[1, 3], [1, 6]])
        mixture = PCAMixture(data, 1, 2)
        model = PCAModel(mixture, 2)
        model.var = 3
        model.S = S
        model.M_inv = M_inv
        model.W = W

        model.set_W()
        new_W = model.W

        #print(new_W)

        res = np.isclose(new_W, np.array([[0, 0], [104 / 87, 208 / 261]]))
        self.assertTrue(res.all())
Ejemplo n.º 12
0
    def test_calc_prob_data(self):
        data = np.array([[1, 3], [1, 6]])
        mean = np.array([1, 5])
        C_det = 0.75
        C_inv = np.array([[4, -2], [-2, 4]]) / 3
        mixture = PCAMixture(data, 1, 2)
        model = PCAModel(mixture, 2)
        mixture.models = [model]
        model.mean = mean
        model.C_det = C_det
        model.C_inv = C_inv

        prob = mixture.calc_prob_data(data[0])
        real_prob = 0.0127694115
        self.assertAlmostEqual(prob, real_prob)

        data = np.array([[1, 3], [1, 6]])
        mean1 = np.array([1, 5])
        C_det1 = 0.75
        C_inv1 = np.array([[4, -2], [-2, 4]]) / 3
        mean2 = np.array([2, 2])
        C_det2 = 1
        C_inv2 = np.array([[1, 0], [0, 1]])
        mixture = PCAMixture(data, 2, 2)
        model1 = PCAModel(mixture, 2)
        model2 = PCAModel(mixture, 2)
        mixture.models = [model1, model2]
        model1.mean = mean1
        model1.C_det = C_det1
        model1.C_inv = C_inv1
        model2.mean = mean2
        model2.C_det = C_det2
        model2.C_inv = C_inv2

        prob = mixture.calc_prob_data(data[0])
        real_prob = 0.0356596215
        self.assertAlmostEqual(real_prob, prob)
Ejemplo n.º 13
0
import numpy as np
from pca_mixtures.funcs import PCAMixture
import matplotlib.pyplot as plt

data = np.loadtxt("dataset/tobomovirus.txt")
#print(data)
mixture = PCAMixture(data, 1, 2)
mixture.fit()


class Plotter(object):
    def __init__(self, mixture):
        self.mixture = mixture

    def plot_all_data(self, skip_improbable):
        for model in self.mixture.models:
            self._plot_data(data, model, skip_improbable)

    def _plot_data(self, data, model, skip_improbable):
        latent_positions = model.posterior(data)
        plt.figure()
        X = latent_positions[:, 0]
        Y = latent_positions[:, 1]
        plt.scatter(X, Y)
        plt.figure()
        nums = [str(i) for i in range(len(data))]
        for x, y, txt in zip(X, Y, nums):
            plt.text(x, y, txt)


plotter = Plotter(mixture)
Ejemplo n.º 14
0
 def test_calc_resp(self):
     data = np.random.multivariate_normal([0, 0], [[2, 1], [1, 1]], 2)
     mixture = PCAMixture(data, 1, 2)
     model = PCAModel(mixture, 2)
     self.assertEqual(model.calc_resp(0.5, 2, 0.5), 0.125)
Ejemplo n.º 15
0
    def test_get_model_resps(self):
        # Test 1
        # ----------------------------------
        data = np.array([[1, 3], [1, 6]])
        mixture = PCAMixture(data, 1, 2)
        model = PCAModel(mixture, 2)
        mean = np.array([1, 5])
        C_det = 0.75
        C_inv = np.array([[4, -2], [-2, 4]]) / 3
        mixture = PCAMixture(data, 1, 2)
        model = PCAModel(mixture, 2)
        mixture.models = [model]
        model.mean = mean
        model.C_det = C_det
        model.C_inv = C_inv

        resps = mixture.get_model_resps(model)
        res = np.array_equal(resps, np.array([1, 1]))

        self.assertTrue(res)

        # Test 2
        # ---------------------------------
        data = np.array([[1, 3], [1, 6]])
        mean1 = np.array([1, 5])
        C_det1 = 0.75
        C_inv1 = np.array([[4, -2], [-2, 4]]) / 3
        mean2 = np.array([2, 2])
        C_det2 = 1
        C_inv2 = np.array([[1, 0], [0, 1]])
        mixture = PCAMixture(data, 2, 2)
        model1 = PCAModel(mixture, 2)
        model2 = PCAModel(mixture, 2)
        mixture.models = [model1, model2]
        model1.mean = mean1
        model1.C_det = C_det1
        model1.C_inv = C_inv1
        model2.mean = mean2
        model2.C_det = C_det2
        model2.C_inv = C_inv2

        resps1 = mixture.get_model_resps(model1)
        self.assertAlmostEqual(resps1[0], 0.1790458078)
        self.assertAlmostEqual(resps1[1], 0.99965691)

        resps2 = mixture.get_model_resps(model2)
        self.assertAlmostEqual(resps2[0], 0.8209541918)
        self.assertAlmostEqual(resps2[1], 3.43090081e-04)