Tutorial 8: For loops and dynamic models
Objectives:
- Define for loops
- Assign vector elements inside a for loop
- Use for loops for iteratively solving a dynamic model
For loops and vectors
components of for loops
A for loop is a programming structure that repeats some code (called the body) a specified number of time. For example, you can write “Hello!” ten times like this:
The for loop starts with the keyword for, then has the expression in parentheses (i in 1:10). i is called the loop variable, in is another keyword, and 1:10 is a loop vector. After the first line there is a curly bracket { and everything that follows until the closing bracket } is the loop body. The loop body is executed as many times as there are elements in the loop vector (in this example, 10 times) and the only thing that changes is the values of the loop variable i, which starts with the first element of the vector (1) and goes until it reaches the last element (10) and then stops:
It can be used to do repetitive calculations, for example, adding up all the integers in an array (of course the built-in function sum() will do it too):
using vectors with loops
It is especially useful to use loops to assign vector variables one element at a time. The loop variable (i) is usually used for indexing the vector variable. There are several necessary features of using vectors in for loops, so let us take an example script and break them down in this example of calculating a vector of the sum of integers from 1 up to the current number:
- Pre-allocate the vector variable - i.e. create a vector of the correct length prior to the loop by filling it with a placeholder value, e.g. 0 or NA (not a value).
- Only put in the loop what has to be repeated (e.g. don’t put the pre-allocation inside the loop);
- Be careful with indexing inside for loops - this means paying careful attention to the lowest and the highest index that you’re using inside the loop. For example, if your loop vector goes from 1 to max (as above), the total number of elements in your vector will be max+1, because we assigned the element with index i+1 in the loop:
using for loops for solving discrete-time dynamic models
A first-order discrete time model of a variable \(X(t)\) can be defined by an equation where the next value of the variable \(X(t+1)\) is computed from the current value \(X(t)\). For example the model
\[ X(t+1) = 1.5X(t)\]
describes a variable that is multiplied by 1.5 every time step. A for loop can be used to iteratively compute its solution, starting with a given initial value \(X(0)\). The script below starts with the initial value 4, computes the solution for 10 steps and plots it (note that the time vector starts at 0 and goes through 10, while the index of the variable X goes from 1 to 11).
In the famous Fibonacci model the next value is defined to be the sum of two previous values in the sequence:
\[ F(t+2) = F(t+1) + F(t)\] The script below iteratively calculates the solution, starting with initial values of \(F(0)= F(1) = 1\) and plots the resulting sequence together with an exponential function that approximates it.
Exercises:
- Assign the value 5 to a variable named
mine
, then multiply it by 1.03, replacing the old value of the variable.
- Write a script to take the variable
mine
with values 5 and multiply it by 1.03 one hundred times using a for loop. Useprint()
to show the result.
- Use
rep()
to preallocate a vector of valuesmine
to be 101 zeros, then assign the first element to 5. Use a for loop to calculate 100 new values by multiplying the previous one by 1.03 and assigning them to sequential elements of the vector, then plot that vector as a function of the vector time.
- Fix the indexing error in the script below for calculating a vector of factorials
fact
. Each element of the vectorfact[i]
should be equal to \(i!= 1 \times 2 \times 3 ... \times i\), so for examplefact[2]
should be 2 andfact[3]
should be 6.