# Introduction to GillespieSSA2 In GillespieSSA2: Gillespie's Stochastic Simulation Algorithm for Impatient People

```set.seed(1)
knitr::opts_chunk\$set(fig.width = 6, fig.height = 4)
```

In order to invoke `ssa()`, the stochastic model needs at least three components:

• the initial state vector (`initial_state`),
• the reactions (`reactions`),
• the final time of the simulation (`final_time`).

The initial state vector defines the population sizes in all the states at \$t = 0\$. For example, for a system with two species `prey` and `predators` where both have an initial population size of 1000, the initial state vector is defined as follows.

```library(GillespieSSA2)
initial_state <- c(prey = 1000, predators = 1000)
```

The reactions define the change in the number of individuals that can occur at any given timepoint during the simulation. During an infinitesimal period of time, the reaction can occur with a probability defined by its propensity function. For example, a system with abovementioned species could have three reactions; one in which the prey population grows, one in which the predator population grows by feasting on the prey, and one in which the predator population diminishes. The matrix could then be defined as follows.

```params <- c(c1 = 10, c2 = 0.01, c3 = 10)
reactions <- list(
#        propensity function        effects                          name for reaction
reaction(~c1 * prey,                c(prey = +1),                    name = "prey_up"),
reaction(~c2 * prey * predators,    c(prey = -1, predators = +1),    name = "predation"),
reaction(~c3 * predators,           c(predators = -1),               name = "pred_down")
)
```

The simulation can be started by calling the `ssa()` function.

```out <-
ssa(
initial_state = initial_state,
reactions = reactions,
params = params,
method = ssa_exact(),
final_time = 5,
census_interval = .001,
verbose = TRUE,
sim_name = "My first SSA run!"
)
```
```print(out\$stats)
```
```plot_ssa(out)
```

## Try the GillespieSSA2 package in your browser

Any scripts or data that you put into this service are public.

GillespieSSA2 documentation built on Jan. 24, 2023, 1:10 a.m.