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:

  1. 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).
  1. Only put in the loop what has to be repeated (e.g. don’t put the pre-allocation inside the loop);
  1. 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:

  1. Assign the value 5 to a variable named mine, then multiply it by 1.03, replacing the old value of the variable.

in the last line, use mine on both sides of the assignment

  1. Write a script to take the variable mine with values 5 and multiply it by 1.03 one hundred times using a for loop. Use print() to show the result.

use the for loop structure as defined in the tutorial above place the line of code you wrote in exercise 1 inside the for loop

  1. Use rep() to preallocate a vector of values mine 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.

assign 5 to mine[1] use the for loop structure from exercise 2 use index i to indicated the current element of mine: mine[i]

  1. Fix the indexing error in the script below for calculating a vector of factorials fact. Each element of the vector fact[i] should be equal to \(i!= 1 \times 2 \times 3 ... \times i\), so for example fact[2] should be 2 and fact[3] should be 6.

check what each element is multiplied by