Optimising Monte Carlo simulation

I am trying to run a Monte Carlo simulation on a very complex equation (19 input variables) at about 1-10k iterations. Some of the inputs remain constant throughout the computation, while others (8 variables) are randomised at each iteration using the norminv function and a random probability. As you might imagine, this simulation becomes very expensive as the amount of iterations are increased.
Presently, I am using the parfor function on a 4-core laptop with each run needing around 5min per 1000 iterations. The 8 variable inputs are recalculated at each iteration and then passed to a function that calculates the final answer (although I previously had this in the body of the loop and it didn't make much difference).
My question is, how can I optimise this computation? I would need to run several of these simulations for my project and would like to trim down the time.

Respuestas (1)

Jeff Miller
Jeff Miller el 7 de Dic. de 2020

1 voto

Some suggestions--hopefully not a waste of your time:
  1. Use the profiler to find out where time is spent, so you can focus your speed-up efforts on those portions of the code.
  2. It sounds like 11 of your variables are fixed across all iterations. Maybe you can compute pieces of your very complex equation that use those variables just once in advance, saving yourself recomputation of those same values at each iteration.
  3. "randomised ...using norminv and a random p" It's probably faster to just generate a random number directly rather than generating p and then looking up the corresponding normal.
  4. "randomised at each iteration..." Its probably faster to generate & store vectors of 1-10k random values before starting the iteration process, and then using the stored random value for each iteration. That cuts down on function calling overhead because you only call the random function(s) once rather than 1-10k times.

4 comentarios

Johan Burger
Johan Burger el 8 de Dic. de 2020
Editada: Johan Burger el 8 de Dic. de 2020
Thanks for your help Jeff, your suggestions seem sound. I spent some time today trying to streamline the code and while I saw some improvement, it was not anywhere near what I was expecting. I will address and perhaps clarify my problem by answering your four points:
  1. I ran the profiler first thing, but I'm afraid I don't know how to interpret the results. Some of the functions that are the most demanding seem to be underlying MATLAB functions, which I don't understand.
  2. I followed this piece of advice (in combination with #4) and pre-calculated the variables outside the main for-loop. Although this seemed like a sure winner, it did not make that much of a difference.
  3. I should clarify. The randomised variables are drawn from a known mean and standard deviation value. The reason why I did it this way instead of selecting a random value within the range is to preserve any probabilistic information from the source data. I could be mistaken here.
  4. See point 2.
I have also spoken to a collegue, who advised that parfor is not necessarily faster and that the computations I am performing should be well enough optimised for mulitocore processing. If it would help, I could describe my code in greater detail.
Edit: Grammar
Jeff Miller
Jeff Miller el 8 de Dic. de 2020
Johan, Sorry these suggestions didn't lead to much improvement, but I'm afraid I don't really have any further ideas. If you can post the code and profiler output, maybe someone will spot an issue.
Johan Burger
Johan Burger el 9 de Dic. de 2020
Thank you Jeff. I have thought about my problem and have decided to approach it from another angle. Thanks
Johan Burger
Johan Burger el 10 de Dic. de 2020
Turns out the problem was that I was using the symsum function and calling it 3-4 times per simulation. Instead I just used normal summation and it drastically increased the speed of computation!

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Preguntada:

el 7 de Dic. de 2020

Comentada:

el 10 de Dic. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by