Monte Carlo algorithm

Source: Internet
Author: User

First, the concept

Monte Carlo method, also called statistical simulation method, random sampling technology, is a stochastic simulation method, based on the probability and statistical theory of a method of calculation, is the use of random numbers (or more common pseudo-random numbers) to solve many computational problems. The solved problem is associated with a certain probability model to obtain approximate solution of the problem. In order to indicate the probabilistic statistical characteristics of this method symbolically, it is named after the casino Monte Carlo.

(The following understanding refers to self-linking: 54434280, that's pretty good)

Image overlay text recognition in machine learning/deep learning requires a lot of training samples, automatically generating samples (using the program to overlay text on the background image) is a way to get a sample. But the color value (in order to take into account the direction of the students, forgive me with a so unprofessional vocabulary, this value can be RGB to [0,1] interval mapping, so that it can represent the nature of the color) selection is very important, in order to prevent (control) the occurrence of overlapping text and background image color values similar to the situation occurs, The color value of the superimposed text is best subject to our assigned probability distribution. This requires the creation of color values based on a given probability distribution-a problem that the Monte Carlo method specializes in solving.

In short, the Monte Carlo approach is to generate samples, i.e. Monte Carlo sampling. That is, according to the probability density function f (x) of a known distribution, the production F ( x ) "is a sample X that obeys this distribution.

Second, the simple use case pi π solution

Matlab code:

function [Pi] = montecarlomethod (num)% num is the number of experiments% r is the circle radius% m is the number of points falling into the circle m=0;% unit circle r=1;for i=1:num    x=-1+rand*2*r;      % generates random number coordinates x    y=-1+rand*2*r;      % generates random number coordinates y    if (x^2+y^2 <= r^2)        m=m+1;          % endendpi=4*m/num;fprintf points in the Circle    ("When the number of experiments is n =%d, the Monte Carlo method calculates the pi as%d\n", NUM,PI); end

C + + code:

#include <iostream>#include<ctime>#defineR 1intMain () {intnum=0;//Number of experiments    intm =0;//The point in Circle    Doublex =0, y=0; using namespacestd; cout<<"Please enter the number of experiments: \ n"; CIN>>num;    Srand ((unsigned) time (NULL));  for(inti =0; i < num; ++i) {x= -1+2*rand ()/Double(Rand_max); Y= -1+2*rand ()/Double(Rand_max); if(x*x+ y*y <= R *R)++m; }    DoublePi1 = (Double)4.0* M/num; cout<<"The final Monte Carlo calculation of the pi="<< pi1 <<Endl; System ("Pause"); return 0;

According to the above method to calculate, when the number of experiments reached 100,000 times, the calculated π value is already 3.1404.

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.