Monte Carlo algorithm (or Monte Carlo method)-Monte Carlo Method

Source: Internet
Author: User
Tags random seed

It is a very important numerical calculation method guided by probability statistics theory. A random number (or a more common pseudo-random number) is used to solve many computing problems.

A Calculation Method Based on the Theory and Method of probability and statistics. It associates the problem solved with a certain probability model and uses an electronic computer for statistical simulation or sampling, to obtain the approximate solution of the problem, also known as the statistical simulation method or statistical test method. 

Basic Idea of Monte Carlo Method

 

When the problem is solved by the probability of a random event or the expected value of a random variable, a certain "experiment" method is used, estimate the probability of a random event based on the Occurrence Frequency of the event, or obtain some digital features of the random variable and use it as the solution to the problem. There is an example that gives you a more intuitive understanding of the Monte Carlo Method: Suppose we want to calculate the area of an irregular graph, then the degree of irregularity and Analytical computation of the graph (for example, points) the complexity is proportional. How is the Monte Carlo method calculated? Suppose you have a bag of beans that are evenly distributed to the graph and then count the number of beans in the graph. The number of beans is the area of the graph. The smaller your beans, the more you scatter, the more accurate the results. Here we want to assume that all beans are on a plane and there is no overlap between them.

 

Working Process of Monte Carlo Method

 

When solving practical problems, the Monte Carlo method has two main tasks:

    1. When a process is simulated using the Monte Carlo method, random variables with various probability distributions are generated.
    2. The statistical method is used to estimate the Digital features of the model to obtain the numerical solution of the actual problem.

Monte Carlo Method for Molecular Simulation

The Monte Carlo method is used for molecular simulation as follows:

  1. A random number generator is used to generate a Random molecular structure.
  2. There is no rule change to the particle coordinates of the molecular structure to generate a new molecular structure.
  3. Calculate the energy of new molecular architectures.
  4. Compare the new molecular configuration with the energy changes of the molecular configuration before the change, and determine whether to accept the configuration.
    • If the new molecular configuration energy is lower than the energy of the original molecular configuration, the new configuration is accepted and the configuration is used to repeat the next iteration.
    • If the new molecular configuration energy is higher than the energy of the original molecular configuration, the Boltzmann factor is calculated and a random number is generated.
      • If the random number is greater than the calculated Boltzmann factor, discard this configuration and re-calculate it.
      • If the random number is smaller than the calculated Boltzmann factor, this configuration is accepted and the configuration is used to repeat the next iteration.
  5. In this way, iterative computation is performed until the final search of the molecular pattern below the given energy condition ends.

Monte Carlo Method Application

Circumference Rate

The Monte Carlo method can be used to calculate the circumference rate: let the computer randomly generate the number between two 0 and 1 each time to check whether the points with these two real numbers as the horizontal and vertical coordinates are in the unit circle. Generate a series of random points and count the points in the unit circle to the total points. (the ratio of the circular area to the square area is Pi: 4, and the PI is the circumference rate ), the more random points are obtained (even if the first 4 digits match the circumference rate), the closer the result is to the circumference rate. In fact, the random numbers produced by computers can only be accurate to a certain number of digits, and cannot generate any real numbers (such as irrational numbers). The above method splits the planes into grids, the calculated area is more or less different from the circle.

Monte Carlo Algorithm -Application Program --- Monte Carlo method (Monte Carlo) Integration

CSource code: # Include <iostream. h>
# Include <time. h> /* Srand (), Rand ()*/
# Include <math. h> /* Sin ()*/
# Include <stdlib. h> /* Rand_max */

Using namespace STD;

Double funcone (Double X );
/* Accumulate a metafunction */
Double Monte carloone (double (* p) (double), double A, double B, double L, double H );
/* Interface for calling one-dimensional function credits */
Double functwo (Double X, Double Y );
/* Product binary function */
Double func1 (Double X );
/* Minimum point y = func1 (x )*/
Double func2 (Double X );
/* Credit Limit y = func2 (x )*/
Double moncarlotwo (double (* func1) (double), double (* func2) (double), double (* p) (double, double), double A, double B, double L, double H, double L, double H );
/* Call the interface for binary function credits.
Double functhree (Double X, Double Y, Double Z );
/* Credits Trielement function */
Double func3 (Double X, Double Y );
/* Lower limit of points z = func3 (x, y )*/
Double func4 (Double X, Double Y );
/* Credit Limit z = func4 (x, y )*/
Double moncarlothree (double (* func1) (double), double (* func2) (double), double (* func3) (double, double), double (* func4) (double, double), double (* p) (Double, double, double), double A, double B, double C, double D, double L, double H, double L, double H );
/* Triples function credits call the interface.

Double funcone (Double X)
{
Return sin (X );
}

Double Monte carloone (double (* p) (double), double A, double B, double L, double H)
{
Unsigned n = 100000000;
/* 1e8 */
Double X, Y;
/* Random points */
Double T;
/* Change yuan */
Unsigned m = 0;
/* Number of vertices in the shadow part */
Srand (unsigned) Time (null ));
/* Generate random seed */
For (unsigned int I = 0; I
{
T = double (RAND ()/rand_max;
/* Generate 0 ~ 1 random Floating Point Number */
Y = double (RAND ()/rand_max;
X = a + (B-a) * t;
/* Get ~ Random floating point number of B */
/* Random points in the shadow area */
If (y * (H-L) <(p (x)-l) m ++;
}
Double result = (H-L) * (B-a) * M/N + (B-a) * l;
/* Double Type multiplied by unsigned type to double type */
Return result;
/* Point result */
}

Double functwo (Double X, Double Y)
{
Return X + Y;
}

Double func1 (Double X)
{
Return X;
}

Double func2 (Double X)
{
Return x * X;
}

Double moncarlotwo (double (* func1) (double), double (* func2) (double), double (* p) (double, double), double A, double B, double L, double H, double L, double H)
{
Unsigned n = 100000000;
/* 1e8 */
Double X, Y, Z;
/* Random points */
Double T, S;
/* Change yuan */
Unsigned m = 0, m = 0;
/* Number of vertices in the shadow part */
Srand (unsigned) Time (null ));
/* Generate random seed */
For (unsigned int I = 0; I
{
T = double (RAND ()/rand_max;
/* Generate 0 ~ 1 random Floating Point Number */
S = double (RAND ()/rand_max;
X = a + (B-a) * t;
/* Get ~ Random floating point number of B */
Y = L + (H-l) * s;
/* Get l ~ Random floating point number of H */
If (Y> func1 (x) & Y
{
M ++;
/* Note that M corresponds to one-dollar points */
Z = double (RAND ()/rand_max;
/* Random points in the shadow area */
If (z * (H-L) <(p (x, y)-l) m ++;
}
}
Double result = (H-L) * (B-a) * (H-l) * M/N + (B-a) * (H-l) * l * M/N;
/* Double Type multiplied by unsigned type to double type */
Return result;
}

Double functhree (Double X, Double Y, Double Z)
{
Return X + Y + z;
}

Double func3 (Double X, Double Y)
{
Return 0;
}

Double func4 (Double X, Double Y)
{
Return functwo (x, y );
}

Double moncarlothree (double (* func1) (double), double (* func2) (double), double (* func3) (double, double), double (* func4) (double, double), double (* p) (Double, double, double), double A, double B, double C, double D, double L, double H, double L, double H)
{
Unsigned n = 100000000;
/* 1e8 */
Double X, Y, Z, U;
/* Random points */
Double T, S, K;
/* Change yuan */
Unsigned m = 0, m = 0;
/* Number of vertices in the shadow part */
Srand (unsigned) Time (null ));
/* Generate random seed */
For (unsigned int I = 0; I
{
T = double (RAND ()/rand_max;
/* Generate 0 ~ 1 random Floating Point Number */
S = double (RAND ()/rand_max;
K = double (RAND ()/rand_max;
X = a + (B-a) * t;
/* Get ~ Random floating point number of B */
Y = C + (D-C) * s;
/* Get C ~ Random floating point number of D */
If (Y> func1 (x) & Y
{
K = double (RAND ()/rand_max;
Z = L + (H-l) * K;
/* Get l ~ Random floating point number of H */
/* Random points in the shadow area */
If (z> func3 (x, y) & Z
{
M ++;
/* Note that M corresponds to binary points */
U = double (RAND ()/rand_max;
If (u * (H-L) <(p (x, y, z)-l ))
M ++;
}
}
}
Double result = (H-L) * (B-a) * (D-C) * (H-l) * M/N + (B-a) * (D-C) * (H-l) * l * M/N;
/* Double Type multiplied by unsigned type to double type */
Return result;
}

Int main ()
{
Double A, B;
/* X credit limit */
Double L, h;
/* Y value range or y point limit */
Double result;
/* Point result */

A = 0, B = 3.1415926;
L = 0, H = 1;
Result = Monte carloone (funcone, A, B, l, H );
Cout < <"\ N ";

A = 1.0, B = 2.0;
L = 1.0, H = 4.0;
Double L = 2.0, H = 6.0;
/* Z value range */
Result = Monte carlotwo (func1, func2, functwo, A, B, l, H, L, H );
Cout < <"\ N ";

A = 1.0, B = 2.0;
Double C = 1.0, D = 4.0;
L = 0.0, H = 6.0;
L = 2.0, H = 12.0;
Result = Monte carlothree (func1, func2, func3, func4, functhree, A, B, C, D, L, H, L, H );
Cout < <"\ N ";

Return 0;
}

 Running result:
2.00011
3.35045
20.994
The exact values are:2
3.35
20.9964286


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.