Exemplo n.º 1
0
 def test_log_density_cens_norm_at_bounds(self):
     mu = 4
     sigma = 2
     a = 3
     b = 6
     data = np.array([3, 6])
     dist = CensoredNormalDistribution(mu=mu, sigma=sigma, lower=a, upper=b)
     actual = dist.log_density(data)
     assert all(i > 1 for i in actual)
Exemplo n.º 2
0
 def test_log_density_cens_norm(self):
     mu = 4
     sigma = 2
     a = 3
     b = 6
     data = np.array([2, 7])
     dist = CensoredNormalDistribution(mu=mu, sigma=sigma, lower=a, upper=b)
     actual = dist.log_density(data)
     expected = np.array([-9999, -9999])
     assert (actual == expected).all()
Exemplo n.º 3
0
 def test_compare_censored_to_normal(self):
     mu = 4
     sigma = 2
     a = 3
     b = 6
     data = np.array([3.5, 4, 5, 5.5])
     dist = CensoredNormalDistribution(mu=mu, sigma=sigma, lower=a, upper=b)
     norm = s.norm(loc=mu, scale=sigma)
     actual = dist.log_density(data)
     expected = norm.logpdf(data)
     assert (actual == expected).all()
Exemplo n.º 4
0
 def test_estimate_parameters_cens_norm_neg_mean(self):
     mu = -2
     sigma = 1
     a = -3
     b = -1
     data = np.random.normal(loc=mu, scale=sigma, size=1000)
     data = np.array(list(filter(lambda x: x > a and x < b, data)))
     dist = CensoredNormalDistribution(mu=mu, sigma=sigma, lower=a, upper=b)
     actual = dist.estimate_parameters(data, np.ones((data.shape)))
     new_mu = dist.mu
     new_sigma = dist.sigma
     assert isclose(mu, new_mu, abs_tol=0.5)
     assert new_sigma > 0
Exemplo n.º 5
0
 def test_estimate_parameters_cens_norm(self):
     mu = 4
     sigma = 2
     a = 3
     b = 6
     data = np.random.normal(loc=mu, scale=sigma, size=1000)
     data = np.where(data <= a, a, data)
     data = np.where(data >= b, b, data)
     dist = CensoredNormalDistribution(mu=mu, sigma=sigma, lower=a, upper=b)
     actual = dist.estimate_parameters(data, np.ones((data.shape)))
     new_mu = dist.mu
     new_sigma = dist.sigma
     assert isclose(mu, new_mu, abs_tol=0.5)
     assert new_sigma > 0
Exemplo n.º 6
0
    def test_censored_and_normal_p_values(self):
        data1 = np.random.normal(loc=0, scale=1, size=10000)
        data1 = np.where(data1 <= 0, 0, data1)
        data1 = np.where(data1 >= 1, 1, data1)

        data2 = np.random.normal(loc=3, scale=1, size=10000)

        dist1 = CensoredNormalDistribution(mu=0.5, sigma=1, lower=0, upper=1)
        dist2 = NormalDistribution(mu=3, sigma=1)

        data = np.concatenate((data1, data2))
        dist_list = [dist1, dist2]
        mixture = Data_Rep(data, dist_list)
        dist_weight = mixture.weights[0]

        observed = []
        for i in [-1, 1.5, 5]:
            observed.append(mixture.get_p_value(i))
        expected = [
            0 + s.norm.sf(-4), 1 - (dist_weight + s.norm.cdf(-1.5)),
            0 + s.norm.sf(2)
        ]  #manually get p value here

        for i in range(len(expected)):
            assert isclose(expected[i], observed[i], abs_tol=0.05)
Exemplo n.º 7
0
    def test_censored_p_values(self):
        data1 = np.random.normal(loc=0, scale=1, size=1000)
        data1 = np.where(data1 <= 0, 0, data1)
        data1 = np.where(data1 >= 1, 1, data1)

        data2 = np.random.normal(loc=3, scale=1, size=1000)
        data2 = np.where(data2 <= 2, 2, data2)
        data2 = np.where(data2 >= 4, 4, data2)

        dist1 = CensoredNormalDistribution(mu=0.5, sigma=1, lower=0, upper=1)
        dist2 = CensoredNormalDistribution(mu=1.5, sigma=1, lower=2, upper=4)

        # after dataRep, make sure that params are close to expected

        data = np.concatenate((data1, data2))
        dist_list = [dist1, dist2]
        mixture = Data_Rep(data, dist_list)

        observed = []
        for i in [-1, 1.5, 5]:
            observed.append(mixture.get_p_value(i))
        expected = [1.0, 0.5, 0.0]

        assert observed == expected