Bayesian estimation experiment (matlab)

1. Experimental content

Since many parameters in life, such as measurement errors, product quality indicators, etc., almost obey or approximately obey the normal distribution, it is possible to analyze the Bayesian estimation in the univariate normal distribution and write the corresponding Matlab program to analyze the sample size. The impact on Bayesian estimation error, and then verify the validity of Bayesian estimation.

2. Experimental principle

1. Understand the principle of Bayesian estimation, taking the univariate normal distribution as an example, let X N ={X 1 ,X 2 ,…,X N }z is a sample taken from the normal distribution N(μ, σ 2 ) set. Assuming that the population variance σ 2 is known: μ is an unknown random parameter with an estimator, and there is a prior distribution N(μ 0 , σ 2 0 ), it is required to use the Bayesian estimation method to find the estimator μ ^ , making the final Bayesian risk minimization.
2. Use the function in Matlab to generate a certain number of sample data whose overall distribution density obeys the normal distribution.
3. The Bayesian estimated value calculation formula obtained from the univariate normal distribution: μ^ = N*σ 2 mN/(N σ 2 02 )+σ 2 μ 0 /(N σ 2 02 )
4. Based on the samples generated by the above description, continue to add sample data sets that obey the same normal distribution, and explore the relationship between Bayesian estimates and sample size.

Experimental method set program

1. Assuming p(μ) ~ N(2.8,0.6 2 ), display the data

>> u = 2.4;%总体分布概率的均值
>> sigma = 3.6;%总体分布概率的标准差
>> #假设某一样本:
>> u0 = 2.8;%未知参数分布的均值
>> sigma0 = 0.6;%未知参数分布的标准差
>> num=200;%样本个数
>> XN = normrnd(u,sigma,1,num);
>> mN = sun(XN)/num;%样本均值

Distribution of unknown parameters:
insert image description here
2. Estimate it using Bayesian estimation:

>> 贝叶斯估计值
>> u1 = num*sigma0^2*mN/(num*sigma0^2+sigma^2)+sigma^2*u0/(num*sigma0^2+sigma^2);
>> 计算相对误差
>> abs(u1-u)/u*100
>ans =

    19.25

The value of u1:
insert image description here
3. Based on the above 200 data, continue to add sample data that obey the normal distribution N(2.8,0.6 2 ), and explore the relationship between Bayesian estimated value and sample size:

for num =1:3000
	u = 2.4;%总体分布概率的均值
	sigma = 3.6;%总体分布概率的标准差
	R = normrnd(u,sigma,1,1);%产生一个和前面样本分布一样的样本数据
	sigma0 = 0.6;%未知参数分布的标准差
	u0 = 2.8;%未知参数分布的均值
	XN = [XN,R];%XN的前200个值为上个程序已经产生的样本数据
	mN = sum(XN)/(num+100);
	u1(num)=(num+100)*sigma0^2*mN/((num+100)*sigma0^2+sigma^2+sigma^2*u0/((num+100)*sigma0^2+sigma^2));
	%画出相对误差
	plot(abs(u1-u)/u);
	xlabel("增大样本数");
	ylabel('相对误差')
	grid on;
	hold on;
end
>> plot(u1);
>> xlabel("增大样本数");
>> ylabel('贝叶斯估计值');

insert image description here

insert image description here

Experimental results and analysis

1. Experiment 1: It can be seen that the estimated value of Bayesian is 19.25%, and the error value with the real value is relatively large. This is due to the small number of samples, which leads to insufficient prior information, and the final estimated value is relatively large with the real value. error.
2. Experiment 2: It can be seen that the Bayesian estimated value is far away from the true value when the number of samples is small, and the error is large. As the number of samples increases, it first shows a downward trend, and then gradually stabilizes and approaches the true value. . It shows that the error size of the Bayesian estimate is related to the sample size. When the sample size is large enough, the error between the Bayesian estimate and the estimated true value will be controlled within a reasonable range.

Guess you like

Origin blog.csdn.net/weixin_56260304/article/details/127655710