def test_percentiles_from_mixture(): xscale = Scale(-1, 4) mixture = LogisticMixture( components=[ Logistic(loc=1, s=0.1, scale=xscale), Logistic(loc=2, s=0.1, scale=xscale), ], probs=[0.5, 0.5], ) conditions = mixture.percentiles(percentiles=[0.1, 0.5, 0.9]) for condition in conditions: if condition.max == 0.5: assert condition.p == pytest.approx(1.5, rel=0.01) return conditions
def logistic_mixture_norm_test(): xscale = Scale(-50, 50) return LogisticMixture( components=[Logistic(-40, 1, xscale), Logistic(50, 10, xscale)], probs=[0.5, 0.5], )
def test_mixed_2(point_densities): conditions = ( PointDensityCondition(point_densities["xs"], point_densities["densities"]), IntervalCondition(p=0.4, max=1), IntervalCondition(p=0.45, max=1.2), IntervalCondition(p=0.48, max=1.3), IntervalCondition(p=0.5, max=2), IntervalCondition(p=0.7, max=2.2), IntervalCondition(p=0.9, max=2.3), ) dist = LogisticMixture.from_conditions(conditions, {"num_components": 3}, verbose=True, scale=Scale(0, 1)) assert dist.pdf(-5) == pytest.approx(0, abs=0.1) assert dist.pdf(6) == pytest.approx(0, abs=0.1) my_cache = {} my_cache[conditions] = 3 conditions_2 = ( PointDensityCondition(point_densities["xs"], point_densities["densities"]), IntervalCondition(p=0.4, max=1), IntervalCondition(p=0.45, max=1.2), IntervalCondition(p=0.48, max=1.3), IntervalCondition(p=0.5, max=2), IntervalCondition(p=0.7, max=2.2), IntervalCondition(p=0.9, max=2.3), ) assert hash(conditions) == hash(conditions_2) assert my_cache[conditions_2] == 3
def normalized_logistic_mixture(): return LogisticMixture( components=[ Logistic(loc=0.15, s=0.037034005, scale=Scale(0, 1)), Logistic(loc=0.85, s=0.032395907, scale=Scale(0, 1)), ], probs=[0.6, 0.4], )
def logistic_mixture(): return LogisticMixture( components=[ Logistic(loc=10000, scale=1000), Logistic(loc=100000, scale=10000) ], probs=[0.8, 0.2], )
def logistic_mixture15(): xscale = Scale(-10, 40) return LogisticMixture( components=[ Logistic(loc=10, s=3.658268, scale=xscale), Logistic(loc=20, s=3.658268, scale=xscale), ], probs=[0.5, 0.5], )
def smooth_logistic_mixture(): xscale = Scale(1, 1000000.0) return LogisticMixture( components=[ Logistic(loc=400000, s=100000, scale=xscale), Logistic(loc=700000, s=50000, scale=xscale), ], probs=[0.8, 0.2], )
def logistic_mixture10(): xscale = Scale(-20, 40) return LogisticMixture( components=[ Logistic(loc=15, s=2.3658268, scale=xscale), Logistic(loc=5, s=2.3658268, scale=xscale), ], probs=[0.5, 0.5], )
def logistic_mixture_p_uneven(): xscale = Scale(-10, 20) return LogisticMixture( components=[ Logistic(loc=10, s=3, scale=xscale), Logistic(loc=5, s=5, scale=xscale), ], probs=[1.8629593e-29, 1.0], )
def test_mixture_from_percentile(): for value in [0.01, 0.1, 1, 3]: conditions = [IntervalCondition(p=0.5, max=value)] dist = LogisticMixture.from_conditions(conditions, {"num_components": 1}, verbose=True, scale=Scale(0, 3)) loc = dist.components[0].base_dist.true_loc assert loc == pytest.approx(value, rel=0.1), loc
def logistic_mixture(): xscale = Scale(0, 150000) return LogisticMixture( components=[ Logistic(loc=10000, s=1000, scale=xscale), Logistic(loc=100000, s=10000, scale=xscale), ], probs=[0.8, 0.2], )
def logistic_mixture_p_overlapping(): xscale = three_sd_scale(4000000.035555004, 200000.02) return LogisticMixture( components=[ Logistic(4000000.035555004, 200000.02, xscale), Logistic(4000000.0329152746, 200000.0, xscale), ], probs=[0.5, 0.5], )
def test_weights_mixture(): conditions = [ IntervalCondition(p=0.4, max=1, weight=0.01), IntervalCondition(p=0.5, max=2, weight=100), IntervalCondition(p=0.8, max=2.2, weight=0.01), IntervalCondition(p=0.9, max=2.3, weight=0.01), ] dist = LogisticMixture.from_conditions(conditions, {"num_components": 1}, verbose=True, scale=Scale(0, 3)) assert dist.components[0].base_dist.true_loc == pytest.approx(2, rel=0.1)
def test_mixture_from_percentiles(): conditions = [ IntervalCondition(p=0.1, max=1), IntervalCondition(p=0.5, max=2), IntervalCondition(p=0.6, max=3), ] dist = LogisticMixture.from_conditions(conditions, {"num_components": 4}, verbose=False, scale=Scale(0, 3)) for condition in conditions: assert dist.cdf(condition.max) == pytest.approx(condition.p, rel=0.1)
def get_true_scale_mixture( self, normalized_dist: LogisticMixture) -> LogisticMixture: """ Convert a normalized logistic mixture distribution to a logistic on the true scale of the question. :param normalized_dist: normalized logistic mixture dist :return: same distribution rescaled to the true scale of the question """ return normalized_dist.denormalize(self.scale)
def get_true_scale_mixture( self, normalized_dist: LogisticMixture) -> LogisticMixture: """ Convert a normalized logistic mixture distribution to a logistic on the true scale of the question. :param normalized_dist: normalized logistic mixture dist :return: same distribution rescaled to the true scale of the question """ true_scale_logistics = [ self.get_true_scale_logistic(c) for c in normalized_dist.components ] return LogisticMixture(true_scale_logistics, normalized_dist.probs)
def truncated_logistic_mixture(): xscale = Scale(5000, 120000) return LogisticMixture( components=[ Truncate( Logistic(loc=10000, s=1000, scale=xscale), floor=5000, ceiling=500000 ), Truncate( Logistic(loc=100000, s=10000, scale=xscale), floor=5000, ceiling=500000 ), ], probs=[0.8, 0.2], )
def test_mixture_from_point_densities(point_densities): conditions = [ PointDensityCondition(point_densities["xs"], point_densities["densities"]) ] mixture = LogisticMixture.from_conditions( conditions, {"num_components": 3}, Scale(min(point_densities["xs"]), max(point_densities["xs"])), ) for (x, density) in zip(point_densities["xs"], point_densities["densities"]): assert mixture.pdf(x) == pytest.approx(density, abs=0.2)
def test_percentile_roundtrip(fixed_params): conditions = [ IntervalCondition(p=0.01, max=0.61081324517545), IntervalCondition(p=0.1, max=0.8613634657212543), IntervalCondition(p=0.25, max=1), IntervalCondition(p=0.5, max=1.5), IntervalCondition(p=0.75, max=2), IntervalCondition(p=0.9, max=2.1386364698410034), IntervalCondition(p=0.99, max=2.3891870975494385), ] mixture = LogisticMixture.from_conditions( conditions, fixed_params, scale=Scale(0, 4), verbose=True, ) recovered_conditions = mixture.percentiles( percentiles=[condition.p for condition in conditions]) for (condition, recovered_condition) in zip(conditions, recovered_conditions): assert recovered_condition.max == pytest.approx(condition.max, rel=0.1)