This quiz is set to take you no more than 30 minutes. It does not require any packages.

Question 1: Debugging a for loop

The code below is inteded to implement a for loop to calculate the sum of all elements in a vector named q1vec. It contains one major error which will make the answer incorrect. What is the error?

q1vec <- rnorm(n=100, mean=0, sd=1) # generate 100 random numbers from a Normal distribution with mean 0 and sd 1
sum_of_q1vec <- 0 # initialize the object which will hold the answer
for(i in length(q1vec)) {
  sum_of_q1vec <- sum_of_q1vec + q1vec[i]
}

Question 2: An iterative calculation

Let’s generate a capital accumulation path according to the following equation: \[ K_{t+1} = 0.9 K_t + X_t .\] You’ll learn more about capital accumulation in Macro Theory. The idea is that we can accumulate capital (e.g. “useful machines for producing cloth”) by investing (that’s \(X_t\)), but capital depreciates due to wear and tear so we only get to keep about 90% of what we had in the previous time period (that’s why we have the \(0.9 K_t\) on the right-hand side of the equation).

Suppose we invest 1 unit every period, i.e. \(X_t = 1\) always. Use a for loop to simulate the capital accumulation process for 100 time periods.

What is the amount of capital at time period 100? (Report your answer to 4 decimal places.)

K <- rep(0,length.out=100)

for(time in 1:(length(K)-1) ) { # note that we can stop at the 99th entry in the vector since we're going to fill the time+1 slot. 99+1 = 100, so that's good enough.
  # FILL THIS LINE IN. WHAT SHOULD GO HERE?
}

K[100] # check the 100th element of K

Question 3: A nonlinear iterative calculation

Now let’s repeat the same calculation, but with a small change that will make it effectively impossible to calculate the accumulation path by hand (but very easy by modifying your for loop). The new accumulation equation is \[ K_{t+1} = 0.9 K_t + K_t^{0.5} + X_t .\] The new term, \(K_t^{0.5}\), represents the productive output of our capital which we reinvest. Again, you will learn more about this in Macro Theory.

Suppose we invest 1 unit every period, i.e. \(X_t = 1\) always, in addition to the reinvestment happening through \(K_t^{0.5}\). Use a for loop to simulate the capital accumulation process for 100 time periods.

What is the amount of capital at time period 100? (Report your answer to 4 decimal places.)

K <- rep(0,length.out=100)

for(time in 1:(length(K)-1) ) { # note that we can stop at the 99th entry in the vector since we're going to fill the time+1 slot. 99+1 = 100, so we stop at 99=length(K)-1.
  # FILL THIS LINE IN. WHAT SHOULD GO HERE?
}

K[100]
## [1] 0