Monte Carlo (Lo) simulation - teach you mathematical modeling

definition

Monte Carlo method, also known as statistical simulation method and random sampling technique, is a random simulation method, a calculation method based on probability and statistical theoretical methods, which uses random numbers (or more common pseudo-random numbers) to solve Many ways to compute problems. Connect the problem to be solved with a certain probability model, and use an electronic computer to realize statistical simulation or sampling to obtain an approximate solution to the problem. In order to symbolically show the probabilistic and statistical characteristics of this method, it is named after the casino city of Monte Carlo.

In layman's terms, it is a method of simulating the optimal solution (approximate optimal solution) by generating a large number of random numbers.

principle

According to the theorem of large numbers, when the sample size is large enough, the frequency of an event is its probability.

Note: Monte Carlo simulation is not an algorithm. To be precise, it is just an idea, or a method.

Application example (with MatLab code)

Application 1: Estimating the value of the base e of the natural logarithm

Similar to Buffon who estimated the value of π with the needle throwing experiment, a group of people each write a card with their own name on the card. Collect the cards, shuffle them, and distribute them randomly to everyone. The probability that everyone does not get their own card is close to 1/e. Do this experiment a few more times, replace the probability with frequency, and find the reciprocal.

MatLab code example

clear;clc
tic  %计算tic和toc中间部分的代码的运行时间
n = 100; % 总人数
N = 10000000; % 循环的次数
unequal = 0; % 每个人收到的卡片都不是自己的

for i = 1:N % 循环N次
    X = randperm(n); % 生成新的随机数序列,模拟给每个人发卡片
    judge = 0; % judge = 0,则每个人收到的卡片都不是自己的
    for j = 1:n
        if X(j) == j
            judge = 1; % 如果存在有人收到的卡片是自己的,便记judge为1,跳出循环
            break;
        end
    end
    if judge == 0
        unequal = unequal + 1;
    end
end

e_1 = unequal / N; % 每个人拿到的都不是自己卡片的概率为1/e
e = e_1^-1; % 取倒数求e
disp(['蒙特卡罗估计的自然常数e的值为',num2str(e)]); % 打印e的值(自然常数真实值约为2.7182)
toc  %计算tic和toc中间部分的代码的运行时间

output result

蒙特卡罗估计的自然常数e的值为2.7188
时间已过 54.929325 秒。

Simplified version of the code

clear;clc
tic  %计算tic和toc中间部分的代码的运行时间
n = 100; % 总人数
N = 10000000; % 循环的次数
unequal = 0; % 每个人收到的卡片都不是自己的

for i = 1:N % 循环N次
    X = randperm(n); % 生成新的随机数序列,模拟给每个人发卡片
    if isempty(find(X == [1:n])) % 直接将新的随机数序列与1-n的顺序数列作比较
        % find(X)可以用来返回这个向量中非零元素的下标,如果X中所有元素都为0,则返回空值
        % isempty(X)函数可以用来判断X是否为空, 如果X为空, isempty(X) 返回逻辑值1(true),否则返回逻辑值0(false)。
        unequal = unequal + 1;
    end
end

e_1 = unequal / N; % 每个人拿到的都不是自己卡片的概率为1/e
e = e_1^-1; % 取倒数求e
disp(['蒙特卡罗估计的自然常数e的值为',num2str(e)]); % 打印e的值(自然常数真实值约为2.7182)
toc  %计算tic和toc中间部分的代码的运行时间

output result

蒙特卡罗估计的自然常数e的值为2.718
时间已过 60.641203 秒。

It can be seen that the value of the natural constant e estimated by Monte Carlo is basically close to its actual value.
If you want to know the specific mathematical derivation process of this question, it can be seen that Zhihu- Buffon estimated the value of π with the needle throwing experiment, so what simple method can be used to estimate the value of the base e of the natural logarithm?

Application 2: Solving nonlinear programming problems

Monte Carlo Solving Nonlinear Programming Problems

(1) MatLab code - find the minimum value for the first time

clc,clear;
format long g   %可以将Matlab的计算结果显示为一般的长数字格式(默认会保留四位小数,或使用科学计数法)
tic  %计算tic和toc中间部分的代码的运行时间
n = 10000000; % 生成的随机数的组数
x1 = unifrnd(0,16,n,1); % 生成在[0,16]之间均匀分布的随机数组成的n行1列的向量构成x1
x2 = unifrnd(0,8,n,1);  % 生成在[0,8]之间均匀分布的随机数组成的n行1列的向量构成x2
fmin = inf; % 初始化 fmin = 正无穷
X = zeros(1,2); % 初始化储存最小处x1, x2值的数组
for i = 1:n
    x = [x1(i), x2(i)];
    if( x(1)+2*x(2) < 16 & x(1)+2*x(2) < 16 )
        r = 2*(x(1)^2)+x(2)^2-x(1)*x(2)-8*x(1)-3*x(2);
        if(r < fmin)
            fmin = r;
            X = x;
        end
    end
end
disp(['f(x)最小值为:', num2str(fmin)]); % 注意:此处原本的字符串不能用双引号。
disp('最小处x1, x2值为:');
disp(X);
toc  %计算tic和toc中间部分的代码的运行时间 

output result

f(x)最小值为:-15.1429
x1, x2值为
          2.71485111081151          2.85830659834113
          
时间已过 1.780016 秒。

(2) MatLab code - narrow down the range and re-simulate to get more accurate values

clc,clear;
format long g   %可以将Matlab的计算结果显示为一般的长数字格式(默认会保留四位小数,或使用科学计数法)
tic  %计算tic和toc中间部分的代码的运行时间
n = 10000000; % 生成的随机数组数
x1 = unifrnd(2.5,3,n,1); % 生成在[2.5,3]之间均匀分布的随机数组成的n行1列的向量构成x1
x2 = unifrnd(2.5,3,n,1);% 生成在[2.5,3]之间均匀分布的随机数组成的n行1列的向量构成x2
fmin = inf; % 初始化 fmin = 正无穷
X = zeros(1,2); % 初始化储存最小处x1, x2值的数组
for i = 1:n
    x = [x1(i), x2(i)]; % 构造x向量,储存第i个x1,x2对应值。
    if(x(1)+2*x(2) < 16 & x(1)+2*x(2) < 16)
        r = 2*(x(1)^2)+x(2)^2-x(1)*x(2)-8*x(1)-3*x(2);
        if(r < fmin)
            fmin = r;
            X = x;
        end
    end
end
disp(['f(x)最小值为:', num2str(fmin)]); % 注:此处原本的字符串不能用双引号。
disp('最小处x1, x2值为:');
disp(X);
toc  %计算tic和toc中间部分的代码的运行时间 

output result

 f(x)最小值为:-15.1429
 x1, x2值为
           2.71423617782718          2.85717715949967

 时间已过 1.758372 秒。

Guess you like

Origin blog.csdn.net/qq_61539914/article/details/126371134