I'm using Mathematica to double-check that I correctly derived first order conditions for the models I'm researching. Before I get started, I wanted to actually teach myself how to use Mathematica, and check that I can derive FOC's for a model I know. It's the (fairly simple) stochastic growth model with additive labor. I'm solving the model by setting up the following Lagrangian function.

In[24]:= sgmodEq1 = 
 util[c[t], l[t]] == (c[t]^(1 - \[Phi])/(1 - \[Phi])) + 
   Subscript[\[Chi], 0] z[t] ((1 - l[t])^(1 - \[Chi])/(1 - \[Chi]))

In[25]:= sgmodEq2 = 
 f[k[t], l[t]] == a[t] *k[t]^\[Alpha]*(z[t]*l[t])^(1 - \[Alpha])

In[27]:= sgmodEq3 = \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(t = 0\), \(\[Infinity]\)]\(
\*SuperscriptBox[\(\[Beta]\), \(t\)]*\((util[c[t], 
      l[t]] + \[Lambda][
       t]*\((f[k[t], l[t]] - c[t] + \((1 - \[Delta])\)*k[t] - 
        k[t + 1])\))\)\)\)

In[28]:= sgmodEq4 = D[sgmodEq3, k[t + 1]]

Out[28]= \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(k[1] = 0\), \(\[Infinity]\)]\(
\*SuperscriptBox[\(\[Beta]\), \(K[1]\)] \[Lambda][K[1]] \((\(-
\*SubscriptBox[\(\[Delta]\), \(t, K[1]\)]\) + \((1 - \[Delta])\) 
\*SubscriptBox[\(\[Delta]\), \(1 + t, K[1]\)] + 
\*SubscriptBox[\(\[Delta]\), \(1 + t, K[1]\)] 
\(\*SuperscriptBox[\(f\), \((1, 0)\)]\)[k[K[1]], l[K[1]]] - 

If you're not familiar with the problem, you should know this is not the correct answer by the fact that $\delta$ is a function of K[1], which is now capitalized for some reason?

Before I set up the Lagrangian, I checked that I was able to correctly take partial derivatives of the utility function and production function. Afterward, I also checked that if I break the problem up into pieces, without the summation, I could get the correct answer, as follows:

In[29]:= sgmodEq5 = \[Beta]^
     l[t]] + \[Lambda][
      t]*(f[k[t], l[t]] - c[t] + (1 - \[Delta])*k[t] - k[t + 1]))

In[30]:= sgmodEq6 = \[Beta]^(
  t + 1)*(util[c[t + 1], 
     l[t + 1]] + \[Lambda][
      t + 1]*(f[k[t + 1], l[t + 1]] - 
       c[t + 1] + (1 - \[Delta])*k[t + 1] - k[t + 2]))

In[31]:= sgmodEq7 = D[sgmodEq5, k[t + 1]]

Out[31]= -\[Beta]^t \[Lambda][t]

In[32]:= sgmodEq8 = D[sgmodEq6, k[t + 1]]

Out[32]= \[Beta]^(1 + t) \[Lambda][1 + t] (1 - \[Delta] + 
RowBox[{"1", ",", "0"}], ")"}],
MultilineFunction->None]\)[k[1 + t], l[1 + t]])

The solution to sgmodEq4 is sgmodEq7+sgmodEq8.

Also, I wish I could attach a notebook, because the code looks super messy. Basically, I'm doing this:

$ \begin{align*} \mathscr{L} &= \sum_{t=0}^\infty \beta^t \left( \left( \frac{C_t^{1-\phi}}{1-\phi} + \chi_0 Z_t \frac{(1-L_t)^{1-\chi}}{1-\chi} \right) \right. \\[1ex] &+\left. \lambda_t \left( A_t K_t^{\alpha} (Z_t L_t)^{1-\alpha} - C_t + (1-\delta) K_t - K_{t+1}\right) \right) \end{align*} $

And this is the result I should be getting:

$ \frac{\partial \mathscr{L}}{\partial k_{t+1}}:\ \lambda_t = \beta \ \lambda_{t+1} \left( \alpha A_t K_t^{\alpha-1} (Z_t L_t)^{1-\alpha} + (1-\delta) \right) $

And, instead, I am getting this nonsense:

$ \sum_{K[1]=0}^\infty \beta^{K[1]} \lambda[K[1]] \left( -\delta_{t,K[1]} + (1-\delta) \delta_{1+t,K[1]} + \delta_{1+t,K[1]} f^{(1,0)}[k[K[1]],l[K[1]]] \right) $

But, I can get the right answer if I break the problem into parts. So, my questions are: (1) Why do I get the wrong answer (and such a strange answer) when I put all the pieces together?

And, (2) how can I adjust my code so that I can correctly take the partial derivative of the Lagrangian function, including the summation, and get the right answer?

  • 1
    $\begingroup$ Does Assuming[t ∈ NonNegativeIntegers, D[sgmodEq3, k[t + 1]]] help? $\endgroup$
    – Carl Woll
    Commented Jun 24, 2021 at 22:45
  • 5
    $\begingroup$ I would avoid using subscripts until you have solved all your other problems $\endgroup$
    – mikado
    Commented Jun 25, 2021 at 6:48

1 Answer 1


Changing Subscript[\[Chi], 0] to \[Gamma], and using Assuming[t ∈ NonNegativeIntegers, D[sgmodEq3, k[t + 1]]] gives me this result:

$ -\beta^t \left( \lambda_t - \beta \lambda_{t+1} + \beta \delta \lambda_{t+1} - \beta \lambda_{t+1} f^{(1,0)}(k_{t+1},l_{t+1}) \right) $

which is the correct answer when I set it equal to zero, rearrange and simplify.

(I guess now I just need to learn how to get Mathematica to group terms, keep terms as I originally grouped them and simplify.)

Thank you all!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.