0

yは分布に従う乱数を含むベクトルであると仮定しf(x)=sqrt(4-x^2)/(2*pi)ます。現時点では、コマンドを使用していhist(y,30)ます。f(x)=sqrt(4-x^2)/(2*pi)分布関数を同じヒストグラムにプロットするにはどうすればよいですか?

4

3 に答える 3

1

ミックスに別の例を追加しましょう。

%# some normally distributed random data
data = randn(1e3,1);

%# histogram
numbins = 30;
hist(data, numbins);
h(1) = get(gca,'Children');
set(h(1), 'FaceColor',[.8 .8 1])

%# figure out how to scale the pdf (with area = 1), to the area of the histogram
[bincounts,binpos] = hist(data, numbins);
binwidth = binpos(2) - binpos(1);
histarea = binwidth*sum(bincounts);

%# fit a gaussian
[muhat,sigmahat] = normfit(data);
x = linspace(binpos(1),binpos(end),100);
y = normpdf(x, muhat, sigmahat);
h(2) = line(x, y*histarea, 'Color','b', 'LineWidth',2);

%# kernel estimator
[f,x,u] = ksdensity( data );
h(3) = line(x, f*histarea, 'Color','r', 'LineWidth',2);

legend(h, {'freq hist','fitted Gaussian','kernel estimator'})

履歴

于 2013-04-19T19:23:49.247 に答える
1

別の分布関数である標準法線の例を見てみましょう。やりたいことを正確に実行するには、次のようにします。

nRand = 10000;
y = randn(1,nRand);
[myHist, bins] = hist(y,30);
pdf = normpdf(bins);
figure, bar(bins, myHist,1); hold on; plot(bins,pdf,'rx-'); hold off;

ただし、これはおそらくあなたが実際に望んでいるものではありません。なんで?密度関数が、ヒストグラム プロットの下部にある細い線のように見えることに気付くでしょう。これは、ヒストグラムがビン内の数のカウントであるのに対し、密度関数は正規化されて 1 つに統合されるためです。ビンに何百ものアイテムがある場合、密度関数がスケールで一致する方法がないため、スケーリングまたは正規化の問題があります。ヒストグラムを正規化するか、スケーリングされた分布関数をプロットする必要があります。分布関数をスケーリングして、ヒストグラムを見たときにカウントが適切になるようにすることを好みます。

normalizedpdf = pdf/sum(pdf)*sum(myHist);
figure, bar(bins, myHist,1); hold on; plot(bins,normalizedpdf,'rx-'); hold off;

あなたのケースは同じですが、normpdf コマンドの代わりに指定した関数 f(x) を使用します。

于 2013-04-19T18:30:07.907 に答える