Monte Carlo algorithm

Source: Internet
Author: User

Starting today to study sampling Methods, mainly MCMC algorithm . This article is the opening article, first to understand the Monte Carlo algorithm .

Contents

1. Monte Carlo Introduction

2. Application of Monte Carlo

3. Monte Carlo Integration

1. Monte Carlo Introduction

Monte Carlo Method , also known as the Statistical simulation method , was the mid 1940s due to the scientific and technological

Development and the invention of electronic computers, a kind of very important numerical calculation method which is guided by probability statistic theory is presented. is to instruct

with random number (or pseudo-random number) to solve many computational problems. It corresponds to a deterministic algorithm. Monte Carlo method in gold - Melt Engineering

Studies, Macroeconomics, computational physics (such as particle transport calculations, quantum thermodynamics calculations, aerodynamics calculations) and other fields are widely used.

Monte Carlo method in the 1940s the United States in the Second World War in the development of the atomic bomb "Manhattan plan" member S.M. Ulam

And J. von Neumann first proposed. Mathematician von Neumann named this method with the Monte carlo-of the world-famous casino, Monaco,

It was a mystery. Prior to this, the Monte Carlo method already existed. In 1777, French mathematician Buffon put forward a needle-casting experiment

of the method to find Pi, which is considered the origin of Monte Carlo method.

In addition, the quasi-Monte Carlo algorithm has been developed rapidly in recent years. This method replaces the Monte Carlo algorithm with a deterministic super-uniform distribution .

The random number sequence is hundreds of times times higher than the normal Monte Carlo algorithm for some specific problems.

Due to the randomness of the random number, when we use n random points to solve the specific problem by Monte Carlo method, the approximate solution is calculated by the error

The difference is very small, but there must be a definite average, that is, some errors are greater than this value, while the rest of the error is less than this value. In view of this, it is obvious that Ken

So that the absolute value of the error is not greater than the average. If we can construct such a set of points, we can make the original method

Make a big improvement. The quasi-Monte Carlo method is proposed for this, and it is committed to constructing a point set whose error is significantly better than the average error.

The solution is consistent with the Monte Carlo method, except that the random number used is not the same. When solving the problem by Monte Carlo method, the result is good or bad

is mainly the uniformity of the random number sequence. In the quasi-Monte Carlo method, the set of uniformly distributed points with low deviation is more uniform than the pseudo-random number sequence.

And the real error is obtained by using the quasi-Monte Carlo method, which avoids the defect of probability error obtained by Monte Carlo method.


Therefore, the key to solve the problem by using the quasi-Monte Carlo method is how to find a uniformly scattered point set. At present, the common point set has the GLP point set (good lattice

Set of sub-points, good lattice point set), GP points set (good set, good point set), Halton points set and its variants,

Hammersley point sets and so on.

The theoretical basis of Monte Carlo method is the law of large numbers . The law of large numbers is the law that describes the result of quite a number of repeated tests, according to which the law knows

The higher the number of samples, the more the average is closer to the real value.

2. Application of Monte Carlo

The most classic application is to use Monte Carlo algorithm to find PI. The code is as follows

Code:

#include <bits/stdc++.h> #define Max_iters 1000000using namespace std;double Rand (double L, double R) {return L + (R- L) * RAND () * 1.0/RAND_MAX;} Double Getpi () {Srand (Time (NULL)), int cnt = 0;for (int i = 0; i < max_iters; i++) {Double x = rand ( -1, 1);d ouble y = rand ( -1, 1); if (x * x + y * y <= 1) cnt++;} return CNT * 4.0/max_iters;} int main () {for    (int i = 0; i < i++) cout << getpi () << Endl;return 0;}


3. Monte Carlo Integration

For the Monte Carlo Quadrature, you can refer to the following article first.

Links:http://cos.name/2010/03/monte-carlo-method-to-compute-integration/  

Then the natural constants are obtained by Monte Carlo integration. This is the year Ali of a pen test.

Consider the following points first

Next, the Monte Carlo integral and Newton Leibniz formulas were used to calculate the values of the Monte Carlo method when the samples were many.

Using the Monte Carlo method, the image is roughly as follows

The purpose of the above integral is to find the area of the shadow part, so we first take the random point in the marked rectangle,

For each pair, investigate whether the following conditions are met

Assuming that there is a point that satisfies the above conditions, and that all points have one, the approximate formula is

And according to Newton Leibniz formula can get

The results of these two methods should be equal, i.e.

Then write the code!

Code:

#include <bits/stdc++.h> #define Max_iters 10000000using namespace std;struct point{double x, y;}; Double Rand (double L, double R)  {  return L + (r-l) * RAND () * 1.0/RAND_MAX;  Point GetPoint () {Point t;t.x = rand (1.0, 2.0); t.y = rand (0.0, 1.0); return t;}  Double GetResult () {int m = 0;int n = Max_iters;srand (Time (NULL)), for (int i = 0, i < n; i++) {Point t = getpoint ();d ouble res = T.x * T.Y;IF (res <= 1.0) m++;} Return POW (2.0, 1.0 * n/m);} int main () {for (int i = 0; i < i++) cout << fixed << setprecision (6) << GetResult () << endl;r Eturn 0;}

Look at the results of the operation, the effect is good. Such as

Monte Carlo algorithm

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.