Advanced Octave A27 — Reproducible Monte Carlo simulation
Advanced GNU Octave with GX-TXT · Module 05
A Monte Carlo simulation allows you to observe how a distribution of inputs translates into a distribution of results. To interpret it you must declare probability model, covariance, number of simulations and random generator.
Objectives and prerequisites
We repeat the sum and synthetic covariance of the lesson A26, along with the numerical skills of the introductory course. We compare a simulation with the theoretical variance 0,15. The test uses GX-TXT dev112-P011-bis and GNU Octave 9.4.0.
Simultaneous observations are not measures. We use normal zero-average inputs with C = [0,04, 0,01; 0,01, 0,09] covariance matrix; the choice of distribution is part of the model, not a property deducted from real data.
Generate related couples
Import monte_carlo.m, record it in the Session and run it in the Runner.
rng(2026,'twister');
C = [0.04,0.01;0.01,0.09];
L = chol(C,'lower');
n = 10000;
samples = L*randn(2,n);
y = samples(1,:)+samples(2,:);
stats = gxtxt_api_call('NUMERIC_API_v1','statistics',{y});
chol(C,'lower') builds a L matrix such that C = L*L', when the matrix is suitable for decomposition. By multiplying two rows of independent normal numbers for L, the program produces couples with the desired covariance in the model. It then suppresses the components and asks for statistics at the public numerical service.
In the test with seed 2026 and 10,000 samples the simulated average is about 0,002950 and the sample variance 0,148250, near 0,15. They are not exactly the same: a finished random sample has its own variability. The seed makes the sequence repeatable in the context of the generator and the version used; it does not guarantee identity between implementations or different versions.
Comparison and limits
For the linear sum of A26 the theoretical variance is known and the comparison is directed. In a non-linear model, Monte Carlo can describe forms of distribution that a local propagation of first order does not capture well, but the result remains conditioned to the distributions and dependencies chosen in the input. Increased samples generally reduces the simulation error; it does not correct a wrong probability model.
Exercise
- Why does the code use a factorization of covariance?
- Does the Monte Carlo variance have to be identical to
0,15with a finite number of samples? - Does the seed show that the chosen distribution correctly describes a real experiment?
Worked solution
- To transform independent normal samples into pairs with the covariance specified by the model.
- No. The result is an estimate subject to sample variability.
- No. The seed helps to repeat the simulation; the hypotheses of the model must be justified separately.
Other steps of this project
- Advanced Octave A75 — Dynamical systems in phase space
- Advanced Octave A81 — Geography of the waves and a 3D surface
- Advanced Octave A82 — Age, sex and demographic inequalities
- Advanced Octave A83 — All-cause mortality, certified COVID deaths and surveillance
- Advanced Octave A84 — A reproducible public account
- Advanced Octave A73 — Optimization landscapes and 3D trajectories