polyPdmpModel-class: Class polyPdmpModel

Description Slots Ratepolys Dynpolys Note Examples

Description

An S4 class to represent polynomial piecewisc deterministic markov processes (polynomial PDMPs). These processes are PDMPs with polynomial rate functions and polynomial dynamics. This makes it possible to approximate the moments of the process without the need of simulation, see momApp. On the downside, simulations take much longer than simulations of the same PDMP represented as pdmpModel.
The class is based on the pdmpModel class of package pdmpsim but introduces additional slots that replace the slots dynfunc and ratefunc, namely dynpolys, dynsprays, ratepolys and ratesprays. Only slots dynpolys and ratepolys have to be defined by the user. The other slots (including the still existing dynfunc and ratefunc) will be set automatically. To represent polynomials in R, package spray is used, which stores the coefficient matrices of polynomials as sparse arrays.

Slots

dynpolys

an object of class language, more precisely a quoted list. Every element of this list contains the ODE for one of the continous variables. These ODEs are given as spray objects and can be defined in two different ways. See section dynpolys for further information.

ratepolys

an object of class language, more precisely a quoted list.This list contains the rates for every jumptype. Every element is itself a list, its length is determined by the number of different values of the discrete variable (length(discStates(obj)[[1]])). See section ratepolys for further information.

dynsprays

a nested list of spray objects. This slot is generated automatically out of slot dynpolys. Do not edit it by hand.

ratesprays

a nested list of spray objects. This slot is generated automatically out of slot ratepolys. Do not edit it by hand.

Ratepolys

Slot ratepolys is a quted list. The length of the list determines the number of existing jumptypes (jumptypes are used in slot jumpfunc to determine the next state the process jumps too). It contains the rates determining the probability of a jump to be of type 1, 2, and so on. The rates are polynomials that usually depend on the value of the discrete variable at the time when the jump occurs. They are given as a list of spray objects or numbers, for every possible discrete state separately (in the same order as the states are given in slot discStates).

For example, let's assume that we have three different jumptypes and a discrete variable d that can take the values 0 or 1 (so slot discStates would be defined as list(d = 0:1)). Then slot ratepolys will be given as follows:

1
2
3
4
5
quote(list(
  list(rate of jtype 1 with d = 0, rate of jtype 1 with d = 1),
  list(rate of jtype 2 with d = 0, rate of jtype 2 with d = 1),
  list(rate of jtype 3 with d = 0, rate of jtype 3 with d = 1),
))

Every rate of jtype i with d = j is a spray object, for example linear(1:3).

Dynpolys

Slot dynpolys is a quoted list. The length of the list equals the number of continuous variables of the model. Every element contains the ODE for one of the continuous variables, given in the same order as in slot init and given as spray objects.
The ODEs usually depend on the discrete variable, lets call it d. An example would be df/dt = -3f if d = 0 and df/dt = -3f + 1 if d = 1. Here, f is the only continous variable of the model. Every ODE can be defined in two different ways:

In our example, we get

The last variant is only possible, because in this example we have the possiblity to write both ODEs in one formula: df/dt = -3f + d.

Note

Currently, all methods only work for PDMPs with a single discrete variable. Some methods require the discrete variable to be the last entry in slot init. Note that it is always possible to redefine a PDMP with multiple discrete variables in a way that only one discrete variable is necessary.

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#### a simple PDMP ####

#------ pdmpModel version -----
simplePdmp <- new("pdmpModel",
                  descr = "A simple PDMP",
                  init = c(f = 0, d = 0),
                  discStates = list(d = c(-1, 0, 1)),
                  times = c(from = 0, to = 10, by = 0.01),
                  dynfunc = function(t, x, parms) c(x["d"], 0),
                  ratefunc = function(t, x, parms) c(1+x["d"], 1-x["d"]),
                  jumpfunc = function(t, x, parms, jtype){
                    c(0, switch(jtype, x["d"]-1, x["d"]+1))
                  }
)

#------ polyPdmpModel version -----
library("spray")
simplePoly <- new("polyPdmpModel",
                  descr = "polyModel with two jumptypes",
                  init = c(f = 0, d = 0),
                  times = c(from = 0, to = 10, by = 0.01),
                  discStates = list(d = -1:1),
                  dynpolys = quote(list(
                    list(overall = lone(2,2)) # variant 2
                  )),
                  ratepolys = quote(list(
                    list(0, 1, 2), # first jumptype
                    list(2, 1, 0)  # second jumptype
                  )),
                  jumpfunc = function(t, x, parms, jtype){
                    c(0, switch(jtype, x["d"]-1, x["d"]+1))
                  }
)

#------- comparison of the models --------------
identical(sim(simplePoly, outSlot = FALSE, seed = 5),
          sim(simplePdmp, outSlot = FALSE, seed = 5))

#### the toggleSwitch model ####

#------ pdmpModel version -----
genePdmpT <- new("pdmpModel", 
                 descr = "toggleswitch with two promotors",
                 parms = list(bA = 0.5, bB = 0.5, aA = 2, aB = 4, 
                               k01A = 0.5, k10A = 2, k01B = 0.3, k10B = 3),
                 init = c(fA = 0.5, fB = 0.5, d = 4),
                 discStates = list(d = 1:4),
                 dynfunc = function(t, x, parms) {
                   df <- with(as.list(c(x, parms)), 
                              c(-bA * fA, -bB * fB) + switch(d, 
                                                              c(0, 0), 
                                                              c(aA, 0), 
                                                              c(0, aB), 
                                                              c(aA, aB)))
                   return(c(df, 0))
                 }, 
                 ratefunc = function(t, x, parms) {
                   return(with(as.list(c(x, parms)),
                               c(switch(d, k01B, k01B, k10B*fA, k10B*fA),
                                 switch(d, k01A, k10A*fB, k01A, k10A*fB))))
                 }, 
                 jumpfunc = function(t, x, parms, jtype) {
                   c(x[1:2], switch(jtype, 
                                    switch(x[3], 3, 4, 1, 2), 
                                    switch(x[3], 2, 1, 4, 3)))
                 }, 
                 times = c(from = 0, to = 20, by = 0.01), 
                 solver = "lsodar")

#------ polyPdmpModel version -----
genePolyT <- new("polyPdmpModel",
                 descr = "toggleswitch with two promotors (polynomial version)",
                 parms = list(bA = 0.5, bB = 0.5, aA = 2, aB = 4, 
                               k01A = 0.5, k10A = 2, k01B = 0.3, k10B = 3),
                 init = c(fA = 0.5, fB = 0.5, d = 4), 
                 discStates = list(d = 1:4),
                 dynpolys = quote(list(
                   list(overall = -bA*lone(1,3), specific = list(0, aA, 0, aA)),
                   list(overall = -bB*lone(2,3), specific = list(0, 0, aB, aB))
                 )), 
                 ratepolys = quote(list(  
                   list(k01B, k01B, k10B*lone(1,3), k10B*lone(1,3)),
                   list(k01A, k10A*lone(2,3), k01A, k10A*lone(2,3))
                 )),
                 jumpfunc = function(t, x, parms, jtype) {
                   c(x[1:2], switch(jtype, 
                                    switch(x[3], 3, 4, 1, 2), 
                                    switch(x[3], 2, 1, 4, 3)))
                 }, 
                 times = c(from = 0, to = 20, by = 0.01), 
                 solver = "lsodar")

#------- comparison of the models --------------
identical(sim(genePdmpT, outSlot = FALSE, seed = 10),
          sim(genePolyT, outSlot = FALSE, seed = 10))

data("toggleSwitch")
times(toggleSwitch) <- c(from = 0, to = 20, by = 0.01)
all.equal(sim(genePdmpT, outSlot = FALSE, seed = 20)[, c("fA", "fB")],
          sim(toggleSwitch, outSlot = FALSE, seed = 20)[, c("fA", "fB")],
          check.attributes = FALSE)

CharlotteJana/pdmppoly documentation built on Sept. 4, 2019, 4:40 p.m.