Skip to content

Commit aa43c0b

Browse files
committed
update lecture with new notes
1 parent e92331d commit aa43c0b

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

lectures/calvo_gradient.md

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -944,22 +944,22 @@ in the code.**
944944
945945
**Response note to Humphrey** Shouldn't it instead be $ \vec \beta \cdot \beta \cdot (\vec \mu @ B)^T(\vec \mu @ B)$?
946946
947-
**Response note to Tom**: Thanks so much for pointing this out, you are right! That is what in my code. Sorry for the typo. I think in every case, we have $\vec{\beta} \cdot \vec{\beta}$. Perhaps we can just define:
947+
**Response note to Tom**: Thanks so much for pointing this out. You are right! That is what is in my code. Sorry for the typo. I think in every case, we have $\vec{\beta} \cdot \vec{\beta}$. Perhaps we can just define:
948948
949949
$$
950-
\vec{\beta} = \begin{bmatrix} 1 \\ \beta \\ \vdots \\ \beta^{T-1} \\ \frac{\beta^{T}}{(1-\beta)} \end{bmatrix}
950+
\vec{\beta} = \begin{bmatrix} 1 \\ \beta \\ \vdots \\ \beta^{T-1} \\ \frac{\beta^T}{1-\beta} \end{bmatrix}
951951
$$
952952
953953
Then we have:
954954
955955
$$
956-
\sum_{t=0}^\infty \beta^t \theta_t = \vec{\mathbf{1}} @ (\vec{\beta} \cdot (B @ \vec{\mu}))
956+
\sum_{t=0}^\infty \beta^t \theta_t = \vec{\mathbf{1}}(\vec{\beta} \cdot (B \cdot \vec{\mu}))
957957
$$
958958
959959
and
960960
961-
$$
962-
\sum_{t=0}^\infty \beta^t \theta_t^2 = \vec{\beta} \cdot (\vec{\mu}^T B^T)(B \vec{\mu})
961+
$$
962+
\sum_{t=0}^\infty \beta^t \theta_t^2 = \vec{\beta} \cdot (\vec{\mu}^T B^T) \cdot (B \vec{\mu})
963963
$$
964964
965965
and
@@ -971,9 +971,7 @@ $$
971971
It follows that
972972
973973
$$
974-
\begin{aligned}
975-
J = V - h_0 = \sum_{t=0}^\infty \beta^t (h_1 \theta_t + h_2 \theta_t^2 - \frac{c}{2} \mu_t^2) = h_1 \cdot \vec{\mathbf{1}} @ (\vec{\beta} \cdot (B @ \vec{\mu})) + h_2 \cdot \vec{\beta} \cdot (\vec{\mu}^T B^T)(B \vec{\mu}) - \frac{c}{2} \vec{\mu}^T \vec{\beta} \vec{\mu}
976-
\end{aligned}
974+
J = V - h_0 = \sum_{t=0}^\infty \beta^t (h_1 \theta_t + h_2 \theta_t^2 - \frac{c}{2} \mu_t^2) = h_1 \cdot \vec{\mathbf{1}} (\vec{\beta} \cdot (B \cdot \vec{\mu})) + h_2 \cdot \vec{\beta} \cdot (\vec{\mu}^T B^T) \cdot (B \vec{\mu}) - \frac{c}{2} \cdot \vec{\mu}^T \vec{\beta} \vec{\mu}
977975
$$
978976
979977
So
@@ -983,22 +981,31 @@ $$
983981
$$
984982
985983
$$
986-
\frac{\partial}{\partial \vec{\mu}} \left( h_2 \vec{\beta} \cdot (\vec{\mu}^T M \vec{\mu}) \right) = h_2 \vec{\beta} \cdot 2M \vec{\mu} = 2 h_2 (\vec{\beta} \cdot M \vec{\mu}) \; \text{where } M = B^T B
984+
\frac{\partial}{\partial \vec{\mu}} \left( h_2 \vec{\beta} \cdot (\vec{\mu}^T M \vec{\mu}) \right) = h_2 \vec{\beta} \cdot 2M \vec{\mu} = 2 h_2 (\vec{\beta} \cdot M \vec{\mu}) \quad \text{where } M = B^T B
987985
$$
988986
989987
$$
990988
\frac{\partial}{\partial \vec{\mu}} \left( -\frac{c}{2} \vec{\mu}^T \vec{\beta} \vec{\mu} \right) = -\frac{c}{2} (2 \vec{\beta} \vec{\mu}) = -c \vec{\beta} \vec{\mu}
991989
$$
992990
993-
It follows that
991+
Then we have
994992
995993
$$
996994
\frac{\partial J}{\partial \vec{\mu}} = h_1 B^T \vec{\beta} + 2 h_2 (\vec{\beta} \cdot B^T B \vec{\mu}) - c \vec{\beta} \vec{\mu}
997995
$$
998996
999-
But I think it is safe to ask `JAX` to compute the gradient of $J$ with respect to $\vec \mu$, so we can avoid the manual computation above.
997+
Hence
998+
999+
$$
1000+
\vec{\mu}^R = \left(2h_2 B^T \operatorname{diag}(\vec{\beta}) B - c \operatorname{diag}(\vec{\beta})\right)^{-1} \left(-h_1 B^T \vec{\beta}\right) \; \text{where } \operatorname{diag}(\vec{\beta}) = \vec{\beta} \cdot \mathbf{I}
1001+
$$
1002+
1003+
The function `compute_μ` tries to implement this analytical solution, but the result agrees with our original method at first, then differs at the last few values (please find the function below).
1004+
1005+
I am not yet sure where this discrepancy comes from, but I think it is safe to ask `JAX` to compute the gradient of $J$ with respect to $\vec{\mu}$ because it agrees with our previous lecture and generates the same $V$ value. Hence, it helps us avoid the manual computation above.
1006+
1007+
Please kindly let me know your thoughts on this.
10001008
1001-
Please kindly let me know your thoughts.
10021009
10031010
10041011
**End of Humphrey's note**
@@ -1082,6 +1089,27 @@ def compute_J(μ, β, c, α=1, u0=1, u1=0.5, u2=3):
10821089
βμ_square_sum = 0.5 * c * β_vec * μ.T @ μ
10831090
10841091
return βθ_sum + βθ_square_sum - βμ_square_sum
1092+
1093+
def compute_μ(β, c, T, α=1, u0=1, u1=0.5, u2=3):
1094+
h0 = u0
1095+
h1 = -u1 * α
1096+
h2 = -0.5 * u2 * α**2
1097+
λ = α / (1 + α)
1098+
1099+
A = jnp.eye(T+1) - λ*jnp.eye(T+1, k=1)
1100+
B = (1-λ) * jnp.linalg.inv(A)
1101+
1102+
β_vec = jnp.hstack([β**jnp.arange(T),
1103+
(β**T/(1-β))])
1104+
1105+
A = 2 * h2 * (B.T @ jnp.diag(β_vec) @ B) - c * jnp.diag(β_vec)
1106+
b = - h1 * (B.T @ β_vec)
1107+
1108+
return jnp.linalg.solve(A, b)
1109+
1110+
print('\n', compute_μ(β=0.85, c=2, T=39))
1111+
1112+
compute_V(compute_μ(β=0.85, c=2, T=39), β=0.85, c=2)
10851113
```
10861114
10871115
```{code-cell} ipython3

0 commit comments

Comments
 (0)