def test_rate_to_probability_vectorizability(): rate = 0.001 rate = np.array([rate] * 100) prob = rate_to_probability(rate) assert round(prob[10], 5) == round(0.00099950016662497809, 5) assert round(np.sum(rate), 5) == round(np.sum(probability_to_rate(prob)), 5)
def on_time_step(self, event): pop = self.population_view.get(event.index, query="alive =='alive' and sex != 'nan'") pop['time_since_last_migration'] = event.time - pop['last_outmigration_time'] # only allow individuals that have not migrated internaly on the last year. pop = pop[(pop['time_since_last_migration'] > pd.Timedelta("365 days")) | (pop['time_since_last_migration'].notnull() == False)] prob_df = rate_to_probability(pd.DataFrame(self.int_outmigration_rate(pop.index))) prob_df['No'] = 1-prob_df.sum(axis=1) pop['internal_outmigration'] = self.random.choice(prob_df.index, prob_df.columns, prob_df) int_outmigrated_pop = pop.query('internal_outmigration != "No"').copy() if not int_outmigrated_pop.empty: int_outmigrated_pop['internal_outmigration'] = pd.Series('Yes', index=int_outmigrated_pop.index) int_outmigrated_pop['last_outmigration_time'] = event.time int_outmigrated_pop['previous_LAD_locations'] += int_outmigrated_pop['location'] int_outmigrated_pop['previous_MSOA_locations'] += int_outmigrated_pop['MSOA'] new_MSOA, new_LAD = self.assign_internal_migration(int_outmigrated_pop) int_outmigrated_pop['MSOA'] = new_MSOA int_outmigrated_pop['location'] = new_LAD self.population_view.update(int_outmigrated_pop[['last_outmigration_time', 'internal_outmigration', 'previous_LAD_locations', 'previous_MSOA_locations', 'MSOA', 'location']])
def on_time_step(self, event): pop = self.population_view.get(event.index, query="alive =='alive'") prob_df = rate_to_probability(pd.DataFrame(self.mortality_rate(pop.index))) prob_df['no_death'] = 1-prob_df.sum(axis=1) prob_df['cause_of_death'] = self.random.choice(prob_df.index, prob_df.columns, prob_df) dead_pop = prob_df.query('cause_of_death != "no_death"').copy() if not dead_pop.empty: dead_pop['alive'] = pd.Series('dead', index=dead_pop.index) dead_pop['exit_time'] = event.time dead_pop['years_of_life_lost'] = self.life_expectancy(dead_pop.index) self.population_view.update(dead_pop[['alive', 'exit_time', 'cause_of_death', 'years_of_life_lost']])
def on_time_step(self, event): pop = self.population_view.get( event.index, query="alive =='alive' and sex != 'nan'") prob_df = rate_to_probability( pd.DataFrame(self.emigration_rate(pop.index))) prob_df['no_emigration'] = 1 - prob_df.sum(axis=1) prob_df['emigrated'] = self.random.choice(prob_df.index, prob_df.columns, prob_df) emigrated_pop = prob_df.query('emigrated != "no_emigration"').copy() if not emigrated_pop.empty: emigrated_pop['alive'] = pd.Series('emigrated', index=emigrated_pop.index) emigrated_pop['emigrated'] = pd.Series('Yes', index=emigrated_pop.index) emigrated_pop['exit_time'] = event.time self.population_view.update( emigrated_pop[['alive', 'exit_time', 'emigrated']])
def test_rate_to_probability_symmetry(): rate = np.array([0.0001]) for _ in range(100): prob = rate_to_probability(rate) assert np.isclose(rate, probability_to_rate(prob)) rate += (1 - 0.0001) / 100.0
def test_rate_to_probability(): rate = np.array([0.001]) prob = rate_to_probability(rate) assert np.isclose(prob, 0.00099950016662497809)
def _probability(self, index): return rate_to_probability(self.transition_rate(index))
def _probability(self, index): effective_rate = self.transition_rate(index) return rate_to_probability(effective_rate)