def test_InferredContactMultipliers(): ip = inf.InferredContactMultipliers(pd.DataFrame(), pd.DataFrame(), 0.2, 1., [], np.random.default_rng(123)) assert ip.value.empty assert ip.mean.empty assert ip.stddev == 0.2 assert ip.kernel_sigma == 1. assert ip.partitions == [] assert ip.rng.__eq__(np.random.default_rng(123))
def test_Particle_validate(): rng = np.random.default_rng(123) value = pd.DataFrame([{"Time": 0., "Value": 0.5}]) mean = pd.DataFrame([{"Time": 0., "Value": 1.}]) good_infection_proba = inf.InferredInfectionProbability(value, mean, 2.0, 1.0, rng) value = pd.DataFrame([{"Time": 0., "Value": -0.5}]) mean = pd.DataFrame([{"Time": 0., "Value": 1.}]) bad_infection_proba = inf.InferredInfectionProbability(value, mean, 2.0, 1.0, rng) value = pd.DataFrame([{"Health_Board": "hb1", "Age": "[17,70)", "Infected": 0}, {"Health_Board": "hb2", "Age": "[17,70)", "Infected": 30}]) mean = pd.DataFrame([{"Health_Board": "hb1", "Age": "[17,70)", "Infected": 10}, {"Health_Board": "hb2", "Age": "[17,70)", "Infected": 50}]) good_initial_infections = inf.InferredInitialInfections(value, mean, 0.2, 10., 1., rng) value = pd.DataFrame([{"Health_Board": "hb1", "Age": "[17,70)", "Infected": -10}, {"Health_Board": "hb2", "Age": "[17,70)", "Infected": 10}]) mean = pd.DataFrame([{"Health_Board": "hb1", "Age": "[17,70)", "Infected": 10}, {"Health_Board": "hb2", "Age": "[17,70)", "Infected": 50}]) bad_initial_infections = inf.InferredInitialInfections(value, mean, 0.2, 10., 1., rng) value = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 4.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 5.}]) mean = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 2.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 4.}]) good_cm = inf.InferredContactMultipliers(value, mean, 0.2, 1., [dt.date.min, dt.date(2020, 2, 1), dt.date.max], rng) value = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 0.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 5.}]) mean = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 2.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 4.}]) bad_cm = inf.InferredContactMultipliers(value, mean, 0.2, 1., [dt.date.min, dt.date(2020, 2, 1), dt.date.max], rng) assert inf.Particle({ "infection-probability": good_infection_proba, "initial-infections": good_initial_infections, "contact-multipliers": good_cm }).validate_particle() assert ~inf.Particle({ "infection-probability": bad_infection_proba, "initial-infections": bad_initial_infections, "contact-multipliers": bad_cm }).validate_particle()
def test_InferredContactMultipliers_perturbation_pdf_value_close_to_zero(bump): value = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 0.5}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 5.}]) mean = pd.DataFrame() ip1 = inf.InferredContactMultipliers(value, mean, 0.2, 1., [dt.date.min, dt.date(2020, 2, 1), dt.date.max], np.random.default_rng(123)) assert ip1.perturbation_pdf(pd.DataFrame([ {"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 0.5 + bump}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 5. + bump}])) == (1. / 1.5) * 0.5
def test_InferredContactMultipliers_prior_pdf(): value = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 0.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 5.}]) mean = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 2.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 4.}]) ip1 = inf.InferredContactMultipliers(value, mean, 0.2, 1., [dt.date.min, dt.date(2020, 2, 1), dt.date.max], np.random.default_rng(123)) assert ip1.prior_pdf() == 0. value = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 10000.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 5.}]) mean = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 2.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 4.}]) ip1 = inf.InferredContactMultipliers(value, mean, 0.2, 1., [dt.date.min, dt.date(2020, 2, 1), dt.date.max], np.random.default_rng(123)) assert pytest.approx(ip1.prior_pdf(), 0.0, 1e-8) value = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 3.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 5.}]) mean = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 2.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 4.}]) ip1 = inf.InferredContactMultipliers(value, mean, 0.2, 1., [dt.date.min, dt.date(2020, 2, 1), dt.date.max], np.random.default_rng(123)) assert pytest.approx(ip1.prior_pdf(), 0.012751291705831346, 1e-8)
def test_InferredContactMultipliers_validate(): value = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 4.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 5.}]) mean = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 2.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 4.}]) ip1 = inf.InferredContactMultipliers(value, mean, 0.2, 1., [dt.date.min, dt.date(2020, 2, 1), dt.date.max], np.random.default_rng(123)) assert ip1.validate() value = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 0.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 5.}]) mean = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 2.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 4.}]) ip1 = inf.InferredContactMultipliers(value, mean, 0.2, 1., [dt.date.min, dt.date(2020, 2, 1), dt.date.max], np.random.default_rng(123)) assert ~ip1.validate() value = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": -4.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 5.}]) mean = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 2.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 4.}]) ip1 = inf.InferredContactMultipliers(value, mean, 0.2, 1., [dt.date.min, dt.date(2020, 2, 1), dt.date.max], np.random.default_rng(123)) assert ~ip1.validate()
def test_Particle(): rng = np.random.default_rng(123) infection_probability = inf.InferredInfectionProbability(pd.DataFrame(), pd.DataFrame(), 2.0, 1.0, rng) initial_infections = inf.InferredInitialInfections(pd.DataFrame(), pd.DataFrame(), 0.2, 10., 1., rng) contact_multipliers = inf.InferredContactMultipliers(pd.DataFrame(), pd.DataFrame(), 0.2, 1., [], rng) particle = inf.Particle({ "infection-probability": infection_probability, "initial-infections": initial_infections, "contact-multipliers": contact_multipliers }) assert len(particle.inferred_variables) == 3 assert list(particle.inferred_variables.keys()) == ["infection-probability", "initial-infections", "contact-multipliers"] inf.Particle({})
def test_InferredContactMultipliers_generate_perturbated(): value = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 4.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 5.}]) mean = pd.DataFrame([{"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 2.}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 4.}]) ip1 = inf.InferredContactMultipliers(value, mean, 0.2, 1., [dt.date.min, dt.date(2020, 2, 1), dt.date.max], np.random.default_rng(123)) ip2 = ip1.generate_perturbated() pd.testing.assert_frame_equal(ip1.value, value) pd.testing.assert_frame_equal(ip2.value, pd.DataFrame([ {"Date": "2020-01-10", "Movement_Multiplier": 1., "Contact_Multiplier": 4.364703726496}, {"Date": "2020-02-10", "Movement_Multiplier": 2., "Contact_Multiplier": 4.107642037604}])) pd.testing.assert_frame_equal(ip1.mean, ip2.mean) assert ip2.stddev == ip1.stddev assert ip2.kernel_sigma == ip1.kernel_sigma assert ip2.partitions == ip1.partitions assert ip2.rng.__eq__(ip2.rng)