标签:
step1 生成服从U(0,1)分布的u1,u2;
step2 令 y = [-2*ln(u1)]^0.5*sin(2*pi*u2);
step3 令 x = miu + y*delta,其中miu为均值,delta为标准差
代码:
clear all; close all; xaxis = -5:0.1:5; %均值为1方差为1 miu = 1; delta = 1; PDFGaussian = 1/sqrt(2*pi*delta^2)*exp(-1/2*((xaxis - miu)/delta).^2); CDFGaussian = 1/2*(1 + erf((xaxis - miu)/(sqrt(2)*delta))); PDFGaussian_0 = 1/sqrt(2*pi*delta^2); plot(xaxis,CDFGaussian,‘LineWidth‘,2); hold on plot(xaxis,PDFGaussian,‘LineWidth‘,2); plot([xaxis(1),xaxis(end)],[0.5 0.5],‘r:‘,‘LineWidth‘,2); plot([miu,miu],[0,1],‘r:‘,‘LineWidth‘,2); plot([xaxis(1),xaxis(end)],[PDFGaussian_0,PDFGaussian_0],‘r:‘,‘LineWidth‘,2); title(‘Gaussian Distribution‘) xlabel(‘Random variable‘) ylabel(‘PDF and CDF‘) N = 100000 u1 = rand(1,N); u2 = rand(1,N); y = (-2*log(u1)).^0.5.*sin(2*pi*u2); var = miu + delta*y; [sum,loc] = hist(var,100); prob = sum/N; prob = [0, prob, 0]; step = loc(2) - loc(1); loc = [loc(1)-step/2, loc, loc(end)-step/2]; prob = cumsum(prob); plot(loc,prob,‘g*‘); legend(‘Theoretical CDF‘,‘Theoretical PDF‘,‘data‘,‘data‘,‘data‘,‘Simulated CDF‘);
标签:
原文地址:http://www.cnblogs.com/waimen/p/5761777.html