Vectors (Arrays)
Vectors are a powerful tool that play a key role in agent-based modeling. Vectors are also very useful in System Dynamics modeling allowing you to easily extend a model to represent different categories or classes of behavior.
For example, imagine you wanted to create a population model of North America. You could build the model using a single stock representing the population for the entire continent. Once you completed this model, you may decide that you now want to track the populations for Mexico, Canada and the United States separately. One way to do this would be to duplicate your model structure for each of these three countries. Though workable, this process would be laborious and prone to error. With vectors, you can achieve the same effect as duplicating the model structure just by adjusting your model equations slightly.
In this section, we will introduce vectors to you and show you how to use them to keep track of different categories in your model. As an example, here is that North America population model implemented using vectors, starting from 2013 populations. Just a single Population stock, a single Growth flow and a single Net Growth Rate variable cover all three countries.
What is a Vector
A vector is just an ordered list of numbers surrounded by curly braces. This is a vector of the squares of the numbers one, two, and three:
You can do mathematical operations on vectors like you would with regular numbers. The operations are applied element-wise. For example:
Note
Element-wise operations are carried out, including for multiplication of vectors or matrices. See Macros and Globals if you need matrix multiplication.
In addition to regular vectors, Insight Maker also has named vectors. In named vectors each element of the vector has a name (which is case-insensitive). For example:
You can apply mathematical operations to named vectors just like you would for regular vectors. For example:
Vectors are sometimes referred to as "arrays" or "lists" in other languages and named vectors are sometimes referred to as "associative arrays", "objects" or "dictionaries".
Multidimensional Vectors (Matrices)
A matrix in Insight Maker is simply a vector of vectors. For instance to define a 2x2 matrix you could use the following:
You can do the same with named vectors. Say for instance we wanted to track a rabbit population by both gender and country, we could use something like:
If you put this is the initial value for a population stock, you will now be simulating four distinct populations.
In this case, for the birth rate equation, Insight Maker will work correctly either with one number (all the populations have the same birth rate), or a vector for the genders (each gender has a different birth rate), or a vector for the countries (each country has a different birth rate), or a matrix of countries and genders (each country and gender has a different birth rate). Insight Maker will do the right thing for you in each case.
Multidimensional vectors can start to become clunky if you type them in manually. If you have the same values for some of the elements (such as all the countries have the same population sizes) you can use other vector functions to help construct the vector. For instance the "Repeat" function is quite useful:
Selecting Elements and Collapsing Matrices
Vector indexing is 1-based, the first element has index 1. You can select an element from a vector like so:
You can do the same with named vectors:
For named vectors you can also use a shorthand notation "." to select single elements:
You can also use vectors in your selections to select multiple elements at a time:
For matrices, you list your selectors sequentially dimension by dimension. Each editor below first defines a Rabbits matrix (using <- to assign it to a local variable) and then selects from it. Try editing a selection to see the result change:
If you use a "*" instead of an element name, Insight Maker returns the whole vector along that dimension:
You can also collapse or summarize elements from a matrix by using the name of a function in the selection. The function is used to aggregate elements along that dimension. For example, to get the total number of rabbits by country:
To get the average number of males and females in the countries:
All standard Insight Maker vector functions (Mean, Median, StdDev, etc...) can be used in this way. Additionally, you can also write and use custom functions.
Wildcards
"*" in vectors is a wildcard and will match missing elements. For instance if we wanted the birth rates of Canada and USA to be 0.1, but for all other countries to be 0.2, we could use:
Multi-word Names
If you have a name that contains spaces, you need to quote it when you declare the vector:
