Read Models
Here we read the target product model, prod, and its two
factor models, si and age, into R.
prod = Compartmental(prod_dir)
si = Compartmental(si_dir)
age = Compartmental(age_dir)The goal of this document is to multiply si and
age to see if we can recover our target,
prod.
Types of Variables
Each Model has the following set of variables.
si$variables$all()
#> Epi
#> 1 S
#> 2 I
#> 3 infection
#> 4 recovery
#> 5 transmission
age$variables$all()
#> Age
#> 1 young
#> 2 old
#> 3 aging
prod$variables$all()
#> Epi Age
#> 1 S young
#> 2 S old
#> 3 I young
#> 4 I old
#> 5 infection young
#> 6 infection old
#> 7 recovery young
#> 8 recovery old
#> 9 S aging
#> 10 I aging
#> 11 transmissionThese variables are categorized into various types that distinguish their roles in the dynamics.
The state variables represent the nodes in the compartmental diagram.
si$variables$state()
#> Epi
#> 1 S
#> 2 I
age$variables$state()
#> Age
#> 1 young
#> 2 old
prod$variables$state()
#> Epi Age
#> 1 S young
#> 2 I young
#> 3 S old
#> 4 I oldThe model definition also recognizes two subsets of these states – infectious and infected – which in these models are identical because there is no latency period.
si$variables$infectious_state()
#> Epi
#> 1 I
age$variables$infectious_state()
#> [1] Age
#> <0 rows> (or 0-length row.names)
prod$variables$infectious_state()
#> Epi Age
#> 1 I young
#> 2 I old
si$variables$infected_state()
#> Epi
#> 1 I
age$variables$infected_state()
#> [1] Age
#> <0 rows> (or 0-length row.names)
prod$variables$infected_state()
#> Epi Age
#> 1 I young
#> 2 I oldNote that the age model has no infection, and so these
types of states are not present.
The flow variables represent the flows between states in the compartmental diagram.
si$variables$flow()
#> Epi
#> 1 infection
#> 2 recovery
age$variables$flow()
#> Age
#> 1 aging
prod$variables$flow()
#> Epi Age
#> 1 infection young
#> 2 infection old
#> 3 recovery young
#> 4 recovery old
#> 5 S aging
#> 6 I agingAnd a subset of flows are those involved in infection.
si$variables$infection_flow()
#> Epi
#> 1 infection
age$variables$infection_flow()
#> [1] Age
#> <0 rows> (or 0-length row.names)
prod$variables$infection_flow()
#> Epi Age
#> 1 infection young
#> 2 infection oldBut what about other variables that are neither states nor flows? We can list these other variables.
si$variables$other()
#> Epi
#> 1 S
#> 2 I
#> 3 infection
#> 4 recovery
#> 5 transmission
age$variables$other()
#> Warning in age$variables$other():
#> There are no other variables in this model
#> except for state and flow variables.
#> NULL
prod$variables$other()
#> Epi Age
#> 1 S young
#> 2 S old
#> 3 I young
#> 4 I old
#> 5 infection young
#> 6 infection old
#> 7 recovery young
#> 8 recovery old
#> 9 S aging
#> 10 I aging
#> 11 transmissionWe see that the only variables in the age model are
states and flows. But we also see that the si and
prod models have a transmission variable.
Multiplying Variables
We can recover each kind of variable in the prod model
by operating on the factor models, si and age.
The state variables in prod are a Cartesian product of the
state variables in si and age.
cartesian(si$variables$state(), age$variables$state())
#> Epi Age
#> 1 S young
#> 2 I young
#> 3 S old
#> 4 I old
prod$variables$state()
#> Epi Age
#> 1 S young
#> 2 I young
#> 3 S old
#> 4 I oldThe flow variables in prod are a union of two Cartesian
products.
union_vars(
cartesian(si$variables$flow(), age$variables$state()),
cartesian(si$variables$state(), age$variables$flow())
)
#> Epi Age
#> 1 infection young
#> 2 recovery young
#> 3 infection old
#> 4 recovery old
#> 5 S aging
#> 6 I aging
prod$variables$flow()
#> Epi Age
#> 1 infection young
#> 2 infection old
#> 3 recovery young
#> 4 recovery old
#> 5 S aging
#> 6 I agingThe other variable in prod, transmission.,
can be obtained by taking the union of the other variables in
si and age.
union_vars(si$variables$other(), age$variables$other())
#> Warning in age$variables$other():
#> There are no other variables in this model
#> except for state and flow variables.
#> Epi
#> 1 S
#> 2 I
#> 3 infection
#> 4 recovery
#> 5 transmission
prod$variables$other()
#> Epi Age
#> 1 S young
#> 2 S old
#> 3 I young
#> 4 I old
#> 5 infection young
#> 6 infection old
#> 7 recovery young
#> 8 recovery old
#> 9 S aging
#> 10 I aging
#> 11 transmissionPutting this all together we have.
union_vars(
cartesian(si$variables$state(), age$variables$state()),
cartesian(si$variables$flow(), age$variables$state()),
cartesian(si$variables$state(), age$variables$flow()),
si$variables$other(),
age$variables$other()
)
#> Warning in age$variables$other():
#> There are no other variables in this model
#> except for state and flow variables.
#> Epi Age
#> 1 S young
#> 2 I young
#> 3 S old
#> 4 I old
#> 5 infection young
#> 6 recovery young
#> 7 infection old
#> 8 recovery old
#> 9 S aging
#> 10 I aging
#> 11 S
#> 12 I
#> 13 infection
#> 14 recovery
#> 15 transmission
prod$variables$all()
#> Epi Age
#> 1 S young
#> 2 S old
#> 3 I young
#> 4 I old
#> 5 infection young
#> 6 infection old
#> 7 recovery young
#> 8 recovery old
#> 9 S aging
#> 10 I aging
#> 11 transmissionMultiplying Flows
si_flows = si$flows()
age_flows = age$flows()
si_flows$from_to_partition = "Age"
si_flows$from_flow_partition = "Age"
age_flows$from_to_partition = "Epi"
age_flows$from_flow_partition = "Epi"
prod_flows = rbind(si_flows, age_flows)
prod_flows
#> from to flow type from_partition to_partition flow_partition
#> 1 S I infection per_capita Epi Epi Epi
#> 2 I S recovery per_capita Epi Epi Epi
#> 3 young old aging per_capita Age Age Age
#> from_to_partition from_flow_partition to_flow_partition
#> 1 Age Age Null
#> 2 Age Age Null
#> 3 Epi Epi Null
prod$flows()
#> from to flow type from_partition to_partition flow_partition
#> 1 S I infection per_capita Epi Epi Epi
#> 2 I S recovery per_capita Epi Epi Epi
#> 3 young old aging per_capita Age Age Age
#> from_to_partition from_flow_partition to_flow_partition
#> 1 Age Age Null
#> 2 Age Age Null
#> 3 Epi Epi NullRoadmap
Here we outline what development needs to take place in order to complete this document.
- Add transmission matrix step to
StandardExprs - Align transmission matrix specifications
- Pass required indices for lining up the transmission matrix to the flow vector and state vector.
Scratch
prod$labels$state()
#> [1] "S.young" "I.young" "S.old" "I.old"
prod$labels$flow()
#> [1] "infection.young" "infection.old" "recovery.young" "recovery.old"
#> [5] "S.aging" "I.aging"
si$variables$all()
#> Epi
#> 1 S
#> 2 I
#> 3 infection
#> 4 recovery
#> 5 transmission
si$flows_expanded()
#> from to flow type
#> 1 S I infection per_capita
#> 2 I S recovery per_capita
age$variables$all()
#> Age
#> 1 young
#> 2 old
#> 3 aging
age$flows_expanded()
#> from to flow type
#> 1 young old aging per_capita
prod$variables()
prod$flows_expanded()
UserExpr(prod)$expand_vector_expressions()
prod$expr_list()$print_exprs()Simulating from the Product Model
- Do the model definition by hand so that it works
- Specify numerical inputs
- Run the the simulation
- Verify the results
Steps in Taking Model Products
We have a nested set of operations.
- Cartesian product of state variables
- Cartesian product of state and flow variables (once in each direction)
- Matrix product of transmission matrix (lots of variations available here, for example Kronecker product)
A modeller may choose to do just 1, just 1-2, or 1-3.