# Time-varying values

#### 2020-04-08

In more complex Markov models state values or transition probabilities can vary with time. These models are called non-homogeneous or time-inhomogeneous Markov models. A further distinction can be made depending on whether state values or transition probabilities:

1. depend on how long the entire model has been running (model-time dependency);
2. depend on how long an individual has been in a given state (state-time dependency).

These two situations can be modelled using the `model_time` (or its alias `markov_cycle`) and `state_time` variables, respectively.

## How to specifiy time-dependency

These variables takes increasing values with each cycles, starting from 1. For example the age of individuals at any moment can be defined as `Initial age + model_time`. The time an individual spends in a state is equal to `state_time`.

Both variables can be used in `define_parameters()`, `define_state()`, or `define_transition()`:

    define_parameters(
      mr = exp(- state_time * lambda),
      age = 50 + model_time
    )

    ## 2 unevaluated parameters.
    ##
    ## mr = exp(-state_time * lambda)
    ## age = 50 + model_time

    define_state(
      cost = 100 - state_time,
      effect = 10
    )

    ## A state with 2 values.
    ##
    ## cost = 100 - state_time
    ## effect = 10

    f <- function(x) abs(sin(x))

    define_transition(
      C,  f(state_time),
      .1, .9
    )

    ## No named state -> generating names.

    ## A transition matrix, 2 states.
    ##
    ##   A   B
    ## A C   f(state_time)
    ## B 0.1 0.9

## Details

Using `model_time` in a model does not change the execution speed of the analysis. On the other hand adding `state_time` may slow down the analysis, especially if the model is run for many cycles and a transition probability depends on `state_time`.1

To mitigate this drawback it is possible to limit the number of state expansion with `state_time_limit`. Because most time-varying values reach an asymptotic value quite fast, it is unnecessary to expand the states any further. The last cycle value is repeated until the end.

The limit can be defined globally, per state, or per model and state. In the following example probabilities are kept constant after 10 cycles for state B and 20 cycles for state D in strategy I, and 15 cycles in state B in strategy II.

    run_model(
      I =  strat_1,
      II = strat_2,
      cycles = 100,
      state_time_limit = list(
        I = c(B = 10, D = 20),
        II = c(B = 15)
      )
    )

1. In this situation the complexity is proportional to the square of the number of cycles.↩