# StochSD

StochSD (Stochastic System Dynamics) is a free, open-source Continuous System Simulation (CSS) package intended for small and medium-sized models in education, self-studies and research. Technically, StochSD is based on the Insight Maker engine with its DE-solver, function library, error checker, macro facility, etc., while the design, graphical user's interface, construction elements, result presentation, file handling, link checking, etc. are different. Also, tools for sensitivity analysis, and optimisation with or without constraints are included. In particular, StochSD includes features for stochastic modelling, post-analysis of multiple simulations, and presentation of the results in statistical form.
The design and development of StochSD were done during 2017–2022 with support from Uppsala University, Karolinska Institute, and the Swedish University of Agricultural Sciences.
StochSD was designed to fulfil the two purposes:
* To provide an open-source CSS language based on the System Dynamics philosophy, where a system is described in terms of stocks (compartments) and flows, and where pedagogic aspects, ease of use and understanding are prioritised.
* To enable a macro (CSS) model in StochSD to produce results that are fully consistent (i.e.,contradiction-free) with those from a micro (Discrete Event Simulation (DES) or Agent-Based Simulation) model of a well-defined system under study. This old consistency problem was stepwise solved between 2000 and 2010. This property is denoted Full Potential CSS modelling, see below.

## Education and self-studies

To support the use in education and self-studies, the StochSD package as well as course material to enable courses in classical CSS and Full Potential CSS modelling and simulation are provided at the StochSD website. This course material contains lectures, laboratory exercises, instructive models, and StochSD manuals, which mainly are based on material developed at Uppsala University and the Swedish University of Agricultural Sciences for courses in modelling and simulation.
StochSD is downloaded in many countries from all continents, and can also be run directly in a web browser (of which there is no statistics). It is described, compared and discussed, demonstrated in educational videos, and also referred to in various languages.

## Full potential CSS modelling

The Full Potential CSS concept is an extension of classical Continuous System Simulation, which provides the rules to make the results from macro-modelling consistent with those from micro-modelling.
Briefly, in addition to modelling and simulating continuous flows between stocks represented by ‘real numbers’, StochSD can also handle transitions of discrete entities by integer numbers. But in contrast to including individual entities into a CSS model, StochSD preserves the aggregated macro approach for discrete entities by transferring integer number of entities (e.g., arrivals, accidents, deaths) during a time-step. However, such transitions may happen irregularly over time, so stochasticity often plays a crucial role in their modelling. Therefore, StochSD contains powerful random functions to model uncertainties of different kinds, as well as devices to collect statistics during a simulation and from multiple replications of the same stochastic model. In StochSD the construction and simulation of e.g. queuing models or combined discrete and continuous models are done in a straightforward way (see example below).
The Full Potential concept also includes rules for how a stage has to be expanded into a structure of stocks and flows in order to reproduce a specific sojourn-time distribution, how attributes are to be handled, and where and how different types of uncertainty (structural, transition, initial value, parameter, and signal uncertainties) should be implemented.

### Example: continuous vs. combined discrete and continuous model

A continuous prey-predator model and a combined model with continuous prey (e.g. X= Grass) and discrete predators (e.g. Y = Sheep), as well as a replication of the combined model are shown below.
Continuous model
${\begin{array}{lll}X(t+DT)=X(t)+DT\cdot (F1-F2-F3)&{\text{(CONTINUOUS GRASS)}}\\F1=a\cdot X&{\text{(Breeding)}}\\F2=b\cdot X\cdot Y&{\text{(Preyed upon)}}\\F3=c\cdot X\cdot X&{\text{(Internal competition)}}\\Y(t+DT)=Y(t)+DT\cdot (F4-F5)&{\text{(CONTINUOUS SHEEP)}}\\F4=d\cdot X\cdot Y&{\text{(Births)}}\\F5=e\cdot Y&{\text{(Deaths)}}\\\end{array}}$
Now assume that the births and deaths of the sheep are discrete random events with the expected rate d⋅X⋅Y and e⋅Y, respectively, then the number of events per time interval is Poisson distributed. The StochSD function PoFlow(expected_value) handles this by drawing a random number for each DT. The modifications to obtain a combined model are shown in red, below.
Combined discrete and continuous model
${\begin{array}{lll}X(t+DT)=X(t)+DT\cdot (F1-F2-F3)&{\text{(CONTINUOUS GRASS)}}\\F1=a\cdot X&{\text{(Breeding)}}\\F2=b\cdot X\cdot Y&{\text{(Preyed upon)}}\\F3=c\cdot X\cdot X&{\text{(Internal competition)}}\\Y(t+DT)=Y(t)+DT\cdot (F4-F5)&{\text{(DISCRETE SHEEP)}}\\F4=\color {red}PoFlow(\color {black}d\cdot X\cdot Y\color {red})\color {black}&{\text{(Discrete births)}}\\F5=\color {red}PoFlow(\color {black}e\cdot Y\color {red})\color {black}&{\text{(Discrete deaths)}}\\\end{array}}$
In the replication shown, the discrete predators became extinct at around 235 time units (e.g., months). The continuous prey then increases logistically to an equilibrium without further stochastic variations.
Comparing a continuous prey-predator model with the combined model reveals that the continuous model, starting at the equilibrium state (as in this example), will only produce two straight horizontal lines. Starting the continuous model outside the equilibrium state will make it approach the equilibrium for both species without lasting variations. Further, a phenomenon such as extinction cannot occur for a continuous model.
Creating the combined model, shown above, is straightforward. The traditional alternative of constructing a combined continuous and discrete model, with its mixture of disparate DES and CSS concepts, synchronisation of two different time-handling methods, and requiring a special combined simulation language is not an attractive option for a macro study.