Week 9 Homework Feedback Summary: Numerical Approximation & Simulation
Week 9 Homework Feedback Summary: Numerical Approximation & Simulation
Overview
Topic: Monte Carlo OLS Simulation & Numerical Integration (Trapezoidal vs Simpson’s Rule) Total Submissions Assessed: 21 Grade Distribution:
- ✅ Pass: 19 students (90%)
- ⚠️ Partial: 2 students (10%)
- ❌ Fail: 0 students (0%)
Overall, the submissions demonstrated a strong understanding of the Monte Carlo method and the basic principles of numerical integration. Most students correctly implemented the OLS simulation and observed the consistency of the estimator. The main differentiator in this assignment was the technical implementation of Simpson’s Rule, specifically handling grid parity.
🚨 Critical Issues (Must Fix)
1. Simpson’s Rule Parity Requirement
The Issue: Simpson’s 1/3 rule requires an odd number of grid points (which implies an even number of intervals). The homework requested simulations for $N \in {50, 200, 1000}$, which are all even numbers. The Error: Many students applied the standard formula blindly to these even grids.
- Result: This leads to either an index out of bounds error, skipping the last point, or applying the wrong weight to the final interval.
The Fix:
You must check the grid size and adjust if necessary:
if mod(N, 2) == 0 % Grid is even (intervals odd). Strategy: Drop the last point. c_simp = c(1:end-1); y_simp = y(1:end-1); h_simp = c_simp(2) - c_simp(1); % Apply formula to c_simp... else % Grid is odd. Proceed normally. endAlternative: Interpolate to create a new point (N+1) or use the Trapezoidal rule for the single last interval.
2. MATLAB File Structure
The Issue: Defining a function at the top of a script file. The Error:
function out = myFunc(in) ... end
% Script code follows...
clear; clc;
Why it’s wrong: In MATLAB, if a function is at the top, the file is treated as a function file, not a script. Variables defined in the “script” portion are local to that function and not available in the workspace. The Fix:
- Option A: Put functions at the very end of the script.
- Option B: Save functions in separate
.mfiles (e.g.,montecarlo_ols.m).
⚠️ Frequent Slips (Watch Out)
1. Random Number Generation Scaling
The Issue: Generating error terms with standard deviation $\sigma$.
The Error: u = randn(n, sigma^2);
- Why:
randn(d1, d2)creates a matrix of size $d1 \times d2$. If $\sigma^2 = 1$, it works by accident. If $\sigma=2$, it creates an $n \times 4$ matrix! The Fix:u = sigma * randn(n, 1);(Scale the standard normal draws).
2. OLS Formula
The Issue: Calculating statistics before the simulation.
The Error: Computing mean(beta1_hat) immediately after beta1_hat = zeros(R,1).
- Result: You get a mean of 0, regardless of the simulation. The Fix: Ensure statistics are calculated after the loop that fills the vector.
🏆 Best Practices Observed
- Robust Algorithms: Explicitly checking
if mod(N,2)==0(seen in Sofia Bruga, Alessandra Nishaj, Kamila Murzabulatova, Iusupova Farangizbegim, Klea Lushaj, Alice Ciavatta, Matteo Pugliese, Simone Iudice, Felice Urciuoli). - Hybrid Integration: Handling even grids by using Simpson’s rule for the first $N-1$ points and the Trapezoidal rule for the final interval (Islomjon Shermirzaev, Giovanni Di Miele). This is a very sophisticated and correct approach that uses all data points.
- Re-discretization: Creating a new grid with $N+1$ points when $N$ is even to satisfy Simpson’s requirement (Kamila Murzabulatova, Alessandra Nishaj).
- Log-Log Convergence Plots: Plotting the absolute error on a log-log scale to visualize the order of convergence ($O(N^{-2})$ vs $O(N^{-4})$) (Islomjon Shermirzaev, Alessandra Nishaj).
- Coverage Probability: Going beyond the requirements to calculate the coverage probability of the confidence intervals (Kamila Murzabulatova).
💡 Teaching Points for Class
- Numerical “Fine Print”: Algorithms often have strict requirements (like parity for Simpson’s). Coding is not just translating a formula; it’s managing these constraints.
- Script vs. Function: Clarify the difference between a script (sequence of commands) and a function (black box with inputs/outputs).
- Visualization: The power of log-log plots for analyzing convergence rates of numerical methods.