Value Function Iteration question

Hi,

I am new to Python and I am starting to learn about dynamic programming. I was going through an example on how to do value function iteration in stochastic environment. This is the code for the loop for simple neoclassical growth model, with two states capital K and productivity A (where Nk and Na denote the number of grid points, respectively):

I have some trouble understanding how Python stores the results: in this case, utility would be initially Nk by (Na x Nk) matrix, if stacked horizontally rather than vertically as in this example here (slide 31). So, my question is does U[:,j]=u(c) in the code above rewrite u(c) for each state Na as a column vector, or how does it work?

Thanks in advance!

Hi Blue,

I think your understanding is mostly accurate.

Although the code defining K is not provided, I assume it is a vector with the length Nk below.

The line you are referring to is assigning the result of u(C) the j^{th} column of U (U[:, j] = u(C)). This is called indexing in NumPy. Because we have created matrices with zeros before the for loop (U = np.zeros((Nk, Na))), we can index the Nk \times Na matrix U and assign the vectors as the j^{th} columns.

Hope this helps you understand the code. Please let me know if there are any follow-up questions.

Thank you very much, Humphrey! It is clear now.