---
title: "Day 6 In-class Assignment"
engine: julia
format:
  html:
    toc: true
    embed-resources: true
execute:
  echo: true
  eval: true
  warning: false
  message: false
---

::: {.callout-note}
## Submission and grading

- 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.
:::

```{julia}
using DataFrames
using Distributions
using FastGaussQuadrature
using ForwardDiff
using LinearAlgebra
using Plots
using QuasiMonteCarlo
```

## Question 1: Market-share integration

One market contains three inside goods and an outside good. The outside-good utility is normalized to zero. For inside good $j$,

$$
u_{ij}
=
\delta_j+\mu_{ij}+\varepsilon_{ij},
$$

where

$$
\delta_j
=
\beta x_j-\alpha p_j
$$

and

$$
\mu_{ij}
=
-\sigma_p p_j\nu_{i1}
+\sigma_xx_j\nu_{i2},
\qquad
(\nu_{i1},\nu_{i2})\sim N(0,I_2).
$$

The conditional choice probability and market share are

$$
P_j(\nu_i)
=
\frac{\exp(\delta_j+\mu_{ij})}
{1+\sum_{\ell=1}^3\exp(\delta_\ell+\mu_{i\ell})},
\qquad
s_j=E_\nu[P_j(\nu)].
$$

Use the supplied product data and parameters.

```{julia}
product_data = (
    price = [1.0, 1.4, 1.8],
    characteristic = [0.5, 1.2, 0.9],
)

demand_parameters = (
    price_coefficient = 1.5,
    characteristic_coefficient = 0.5,
    price_standard_deviation = 0.4,
    characteristic_standard_deviation = 0.6,
)
```

### 1. Conditional choice probabilities

Define a function that returns the three conditional choice probabilities for a given normal draw.

```{julia}

```

### 2. Gauss--Hermite reference values

Use 30-node Gauss--Hermite quadrature in each dimension to compute reference market shares. These are numerical reference values, not analytical solutions.

```{julia}

```

### 3. Halton convergence

Generate one 4,096-point sequence of two-dimensional Halton draws. Using prefixes of this sequence, compute the inside-good shares for $N=32,64,\ldots,4096$ joint draws.

For each product, plot the estimated share against $N$. Add the corresponding Gauss--Hermite reference share as a horizontal line and summarize the convergence pattern.

```{julia}

```

## 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.

```{julia}

```

### 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.

```{julia}

```

### 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.

```{julia}

```
