StochSD: A Full Potential CSS Language for Dynamic and Stochastic Modelling, Simulation and Statistical Analysis

It is vital that a well-defined conceptual model can be realized by a macro-model (e.g., a Continuous System Simulation (CSS) model) or a micro-model (e.g., an Agent-Based model or Discrete Event Simulation model) and still produce mutually consistent results. The Full Potential CSS concept provides the rules so that the results from macro-modelling become fully consistent with those from micro-modelling. This paper focuses on the simulation language StochSD (Stochastic System Dynamics), which is an extension of classical Continuous System Simulation that implements the Full Potential CSS concept. Thus, in addition to modelling and simulating continuous flows between compartments represented by “real” numbers, it can also handle transitions of discrete entities by integer numbers, enabling combined models to be constructed in a straight-forward way. However, transition events of discrete entities (e.g., arrivals, accidents, deaths) usually 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, together with devices to collect statistics during a simulation or from multiple replications of the same stochastic model. Also, tools for sensitivity analysis, optimisation and statistical analysis are included. In particular, StochSD includes features for stochastic modelling, post-analysis of multiple simulations, and presentation of the results in statistical form. In addition to making StochSD a Full Potential CSS language, sive material for a course in Modelling and Simulation in form of PowerPoint lectures and laboratory exercises.


Introduction
To model and simulate a system under study, you can choose between creating a micro model [e.g., an Agent-Based Model (ABM) or a Discrete Event Simulation (DES) model] by describing each entity of interest, or a macro model [e.g., a Continuous System Simulation (CSS) model] by only describing the number of different types of entities or the number of entities in defined stages (e.g., Susceptible, Sick, Immune).
However, a major problem in simulation has been that a micro model and a macro model of the same well-defined conceptual model of a system under study often produce mutually inconsistent (i.e., contradictory) results. Based on a series of papers that theoretically solved this problem, the authors of this paper have constructed a CSS program package that contains the facilities to create and simulate CSS models that are fully consistent with a well-defined conceptual model.

Research Background
The consistency problem was investigated in a number of studies to identify why micro and macro realisations of the same conceptual model often produce inconsistent results. A cornerstone was here the method to transfer a number of discrete entities between stocks (compartments) during a time step in CSS, published by L.G. in 2000 [1]. (This idea is the same as used in Gillespie's τ-leap (or tau-leap) method published the following year [2].) Then the fundamental problem of queuing, which is essential in micro modelling, could be addressed so that queues could be implemented into a stochastic CSS model [3]. In papers [4] [5] [6] the consistency problem was further investigated with focus on different types of stochasticities, stage-to-compartment expansion in order to preserve the sojourn time distribution in the stage, and the inclusion of attributes into a CSS model.
Finally, it became possible to transform a well-defined conceptual model into an ABM, then to a DES model, and further into a CSS model by a series of transformations without creating inconsistencies. This was published by Gustafsson and Sternad in OJMSi in 2016 [7].
The following year, the conditions required for a CSS model to preserve the properties of a conceptual model were studied in details. This resulted in the "Full Potential CSS" theory [8], which in short involves: 1) describing discrete L. Gustafsson  It was also shown that the Full Potential CSS approach would provide a simple and straightforward solution to combined continuous and discrete simulation.
However, even though the theoretical problems were solved, existing CSS languages did not support and simplify the Full Potential CSS approach. These In short, the modelling and simulation package, StochSD, solves the following problems:  It supports Full Potential CSS modelling and simulation to remove inconsistencies in results.  It provides a simple way to combined continuous and discrete simulation.
 It provides StochSD for free to use in all kinds of courses, self-studies and research with CSS.  In addition, extensive material for Full Potential CSS courses in form of lectures, laboratory exercises and instructive examples are also provided for free on the StochSD homepage (https://stochsd.sourceforge.io/homepage/#/). In this paper, the simulation language StochSD and its use and possibilities are presented and demonstrated.

Purpose and Overview of StochSD
StochSD is an open-source, Full Potential CSS language 1 that has two main purposes: 1) To enable Full Potential CSS modelling and simulation within the CSS paradigm. This enables results consistent with those from Discrete Event Simulation or Agent-Based simulation of the same system. 2) To provide an open-source CSS language based on the System Dynamics philosophy for construction and simulation of small and medium-sized models in education, self-studies and research. Therefore, pedagogic aspects, ease of use and understanding are prioritised.
StochSD was developed by the authors during 2016-2021. To make StochSD and its philosophy easy to comprehend and use, it is based on the System Dynamics approach [9] [10] [11], where the system under study is described in terms of stocks and flows. StochSD complements classical CSS with a number of facilities to enable discrete modelling, in particular for discrete and stochastic transfers into, out of or between stocks. It also includes software for stochastic 1 StochSD can be run within a browser (Google Chrome, Mozilla Firefox or Microsoft Edge) or downloaded from the StochSD homepage. Try it! Open Journal of Modelling and Simulation modelling, multiple runs, statistical analyses of the results and result presentation in statistical terms.
Technically, StochSD is written in JavaScript. It is based on the System Dynamics part of the modelling and simulation language Insight Maker [12] [13] [14] and a number of other packages that are all open-source. Technical aspects of StochSD and its structure are described in Appendix A.
StochSD is available for Windows, macOS and Linux and as a web application, which means that it can also be run under other operating systems.
StochSD has been tested in a number of university courses. Great effort has been spent on debugging, pedagogic aspects, flexibility and easy handling.

The StochSD Simulation Package
A CSS model is in principle based on differential and algebraic equations (rewritten in numerical form), which in StochSD are structured in terms of stocks and flows into, out of or between stocks, in accordance with the concept of System Dynamics [9] [10] [11].
In the System Dynamics approach, the model is constructed by describing the system under study in terms of primitives: Stock: (state variable, compartment), Flow: ⇒ (continuous flow or transition of entities), Auxiliary: Ο (for algebraic calculation), Parameter:  (for describing impact from the system's environment) and Link: → (to transfer causal information from one primitive to another). The StochSD model structure is built in a click-and-draw manner using these primitives. Thereafter, each primitive (except for Link) is opened to define a value or algorithm.
The System Dynamics approach has a number of advantages. The underlying bathtub analogy of a stock filled and drained by inflows and outflows makes the underlying "integration over time" concept intuitive. Placing the attention first on the model structure of interacting elements, and then on the algorithm of each element, leads to a focused and efficient way of modelling. Further, the various types of stochasticity are closely related to the different primitives used in System Dynamics.
In StochSD, continuous amounts are represented by rational numbers (called "real" or "float" in computer jargon) and the numbers of discrete entities are represented by integer numbers (or by "real" numbers if the entities are so many that they can be regarded as a continuous amount, e.g. water molecules in a river). However, representation of discrete entities in CSS has consequences that require a sequence of issues to be considered.
For a continuous flow, a fraction of the content in a stock will pass through a flow during a time step, but this has no counterpart for the transition of discrete Open Journal of Modelling and Simulation entities. The transition of entities between stocks must respect the integrity of the entities. (Also, for chemical processes where the entities become redefined, integrity is to be preserved). The transfer of entities can be regular or irregular.  Transitions that occur regularly or are scheduled are straightforward to model, e.g. production of items by a machine can be described by a pulse function or a table function.
 Irregular transitions, on the other hand, occur at random points in time that cannot be foreseen. For example, radioactive decay, encounters between prey and predator, or arrival of customers at a shop are processes that usually have to be described by statistical means. This is often done using a Poisson process with a rate (intensity) of λ events per time unit (usually varying over time). Such an irregular process is realised by stochastic transitions in a flow.
We denote this transition stochasticity. Later in this paper, we show how StochSD simplifies the modelling of a Poisson process by a new function: The possibility to draw random numbers from statistical distributions of various kinds (also empirical derived from observations) is also essential.
However, transition stochasticity, like other stochasticities, in a simulation model causes the results to vary between simulation runs (replications). For a deterministic model, a scalar value of a studied quantity is obtained from one simulation, but for a stochastic model a distribution of values is obtained that will require many replications to be well reproduced. Therefore, the results from these replications must be collected and statistically analysed, to provide statistical measures. For this reason, StochSD also includes a statistical tool called StatRes (Statistical Results), which collects results from a specified number of replications, performs statistical analysis and displays the statistical results in the form of means, standard deviations, confidence intervals (CI), min & max values, percentiles of selected quantities and correlations between these quantities.
These statistical results can also be graphically presented in histograms and scatter plots.
An overview of issues supported by StochSD to become Full Potential is presented in Figure 1. In the following, we assume basic understanding of classical CSS and only discuss and exemplify the additional possibilities associated with StochSD following the numbered items, to , in this figure. These issues are then discussed in Sections 2.1 to 2.7. Figure 1)

Inclusion of Discrete Entities ( in
where the output DT•F is the "real" fraction that decays during a time step, DT. This fraction is proportional to the number of remaining radioactive atoms X(t) and inversely proportional to the time constant T. In a stochastic model DT•F must be an integer number that is Poisson distributed with the same expected outcome. The model then becomes: where Po[~] means that a Poisson distributed random number generator with ~ as argument is called at each time step. As seen, only the flow statement has to be adjusted to make the deterministic model stochastic and remain integer-based. This is done by including the Poisson random number function, which draws an integer random number for each DT in the transition flow. The deterministic expression here becomes an argument to the Poisson distribution.
A programming comment is that you may not have DT•F on the left-hand side of an assignment statement (which was used to illustrate the similarity between the deterministic and stochastic flows). In programming, you must code: F(t) = X(t)/T and F(t) = Po[DT•X(t)/T]/DT, respectively.
In Figure 2, we show the two models and their behaviours for the case X(0) = 30 radioactive atoms with a time constant of T = 10 time units. 

Combined Continuous & Discrete Simulation ( in Figure 1)
A powerful bonus is that combined continuous and discrete simulation becomes easy to perform. The following is a slightly adjusted example from [8].
Example 2: Combined continuous and discrete prey-predator model Conceptual model: The Lotka-Volterra equations describe a prey-predator system for two species, X and Y, by differential equations [17] [18] [19]. The prey breeds at a rate proportional to its size X, and is reduced because of encounters with predators, Y, which is proportional to X•Y. We also include competition among the prey, proportional to X 2 . The encounters with prey give the predators the energy to breed, so they increase in proportion to X•Y. Finally, the deaths of the predators are proportional to their number Y. The continuous Lotka-Volterra model then takes the mathematical form: where a and d are fertility constants, b and e mortality constants, and c is a proportionality constant for competition. Now, we want the prey to be a continuous amount of biomass X (e.g. of grass), while the predators Y are represented by a discrete number of entities (e.g. sheep).
The combined continuous-discrete model is easily obtained by making the inflows and outflows to and from Y stochastic, according to the Poisson distribution, whereby a random and integer number of entities transfers into and out of Y at each time step. Initialising "sheep" to an integer number will thus keep the number of sheep integer. Below, the combined model is shown where the modifications from the continuous to discrete form are shown in Equations (12) and (13): Comparing the continuous model with the combined model reveals that the continuous model, starting at the equilibrium state, will only produce two straight horizontal lines: X(t) = 60 tons of grass and Y(t) = 28 sheep. 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 the continuous model.  Note how easily a combined CSS model can be constructed in StochSD. The alternative of constructing a combined continuous and discrete model, with its mixture of disparate micro and macro concepts, time-handling methods and requiring a special combined simulation language, is not an attractive option for a macro study.

Uncertainty Description ( in Figure 1)
Uncertainties can be of two main types, structural and statistical. We first briefly describe the possible uncertainties with the help of an example and then discuss each of them in more detail, referring to this example. Example 3: An epidemic SIR model (to be discussed throughout Section 2.3) An epidemic SIR model, named after its three sequential stages (Susceptible, Infectious and Recovered) of an infectious disease process, is shown in Figure 4 [18] [19] [20]. To make it simple, each stage is represented by a single stock. An Authority that will react when the number of infectious individuals becomes too high is also included.
This example is used to demonstrate where and how different types of uncertainty can be described. Uncertainty may be about: 1) the model structure, 2) the  The five types of uncertainty to be discussed (structural, initial value, transition, parameter, and signal) all refer back to this example. Of these five types, structural uncertainty refers to the overall structure of the model or to how a stage or attribute should be represented, and can be handled by alternative model structures or sub-structures. The other four uncertainties refer to different types of primitives. These are handled by describing the uncertainty by using a specified statistical distribution from which to draw random numbers. Figure 1)

Structural Uncertainty ( a in
What model structure is most appropriate for the study? This issue is the same whether the model is continuous or discrete, deterministic or stochastic. The structure of the SIR model in Figure 4 can be questioned. One possibility is that an infected person becomes infectious after a latent period. This can be modelled by including an Exposed (E) stage between the Susceptible and the Infectious stages. (The model is then called a SEIR model.) Another possibility is that Recovered (immune) persons will lose their immunity after some time, thus Open Journal of Modelling and Simulation returning to the Susceptible stage (making a so-called SIRS or SEIRS model). In particular, it is important to understand the difference between a stage and a stock, and to consider whether it is important to include further attributes.

Stage-to-compartment expansion
On a more detailed level, the expansion of a stage into a structure of compartments (in parallel and/or series) to generate a proper sojourn time distribution in the stage is another example of structural uncertainty. (By modelling the infectious stage by a single compartment, the sojourn-time distribution is implicitly assumed to be exponential, which is very untypical for e.g. a pregnancy or a trip from A to B.)

Attribute expansion
When introducing an important attribute, e.g. a risk attribute to model disease behaviour and progression for high-and low-risk individuals, two or more separate (but interacting) chains of compartments and flows may be necessary.
Structural uncertainty is about exploring alternative model structures to find that generating the most similar behaviour to the studied system. Figure 1)

Statistical Uncertainties ( b, c, d and e in
The reason for describing uncertainties in a model is that uncertainties contain information. If one can describe the distribution of observations and the value of its parameters (e.g. mean, standard deviation, min, max), then more information is included. This, when correctly done, makes the model behave more realistically and display different possible behaviours.
Uncertainties are often handled by using an estimated average value in the model to keep the model deterministic. This may work in some cases, but may be disastrous in others. For example, a model of a queuing system using an average time, a, between arriving customers and an average service time, s, (where s < a) will never make a queue, while a stochastic model (using the same a and s) can cause large queues and waiting times.
Each type of statistical uncertainty is associated with a specific primitive: Initial uncertainty with stocks, transition stochasticity with flows, parameter uncertainty with parameters, and signal uncertainty with links controlled by an auxiliary. These uncertainties are all treated in a similar way, by first describing the uncertainty by a statistical distribution and then drawing random numbers from this distribution.
Initial value uncertainty ( b in Figure 1) Information about the initial number of individuals in the three stocks (S, I, R) shown in Figure 4 may be uncertain. However, assume that we know that the total population under study is N = 1000 people and that three of these individuals have returned from abroad bringing home a new disease, but we do not know whether one, two or all three of these returned infectious. The initial value for the stock I (0) is then 1, 2 or 3. Assuming that the probability is 60% for I (0) = 1, 30% for I (0) = 2 and 10% for I (0) = 3, a uniformly distributed random number (here called [Rand]) between 0 and 1 can be drawn and used to initialise (Note that if you place the call to the RNG within the algorithm, you will obtain two calls giving the algorithm a different meaning.) Further, for the remaining 1000 − I(0) people, we assume that the statistics indicate that an individual has a 40% chance of being immune (belonging to the R-stock in Figure 4) Transition uncertainty ( , c and b in Figure 1) Transition of discrete entities in a flow was partly covered in Section 2.1, because it is intrinsically connected with discreteness.
There are systems where entities are transferred in a deterministic way (which is straightforward to model). However, often the random number of events occurring during a time step is unknown and thus must be handled stochastically.
It is therefore important to be able to handle transition stochasticity within the flows into, out of and between stocks.
In the deterministic case of the SIR model shown in Figure  Parameter uncertainty ( d in Figure 1) Unexplained impact from the environment on the system under study can be modelled by stochastic parameters. In this case, there is no a priori form to describe such uncertainties and only statistical observations or other knowledge about the variations can act as guides.
In the SIR model in Figure 4, the infectious parameter, p, and the parameter describing the sojourn time as infectious, D, may vary with the weather, behaviours of the individuals and other conditions, in an unpredictable way.
A problem with parameter stochasticity is that its modelled influence will change with the size of the time step, DT. If DT is reduced, more random numbers will be drawn per time unit. The effect of this is smoothing, because of the law of large numbers. One way to handle this problem is to change a parameter after fixed periods of time. This allows DT to be less than or equal to the specified time interval, without affecting the parameter stochasticity if DT is changed.


Signal uncertainty ( e in Figure 1) Signals, i.e. communication of information in a system under study, can be distorted and/or delayed.
In Figure 4, this is exemplified by an authority that collects information about the number of infectious persons and analyses the data (which takes time and is never exact). The authority may then issue recommendations about using sanitary measures and avoiding contacts, which later on when the message is received may affect the rate of the "Infect" flow.
The time taken to collect, analyse and distribute the information to the receivers may vary in an unpredictable way (information may be sent by post; it may arrive during the weekend but be noticed first on Monday morning, etc.). Information may also be distorted because of imperfect data, misinterpretation, not reaching all subjects, etc.).
The length of a delay can be generated by drawing a random number from one proper distribution, and the distortion of the number of infectious individuals may be modelled by another stochastic distribution. In this way, uncertainty about signals can be handled by the sending and/or receiving primitive (e.g. an auxiliary).
For signal stochasticity too, the effects of drawing numbers from the statistical distributions are usually sensitive to the time step used for the simulation, so it will be wise to keep the random values constant for fixed periods of time.

Stochastic Input Devices (
in Figure 1) To enable construction of a stochastic CSS model, random number generators (RNGs) of various statistical distributions from which random numbers can be drawn are required. A way to make a replication or series of replications of a stochastic model reproducible is also a valuable asset. Figure 1) Stochasticity is obtained by a function call to a RNG, which returns a sample  Figure 1)  Figure 1)

Making a Stochastic Model Reproducible ( c in
Making a stochastic model reproducible may seem to be a contradiction. However, this is a legitimate technique that has many advantages. It is possible because stochasticity is generated by pseudo-RNGs, where each RNG produces a very long sequence of numbers that has virtually all the statistical properties of the specified statistical distribution. When the sequence finally ends (which will not happen in practice), it starts over and repeats the cycle. When a seed is defined, it indicates where in the cycle to start, which gives the opportunity to repeat a simulation run exactly.
Reproducibility can be used in the following cases:  To demonstrate or reconstruct a simulation run because something of particular interest happens, e.g. a species becomes extinct or a dam bursts, in order to understand why or to demonstrate how this happened.
 When comparing two models, they must be tested under similar conditions. For example, when comparing different designs of a queuing system, having the same sequences of arriving customers for the two cases would eliminate one source of variation in the results. This technique is called variance reduction using common random numbers. Unfortunately, use of variance reduction (except for very simple cases) requires the possibility to assign separate seeds to each RNG, which is not possible in StochSD.
StochSD makes a stochastic simulation model reproducible by locking the seed for the random number generators in the model. Since this feature is global (rather than belonging to each RNG), it is defined as a macro. In the macro, the Open Journal of Modelling and Simulation seed is set to e.g. 17 by writing: SetRandSeed (17).
By changing the argument, another (reproducible) simulation run is obtained. Figure 1) A stochastic model produces outcomes that vary stochastically within a replication and between replications. Therefore, a large number of observations within a replication must be recorded, or the results over many replications must be assembled to provide good statistical estimates.

Statistics Collectors ( in
It is therefore necessary to distinguish between these two kinds of statistics which we denote internal and external. Although the internal and external statistical measures are almost the same, the tools for obtaining them are different. Figure 1) StochSD has statistical functions, inherited from Insight Maker, for min, max, mean, median, standard deviation and correlation, for collecting and calculating internal statistics from a part or the whole simulation:

Collecting Internal Statistics ( a in
In special cases, for example when queues are involved, case-specific devices for average number waiting for a resource, average waiting time, utilization of resources such as taxi cabs, doctors, etc. can be built. This is shown in Example 4 in Section 2.6, below. Figure 1)

Collecting External Statistics ( b in
This concerns the stochastic variations over many replications, where the end results from each replication are collected. Note that functions, which at the end of a replication show the overall maximum, the value when something interesting happens, the cumulated or average performance during the replication, etc.
can be used or easily be constructed.
The collection of external results over many replications is taken care of by a StochSD tool named StatRes, see Section 2.7, below.

Internal Statistics ( in Figure 1)
In the internal case, the statistical estimates are calculated in the model during the simulation, where the functions mentioned in Section 2.5 present the results. Open Journal of Modelling and Simulation Because the observations come from a dynamic (and stochastic) process, there is dependence between X(t) and X(t + T), and between X(t) and another variable Y(t). This means that the samples are not independent, so the Central Limit Theorem does not apply. Thus, confidence intervals based on assumptions of a normal distribution cannot be derived in the internal case (unless it is possible to first divide the sequence into independent time intervals).
In the following example, taken from [8] and simplified, we demonstrate how a queuing system can be modelled and how devices for queuing measures, such as average queue length, average waiting time and utilisation of a server, can be constructed.   Cum_QS_Time accumulates QS over time to obtain the total waiting time in QS.

QS t DT QS t DT In Out
Dividing Cum_QS_Time by Time + eps gives the average number in the queuing system Av_QS, and dividing it by In_Counter + eps gives the average waiting time Av_QS_Time.
(The machine epsilon, eps, is the smallest number that can be represented. It prevents "division by zero" before the counters are incremented.)  Note: A queuing model requires transition stochasticity of discrete entities to be meaningful. A corresponding continuous and deterministic model will erroneously not generate any queues or waiting times.

External Statistics ( in Figure 1)
In the external case, the statistical estimates are collected from many replications. Note that there is a principal difference from the internal case in calcula- StatRes can also presented the statistics graphically in the form of histograms and by scatter plots (X-Y plots), with coefficient of correlation between X and Y calculated. It can also control the seed-of-seeds used to produce a new seed for each replication in order to make the study, based on multiple replications, reproducible.
The communication between StatRes and the model is restricted to before and after full replications, and updating of graphics in the model is disconnected to make StatRes fast. Open Journal of Modelling and Simulation The StochSD model and the results from two replications are shown in Figure   6.
We are now interested in comparing the results from the deterministic and stochastic models: Which force will win? What number of entities will remain after the battle? How long will the battle take? For the deterministic model, a single simulation gives that X will always win, the remaining force is X = 3.00 units and the battle will always take 11.00 time units. To test whether these results are correct and representative, StatRes was used for a sequence of 10,000 replications of the stochastic model. The results are shown in Figure 7.
Summing up the results obtained from 10,000 replications shows that the results from the deterministic model were all wrong: 1) X wins on average 71% of the battles (95% CI: 0.70 -0.72)-not all battles, and Y wins 29%.
2) The expected number of X entities after battle is 2.57 entities (95% CI: 2.53 -2.61), which deviates significantly from the deterministic model result of X = 3 entities.
3) The expected number of Y entities after a battle is 0.77 (95% CI: 0.74 -0.80),  This example also illustrates that linearity does not ensure unbiased results for a deterministic model. For a deeper discussion, see [6].  Comment: Do not confuse the confidence interval (CI) around an average estimate (given above) with the spread of results for a quantity. For example, the average length of a battle was 9.70 time units (95% CI: 9.59 -9.81), while the interval covering 95% of the battles varied between 2.30 and 24.25 time units. (This was easily obtained by setting the "Percentile" percentage to 2.5 and 97.5, respectively and reading the percentile values for End_Time.) Histograms and correlations can also be used in StatRes, and the raw data for each replication can be exported to e.g. a spreadsheet or a statistical program as Comma Separated Values (CSV).

Using StochSD
In this chapter, we discuss what StochSD looks like, how a model is constructed

The StochSD Graphical User Interface
When StochSD is opened, the graphical user interface (GUI) shown in Figure 8 appears.
On the upper part of the screen, there is a title bar, a panel with a row of menus and a row of buttons. Below the panel is the modelling window, where a model can be built in a click-and-draw manner.
The Title bar shows the file name with its path, and the time of the last saving (for a saved model file).
The To the right of the Execution buttons is a Progression field, where an orange bar expands over the field during execution and becomes green when the simulation is finished. This field also displays Start Time, Current Time, Integration method and Time Step used. To the right of the Progression field there is a button for the Time Unit, which must be specified before any model building can start.
To the left under the buttons, there is an Inspection field that shows the "equation" of a marked primitive without having to open it. To the far right, there are links to the homepages of StochSD and Insight Maker. Figure 8. The StochSD graphical user interface, which is composed of a title bar, menu bar, button bar and modelling window. At start, the time unit to be used throughout the modelling must be specified (e.g. "hour", "day", "year"; or just "Time Unit" or "t.u." for a generic model where the aim is to demonstrate a principle without limiting it to a time scale). No modelling can begin before this is specified. Open Journal of Modelling and Simulation Under the panel is the Modelling window ("canvas"), where the model is constructed and Tables, Plots, Number boxes, texts, frames, lines, etc. can be added.

Construction and Execution of a Model
StochSD applies the System Dynamics approach, where the model is constructed by the primitives Stock: , Auxiliary: Ο, Parameter: , Converter: (hexagon), Flow: ⇒, Link: →, and Ghost (duplicate of a primitive with a ghost symbol inscribed).
As soon as the time unit is specified (then shown to the right of the button bar), model building can begin. The model structure is built in a click-and-draw manner by placing and connecting the primitives on the canvas. Thereafter, each primitive is opened to define an algorithm or a value. This also applies for the Plots, Tables and Number boxes. Specification of length, time step, etc. is done in the Simulation Settings menu. Then the simulation can be started by pressing the Run button (►). Figure 9 shows a model of radioactive decay similar to that in Example 1 in Section 2.1. Here the Decays flow primitive is opened for definition.
Naming and defining the primitive StochSD's syntax is inherited from Insight Maker. The name and the equation (or more correctly "algorithm in an assignment statement") of a primitive are defined in the dialogue box of a stock, flow, auxiliary, parameter or converter.
The name of a primitive is given in the Name field. It must start with a bracket, [, followed by a string that may only contain the characters A to Z, a to z and _ (anywhere) and 0 to 9 (if not the first character), and ends with a bracket, ], e.g.  In the Definition field, the algorithm is composed of primitives, functions, numbers and operators. Here, brackets must be included around the primitives.
The easiest way to include a primitive is to click it in the list "Linked primitives:" (see Figure 9). (On the canvas, the brackets are hidden and in this paper we usually exclude the brackets around the primitives when explaining a model.) Further, a function ends with parentheses "()", with or without parameters. A number may include a decimal point and e-format, e.g. 1.7e−12 (meaning 1.7 × 10 −12 ) is also allowed.
Primitives, functions and numbers are combined by the arithmetic operators + − * / ^ () for addition, subtraction, multiplication, division, power (x n ) and parentheses. As always, the calculation order is: power, then multiplication and division, and last addition and subtraction. Parentheses can be used to alter this order. After a # sign, a comment on the algorithm can be added.
If the definition is empty, contains unmatched brackets or parentheses, not includes all linked primitives, or includes unlinked primitives then the primitive is marked with "?" to show where there are more to specify or correct.
The StochSD User's Manual and Tutorial [23] provides a detailed description about construction and execution of a StochSD model. This manual is included under the Help menu and can also be downloaded from the StochSD homepage.

The Philosophy behind StochSD
The lesson learnt from several decades of teaching is that it is crucial to give the student a realistic insight into what modelling and simulation is, and what it is not. A model is a huge simplification of reality. "Everything should be made as simple as possible, but no simpler." (attributed to Albert Einstein). "All models are wrong, but some are useful." (attributed to George Box). These statements should be central insights communicated in every course in modelling and simulation. However, it is also important to understand that a model includes more than a simplified description of a system. Therefore, all model constituents should be understood. A StochSD model can be described as an onion with several layers, as shown in Figure 10.
The Core layer of the model describes the dynamic and algebraic relations of the system under study in terms of stocks, flows, auxiliaries, etc. (but no parameters). In the Subjective layer, one can define concepts that have no counterpart in the system under study, for example biomass, market price or an objective function (e.g. for optimisation where each unit of Stock 1 is valued at 30 Euros and each unit of Stock2 at 10 Euros.) It is also important to consider that a system under study seldom is unaffected by its environment, i.e. by factors that are outside the system borders (in space, time or details). The Environmental layer of the model therefore includes Initial values (that depend on what has happened before the study period), as well as unexplained influences (from the environment). In StochSD, parameters () are used for this. Parameters can be constants or functions, but they cannot have incoming links since they are unexplained quantities. Open Journal of Modelling and Simulation Figure 10. A model is less and more than a description of a system: The "Core layer" is a huge simplification of the system under study, focusing on dynamic and algebraic relations. In the "Subjective layer", one can create subjective concepts to fit the purpose. In the "Environment layer", external influences on the system under study are described. In the "Technical specifications" layer, start time, length of simulation, step size etc. are specified. Finally, a "Run and Tools layer" allows model behaviour to be generated and investigated.
In practical model building in StochSD, the three inner layers are mixed on the canvas, but the cloud symbols of a flow show that the sources and sinks are located outside the system borders. Further, the parameter symbol () tells that it is an unexplained quantity.
In the Technical specifications layer, start time, length of simulation, integration method, time step and the seed for initiating random number generators are all artificial concepts.
Finally, there is a Run and Tools layer, from where model behaviour can be generated, analysed or optimised in a controlled way. Together, these five layers constitute the model to be studied.

Four Integrated Tools
StochSD has integrated tools for optimisation, sensitivity analysis, statistical analysis, and analysis of parameter variations. These tools require multiple replications to collect results, followed by analysis and result presentation. They are:  Optim-for optimisation or parameter estimation of a deterministic model. This tool is based on a simplex optimiser, which means that it is reasonably efficient and can also deal with constraints [24] [25] [26]. Open Journal of Modelling and Simulation  Sensi-a tool for studying the sensitivity of one or several specified quantities on changing the value of e.g. a parameter or an initial value [27].  StatRes-collects results from multiple replications, makes statistical analyses and presents statistical results (average, standard deviation, max, min, confidence interval, correlation, etc.). The statistical results can also be presented in graphical form [28].  ParmVar-a hybrid of StatRes and Optim for parameter estimation of stochastic models. ParmVar performs multiple replications of a stochastic model, and for each of these replications parameter estimation is made. These multiple estimations of model parameters show the variations in parameter estimates in statistical terms [29]. These tools are described briefly in [23], and in detail in [26] [27] [28] [29].

Comparing Models to Show What Stochasticity Can Reveal
To show the power and versatility of StochSD as a Full Potential CSS language, we conclude this section with an example comparing three deterministic and stochastic models. where encounters between prey X and predator Y bring death to the prey and promote an increase in Y (according to d•X•Y and e•X•Y).
These three deterministic models and their behaviours are shown in Figure  11. Now we introduce transition stochasticity in each flow, because number of births and deaths, number of infected, and number of prey deaths and predator births are independent, random events. Therefore, each "deterministic flow statement" is now replaced by PoFlow (deterministic flow statement), see Figure   12.
The randomly occurring events make the three models reveal their differences in behaviours. In the Logistic model (12a) the population, P, can go up and down, and in one replication P becomes extinct. In the SI model (12b), the number of infectious individuals, I, increases monotonically, but always ends at the same level. In the Pruned Prey-Predator model (12c) the number of predators, Y, also increases monotonically, but ends at different levels. This is described in more detail in [8].  In Figure 12

Discussion and Conclusions
StochSD has two main purposes: 1) To enable Full Potential CCS modelling and simulation, and 2) To provide an open-source CSS language based on the System Dynamics philosophy for construction and simulation of small and medium-sized models in education, self-studies and research, where, pedagogic aspects, ease of use and understanding is prioritised.

Practical Use of Full Potential CCS Modelling and Simulation
The underlying Full Potential CSS theory is presented in [8], and is only briefly discussed and exemplified in this paper. In short, StochSD supports the "Full Potential CSS" requirements by:  Enabling modelling of continuous matter as "real" numbers and discrete entities as integers.  Recommending stage-to-compartment expansion when required.
 Recommending attribute expansion when required.
 Enabling means for structural, transition, initial value, parameter and signal stochasticities.
 Providing tools for collecting both internal statistics (within a replication) and external statistics (over multiple replications).
 Providing tools for multiple runs, statistical analyses and results presentation.
Full Potential CSS is a break from the old tradition where CSS modelling is used to describe both continuous matter and discrete entities as continuous, often with disastrous results.
The old view of simulation in general is well formulated by Wolfgang Kreutzer in his book: System Simulation, Programming Styles and Languages ([30], p. 18): "Modelling styles are paradigms in this sense. Monte Carlo methods, continuous, discrete-event and combined simulation all have their own methodologies, tools, traditions, user communities and prototypical applications." With the Full Potential CSS approach, all four of these "paradigms" can be consistently handled in a unified and smooth way on macro level [7] [8].
StochSD is powerful enough to be used in practical studies in epidemiology, ecology, traffic flows, queuing systems, production and many other fields. In epidemiology, the dominant approach is currently to make (randomised) studies with well-defined start and end points analysed by statistics. However, here the underlying process is dynamic and may involve different types of uncertainties that cannot be handled within a static statistical approach. A scientific study in epidemic modelling based on StochSD is presented in [31].

Use in Education and Self-Studies
To support the use in education and self-studies, we have created the StochSD homepage [32], where we supply free material to enable courses in classical CSS The resources provided at the StochSD homepage include:  The StochSD software package for Windows, Mac OS X and Linux or as a web application. It also means that StochSD is available to each student.  StochSD User's Manual and Tutorial [23].
 Lectures L1 -L9 in form of 260 PowerPoint slides for courses in modelling and simulation, and L0 discussing their use and legal aspects.  Laboratory exercises for Classical CSS (Lab 1 & Lab 2) and Full Potential CSS (Lab 3 -Lab 5).  Instructive example models in StochSD as .ssd files.
 A link to the paper "The Full Potential of Continuous System Simulation Modelling" [8].
A Full Potential CSS course is best conducted in two steps: Step 1: Basic deterministic CSS modelling A course in modelling and simulation should be based both on theory and on practical model building and experimentation. Therefore, lectures teaching the basics about a system under study and a model of it, describing how a modelling project should be performed, as well as the CSS language StochSD are all crucial.
Further, two full-day laboratory exercises based on deterministic modelling and simulation where the student does the modelling, experimentation and thinking is also recommended.
After introducing basic concepts such as system under study, model, purpose, etc., it is appropriate to start with deterministic modelling with the focus on how structure causes behaviour (especially effects of positive and negative loops, oscillations, control, etc.). An excellent introduction to this is "Thinking in Systems" by Donella Meadows [11].
It is also important to understand the difference between stage and compartment (stage-to-compartment expansion) to obtain realistic sojourn time distributions, and how addition of attributes sometimes is necessary but will create a larger model, see [8].
Deterministic models can be based on amounts and sometimes also on fractions. Fractions make a model scalable.
Step 2: Implementing and handling discreteness and stochastics When the student understands deterministic modelling and is familiar with the concepts of stocks and flows, it is time to address how discreteness and uncertainties should be modelled. In particular, it is important to understand how discrete entities are represented and how this often is connected to transition stochasticity. For a discrete model, the actual number of entities has to be modelled (here fractions cannot be used).
Different types of uncertainty (structural, transition, initial value, parameter, and signal) must be understood and should be covered in practical modelling. Open Journal of Modelling and Simulation The effects of stochasticity should also be taught and practised, by building corresponding deterministic and stochastic models and comparing their behaviours. It is also essential to understand the danger of using classical CSS to model discrete entities as continuous.
Further, a stochastic model requires multiple replications, so the student must understand how to analyse stochastic variations within a replication and over multiple replications in statistical terms. Basic statistics (distribution, mean, standard deviation and confidence interval) are required to understand and use statistical functions and the StatRes tool. Two laboratory exercises will consolidate basic knowledge about stochastic modelling and simulation.
In Appendix B, a suggested schedule for an extensive course in "Model building and Simulation using StochSD" gives an overview of the provided course material. This schedule also indicates the content of each lecture and laboratory exercise. However, the teacher is free to use or modify, change, translate, etc. the material to form his or her own course (with exceptions for six slides from Pixabay [33] and one from OpenStreetMap [34], where their generous licenses must be respected-see Lecture 0: Introduction & License at the StochSD homepage).

Conclusions
The CCS Language StochSD has the two purposes: 1) To make macro modelling consistent with micro modelling by enabling use of Full Potential CSS. 2) To provide a free, open-source, pedagogic tool for courses in macro modelling and simulation.
StochSD was mainly developed and tested in a number of university courses during 2019-2021, and was officially released in January 2021. In January 2022, an improved version was released. Until now (mid-February, 2022) more than eleven hundred downloads of StochSD in 58 countries have been done, as well as an unknown number of the web version of StochSD. The Full Potential aspects have also been thoroughly tested, e.g., in [31]. Our conclusion is that StochSD works well both technically and pedagogically.
StochSD is meant to be easy to use and understand, why we have not included vector modelling (although this is possible-but not graphically supported), nor have we included animation of e.g., the content in stocks, values of auxiliaries, etc. One desirable feature would be to rebuild the random number generators (RNG) so that each RNG could have an individual seed instead of sharing a global one. This would enable more powerful variance reduction. Future work will focus more on correctness and pedagogical issues than on making StochSD more advanced.
We also expect the free, extensive course material just included to be helpful, especially to initiate high-quality education in macro modelling and simulation.
Although we find that StochSD works well, it is not perfect. We regard it as the first serious attempt to design a Full Potential CSS software package for stochastic and dynamic macro modelling and simulation. The open-source philosophy, where all code is available for use, modification, improvements or new Open Journal of Modelling and Simulation software, is important to us. We hope that this will be utilised in the future to make CSS modelling and simulation an even more powerful tool based on a stable scientific foundation. Open Journal of Modelling and Simulation cided by the user. On a computer where this is not possible, for example Chromebook, the web application is a way to run StochSD. Figure A1. Components of StochSD and its use of third-party software. D: StochSD-Desktop, meaning that StochSD includes NW.js. W: StochSD-Web, meaning that StochSD must be run within a browser.

L8
The Modelling project Describes the strict requirements for a modelling and simulation project.

L9 Simulation vs. Speculation
Discusses the use and limitations of modelling and simulation. The use of simulation as a crystal ball to predict the future must be condemned, not to cause false, dangerous and costly conclusions.
Exam. Examination (if required). The main examination is completed laboratory exercises.
A possible examination should focus on basic concepts, understanding, how to perform a modelling project, the use and limitations of simulation. Further, the strict distinction between system under study and model, and the understanding where information comes from is crucial.

Book
Suggested book: "Thinking in Systems: A Primer" by Donella Meadows [11].

Project
In a postgraduate course, a Modelling Project can be included.