using DataFrames
using Distributions
using FastGaussQuadrature
using ForwardDiff
using LinearAlgebra
using Plots
using QuasiMonteCarloDay 6 In-class Assignment
- This assignment is graded based on effort. Edit this QMD file and submit it within 24 hours to receive full credit.
- If you finish during class, you may leave, work on the weekly assignment, or do your own work.
- Please do not hesitate to ask the instructor any questions.
Question 2: Numerical differentiation in GMM
Suppose
\[ \log(Y_i)=\theta_0+\varepsilon_i, \qquad \varepsilon_i\sim N(0,\sigma^2). \]
Use \(\theta_0=0.4\) and \(\sigma=0.5\). The population moment vector is
\[ g(\theta) = \begin{pmatrix} \theta_0-\theta\\ m-\exp(\theta+\sigma^2/2) \end{pmatrix}, \qquad m=\exp(\theta_0+\sigma^2/2). \]
The optimally weighted GMM criterion is
\[ Q(\theta)=g(\theta)^\prime\Omega^{-1}g(\theta), \]
where
\[ \Omega = \begin{pmatrix} \sigma^2 & \sigma^2m\\ \sigma^2m & m^2\{\exp(\sigma^2)-1\} \end{pmatrix}. \]
The analytical Jacobian and asymptotic variance of \(\sqrt{n}(\widehat{\theta}-\theta_0)\) are
\[ G = \begin{pmatrix} -1\\ -m \end{pmatrix}, \qquad V=(G^\prime\Omega^{-1}G)^{-1}. \]
In this question, finite difference means manually evaluating the moment function at nearby parameter values. The formula below uses a forward, one-sided step. In contrast, ForwardDiff.jl uses forward-mode automatic differentiation and does not select a finite step size.
1. GMM setup
Define the population moments and the GMM criterion. Compute the analytical asymptotic variance.
2. Finite-difference approximation
Use the finite-difference formula
\[ G_h = \frac{g(\theta_0+h)-g(\theta_0)}{h} \]
for \(h=10^{-1},10^{-2},\ldots,10^{-12}\). Use each Jacobian to compute the corresponding asymptotic variance.
3. Automatic differentiation with ForwardDiff.jl
Use ForwardDiff.jl to compute the Jacobian. Compare its asymptotic variance with the analytical value and the finite-difference estimates.