📜 ⬆️ ⬇️

Monte Carlo simulation in Mathcad Express

On Habré many articles are devoted to Monte Carlo algorithms, for example, this one, yesterday . Both the basic idea and the implementation of the methods are very simple, but the lack of suitable tools for modeling can be a small obstacle. For those readers, for whom the problem is relevant, I advise you to use the free math editor Mathcad Express, about which I write in my blog .

Mathcad Express is the “light” version of the well-known PTC Mathcad Prime package, in which most of the functionality is turned off. Nevertheless, pseudo-random number sensors remain available, which makes it possible to implement (fairly quickly and visually) various statistical models based on Monte-Carlo algorithms. Immediately, I’ll make a reservation that some solutions will not be the best from the point of view of users of the commercial version of Mathcad Prime, however, they are guaranteed not to take us beyond the functionality of the free Mathcad Express.

Let me remind you that Monte Carlo algorithms are a generic name for a group of numerical methods based on programmatically creating a specific sequence of pseudo-random numbers that simulate a particular effect, for example, a sequence of equipment failures . Having received a large number of realizations of the random process, one can hope that its probabilistic characteristics will coincide with similar values ​​of the real-world problem being solved. The file with further calculations in the Mathcad and XPS formats is here .
')

Part 1. How to generate a sample of pseudo-random numbers


In Mathcad Express, a number of pseudo-random number generators are available, creating pseudo-random data samples with different distribution laws. To create a vector of N pseudo-random numbers, only one line of the Mathcad document is needed. For example, to generate N = 5 pseudo-random numbers with a normal distribution (zero mean and unit variance) can be as follows:



It is convenient to visualize the vectors of random numbers on the graphs as follows: one sample (ie, components of one of the random T1 vectors) on the abscissa axis, and another sample (another random vector T2 ) - on the ordinate axis. The following figure shows graphs of pairs of pseudo-random numbers for the exponential (left) and normal (right) distribution. Distribution parameters are given in the formulas above the graphs.




The following figure shows similar pairs of pseudo-random numbers G1 and G3 with a normal distribution and different variance (graph on the left) and the method of generating correlated pseudo-random numbers G1 and G4 (graph on the right).



Generally speaking, quite a few pseudo-random number sensors are available in Mathcad Express:


Part 2. How to perform statistical calculations


The simplest formulas of statistics can be calculated sample estimates of mean values, variances and standard deviations:



Similarly, we can estimate the sample correlation coefficient:



I gave an example of modeling site visitors conversion using correlated pseudo-random numbers in the article on correlation and regression .

Part 3. How to build a histogram


We give a simple recipe for plotting histograms. In the commercial version of Mathcad Prime, the preparation of histogram data is reduced to the use of one built-in function:



(By the way, for these types of distributions, built-in functions are also provided for both the probability density and the distribution function, as shown in the example of a Gaussian pseudo-random sample). To build a graph-histogram in Mathcad Express, you must manually divide the interval (a, b) into which the data fall into M intervals and perform simple mathematical operations available in Mathcad Express (which, if interested in the reader, he will find with calculations ).



As an example, we use pseudo-random data with an exponential distribution (one of the practical applications of which is described in the last article ).

In conclusion, we note that for the histogram one should choose the appropriate type of chart:



To summarize: using the free Mathcad Express RTS math package, it can be quite simple, using the usual mathematical notation and without resorting to programming, to implement Monte Carlo models. I also hope that I will be able to tell about other uses of Mathcad in data analysis in the following articles.

Source: https://habr.com/ru/post/272713/


All Articles