如何在直方图上绘制概率密度函数? [英] How to plot a probability density function on a histogram?

查看:1137
本文介绍了如何在直方图上绘制概率密度函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的函数称为DicePlot,可模拟滚动10个骰子5000次.在该函数中,它计算每卷10个骰子的值的总和,这将是1×5000向量,并绘制相对频率直方图,并以相同的方式选择条带的边缘,其中直方图中的每个条带应表示骰子总和的可能值.

My function called DicePlot, simulates rolling 10 dice 5000 times. In the function, it calculates the sum of values of the 10 dice of each roll, which will be a 1 × 5000 vector, and plot relative frequency histogram with edges of bins being selected in the same manner where each bin in the histogram should represent a possible value of for the sum of the dice.

计算出1×5000个骰子和的均值和标准差,并在相对频率直方图的顶部绘制正态分布的概率密度函数(计算出均值和标准差).

The mean and standard deviation are computed of the 1 × 5000 sums of dice values and the probability density function of normal distribution (with the mean and standard deviation that is computed) on top of the relative frequency histogram is plotted.

我已经做好了一切,但是我对如何绘制概率密度函数感到困惑.任何帮助表示赞赏.谢谢!

I have everything done, but i'm confused on how to plot the probability density function. any help is appreciated. thanks!

作为参考,该图应该看起来像!

for reference the graph is supposed to look like!

function DicePlot ( throw_num, die_num )

throw_num=5000
die_num= 10

  throws = rand ( throw_num, die_num );

  throws = ceil ( 6 * throws );

  for i = die_num : die_num*6
    j = find ( score == i );
    y(i-die_num+1) = length ( j ) / throw_num;
  end 

  bar ( x, y )

  xlabel ( 'Score' )
  ylabel ( 'Estimated Probability' )


  score_ave = sum ( score(1:throw_num) ) / throw_num;
  score_var = var ( score );



  return
end

推荐答案

我已将答案中的代码添加到

I've added to the code from my answer to your previous question to plot a scaled Gaussian pdf over the top of your histogram. The two key additions are as follows: 1) Use hold on and hold off to get the histogram and plot on the same figure. 2) Scale the output of normpdf to the appropriate size so it is on the same scale as the histogram.

另一件事,我不禁注意到您尚未将我先前的回答中的建议纳入您的功能中.有什么特殊原因吗?除非我能看到证据表明您已将您过去提出的建议纳入您的工作中,否则我当然不会+1您的问题!现在您已经离开了,让我听起来像我的高中老师之一! :-)

One other thing, I can't help but notice you haven't incorporated the suggestions from my previous answer into your function yet. Any particular reason for this? I certainly will not +1 your question unless I can see evidence that you've incorporated the suggestions you've had in the past into your work! And now you've gone and made me sound like one of my high-school teachers! :-)

%#Define the parameters
NumDice = 2;
NumFace = 6;
NumRoll = 500;

%#Generate the rolls and obtain the sum of the rolls
AllRoll = randi(NumFace, NumRoll, NumDice);
SumRoll = sum(AllRoll, 2);

%#Determine the bins for the histogram
Bins = (NumDice:NumFace * NumDice)';

%#Build the histogram
hist(SumRoll, Bins);
title(sprintf('Histogram generated from %d rolls of %d %d-sided dice', NumRoll, NumDice, NumFace));
xlabel(sprintf('Sum of %d dice', NumDice));
ylabel('Count');
hold on

%#Obtain the mean and standard deviation of the data
Mu = mean(SumRoll);
Sigma = sqrt(var(SumRoll));

%#Obtain the Gaussian function using 4 standard deviations on either side of Mu
LB = Mu - 4 * Sigma; UB = Mu + 4 * Sigma;
Partition = (LB:(UB - LB) / 100:UB)';
GaussianData = normpdf(Partition, Mu, Sigma);

%#Scale the Gaussian data so the size matches that of the histogram
GaussianData = NumRoll * GaussianData;

%Plot the Gaussian data
plot(Partition, GaussianData, '-r');
hold off

ps,如果您不知道先验直方图应为高斯(由于中心极限定理),则还可以使用统计工具箱中的ksdensity来获得使用核函数的经验密度.

ps, if you didn't know a priori that the histogram should be Gaussian (because of a central limit theorem), then you could also use ksdensity from the statistics toolbox to get the empirical density using a kernel function.

这篇关于如何在直方图上绘制概率密度函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆