def test_InferredInfectionProbability_prior_pdf_beta_degenerated_to_uniform_several_values(proba): value = pd.DataFrame([{"Time": 0., "Value": proba}, {"Time": 1., "Value": 0.5}]) mean = pd.DataFrame([{"Time": 0., "Value": 0.5}, {"Time": 1., "Value": 0.5}]) ip1 = inf.InferredInfectionProbability(value, mean, 1.0, 1.0, np.random.default_rng(123)) assert ip1.prior_pdf() == 1. value = pd.DataFrame([{"Time": 0., "Value": proba}, {"Time": 1., "Value": 0.5}]) mean = pd.DataFrame([{"Time": 0., "Value": 0.5}, {"Time": 1., "Value": 0.5}]) ip1 = inf.InferredInfectionProbability(value, mean, 1.0, 1.0, np.random.default_rng(123)) assert ip1.prior_pdf() == 1.
def test_InferredInfectionProbability_validate(): value = pd.DataFrame([{"Time": 0., "Value": 0.5}]) mean = pd.DataFrame([{"Time": 0., "Value": 1.}]) ip1 = inf.InferredInfectionProbability(value, mean, 2.0, 1.0, np.random.default_rng(123)) assert ip1.validate() value = pd.DataFrame([{"Time": 0., "Value": -0.5}]) mean = pd.DataFrame([{"Time": 0., "Value": 1.}]) ip1 = inf.InferredInfectionProbability(value, mean, 2.0, 1.0, np.random.default_rng(123)) assert ~ip1.validate()
def test_InferredInfectionProbability(): ip = inf.InferredInfectionProbability(pd.DataFrame(), pd.DataFrame(), 2.0, 1.0, np.random.default_rng(123)) assert ip.value.empty assert ip.mean.empty assert ip.shape == 2. assert ip.kernel_sigma == 1. 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_InferredInfectionProbability_perturbation_pdf_multiple_values_unit_noise(): value = pd.DataFrame([{"Time": 0., "Value": 0.05}, {"Time": 1., "Value": 0.05}]) mean = pd.DataFrame() ip1 = inf.InferredInfectionProbability(value, mean, 4.0, 1.0, np.random.default_rng(123)) assert ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 0.05}, {"Time": 1., "Value": 0.05}])) == 1. assert ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 0.}, {"Time": 1., "Value": 0.}])) == 1. assert ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 1.}, {"Time": 1., "Value": 1.}])) == 1. assert ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": -0.05}, {"Time": 1., "Value": -0.05}])) == 0. assert ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 1.05}, {"Time": 1., "Value": 1.05}])) == 0. assert ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 2.05}, {"Time": 1., "Value": 2.05}])) == 0.
def test_InferredInfectionProbability_generate_perturbated(): value = pd.DataFrame([{"Time": 0., "Value": 0.5}]) mean = pd.DataFrame([{"Time": 0., "Value": 1.}]) ip1 = inf.InferredInfectionProbability(value, mean, 2.0, 1.0, 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([{"Time": 0., "Value": 0.682351863248}])) pd.testing.assert_frame_equal(ip1.mean, ip2.mean) assert ip2.shape == ip1.shape assert ip2.kernel_sigma == ip1.kernel_sigma assert ip2.rng.__eq__(ip2.rng)
def test_InferredInfectionProbability_perturbation_pdf_small_noise(): value = pd.DataFrame([{"Time": 0., "Value": 0.05}]) mean = pd.DataFrame() ip1 = inf.InferredInfectionProbability(value, mean, 4.0, 0.01, np.random.default_rng(123)) pytest.approx(ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 0.05}])), 50., 1e-8) pytest.approx(ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 0.06}])), 50., 1e-8) pytest.approx(ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 0.04}])), 50., 1e-8) assert ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 0.07}])) == 0. assert ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 0.03}])) == 0. assert ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 0.}])) == 0. assert ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 1.}])) == 0. assert ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": -0.05}])) == 0. assert ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 1.05}])) == 0. assert ip1.perturbation_pdf(pd.DataFrame([{"Time": 0., "Value": 2.05}])) == 0.
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_InferredInfectionProbability_prior_pdf_beta_several_values(): value = pd.DataFrame([{"Time": 0., "Value": 0.05}, {"Time": 1., "Value": 0.05}]) mean = pd.DataFrame([{"Time": 0., "Value": 0.05}, {"Time": 1., "Value": 0.05}]) ip1 = inf.InferredInfectionProbability(value, mean, 4.0, 1.0, np.random.default_rng(123)) assert pytest.approx(ip1.prior_pdf(), 16.03449072358245**2, 1e-8)