MATLAB: Monte Carlo

monte carlo

[EDIT: Thu May 12 22:03:30 UTC 2011 Duplicate Removed – MKF]
Hey guys,
how can I let certain parameters in my model have a range using Monte Carlo simulations?
I have a relatively simple model and I would want to designate a range to certain parameters. I would like to create scenarios like holding one parameter in its lowest part of the range while another in its highest part, etc.
Cheers, Ruben

Best Answer

This example is what you're looking for: http://www.vertex42.com/ExcelArticles/mc/MatlabMCExample.html
MC is computationally heavy. For this example taken from the link I repeat the procedure 100,000 times drawing samples of 1000. It takes 13 seconds using very low amount of memory.
In MC in general the more you have the better, thus you could optimize this example to generate rand(n,something) to fit the 80% of your memory and reduce the number of loops.
tic
n = 1000;
l = 1e5;
s.ymean = zeros(n,1);
s.ystd = s.ymean;
for ii = 1:l
x1 = randn(n,1) * 5 + 100;
x2 = rand (n,1) * 10 + 5;
y = x2.^2 ./ x1;
s.ymean(ii) = mean(y);
s.ystd (ii) = std(y);
end
toc