def test_class_Chain(): """tests to ensure the behaviour class Chain""" test_model = Model('test_model') from_pop = Population('from_pop', 0.) to_pop = Population('to_pop', 0.) pop_a = Population('pop_a', 0.) pop_b = Population('pop_b', 0.) start = 100000 from_pop.future = [start] mean = 10. sigma = 2. delay_pars = {'mean': Parameter('mean', mean, parameter_min=0.1, parameter_max=100.), 'sigma': Parameter('sigma', sigma, parameter_min=0.1, parameter_max=1000.)} delay = Delay('delay', 'norm', delay_pars, test_model) frac = 0.8 fraction = Parameter('frac', frac) chain = [] chain.append(Propagator('prop_0', from_pop, pop_a, fraction, delay)) chain.append(Propagator('prop_1', pop_a, pop_b, fraction, delay)) test_chain = Chain('test_chain', from_pop, to_pop, chain, fraction, delay, test_model) test_model.add_connector(test_chain) for time_step in [1., 1. / 4.]: test_model.set_time_step(time_step) for func in [test_chain.update_expectation, test_chain.update_data]: EPS = 0.02 if func == test_chain.update_data: EPS = 0.2 to_pop.reset() pop_a.reset() pop_b.reset() func() for pop in [to_pop, pop_b]: distribution = pop.future total = start * (1. - frac ** 2) * frac ave = mean std_dev = sigma if pop == pop_b: total = start * frac ** 2 ave = 2. * mean std_dev = np.sqrt(2.) * sigma sum_p = 0. sum_tp = 0. sum_ttp = 0. for i in range(len(distribution)): sum_p += distribution[i] sum_tp += i * distribution[i] * time_step sum_ttp += i * i * distribution[i] * time_step ** 2 assert np.abs(sum_p - total) < EPS * total est_mean = sum_tp / total assert np.abs(est_mean - ave) < EPS * ave est_sigma = np.sqrt(sum_ttp / total - est_mean ** 2) assert np.abs(est_sigma - std_dev) < EPS * std_dev
def test_class_Splitter(): """tests to ensure the behaviour class Splitter""" test_model = Model('test_model') start = 100000 from_pop = Population('from_pop', 0) from_pop.future = [start] to_pops = [Population('to_pop1', 0.), Population('to_pop2', 0.)] fracs = [0.4, 0.6] fraction = Parameter('frac', fracs[0]) mean = 10. std_dev = 4. delay_pars = { 'mean': Parameter('mean', mean, parameter_min=-100., parameter_max=100.), 'sigma': Parameter('sigma', std_dev, parameter_min=-100., parameter_max=100.) } test_delay = Delay('test_delay', 'norm', delay_parameters=delay_pars, model=test_model) test_split = Splitter('test_prop', from_pop, to_pops, [fraction], test_delay) test_model.add_connector(test_split) for time_step in [1., 1. / 4.]: test_model.set_time_step(time_step) for func in [test_split.update_expectation, test_split.update_data]: EPS = 0.01 if func == test_split.update_data: EPS = 0.1 to_pops[0].reset() to_pops[1].reset() func() total = 0 for i in range(2): distribution = to_pops[i].future frac = fracs[i] sum_p = 0. sum_tp = 0. sum_ttp = 0. for i in range(len(distribution)): sum_p += distribution[i] sum_tp += i * distribution[i] * time_step sum_ttp += i * i * distribution[i] * time_step ** 2 total += sum_p assert np.abs(sum_p - start * frac) < EPS * start * frac est_mean = sum_tp / (start * frac) assert np.abs(est_mean - mean) < EPS * mean est_sigma = np.sqrt(sum_ttp / (start * frac) - est_mean ** 2) assert np.abs(est_sigma - std_dev) < EPS * std_dev assert np.abs(total - start) < 0.1
def test_class_Multiplier(): """tests to ensure the behaviour class Multiplier""" test_model = Model('test_model') EPS = 1. n1 = 50. n2 = 20. n3 = 2. scale = 0.1 f_pops = [Population('f1_pop', n1), Population('f2_pop', n2), Population('f3_pop', n3)] to_pop = Population('to_pop', 0.) scale_par = Parameter('alpha', scale) delay = Delay('fast', 'fast') test_multiplier = Multiplier('test_multiplier', f_pops, to_pop, scale_par, delay, model=test_model) test_model.add_connector(test_multiplier) for time_step in [1., 1. / 4.]: test_model.set_time_step(time_step) # expectation: expected = n1 * n2 / n3 * scale * time_step to_pop.reset() test_multiplier.set_distribution('poisson', None) test_multiplier.update_expectation() assert to_pop.future[0] == expected # Poisson n_rep = 1000 n_list = [] for i in range(n_rep): to_pop.reset() test_multiplier.update_data() n_list.append(to_pop.future[0]) assert np.abs(np.mean(n_list) - expected) < EPS assert np.abs(np.std(n_list) - np.sqrt(expected)) < EPS # Negative binomial p_nb = 0.2 nbinom_par = Parameter('nb', p_nb) test_multiplier.set_distribution('nbinom', nbinom_par) n_rep = 1000 n_list = [] for i in range(n_rep): to_pop.reset() test_multiplier.update_data() n_list.append(to_pop.future[0]) assert np.abs(np.mean(n_list) - expected) < EPS assert np.abs(np.std(n_list) - np.sqrt(expected / p_nb)) < EPS
def test_class_Propagator(): """tests to ensure the behaviour class Propagator""" test_model = Model('test_model') start = 100000 from_pop = Population('from_pop', 0) from_pop.future = [start] to_pop = Population('to_pop', 0.) frac = 0.4 fraction = Parameter('frac', frac) mean = 10. std_dev = 4. delay_pars = { 'mean': Parameter('mean', mean, parameter_min=-100., parameter_max=100.), 'sigma': Parameter('sigma', std_dev, parameter_min=-100., parameter_max=100.) } test_delay = Delay('test_delay', 'norm', delay_parameters=delay_pars, model=test_model) test_prop = Propagator('test_prop', from_pop, to_pop, fraction, test_delay) test_model.add_connector(test_prop) for time_step in [1., 1. / 4.]: test_model.set_time_step(time_step) for func in [test_prop.update_expectation, test_prop.update_data]: EPS = 0.01 if func == test_prop.update_data: EPS = 0.1 to_pop.reset() func() distribution = to_pop.future sum_p = 0. sum_tp = 0. sum_ttp = 0. for i in range(len(distribution)): sum_p += distribution[i] sum_tp += i * distribution[i] * time_step sum_ttp += i * i * distribution[i] * time_step ** 2 assert np.abs(sum_p - start * frac) < EPS * start * frac est_mean = sum_tp / (start * frac) assert np.abs(est_mean - mean) < EPS * mean est_sigma = np.sqrt(sum_ttp / (start * frac) - est_mean ** 2) assert np.abs(est_sigma - std_dev) < EPS * std_dev
contagious_pop = Population('contagious', initial_contagious_par, 'number of people that can cause someone to become infected', hidden=False, color='red') # this value is only used if the transition is removed trans_rate = Parameter('alpha', 0.4, 0., 2., 'mean number of people that a contagious person infects ' + 'per day', hidden=True) fast_delay = Delay('fast', 'fast', model=bc_model) neg_binom_par = Parameter('neg_binom_p', 0.5, 0.001, 0.999, 'Dispersion parameter p for neg binom') bc_model.add_connector( Multiplier('infection cycle', [susceptible_pop, contagious_pop, total_pop], infected_pop, trans_rate, fast_delay, bc_model, distribution='nbinom', nbinom_par=neg_binom_par)) contagious_frac = Parameter('cont_frac', 0.9, 0., 1., 'fraction of infected people that become contagious', hidden=False) contagious_delay_pars = { 'mean': Parameter('cont_delay_mean', 5., 0., 50., 'mean time from being infected to becoming contagious'), 'sigma': Parameter('cont_delay_sigma', 3., 0.01, 20., 'standard deviation of times from being infected to becoming contagious') } contagious_delay = Delay('cont_delay', 'norm', contagious_delay_pars, bc_model) bc_model.add_connector(
initial_contagious_par = Parameter('cont_0', 55., 0., 5000., 'Number of contagious people at t0', hidden=False) contagious_pop = Population('contagious', initial_contagious_par, 'number of people that can cause someone to become infected', hidden=False, color='red') # this value is only used if the transition is removed trans_rate = Parameter('alpha', 0.390, 0., 2., 'mean number of people that a contagious person infects ' + 'per day', hidden=True) infection_delay = Delay('fast', 'fast', model=bc_model) bc_model.add_connector( Multiplier('infection cycle', [susceptible_pop, contagious_pop, total_pop], infected_pop, trans_rate, infection_delay, bc_model)) contagious_frac = Parameter('cont_frac', 0.9, 0., 1., 'fraction of infected people that become contagious', hidden=False) contagious_delay_pars = { 'mean': Parameter('cont_delay_mean', 2., 0., 50., 'mean time from being infected to becoming contagious'), 'sigma': Parameter('cont_delay_sigma', 1., 0.01, 20., 'standard deviation of times from being infected to becoming contagious') } contagious_delay = Delay('cont_delay', 'norm', contagious_delay_pars, bc_model) bc_model.add_connector(
trans_rate = Parameter( 'alpha', 0.4, 0., 2., 'mean number of people that a contagious person infects ' + 'per day', hidden=True) infection_delay = Delay('fast', 'fast', model=bc_model) neg_binom_par = Parameter('neg_binom_p', 0.5, 0.001, 0.999, 'Dispersion parameter p for neg binom') bc_model.add_connector( Multiplier('infection cycle', [susceptible_pop, contagious_pop, total_pop], infected_pop, trans_rate, infection_delay, bc_model, distribution='nbinom', nbinom_par=neg_binom_par)) contagious_frac = Parameter( 'cont_frac', 0.9, 0., 1., 'fraction of infected people that become contagious', hidden=False) contagious_delay_pars = { 'mean': Parameter('cont_delay_mean', 5., 0., 50., 'mean time from being infected to becoming contagious'),