Monte Carlo Risk Simulator
Operator: Abdulla Al Fahad. How tools are built and checked
A project plan with one number per task hides how wide the outcome really is. Give each uncertain item a minimum, a most likely value and a maximum, add the chance of a risk event, and let the computer draw thousands of possible projects: the spread of the totals is the answer. What you read off is a percentile, such as the cost you would stay under four times in five, or the chance of crossing a target you care about.
Key features
- Eight distributions: normal, lognormal (entered as the variable's own mean and standard deviation), uniform, triangular, PERT, Bernoulli events, discrete lists and empirical columns from a CSV or XLSX
- Plain Monte Carlo or Latin hypercube sampling, 1,000 to 100,000 iterations, with a seed so the same inputs give the same results
- Rank correlations between inputs by the Iman-Conover method, with a clear warning when the requested matrix cannot exist
- Percentiles from P1 to P99, the chance of being above or below a target with its standard error, a histogram, a cumulative curve, a tornado chart and a convergence chart
- Several outputs in one run, each able to use the earlier ones, with an iterations CSV and a summary XLSX to export
How inputs are sampled, correlated and combined
Each input is sampled through its inverse cumulative distribution function: a uniform number between 0 and 1 is turned into a value. For plain Monte Carlo the uniform numbers are independent. For Latin hypercube sampling the 0-to-1 range is cut into one slice per iteration, one draw is taken inside each slice, and the slices are shuffled, so every part of every distribution is covered evenly. Normal values use a rational approximation of the inverse normal CDF accurate to about 1 part in a billion. PERT uses a beta distribution with shape 1 + 4(most likely - min)/(max - min) and 1 + 4(max - most likely)/(max - min), read from a 16,384-point quantile table built once per input.
Correlation is applied after sampling. The page converts your Spearman rank correlations to the equivalent correlations of normal scores (r = 2 sin(pi times rho / 6)), builds scores with that correlation, and reorders each input's sorted values to follow the ranks of the scores. The marginal distributions are untouched, because only the order changes. A requested matrix that is not positive semi-definite, such as three variables that are each strongly correlated with the others in opposite directions, is replaced by the nearest valid one, and the page says how far the largest entry moved.
Formulas are parsed by mathjs and never evaluated by it. The syntax tree is checked against an allowlist (numbers, your input and output names, pi, e, tau, arithmetic, comparisons, and, or, not, the conditional a ? b : c and about 25 named functions) and compiled to plain closures. Assignment, function definitions, indexing, objects, matrices, units and any other function are rejected with a message naming the problem.
How it works
The run happens in a web worker so the page stays responsive, and Cancel stops the worker at once. Samples for every input are drawn first, then correlations are imposed, then each output formula is evaluated for every iteration in order, so a later output can use an earlier one.
Statistics skip any iteration whose result is not a finite number, for example a division by zero, and report how many were skipped. Percentiles interpolate between the sorted values. The tornado chart shows the Spearman rank correlation of each input with the output, and the table in the export gives each input's share of the squared correlations.
The convergence chart plots the running mean with a 95 percent band at about 40 checkpoints. If the band is still moving at the end, raise the iteration count.
When to use it
- A construction estimator has three-point numbers for eight work packages. She runs 50,000 iterations with the packages that share a crew correlated at 0.6 and reads the P80 as the contingency figure.
- A product manager models 5-year NPV with lognormal volume, triangular price and normal unit cost, with volume and price correlated at -0.4. About 72 percent of iterations come out below zero, and the tornado shows volume as the main driver.
- A reliability engineer loads a column of 400 measured repair times as an empirical input and combines it with a Bernoulli parts-delay event to get the 90th-percentile downtime.
- A finance analyst wants to know whether 5,000 iterations are enough for a board paper. She runs the NPV model under plain Monte Carlo and under Latin hypercube with the same seed: when the two P50s agree to the digits she reports, the run has settled, and when they differ she raises the count.
Inputs
- Pick an example or start from the default, then name each input and choose its distribution.
- Enter the parameters; for an empirical input choose a file and the first column with at least five numbers is used.
- Write the output formulas using the input names, for example design + build + delay * penalty.
- Set the iterations, sampling method, seed and, if you want one, a target such as the chance that total is above 600.
- Press Run simulation and read the percentiles, the target probability and the tornado chart.
Worked example
The Project cost estimate example
design PERT(80, 100, 150), build triangular(300, 400, 700), delay Bernoulli(0.3), penalty discrete 0/50/120 with weights 0.5/0.3/0.2, design and build correlated at 0.5; total = design + build + delay * penalty; 20,000 iterations, Latin hypercube, seed 12345; target: total above 600Percentiles, target odds and drivers
Mean 583.3, standard deviation 97.2, P10 464.7, P50 571.8, P90 721.9, range 383.3 to 939.0. The chance that total is above 600 is 40.4% (standard error 0.35 points). The tornado ranks build at 0.94, design 0.57, delay 0.17 and penalty 0.13. The achieved design/build rank correlation is 0.502 against 0.5 requested.Tips for best results
- Use the same seed when comparing two versions of a model, so a change in the answer comes from the model and not from new random numbers.
- Enter lognormal inputs as the quantity's own mean and standard deviation, such as mean 100 and standard deviation 40; the page converts them to the underlying normal's parameters.
- Add a correlation only where you can justify it. Leaving two inputs at 0 treats them as independent, which narrows the output when they really move together.
Limits
The results describe the model you entered. A distribution that is too narrow gives a confident-looking range that the real project never promised.
Probabilities such as 40.4 percent carry a sampling error that shrinks with more iterations; the standard error is shown next to it, and it does not include any error in your inputs.
An empirical distribution interpolates between the sorted data values, so it can produce values between observed ones but never outside the observed minimum and maximum.
Common errors and how to fix them
- "x: \"y\" is not an input, an earlier output or a known constant (pi, e, tau)."
- A formula uses a name that is not defined. Check spelling; names are case-sensitive, and an output can only use outputs listed above it.
- "The requested correlations cannot all hold at once."
- The matrix of correlations is not positive semi-definite. The run continues with the nearest valid matrix, shown in the results; reduce the strongest entries to avoid the adjustment.
- "The most likely value must lie between the minimum and the maximum."
- A triangular or PERT input has a mode outside its range. Fix the three numbers so that min is at most the most likely value, and that is at most max.
Best practices
- Run once at 5,000 iterations while you build the model and again at 50,000 for the figures you report.
- Report the percentile and the sampling method together, for example P80 from 50,000 Latin hypercube iterations with seed 12345.
Accuracy against exact answers
Two independent normals, N(10, 2) and N(20, 3), summed over 100,000 iterations with seed 12345, give a mean of 30.0047 and a standard deviation of 3.5913 under plain Monte Carlo (exact values 30 and 3.6056), and 30.0000 and 3.6030 under Latin hypercube. A PERT(10, 20, 60) input gives a mean of 25.000 and a standard deviation of 8.661 against the exact beta values 25 and 8.660. For correlation, three standard normals at 10,000 iterations with Latin hypercube and the same seed, requested at 0.7, -0.4 and 0.1, come back as 0.7012, -0.3974 and 0.1007, a worst error of 0.0026.
Frequently asked questions
Why does the same seed give the same numbers?
The random numbers come from a seeded generator, xoshiro128**, not from the browser's own random source. The seed fixes the whole sequence, so the same model, seed and iteration count always give identical results.
When should I use the CPM and PERT Schedule page instead?
That page simulates task durations through a network of dependencies and gives finish dates and criticality. This one simulates any cost, schedule or NPV formula you write, with no task network.
Can a formula run code?
No. Formulas are parsed into a syntax tree and compiled from an allowlist of operators and about 25 named functions. Anything else is rejected before a single iteration runs.