如何归一化直方图
how to normalize histogram
我要使用逆向方法创建指数分布。我想标准化直方图。我该怎么做?
这是我的代码
N=100;
Lambda=2;
r=rand(N,1);
X=-log(1-r)/Lambda;
hist(X), colormap(bone);
t = 0:0.01:5;
pdf=Lambda*exp(-Lambda*t);
hold on, plot(t,pdf,'LineWidth',2)
直方图要归一化为单位面积,这样才能和理论pdf进行比较。要标准化为单位面积,您需要除以样本数和 bin 宽度:
N = 100;
Lambda=2;
r = rand(N,1);
X = -log(1-r)/Lambda;
[hy, hx] = hist(X); %/ get histogram values
hy = hy/numel(X)/(hx(2)-hx(1)); %//normalize histogram
bar(hx, hy) %// plot histogram
t = 0:0.01:5;
pdf = Lambda*exp(-Lambda*t);
hold on, plot(t,pdf,'LineWidth',2) %// plot pdf
或者使用新的 histogram
函数(在 R2014b 中引入),它 根据指定的规范化选项自动规范化 :
N = 100;
Lambda=2;
r = rand(N,1);
X = -log(1-r)/Lambda;
histogram(X, 'Normalization', 'pdf') %// plot normalized histogram
t = 0:0.01:5;
pdf = Lambda*exp(-Lambda*t);
hold on, plot(t,pdf,'LineWidth',2) %// plot pdf
我要使用逆向方法创建指数分布。我想标准化直方图。我该怎么做?
这是我的代码
N=100;
Lambda=2;
r=rand(N,1);
X=-log(1-r)/Lambda;
hist(X), colormap(bone);
t = 0:0.01:5;
pdf=Lambda*exp(-Lambda*t);
hold on, plot(t,pdf,'LineWidth',2)
直方图要归一化为单位面积,这样才能和理论pdf进行比较。要标准化为单位面积,您需要除以样本数和 bin 宽度:
N = 100;
Lambda=2;
r = rand(N,1);
X = -log(1-r)/Lambda;
[hy, hx] = hist(X); %/ get histogram values
hy = hy/numel(X)/(hx(2)-hx(1)); %//normalize histogram
bar(hx, hy) %// plot histogram
t = 0:0.01:5;
pdf = Lambda*exp(-Lambda*t);
hold on, plot(t,pdf,'LineWidth',2) %// plot pdf
或者使用新的 histogram
函数(在 R2014b 中引入),它 根据指定的规范化选项自动规范化 :
N = 100;
Lambda=2;
r = rand(N,1);
X = -log(1-r)/Lambda;
histogram(X, 'Normalization', 'pdf') %// plot normalized histogram
t = 0:0.01:5;
pdf = Lambda*exp(-Lambda*t);
hold on, plot(t,pdf,'LineWidth',2) %// plot pdf