Modifying the population using complex policies¶
This notebook shows how complex policies can be applied to people, households, and entire populations.
In [1]:
Copied!
from pam.activity import Activity, Leg
from pam.core import Household, Person, Population
from pam.policy import (
ActivityPolicy,
ActivityProbability,
HouseholdPolicy,
MoveActivityTourToHomeLocation,
PersonAttributeFilter,
PersonPolicy,
PersonProbability,
ReduceSharedActivity,
RemoveActivity,
apply_policies,
)
from pam.utils import minutes_to_datetime as mtdt
from pam.variables import END_OF_DAY
from pam.activity import Activity, Leg
from pam.core import Household, Person, Population
from pam.policy import (
ActivityPolicy,
ActivityProbability,
HouseholdPolicy,
MoveActivityTourToHomeLocation,
PersonAttributeFilter,
PersonPolicy,
PersonProbability,
ReduceSharedActivity,
RemoveActivity,
apply_policies,
)
from pam.utils import minutes_to_datetime as mtdt
from pam.variables import END_OF_DAY
In [2]:
Copied!
def print_simple_stats(population):
"""Print some simple population statistics."""
time_at_home = 0
travel_time = 0
low_income_central_trips = 0
high_income_central_trips = 0
for _hid, hh in population.households.items():
for _pid, person in hh.people.items():
freq = person.freq
for p in person.plan:
if p.act == "travel":
duration = p.duration.seconds * freq / 3600
travel_time += duration
if p.end_location.area == "Westminster,City of London":
if person.attributes["inc"] == "low":
low_income_central_trips += freq
elif person.attributes["inc"] == "high":
high_income_central_trips += freq
else: # activity
if p.act == "home":
duration = p.duration.seconds * freq / 3600
time_at_home += duration
print(f"Population total time at home: {time_at_home:.2f} hours")
print(f"Population total travel time: {travel_time:.2f} hours")
def print_simple_stats(population):
"""Print some simple population statistics."""
time_at_home = 0
travel_time = 0
low_income_central_trips = 0
high_income_central_trips = 0
for _hid, hh in population.households.items():
for _pid, person in hh.people.items():
freq = person.freq
for p in person.plan:
if p.act == "travel":
duration = p.duration.seconds * freq / 3600
travel_time += duration
if p.end_location.area == "Westminster,City of London":
if person.attributes["inc"] == "low":
low_income_central_trips += freq
elif person.attributes["inc"] == "high":
high_income_central_trips += freq
else: # activity
if p.act == "home":
duration = p.duration.seconds * freq / 3600
time_at_home += duration
print(f"Population total time at home: {time_at_home:.2f} hours")
print(f"Population total travel time: {travel_time:.2f} hours")
Create example Population¶
In [3]:
Copied!
population = Population()
population = Population()
Add Smith household¶
In [4]:
Copied!
smith_id = "Smith"
Billy = Person(
"Billy",
freq=1,
attributes={"age": 26, "job": "employed", "gender": "female", "key_worker": False},
)
Billy.add(Activity(1, "home", "a", start_time=mtdt(0), end_time=mtdt(8 * 60)))
Billy.add(
Leg(1, "car", start_area="a", end_area="g", start_time=mtdt(8 * 60), end_time=mtdt(8 * 60 + 20))
)
Billy.add(Activity(2, "work", "g", start_time=mtdt(8 * 60 + 20), end_time=mtdt(17 * 60)))
Billy.add(
Leg(
2,
"car",
start_area="g",
end_area="a",
start_time=mtdt(17 * 60),
end_time=mtdt(17 * 60 + 25),
)
)
Billy.add(Activity(3, "home", "a", start_time=mtdt(17 * 60 + 25), end_time=END_OF_DAY))
Bobby = Person(
"Bobby",
freq=1,
attributes={"age": 6, "job": "education", "gender": "male", "key_worker": False},
)
Bobby.add(Activity(1, "home", "a", start_time=mtdt(0), end_time=mtdt(8 * 60)))
Bobby.add(Leg(1, "walk", "a", "b", start_time=mtdt(8 * 60 + 5), end_time=mtdt(8 * 60 + 30)))
Bobby.add(Activity(2, "education", "b", start_time=mtdt(8 * 60 + 30), end_time=mtdt(16 * 60)))
Bobby.add(Leg(2, "walk", "b", "c", start_time=mtdt(16 * 60), end_time=mtdt(16 * 60 + 35)))
Bobby.add(Activity(3, "home", "a", start_time=mtdt(16 * 60 + 30), end_time=mtdt(18 * 60)))
Bobby.add(Leg(3, "car", "a", "b", start_time=mtdt(18 * 60), end_time=mtdt(18 * 60 + 20)))
Bobby.add(Activity(4, "shop_1", "b", start_time=mtdt(18 * 60 + 20), end_time=mtdt(18 * 60 + 50)))
Bobby.add(Leg(4, "car", "b", "b", start_time=mtdt(18 * 60 + 50), end_time=mtdt(19 * 60)))
Bobby.add(Activity(5, "shop_2", "b", start_time=mtdt(19 * 60), end_time=mtdt(19 * 60 + 50)))
Bobby.add(Leg(5, "car", "b", "a", start_time=mtdt(19 * 60 + 50), end_time=mtdt(20 * 60 + 10)))
Bobby.add(Activity(6, "home", "a", start_time=mtdt(20 * 60 + 10), end_time=END_OF_DAY))
Bradly = Person(
"Bradly",
freq=1,
attributes={"age": 40, "job": "employed", "gender": "male", "key_worker": True},
)
Bradly.add(Activity(1, "home", "a", start_time=mtdt(0), end_time=mtdt(8 * 60)))
Bradly.add(Leg(1, "walk", "a", "b", start_time=mtdt(8 * 60), end_time=mtdt(8 * 60 + 20)))
Bradly.add(Activity(2, "escort", "b", start_time=mtdt(8 * 60 + 20), end_time=mtdt(8 * 60 + 30)))
Bradly.add(Leg(2, "pt", "b", "b", start_time=mtdt(8 * 60 + 30), end_time=mtdt(9 * 60)))
Bradly.add(Activity(4, "work", "b", start_time=mtdt(9 * 60), end_time=mtdt(10 * 60)))
Bradly.add(Leg(3, "pt", "b", "c", start_time=mtdt(10 * 60), end_time=mtdt(10 * 60 + 20)))
Bradly.add(Activity(4, "work", "b", start_time=mtdt(10 * 60 + 20), end_time=mtdt(12 * 60)))
Bradly.add(Leg(4, "pt", "b", "c", start_time=mtdt(12 * 60), end_time=mtdt(12 * 60 + 20)))
Bradly.add(Activity(5, "work", "b", start_time=mtdt(12 * 60 + 20), end_time=mtdt(14 * 60)))
Bradly.add(Leg(5, "pt", "b", "c", start_time=mtdt(14 * 60), end_time=mtdt(14 * 60 + 20)))
Bradly.add(Activity(6, "leisure", "c", start_time=mtdt(14 * 60 + 20), end_time=mtdt(15 * 60 + 30)))
Bradly.add(Leg(4, "pt", "c", "b", start_time=mtdt(15 * 60 + 30), end_time=mtdt(16 * 60 - 10)))
Bradly.add(Activity(5, "escort", "b", start_time=mtdt(16 * 60 - 10), end_time=mtdt(16 * 60)))
Bradly.add(Leg(5, "walk", "b", "a", start_time=mtdt(16 * 60), end_time=mtdt(16 * 60 + 20)))
Bradly.add(Activity(8, "home", "a", start_time=mtdt(16 * 60 + 20), end_time=mtdt(18 * 60)))
Bradly.add(Leg(8, "car", "a", "b", start_time=mtdt(18 * 60), end_time=mtdt(18 * 60 + 20)))
Bradly.add(Activity(9, "shop_1", "b", start_time=mtdt(18 * 60 + 20), end_time=mtdt(18 * 60 + 50)))
Bradly.add(Leg(9, "car", "b", "b", start_time=mtdt(18 * 60 + 50), end_time=mtdt(19 * 60)))
Bradly.add(Activity(10, "shop_2", "b", start_time=mtdt(19 * 60), end_time=mtdt(19 * 60 + 50)))
Bradly.add(Leg(10, "car", "b", "a", start_time=mtdt(19 * 60 + 50), end_time=mtdt(20 * 60 + 10)))
Bradly.add(Activity(11, "home", "a", start_time=mtdt(20 * 60 + 10), end_time=END_OF_DAY))
smiths = Household(smith_id)
for person in [Billy, Bradly, Bobby]:
smiths.add(person)
smiths.people
smith_id = "Smith"
Billy = Person(
"Billy",
freq=1,
attributes={"age": 26, "job": "employed", "gender": "female", "key_worker": False},
)
Billy.add(Activity(1, "home", "a", start_time=mtdt(0), end_time=mtdt(8 * 60)))
Billy.add(
Leg(1, "car", start_area="a", end_area="g", start_time=mtdt(8 * 60), end_time=mtdt(8 * 60 + 20))
)
Billy.add(Activity(2, "work", "g", start_time=mtdt(8 * 60 + 20), end_time=mtdt(17 * 60)))
Billy.add(
Leg(
2,
"car",
start_area="g",
end_area="a",
start_time=mtdt(17 * 60),
end_time=mtdt(17 * 60 + 25),
)
)
Billy.add(Activity(3, "home", "a", start_time=mtdt(17 * 60 + 25), end_time=END_OF_DAY))
Bobby = Person(
"Bobby",
freq=1,
attributes={"age": 6, "job": "education", "gender": "male", "key_worker": False},
)
Bobby.add(Activity(1, "home", "a", start_time=mtdt(0), end_time=mtdt(8 * 60)))
Bobby.add(Leg(1, "walk", "a", "b", start_time=mtdt(8 * 60 + 5), end_time=mtdt(8 * 60 + 30)))
Bobby.add(Activity(2, "education", "b", start_time=mtdt(8 * 60 + 30), end_time=mtdt(16 * 60)))
Bobby.add(Leg(2, "walk", "b", "c", start_time=mtdt(16 * 60), end_time=mtdt(16 * 60 + 35)))
Bobby.add(Activity(3, "home", "a", start_time=mtdt(16 * 60 + 30), end_time=mtdt(18 * 60)))
Bobby.add(Leg(3, "car", "a", "b", start_time=mtdt(18 * 60), end_time=mtdt(18 * 60 + 20)))
Bobby.add(Activity(4, "shop_1", "b", start_time=mtdt(18 * 60 + 20), end_time=mtdt(18 * 60 + 50)))
Bobby.add(Leg(4, "car", "b", "b", start_time=mtdt(18 * 60 + 50), end_time=mtdt(19 * 60)))
Bobby.add(Activity(5, "shop_2", "b", start_time=mtdt(19 * 60), end_time=mtdt(19 * 60 + 50)))
Bobby.add(Leg(5, "car", "b", "a", start_time=mtdt(19 * 60 + 50), end_time=mtdt(20 * 60 + 10)))
Bobby.add(Activity(6, "home", "a", start_time=mtdt(20 * 60 + 10), end_time=END_OF_DAY))
Bradly = Person(
"Bradly",
freq=1,
attributes={"age": 40, "job": "employed", "gender": "male", "key_worker": True},
)
Bradly.add(Activity(1, "home", "a", start_time=mtdt(0), end_time=mtdt(8 * 60)))
Bradly.add(Leg(1, "walk", "a", "b", start_time=mtdt(8 * 60), end_time=mtdt(8 * 60 + 20)))
Bradly.add(Activity(2, "escort", "b", start_time=mtdt(8 * 60 + 20), end_time=mtdt(8 * 60 + 30)))
Bradly.add(Leg(2, "pt", "b", "b", start_time=mtdt(8 * 60 + 30), end_time=mtdt(9 * 60)))
Bradly.add(Activity(4, "work", "b", start_time=mtdt(9 * 60), end_time=mtdt(10 * 60)))
Bradly.add(Leg(3, "pt", "b", "c", start_time=mtdt(10 * 60), end_time=mtdt(10 * 60 + 20)))
Bradly.add(Activity(4, "work", "b", start_time=mtdt(10 * 60 + 20), end_time=mtdt(12 * 60)))
Bradly.add(Leg(4, "pt", "b", "c", start_time=mtdt(12 * 60), end_time=mtdt(12 * 60 + 20)))
Bradly.add(Activity(5, "work", "b", start_time=mtdt(12 * 60 + 20), end_time=mtdt(14 * 60)))
Bradly.add(Leg(5, "pt", "b", "c", start_time=mtdt(14 * 60), end_time=mtdt(14 * 60 + 20)))
Bradly.add(Activity(6, "leisure", "c", start_time=mtdt(14 * 60 + 20), end_time=mtdt(15 * 60 + 30)))
Bradly.add(Leg(4, "pt", "c", "b", start_time=mtdt(15 * 60 + 30), end_time=mtdt(16 * 60 - 10)))
Bradly.add(Activity(5, "escort", "b", start_time=mtdt(16 * 60 - 10), end_time=mtdt(16 * 60)))
Bradly.add(Leg(5, "walk", "b", "a", start_time=mtdt(16 * 60), end_time=mtdt(16 * 60 + 20)))
Bradly.add(Activity(8, "home", "a", start_time=mtdt(16 * 60 + 20), end_time=mtdt(18 * 60)))
Bradly.add(Leg(8, "car", "a", "b", start_time=mtdt(18 * 60), end_time=mtdt(18 * 60 + 20)))
Bradly.add(Activity(9, "shop_1", "b", start_time=mtdt(18 * 60 + 20), end_time=mtdt(18 * 60 + 50)))
Bradly.add(Leg(9, "car", "b", "b", start_time=mtdt(18 * 60 + 50), end_time=mtdt(19 * 60)))
Bradly.add(Activity(10, "shop_2", "b", start_time=mtdt(19 * 60), end_time=mtdt(19 * 60 + 50)))
Bradly.add(Leg(10, "car", "b", "a", start_time=mtdt(19 * 60 + 50), end_time=mtdt(20 * 60 + 10)))
Bradly.add(Activity(11, "home", "a", start_time=mtdt(20 * 60 + 10), end_time=END_OF_DAY))
smiths = Household(smith_id)
for person in [Billy, Bradly, Bobby]:
smiths.add(person)
smiths.people
Out[4]:
{'Billy': <pam.core.Person at 0x106c11600>, 'Bradly': <pam.core.Person at 0x137e48ac0>, 'Bobby': <pam.core.Person at 0x137e35c60>}
In [5]:
Copied!
smiths.plot()
smiths.plot()
In [6]:
Copied!
population.add(smiths)
population.add(smiths)
Add Jones household¶
In [7]:
Copied!
jones_id = "Jones"
Hugh = Person(
"Hugh",
freq=1,
attributes={"age": 100, "job": "unemployed", "gender": "male", "key_worker": False},
)
Hugh.add(Activity(1, "home", "a", start_time=mtdt(0), end_time=mtdt(8 * 60)))
Hugh.add(Leg(1, "walk", "a", "b", start_time=mtdt(8 * 60), end_time=mtdt(8 * 60 + 30)))
Hugh.add(Activity(2, "health", "b", start_time=mtdt(8 * 60 + 30), end_time=mtdt(10 * 60)))
Hugh.add(Leg(2, "walk", "b", "a", start_time=mtdt(10 * 60), end_time=mtdt(10 * 60 + 30)))
Hugh.add(Activity(3, "home", "a", start_time=mtdt(10 * 60 + 30), end_time=mtdt(14 * 60)))
Hugh.add(Leg(3, "walk", "a", "b", start_time=mtdt(14 * 60), end_time=mtdt(14 * 60 + 30)))
Hugh.add(Activity(4, "health", "b", start_time=mtdt(14 * 60 + 30), end_time=mtdt(16 * 60)))
Hugh.add(Leg(4, "walk", "b", "a", start_time=mtdt(16 * 60), end_time=mtdt(16 * 60 + 30)))
Hugh.add(Activity(5, "home", "a", start_time=mtdt(16 * 60 + 30), end_time=END_OF_DAY))
Bridget = Person(
"Bridget",
freq=1,
attributes={"age": 35, "job": "employed", "gender": "female", "key_worker": False},
)
Bridget.add(Activity(1, "home", "a", start_time=mtdt(0), end_time=mtdt(8 * 60)))
Bridget.add(Leg(1, "walk", "a", "b", start_time=mtdt(8 * 60), end_time=mtdt(8 * 60 + 5)))
Bridget.add(Activity(2, "escort", "b", start_time=mtdt(8 * 60 + 5), end_time=mtdt(10 * 60 + 30)))
Bridget.add(Leg(2, "pt", "b", "c", start_time=mtdt(10 * 60 + 30), end_time=mtdt(11 * 60)))
Bridget.add(Activity(3, "work", "c", start_time=mtdt(11 * 60), end_time=mtdt(16 * 60)))
Bridget.add(Leg(3, "pt", "c", "a", start_time=mtdt(16 * 60), end_time=mtdt(16 * 60 + 20)))
Bridget.add(Activity(4, "home", "a", start_time=mtdt(16 * 60 + 20), end_time=mtdt(17 * 60 + 20)))
Bridget.add(Leg(4, "pt", "c", "a", start_time=mtdt(17 * 60 + 20), end_time=mtdt(17 * 60 + 50)))
Bridget.add(Activity(5, "shop", "a", start_time=mtdt(17 * 60 + 50), end_time=mtdt(18 * 60 + 30)))
Bridget.add(Leg(5, "pt", "c", "a", start_time=mtdt(18 * 60 + 30), end_time=mtdt(18 * 60 + 50)))
Bridget.add(Activity(6, "home", "a", start_time=mtdt(18 * 60 + 50), end_time=END_OF_DAY))
jones = Household(jones_id)
for person in [Hugh, Bridget]:
jones.add(person)
jones.people
jones_id = "Jones"
Hugh = Person(
"Hugh",
freq=1,
attributes={"age": 100, "job": "unemployed", "gender": "male", "key_worker": False},
)
Hugh.add(Activity(1, "home", "a", start_time=mtdt(0), end_time=mtdt(8 * 60)))
Hugh.add(Leg(1, "walk", "a", "b", start_time=mtdt(8 * 60), end_time=mtdt(8 * 60 + 30)))
Hugh.add(Activity(2, "health", "b", start_time=mtdt(8 * 60 + 30), end_time=mtdt(10 * 60)))
Hugh.add(Leg(2, "walk", "b", "a", start_time=mtdt(10 * 60), end_time=mtdt(10 * 60 + 30)))
Hugh.add(Activity(3, "home", "a", start_time=mtdt(10 * 60 + 30), end_time=mtdt(14 * 60)))
Hugh.add(Leg(3, "walk", "a", "b", start_time=mtdt(14 * 60), end_time=mtdt(14 * 60 + 30)))
Hugh.add(Activity(4, "health", "b", start_time=mtdt(14 * 60 + 30), end_time=mtdt(16 * 60)))
Hugh.add(Leg(4, "walk", "b", "a", start_time=mtdt(16 * 60), end_time=mtdt(16 * 60 + 30)))
Hugh.add(Activity(5, "home", "a", start_time=mtdt(16 * 60 + 30), end_time=END_OF_DAY))
Bridget = Person(
"Bridget",
freq=1,
attributes={"age": 35, "job": "employed", "gender": "female", "key_worker": False},
)
Bridget.add(Activity(1, "home", "a", start_time=mtdt(0), end_time=mtdt(8 * 60)))
Bridget.add(Leg(1, "walk", "a", "b", start_time=mtdt(8 * 60), end_time=mtdt(8 * 60 + 5)))
Bridget.add(Activity(2, "escort", "b", start_time=mtdt(8 * 60 + 5), end_time=mtdt(10 * 60 + 30)))
Bridget.add(Leg(2, "pt", "b", "c", start_time=mtdt(10 * 60 + 30), end_time=mtdt(11 * 60)))
Bridget.add(Activity(3, "work", "c", start_time=mtdt(11 * 60), end_time=mtdt(16 * 60)))
Bridget.add(Leg(3, "pt", "c", "a", start_time=mtdt(16 * 60), end_time=mtdt(16 * 60 + 20)))
Bridget.add(Activity(4, "home", "a", start_time=mtdt(16 * 60 + 20), end_time=mtdt(17 * 60 + 20)))
Bridget.add(Leg(4, "pt", "c", "a", start_time=mtdt(17 * 60 + 20), end_time=mtdt(17 * 60 + 50)))
Bridget.add(Activity(5, "shop", "a", start_time=mtdt(17 * 60 + 50), end_time=mtdt(18 * 60 + 30)))
Bridget.add(Leg(5, "pt", "c", "a", start_time=mtdt(18 * 60 + 30), end_time=mtdt(18 * 60 + 50)))
Bridget.add(Activity(6, "home", "a", start_time=mtdt(18 * 60 + 50), end_time=END_OF_DAY))
jones = Household(jones_id)
for person in [Hugh, Bridget]:
jones.add(person)
jones.people
Out[7]:
{'Hugh': <pam.core.Person at 0x16424cd00>, 'Bridget': <pam.core.Person at 0x1641253c0>}
In [8]:
Copied!
jones.plot()
jones.plot()
In [9]:
Copied!
population.add(jones)
population.add(jones)
In [10]:
Copied!
print_simple_stats(population)
print_simple_stats(population)
Population total time at home: 74.58 hours Population total travel time: 9.67 hours
In [11]:
Copied!
def discrete_joint_distribution_sampler(obj, mapping, distribution):
"""
Randomly sample from a joint distribution based some discrete features.
Where features are a dictionary structure of features, eg: {'gender':'female'}
Distribution is a nested dict of probabilities based on possible features, eg:
{'0-0': {'male': 0, 'female': 0},... , '90-120': {'male': 1, 'female': 1}}
Mapping provides the feature name for each level of the distribution, eg:
['age', 'gender'].
"""
p = distribution
for key in mapping:
value = obj.attributes.get(key)
if value is None:
msg = f"Cannot find mapping: {key} in sampling features: {obj.attributes}"
raise KeyError(msg)
p = p.get(value)
if p is None:
msg = f"Cannot find feature for {key}: {value} in distribution: {p}"
raise KeyError(msg)
return p
def discrete_joint_distribution_sampler(obj, mapping, distribution):
"""
Randomly sample from a joint distribution based some discrete features.
Where features are a dictionary structure of features, eg: {'gender':'female'}
Distribution is a nested dict of probabilities based on possible features, eg:
{'0-0': {'male': 0, 'female': 0},... , '90-120': {'male': 1, 'female': 1}}
Mapping provides the feature name for each level of the distribution, eg:
['age', 'gender'].
"""
p = distribution
for key in mapping:
value = obj.attributes.get(key)
if value is None:
msg = f"Cannot find mapping: {key} in sampling features: {obj.attributes}"
raise KeyError(msg)
p = p.get(value)
if p is None:
msg = f"Cannot find feature for {key}: {value} in distribution: {p}"
raise KeyError(msg)
return p
In [12]:
Copied!
vulnerable_mapping = ["age", "gender"]
vulnerable_distribution = dict(
zip(
list(range(101)),
[{"male": i / 100, "female": i / 100, "other": i / 100} for i in range(101)],
)
)
vulnerable_mapping = ["age", "gender"]
vulnerable_distribution = dict(
zip(
list(range(101)),
[{"male": i / 100, "female": i / 100, "other": i / 100} for i in range(101)],
)
)
In [13]:
Copied!
dict(list(vulnerable_distribution.items())[0:15])
dict(list(vulnerable_distribution.items())[0:15])
Out[13]:
{0: {'male': 0.0, 'female': 0.0, 'other': 0.0}, 1: {'male': 0.01, 'female': 0.01, 'other': 0.01}, 2: {'male': 0.02, 'female': 0.02, 'other': 0.02}, 3: {'male': 0.03, 'female': 0.03, 'other': 0.03}, 4: {'male': 0.04, 'female': 0.04, 'other': 0.04}, 5: {'male': 0.05, 'female': 0.05, 'other': 0.05}, 6: {'male': 0.06, 'female': 0.06, 'other': 0.06}, 7: {'male': 0.07, 'female': 0.07, 'other': 0.07}, 8: {'male': 0.08, 'female': 0.08, 'other': 0.08}, 9: {'male': 0.09, 'female': 0.09, 'other': 0.09}, 10: {'male': 0.1, 'female': 0.1, 'other': 0.1}, 11: {'male': 0.11, 'female': 0.11, 'other': 0.11}, 12: {'male': 0.12, 'female': 0.12, 'other': 0.12}, 13: {'male': 0.13, 'female': 0.13, 'other': 0.13}, 14: {'male': 0.14, 'female': 0.14, 'other': 0.14}}
In [14]:
Copied!
discrete_joint_distribution_sampler(Bobby, vulnerable_mapping, vulnerable_distribution)
discrete_joint_distribution_sampler(Bobby, vulnerable_mapping, vulnerable_distribution)
Out[14]:
0.06
Household Quarantine¶
Probabilistically apply quarantine to a household (remove all activities - stay at home)
Person-based, all people equal¶
If you have a probability of any person having to be quarantined
In [15]:
Copied!
policy_quarantine = HouseholdPolicy(
RemoveActivity(["work", "health", "leisure", "escort", "shop", "education"]),
PersonProbability(0.0000000000001),
)
policy_quarantine = HouseholdPolicy(
RemoveActivity(["work", "health", "leisure", "escort", "shop", "education"]),
PersonProbability(0.0000000000001),
)
In [16]:
Copied!
q_pop = apply_policies(population, [policy_quarantine])
print_simple_stats(q_pop)
q_pop = apply_policies(population, [policy_quarantine])
print_simple_stats(q_pop)
Population total time at home: 74.58 hours Population total travel time: 9.67 hours
In [17]:
Copied!
q_pop.households["Smith"].plot()
q_pop.households["Smith"].plot()
In [18]:
Copied!
q_pop.households["Jones"].plot()
q_pop.households["Jones"].plot()
Using joint distribution¶
In [19]:
Copied!
policy_quarantine = HouseholdPolicy(
RemoveActivity(["work", "health", "escort", "leisure", "shop", "education"]),
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": vulnerable_mapping, "distribution": vulnerable_distribution},
),
)
policy_quarantine = HouseholdPolicy(
RemoveActivity(["work", "health", "escort", "leisure", "shop", "education"]),
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": vulnerable_mapping, "distribution": vulnerable_distribution},
),
)
In [20]:
Copied!
q_pop = apply_policies(population, [policy_quarantine])
print_simple_stats(q_pop)
q_pop = apply_policies(population, [policy_quarantine])
print_simple_stats(q_pop)
Population total time at home: 41.42 hours Population total travel time: 5.92 hours
In [21]:
Copied!
q_pop.households["Jones"].plot()
q_pop.households["Jones"].plot()
Chaining probabilities¶
In [22]:
Copied!
policy_quarantine = HouseholdPolicy(
RemoveActivity(["work", "health", "escort", "leisure", "shop", "education"]),
[
PersonProbability(0.5),
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": vulnerable_mapping, "distribution": vulnerable_distribution},
),
],
)
policy_quarantine = HouseholdPolicy(
RemoveActivity(["work", "health", "escort", "leisure", "shop", "education"]),
[
PersonProbability(0.5),
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": vulnerable_mapping, "distribution": vulnerable_distribution},
),
],
)
In [23]:
Copied!
q_pop = apply_policies(population, [policy_quarantine])
print_simple_stats(q_pop)
q_pop = apply_policies(population, [policy_quarantine])
print_simple_stats(q_pop)
Population total time at home: 43.67 hours Population total travel time: 1.67 hours
In [24]:
Copied!
q_pop.households["Jones"].plot()
q_pop.households["Jones"].plot()
Remove Education¶
Probabilistically remove education activities from a person and escort from people in the same household
In [25]:
Copied!
edu_mapping = ["job"]
edu_distribution = {"employed": 0, "unemployed": 0, "education": 1}
edu_mapping = ["job"]
edu_distribution = {"employed": 0, "unemployed": 0, "education": 1}
In [26]:
Copied!
key_mapping = ["key_worker"]
key_distribution = {True: 0, False: 1}
key_mapping = ["key_worker"]
key_distribution = {True: 0, False: 1}
In [27]:
Copied!
edu_distribution, key_distribution
edu_distribution, key_distribution
Out[27]:
({'employed': 0, 'unemployed': 0, 'education': 1}, {True: 0, False: 1})
In [28]:
Copied!
policy_remove_education_and_escort = HouseholdPolicy(
RemoveActivity(["education", "escort"]),
[
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": edu_mapping, "distribution": edu_distribution},
),
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": key_mapping, "distribution": key_distribution},
),
],
)
policy_remove_education_and_escort = HouseholdPolicy(
RemoveActivity(["education", "escort"]),
[
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": edu_mapping, "distribution": edu_distribution},
),
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": key_mapping, "distribution": key_distribution},
),
],
)
In [29]:
Copied!
edu_pop = apply_policies(population, [policy_remove_education_and_escort])
print_simple_stats(edu_pop)
edu_pop = apply_policies(population, [policy_remove_education_and_escort])
print_simple_stats(edu_pop)
Population total time at home: 84.25 hours Population total travel time: 7.83 hours
In [30]:
Copied!
edu_pop.households["Smith"].people["Bradly"].plan.print()
edu_pop.households["Smith"].people["Bradly"].plan.print()
0: Activity(act:home, location:a, time:00:00:00 --> 08:00:00, duration:8:00:00) 1: Leg(mode:walk, area:a --> b, time:08:00:00 --> 08:20:00, duration:0:20:00) 2: Activity(act:work, location:b, time:08:20:00 --> 09:20:00, duration:1:00:00) 3: Leg(mode:pt, area:b --> c, time:09:20:00 --> 09:40:00, duration:0:20:00) 4: Activity(act:work, location:b, time:09:40:00 --> 11:20:00, duration:1:40:00) 5: Leg(mode:pt, area:b --> c, time:11:20:00 --> 11:40:00, duration:0:20:00) 6: Activity(act:work, location:b, time:11:40:00 --> 13:20:00, duration:1:40:00) 7: Leg(mode:pt, area:b --> c, time:13:20:00 --> 13:40:00, duration:0:20:00) 8: Activity(act:leisure, location:c, time:13:40:00 --> 14:50:00, duration:1:10:00) 9: Leg(mode:pt, area:c --> a, time:14:50:00 --> 15:10:00, duration:0:20:00) 10: Activity(act:home, location:a, time:15:10:00 --> 16:50:00, duration:1:40:00) 11: Leg(mode:car, area:a --> b, time:16:50:00 --> 17:10:00, duration:0:20:00) 12: Activity(act:shop_1, location:b, time:17:10:00 --> 17:40:00, duration:0:30:00) 13: Leg(mode:car, area:b --> b, time:17:40:00 --> 17:50:00, duration:0:10:00) 14: Activity(act:shop_2, location:b, time:17:50:00 --> 18:40:00, duration:0:50:00) 15: Leg(mode:car, area:b --> a, time:18:40:00 --> 19:00:00, duration:0:20:00) 16: Activity(act:home, location:a, time:19:00:00 --> 00:00:00, duration:5:00:00)
In [31]:
Copied!
q_pop.households["Smith"].plot()
q_pop.households["Smith"].plot()
In [32]:
Copied!
edu_pop.households["Smith"].plot()
edu_pop.households["Smith"].plot()
In [33]:
Copied!
edu_pop.households["Jones"].plot()
edu_pop.households["Jones"].plot()
In [34]:
Copied!
edu_pop.households["Jones"].people["Bridget"].plan.print()
edu_pop.households["Jones"].people["Bridget"].plan.print()
0: Activity(act:home, location:a, time:00:00:00 --> 08:00:00, duration:8:00:00) 1: Leg(mode:walk, area:a --> b, time:08:00:00 --> 08:05:00, duration:0:05:00) 2: Activity(act:escort, location:b, time:08:05:00 --> 10:30:00, duration:2:25:00) 3: Leg(mode:pt, area:b --> c, time:10:30:00 --> 11:00:00, duration:0:30:00) 4: Activity(act:work, location:c, time:11:00:00 --> 16:00:00, duration:5:00:00) 5: Leg(mode:pt, area:c --> a, time:16:00:00 --> 16:20:00, duration:0:20:00) 6: Activity(act:home, location:a, time:16:20:00 --> 17:20:00, duration:1:00:00) 7: Leg(mode:pt, area:c --> a, time:17:20:00 --> 17:50:00, duration:0:30:00) 8: Activity(act:shop, location:a, time:17:50:00 --> 18:30:00, duration:0:40:00) 9: Leg(mode:pt, area:c --> a, time:18:30:00 --> 18:50:00, duration:0:20:00) 10: Activity(act:home, location:a, time:18:50:00 --> 00:00:00, duration:5:10:00)
PersonAttributeFilter¶
You can also use the modify.PersonAttributeFilter
to only affect people with certain attributes.
In [35]:
Copied!
def condition_job_education(val):
return val == "education"
policy_remove_education_and_escort = HouseholdPolicy(
RemoveActivity(["education", "escort"]),
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": key_mapping, "distribution": key_distribution},
),
PersonAttributeFilter({"job": condition_job_education}),
)
def condition_job_education(val):
return val == "education"
policy_remove_education_and_escort = HouseholdPolicy(
RemoveActivity(["education", "escort"]),
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": key_mapping, "distribution": key_distribution},
),
PersonAttributeFilter({"job": condition_job_education}),
)
In [36]:
Copied!
apply_policies(population, [policy_remove_education_and_escort]).households["Smith"].plot()
apply_policies(population, [policy_remove_education_and_escort]).households["Smith"].plot()
In [37]:
Copied!
policy_remove_education_and_escort = HouseholdPolicy(
RemoveActivity(["education", "escort"]),
[
ActivityProbability(["education"], 0.9999),
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": edu_mapping, "distribution": edu_distribution},
),
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": key_mapping, "distribution": key_distribution},
),
],
)
policy_remove_education_and_escort = HouseholdPolicy(
RemoveActivity(["education", "escort"]),
[
ActivityProbability(["education"], 0.9999),
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": edu_mapping, "distribution": edu_distribution},
),
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": key_mapping, "distribution": key_distribution},
),
],
)
In [38]:
Copied!
apply_policies(population, [policy_remove_education_and_escort]).households["Smith"].plot()
apply_policies(population, [policy_remove_education_and_escort]).households["Smith"].plot()
Remove Leisure Activities¶
Remove all leisure activities
In [39]:
Copied!
policy_remove_leisure = PersonPolicy(RemoveActivity(["leisure"]), PersonProbability(1.0))
policy_remove_leisure = PersonPolicy(RemoveActivity(["leisure"]), PersonProbability(1.0))
In [40]:
Copied!
lei_pop = apply_policies(population, [policy_remove_leisure])
print_simple_stats(lei_pop)
lei_pop = apply_policies(population, [policy_remove_leisure])
print_simple_stats(lei_pop)
Population total time at home: 76.08 hours Population total travel time: 9.33 hours
In [41]:
Copied!
lei_pop.households["Smith"].plot()
lei_pop.households["Smith"].plot()
Unemployment/Furlough¶
Probabilistically remove all work activities from a person
In [42]:
Copied!
key_mapping = ["key_worker"]
key_distribution = {True: 0, False: 1}
key_mapping = ["key_worker"]
key_distribution = {True: 0, False: 1}
In [43]:
Copied!
policy_unemployment_and_furlough = PersonPolicy(
RemoveActivity(["work"]),
[
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": key_mapping, "distribution": key_distribution},
)
],
)
policy_unemployment_and_furlough = PersonPolicy(
RemoveActivity(["work"]),
[
PersonProbability(
discrete_joint_distribution_sampler,
{"mapping": key_mapping, "distribution": key_distribution},
)
],
)
In [44]:
Copied!
fur_pop = apply_policies(population, [policy_unemployment_and_furlough])
print_simple_stats(fur_pop)
fur_pop = apply_policies(population, [policy_unemployment_and_furlough])
print_simple_stats(fur_pop)
Population total time at home: 65.33 hours Population total travel time: 8.58 hours
In [45]:
Copied!
fur_pop.households["Smith"].plot()
fur_pop.households["Smith"].plot()
In [46]:
Copied!
fur_pop.households["Jones"].plot()
fur_pop.households["Jones"].plot()
In [47]:
Copied!
policy_reduced_work = ActivityPolicy(RemoveActivity(["work"]), ActivityProbability(["work"], 0.5))
apply_policies(population, [policy_reduced_work]).households["Smith"].plot()
policy_reduced_work = ActivityPolicy(RemoveActivity(["work"]), ActivityProbability(["work"], 0.5))
apply_policies(population, [policy_reduced_work]).households["Smith"].plot()
Remove Shopping¶
Probabilistically remove individual shopping activities from a person
In [48]:
Copied!
policy_remove_shopping = PersonPolicy(RemoveActivity(["shop"]), ActivityProbability(["shop"], 1.0))
policy_remove_shopping = PersonPolicy(RemoveActivity(["shop"]), ActivityProbability(["shop"], 1.0))
In [49]:
Copied!
shop_pop = apply_policies(population, [policy_remove_shopping])
print_simple_stats(shop_pop)
shop_pop = apply_policies(population, [policy_remove_shopping])
print_simple_stats(shop_pop)
Population total time at home: 76.08 hours Population total travel time: 8.83 hours
In [50]:
Copied!
shop_pop.households["Jones"].people["Bridget"].plot()
shop_pop.households["Jones"].people["Bridget"].plot()
Reducing shared activities¶
In [51]:
Copied!
policy_reduce_shopping_activities = HouseholdPolicy(
ReduceSharedActivity(["shop_1", "shop_2", "shop"]),
ActivityProbability(["shop_1", "shop_2", "shop"], 1.0),
)
policy_reduce_shopping_activities = HouseholdPolicy(
ReduceSharedActivity(["shop_1", "shop_2", "shop"]),
ActivityProbability(["shop_1", "shop_2", "shop"], 1.0),
)
In [52]:
Copied!
population["Smith"].shared_activities()
population["Smith"].shared_activities()
Out[52]:
[<pam.activity.Activity at 0x137e48670>, <pam.activity.Activity at 0x137e359c0>, <pam.activity.Activity at 0x137e36170>, <pam.activity.Activity at 0x137e48d30>, <pam.activity.Activity at 0x137e48c70>]
In [53]:
Copied!
shop_reduce_pop = apply_policies(population, [policy_reduce_shopping_activities])
print_simple_stats(shop_reduce_pop)
shop_reduce_pop = apply_policies(population, [policy_reduce_shopping_activities])
print_simple_stats(shop_reduce_pop)
Population total time at home: 76.75 hours Population total travel time: 8.83 hours
In [54]:
Copied!
shop_reduce_pop.households["Smith"].plot()
shop_reduce_pop.households["Smith"].plot()
In [55]:
Copied!
shop_reduce_pop.households["Smith"].people["Bobby"].print()
shop_reduce_pop.households["Smith"].people["Bobby"].print()
Person: Bobby {'age': 6, 'job': 'education', 'gender': 'male', 'key_worker': False} 0: Activity(act:home, location:a, time:00:00:00 --> 08:00:00, duration:8:00:00) 1: Leg(mode:walk, area:a --> b, time:08:00:00 --> 08:25:00, duration:0:25:00) 2: Activity(act:education, location:b, time:08:25:00 --> 15:55:00, duration:7:30:00) 3: Leg(mode:walk, area:b --> c, time:15:55:00 --> 16:30:00, duration:0:35:00) 4: Activity(act:home, location:a, time:16:30:00 --> 00:00:00, duration:7:30:00)
Moving Shopping tours¶
In [56]:
Copied!
policy_move_shopping_tours = PersonPolicy(
MoveActivityTourToHomeLocation(["shop_1", "shop_2"]),
[ActivityProbability(["shop_1", "shop_2"], 1.0)],
)
policy_move_shopping_tours = PersonPolicy(
MoveActivityTourToHomeLocation(["shop_1", "shop_2"]),
[ActivityProbability(["shop_1", "shop_2"], 1.0)],
)
In [57]:
Copied!
shop_tour_pop = apply_policies(population, [policy_move_shopping_tours])
print_simple_stats(shop_tour_pop)
shop_tour_pop = apply_policies(population, [policy_move_shopping_tours])
print_simple_stats(shop_tour_pop)
Population total time at home: 74.58 hours Population total travel time: 9.67 hours
In [58]:
Copied!
shop_tour_pop.households["Smith"].plot()
shop_tour_pop.households["Smith"].plot()
In [59]:
Copied!
# above is equivalent to
policy_move_shopping_tours = PersonPolicy(
MoveActivityTourToHomeLocation(["shop_1", "shop_2"]),
[ActivityProbability(["shop_1"], 1.0), ActivityProbability(["shop_2"], 1.0)],
)
# above is equivalent to
policy_move_shopping_tours = PersonPolicy(
MoveActivityTourToHomeLocation(["shop_1", "shop_2"]),
[ActivityProbability(["shop_1"], 1.0), ActivityProbability(["shop_2"], 1.0)],
)
In [60]:
Copied!
shop_tour_pop = apply_policies(population, [policy_move_shopping_tours])
print_simple_stats(shop_tour_pop)
shop_tour_pop = apply_policies(population, [policy_move_shopping_tours])
print_simple_stats(shop_tour_pop)
Population total time at home: 74.58 hours Population total travel time: 9.67 hours
In [61]:
Copied!
shop_tour_pop.households["Smith"].plot()
shop_tour_pop.households["Smith"].plot()
In [62]:
Copied!
shop_tour_pop.households["Smith"].people["Bradly"].print()
shop_tour_pop.households["Smith"].people["Bradly"].print()
Person: Bradly {'age': 40, 'job': 'employed', 'gender': 'male', 'key_worker': True} 0: Activity(act:home, location:a, time:00:00:00 --> 08:00:00, duration:8:00:00) 1: Leg(mode:walk, area:a --> b, time:08:00:00 --> 08:20:00, duration:0:20:00) 2: Activity(act:escort, location:b, time:08:20:00 --> 08:30:00, duration:0:10:00) 3: Leg(mode:pt, area:b --> b, time:08:30:00 --> 09:00:00, duration:0:30:00) 4: Activity(act:work, location:b, time:09:00:00 --> 10:00:00, duration:1:00:00) 5: Leg(mode:pt, area:b --> c, time:10:00:00 --> 10:20:00, duration:0:20:00) 6: Activity(act:work, location:b, time:10:20:00 --> 12:00:00, duration:1:40:00) 7: Leg(mode:pt, area:b --> c, time:12:00:00 --> 12:20:00, duration:0:20:00) 8: Activity(act:work, location:b, time:12:20:00 --> 14:00:00, duration:1:40:00) 9: Leg(mode:pt, area:b --> c, time:14:00:00 --> 14:20:00, duration:0:20:00) 10: Activity(act:leisure, location:c, time:14:20:00 --> 15:30:00, duration:1:10:00) 11: Leg(mode:pt, area:c --> b, time:15:30:00 --> 15:50:00, duration:0:20:00) 12: Activity(act:escort, location:b, time:15:50:00 --> 16:00:00, duration:0:10:00) 13: Leg(mode:walk, area:b --> a, time:16:00:00 --> 16:20:00, duration:0:20:00) 14: Activity(act:home, location:a, time:16:20:00 --> 18:00:00, duration:1:40:00) 15: Leg(mode:walk, area:a --> a, time:18:00:00 --> 18:20:00, duration:0:20:00) 16: Activity(act:shop_1, location:a, time:18:20:00 --> 18:50:00, duration:0:30:00) 17: Leg(mode:walk, area:a --> a, time:18:50:00 --> 19:00:00, duration:0:10:00) 18: Activity(act:shop_2, location:a, time:19:00:00 --> 19:50:00, duration:0:50:00) 19: Leg(mode:walk, area:a --> a, time:19:50:00 --> 20:10:00, duration:0:20:00) 20: Activity(act:home, location:a, time:20:10:00 --> 00:00:00, duration:3:50:00)
All together now!¶
In [63]:
Copied!
all_together_pop = apply_policies(
population,
[
policy_quarantine,
policy_remove_education_and_escort,
policy_remove_leisure,
policy_unemployment_and_furlough,
policy_remove_shopping,
],
)
print_simple_stats(all_together_pop)
all_together_pop = apply_policies(
population,
[
policy_quarantine,
policy_remove_education_and_escort,
policy_remove_leisure,
policy_unemployment_and_furlough,
policy_remove_shopping,
],
)
print_simple_stats(all_together_pop)
Population total time at home: 83.67 hours Population total travel time: 4.25 hours