Programming lesson
Mastering Monte Carlo Simulations in MATLAB: A Step-by-Step Guide for ECMT2160
Learn how to tackle Monte Carlo simulation assignments in MATLAB with this comprehensive tutorial. Covers joint probability mass functions, conditional expectations, and the Law of Iterated Expectations using real-world examples like gaming dice rolls and AI risk models.
Introduction: Why Monte Carlo Simulations Matter in 2026
Monte Carlo simulations are everywhere in 2026—from AI-powered weather forecasting to risk analysis in fintech apps. In your ECMT2160 Computational Assignment, you'll use MATLAB to simulate dice rolls and explore probability concepts. This tutorial will guide you through the key steps without giving away the exact answers. Whether you're working individually or in a pair, mastering these techniques will help you earn top marks.
Setting the Stage: Your Student ID as a Random Seed
Before you start, run rng(STUDENTID) in MATLAB. This fixes the random number generator so your results are reproducible. Think of it like setting a seed in a Minecraft world—same seed, same world. Your 9-digit student ID ensures your simulation is unique to you.
Question 1: Joint and Marginal Distributions of Dice Rolls
Imagine you're playing a popular battle royale game where two dice determine your loot: the sum (X1) and the maximum (X2). Let's break down the probability structures.
Part (a): Joint Probability Mass Function
Create an 11×6 matrix for P(X1 = x1, X2 = x2). Row index = sum (2 to 12), column index = max (1 to 6). Use nested loops or vectorization. For example, the probability that sum=7 and max=4 is 2/36 (rolls (3,4) and (4,3)). Display with a 3D bar graph using bar3.
% Example structure (not full solution)
X1 = 2:12; X2 = 1:6;
jointPMF = zeros(11,6);
% Fill in using loops or logical indexing
% Then: bar3(jointPMF); xlabel('X2 (max)'); ylabel('X1 (sum)');Part (b): Marginal PMF of X2
Sum over rows of jointPMF to get P(X2). For max=6, probability = 11/36. Use bar for a 2D graph. This is like calculating the chance of getting a legendary item in a game based on max die.
Part (c): Conditional PMF of X1 given X2
Divide each column of jointPMF by the marginal probability of that X2. For X2=1, the only possible sum is 2 (probability 1). Create six separate bar graphs, one for each X2 value. This helps you see how the sum distribution changes when you know the max.
Part (d): Monte Carlo Simulation of Conditional Expectation
Run a loop 10,000 times. Each iteration: generate a random X2 from its marginal distribution (use randi(6,1)), then compute E[X1 | X2] using the conditional PMF. Average these expectations. This is like simulating many rounds of a game to estimate average loot value.
N = 10000;
sumCondExp = 0;
for i = 1:N
x2 = randi(6); % or sample from marginal
condExp = dot(X1, conditionalPMF(:, x2));
sumCondExp = sumCondExp + condExp;
end
avgCondExp = sumCondExp / N;Part (e): Law of Iterated Expectations
The Law of Iterated Expectations (LIE) states E[E[X1|X2]] = E[X1]. Your average conditional expectation should be close to the theoretical E[X1] = 7. In 2026, LIE is used in AI reinforcement learning to decompose value functions. Discuss how your simulation confirms this law.
Question 2: Continuous Bivariate Normal Transform
Now we step into continuous distributions. Let Z be standard normal, with CDF Φ(z). The joint PDF of (Y1, Y2) is defined using a function g and the standard normal PDF.
Part (a): Joint PDF Graph
Create a grid for y1 and y2 from -3 to 3. Compute the joint PDF using the formula: f(y1,y2) = g(Φ(y1), Φ(y2)) * φ(y1) * φ(y2). Use meshgrid and surf for a 3D plot. This is like visualizing the likelihood of two correlated AI model outputs.
[Y1, Y2] = meshgrid(-3:0.1:3);
phi = @(x) exp(-0.5*x.^2)/sqrt(2*pi);
Phi = @(x) 0.5*(1+erf(x/sqrt(2)));
g = @(u1,u2) 4*(u1.*u2).^4 .* exp(-3*(u1+u2)-1).^(-7/3) .* (u1>0 & u1<1 & u2>0 & u2<1);
f = g(Phi(Y1), Phi(Y2)) .* phi(Y1) .* phi(Y2);
surf(Y1, Y2, f);Part (b) and (c): Marginal PDFs
Integrate the joint PDF over y2 to get marginal f1(y1). Use numerical integration like trapz. Plot alongside standard normal PDF φ(y1). The marginal may not be standard normal due to the transformation. Compare shapes—this is like comparing the distribution of a single AI model's output after a complex transformation.
y1_vals = -3:0.1:3;
f1 = zeros(size(y1_vals));
for i = 1:length(y1_vals)
f1(i) = trapz(y2_vals, f(i,:));
end
plot(y1_vals, f1, y1_vals, phi(y1_vals));Part (d): Is the Joint Distribution Multivariate Normal?
Look at your graphs. If the contours are elliptical and both marginals are normal, it could be bivariate normal. But here, the marginals may deviate from normal, and the joint PDF may not be bell-shaped. In 2026, many AI models assume multivariate normality for simplicity, but real data often violates this. Discuss why your results suggest non-normality.
Tips for Submission
- Use MATLAB Live Script (.mlx) for a clean mix of text and code.
- Export to HTML as required.
- Work in pairs but ensure submissions are not identical—discuss concepts, not code.
- Proofread for typos and clarity.
Conclusion
Monte Carlo simulations are a powerful tool in statistics and machine learning. By completing this assignment, you'll gain hands-on experience with probability distributions, conditional expectations, and the Law of Iterated Expectations—skills that are highly relevant in fields like quantitative finance, data science, and AI. Good luck!