Exercise 1: Analytical Forward & Backpropagation for Credit Risk Classification
Calculus & OptimizationFinancial Context: In consumer credit risk underwriting, an institution predicts loan default ($y = 1$) versus repayment ($y = 0$) using financial applicant covariates.
Model Formulation:
Consider a 2-layer Multi-Layer Perceptron (MLP) with:
- Input $x = [x_1, x_2]^T \in \mathbb{R}^2$ where $x_1$ is Debt-to-Income (DTI) and $x_2$ is revolving credit utilization.
- Hidden layer with 2 neurons and ReLU activation $\operatorname{ReLU}(z) = \max(0, z)$: $$z^{[1]} = W^{[1]} x + b^{[1]}, \quad a^{[1]} = \operatorname{ReLU}(z^{[1]})$$ $$W^{[1]} = \begin{pmatrix} 0.5 & -0.2 \\ 0.8 & 0.4 \end{pmatrix}, \quad b^{[1]} = \begin{pmatrix} 0.1 \\ -0.3 \end{pmatrix}$$
- Output layer with 1 neuron and Sigmoid activation $\sigma(z) = \frac{1}{1 + e^{-z}}$ predicting default probability $\hat{y}$: $$z^{[2]} = W^{[2]} a^{[1]} + b^{[2]}, \quad \hat{y} = \sigma(z^{[2]})$$ $$W^{[2]} = \begin{pmatrix} 1.2 & -0.9 \end{pmatrix}, \quad b^{[2]} = -0.1$$
- Loss function: Binary Cross-Entropy $\mathcal{L}(y, \hat{y}) = -[y \ln(\hat{y}) + (1-y)\ln(1-\hat{y})]$.
Questions:
- For applicant $x = [0.6, 0.8]^T$ with true label $y = 1$ (default), calculate $z^{[1]}$, $a^{[1]}$, $z^{[2]}$, the default probability $\hat{y}$, and loss $\mathcal{L}$.
- Prove analytically that $\frac{\partial \mathcal{L}}{\partial z^{[2]}} = \hat{y} - y$.
- Compute exact numerical gradients $\frac{\partial \mathcal{L}}{\partial W^{[2]}}$, $\frac{\partial \mathcal{L}}{\partial b^{[2]}}$, hidden error $\delta^{[1]} = \frac{\partial \mathcal{L}}{\partial z^{[1]}}$, $\frac{\partial \mathcal{L}}{\partial W^{[1]}}$, and $\frac{\partial \mathcal{L}}{\partial b^{[1]}}$.
- Compute updated parameters following one gradient descent step with learning rate $\eta = 0.1$.
View Step-by-Step Mathematical Solution
1. Forward Pass:
$$z_1^{[1]} = 0.5(0.6) + (-0.2)(0.8) + 0.1 = 0.30 - 0.16 + 0.10 = 0.24$$ $$z_2^{[1]} = 0.8(0.6) + 0.4(0.8) - 0.30 = 0.48 + 0.32 - 0.30 = 0.50$$ $$a_1^{[1]} = \max(0, 0.24) = 0.24, \quad a_2^{[1]} = \max(0, 0.50) = 0.50$$ $$z^{[2]} = 1.2(0.24) + (-0.9)(0.50) - 0.1 = 0.288 - 0.450 - 0.100 = -0.262$$ $$\hat{y} = \sigma(-0.262) = \frac{1}{1 + e^{0.262}} \approx \frac{1}{1 + 1.2995} \approx \mathbf{0.4349}$$ $$\mathcal{L}(1, 0.4349) = -\ln(0.4349) \approx \mathbf{0.8327}$$2. Output Gradient Derivation:
$$\frac{\partial \mathcal{L}}{\partial \hat{y}} = -\frac{y}{\hat{y}} + \frac{1-y}{1-\hat{y}} = \frac{\hat{y} - y}{\hat{y}(1-\hat{y})}, \quad \frac{\partial \hat{y}}{\partial z^{[2]}} = \hat{y}(1-\hat{y})$$ $$\frac{\partial \mathcal{L}}{\partial z^{[2]}} = \frac{\partial \mathcal{L}}{\partial \hat{y}} \cdot \frac{\partial \hat{y}}{\partial z^{[2]}} = \frac{\hat{y}-y}{\hat{y}(1-\hat{y})} \cdot \hat{y}(1-\hat{y}) = \mathbf{\hat{y} - y} \quad \blacksquare$$3. Exact Numerical Gradients:
$$\delta^{[2]} = \frac{\partial \mathcal{L}}{\partial z^{[2]}} = 0.4349 - 1 = \mathbf{-0.5651}$$ $$\frac{\partial \mathcal{L}}{\partial W^{[2]}} = \delta^{[2]} (a^{[1]})^T = -0.5651 \times [0.24, 0.50] = \mathbf{[-0.1356, \; -0.2826]}$$ $$\frac{\partial \mathcal{L}}{\partial b^{[2]}} = \delta^{[2]} = \mathbf{-0.5651}$$ $$\delta^{[1]} = ((W^{[2]})^T \delta^{[2]}) \odot \mathbf{1}_{z^{[1]}>0} = \begin{pmatrix} 1.2 \\ -0.9 \end{pmatrix} (-0.5651) \odot \begin{pmatrix} 1 \\ 1 \end{pmatrix} = \mathbf{\begin{pmatrix} -0.6782 \\ 0.5086 \end{pmatrix}}$$ $$\frac{\partial \mathcal{L}}{\partial W^{[1]}} = \delta^{[1]} x^T = \begin{pmatrix} -0.6782 \\ 0.5086 \end{pmatrix} \begin{pmatrix} 0.6 & 0.8 \end{pmatrix} = \mathbf{\begin{pmatrix} -0.4069 & -0.5425 \\ 0.3052 & 0.4069 \end{pmatrix}}$$ $$\frac{\partial \mathcal{L}}{\partial b^{[1]}} = \delta^{[1]} = \mathbf{\begin{pmatrix} -0.6782 \\ 0.5086 \end{pmatrix}}$$4. Gradient Descent Updates ($\eta = 0.1$):
$$W^{[2]\prime} = [1.2, -0.9] - 0.1[-0.1356, -0.2826] = \mathbf{[1.2136, \; -0.8717]}$$ $$b^{[2]\prime} = -0.1 - 0.1(-0.5651) = \mathbf{-0.0435}$$ $$W^{[1]\prime} = \begin{pmatrix} 0.5 & -0.2 \\ 0.8 & 0.4 \end{pmatrix} - 0.1 \begin{pmatrix} -0.4069 & -0.5425 \\ 0.3052 & 0.4069 \end{pmatrix} = \mathbf{\begin{pmatrix} 0.5407 & -0.1458 \\ 0.7695 & 0.3593 \end{pmatrix}}$$ $$b^{[1]\prime} = \begin{pmatrix} 0.1 \\ -0.3 \end{pmatrix} - 0.1 \begin{pmatrix} -0.6782 \\ 0.5086 \end{pmatrix} = \mathbf{\begin{pmatrix} 0.1678 \\ -0.3509 \end{pmatrix}}$$