The goal of this notebook is to present the basics of the simulation in our package.
The goal of this notebook is to present the basics of the simulation in our package.
Let's get familiar with the package. First, we shoud load it.
Let's get familiar with the package. First, we shoud load it.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
usingMarkovProcesses
usingMarkovProcesses
```
```
%% Output
┌ Info: Precompiling MarkovProcesses [top-level]
└ @ Base loading.jl:1260
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Models
## Models
In this package, we are focused on Continuous-Time Markov Chains models that can be described by Chemical Reaction Networks.
In this package, we are focused on Continuous-Time Markov Chains models that can be described by Chemical Reaction Networks.
Let's simulate our first model. We consider the famous SIR epidemiology model described by two reactions:
Let's simulate our first model. We consider the famous SIR epidemiology model described by two reactions:
$$
$$
Infection: S + I \xrightarrow{k_i} 2I \\
Infection: S + I \xrightarrow{k_i} 2I \\
Recovery: I \xrightarrow{k_r} R
Recovery: I \xrightarrow{k_r} R
$$
$$
In the MarkovProcesses package, models are objects that can be instantiatied and their types all derived from the abstract type `Model`. Let's load the SIR model.
In the MarkovProcesses package, models are objects that can be instantiatied and their types all derived from the abstract type `Model`. Let's load the SIR model.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
load_model("SIR")
load_model("SIR")
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
This function searchs a model file called SIR.jl in the models/ directory (at the root of the package). This files creates the necessary resources to create a `ContinuousTimeModel` and store it in a variable called SIR.
This function searchs a model file called SIR.jl in the models/ directory (at the root of the package). This files creates the necessary resources to create a `ContinuousTimeModel` and store it in a variable called SIR.
`simulate` returns a path which type is derived from `AbstractTrajectory`. It can be either an object of type `Trajectory` for models `::ContinuousTimeModel` or `SynchronizedTrajectory` for models that includes an automaton (but this is the subject of another notebook). It is easy to access the values of a trajectory:
`simulate` returns a path which type is derived from `AbstractTrajectory`. It can be either an object of type `Trajectory` for models `::ContinuousTimeModel` or `SynchronizedTrajectory` for models that includes an automaton (but this is the subject of another notebook). It is easy to access the values of a trajectory:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
@showσ[3]# the third state of the trajectory
@showσ[3]# the third state of the trajectory
@showlength_states(σ)# number of states
@showlength_states(σ)# number of states
@showσ["I",4]# Fourth value of the variable I
@showσ[:I,4]# Fourth value of the variable I
@showσ.I[4]# Fourth value of the variable I
@showget_state_from_time(σ,2.3)
@showget_state_from_time(σ,2.3)
```
```
%% Output
%% Output
σ[3] = [7]
σ[3] = [7]
length_states(σ) = 196
length_states(σ) = 196
σ["I", 4] = 8
σ[:I, 4] = 8
get_state_from_time(σ, 2.3) = [76]
σ.I[4] = 8
get_state_from_time(σ, 2.3) = [83]
1-element view(::Array{Int64,2}, 84, :) with eltype Int64:
1-element Array{Int64,1}:
76
83
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
The SIR object includes an observation model symbolized by the vector `SIR.g`. Even if the variables of the model are ["S", "I", "R"], only "I" will be observed.
The SIR object includes an observation model symbolized by the vector `SIR.g`. Even if the variables of the model are ["S", "I", "R"], only "I" will be observed.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
@showSIR.map_var_idx
@showSIR.map_var_idx
@showSIR.g
@showSIR.g
@showsize(σ.values),length(σ["I"])# Only one column which corresponds to the I variable
@showsize(σ.values),length(σ[:I])# Only one column which corresponds to the I variable
A useful feature is that we can change the preallocation of the memory size during the simulation in order to gain computational performance.
A useful feature is that we can change the preallocation of the memory size during the simulation in order to gain computational performance.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
load_model("ER")
load_model("ER")
@showER.buffer_size
@showER.buffer_size
@timevσ=simulate(ER)
@timevσ=simulate(ER)
@showlength_states(σ)
@showlength_states(σ)
```
```
%% Output
%% Output
ER.buffer_size = 10
ER.buffer_size = 10
0.070949 seconds (130.66 k allocations: 7.141 MiB)
0.036003 seconds (54.02 k allocations: 2.972 MiB)
elapsed time (ns): 70948721
elapsed time (ns): 36002718
bytes allocated: 7488324
bytes allocated: 3115887
pool allocs: 130585
pool allocs: 53998
non-pool GC allocs:73
non-pool GC allocs:18
length_states(σ) = 407
length_states(σ) = 385
407
385
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
By default, the buffer size is 10. It means that a matrix of size 10 is allocated anf filled. When it is full, another matrix of size 10 is allocated. But if you know that your simulations will have a certain number of states, you can change the buffer size. It can make a big difference in terms of performance.
By default, the buffer size is 10. It means that a matrix of size 10 is allocated anf filled. When it is full, another matrix of size 10 is allocated. But if you know that your simulations will have a certain number of states, you can change the buffer size. It can make a big difference in terms of performance.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
ER.buffer_size=100
ER.buffer_size=100
@timevσ2=simulate(ER)
@timevσ2=simulate(ER)
@showlength_states(σ2)
@showlength_states(σ2)
```
```
%% Output
%% Output
0.000232 seconds (3.02 k allocations: 212.000 KiB)