【问题标题】:Plotting two sets of data on one histogram在一个直方图上绘制两组数据
【发布时间】:2017-08-13 07:41:30
【问题描述】:

我刚刚开始使用 Octave,并试图模拟定义为的二项式随机变量的 10,000 个结果:

X ~ Bi(5, 0.2)

我用以下函数绘制了结果:

function x = generate_binomial_bernoulli(n,p,m)
  % generate Bi(n, p) outcomes m times

  x = zeros(1,m);       % allocate array for m simulations
  for i = 1:m           % iterate over m simulations
    successes = 0;      % count the number of successful trials per simualtion (0-5)
    for j = 1:n         % iterate through the n trials
      u = rand;         % generate random nuumber from 0-1
      if (u <= p)       % if random number is <= p
        successes++;    % count it as a success
      endif
    end
    x(i) = successes;   % store the number of successful trials in this simulation
  end

  alphabet_x=[0:n];     % create an array from 0 to n        
  hist(x,alphabet_x);   % plot graph     

end

然后我使用generate_binomial_bernoulli(5, 0.2, 10000) 调用该函数。

这是模拟 5 次伯努利试验,每个试验的成功概率为 0.2,重复 5 次试验 10,000 次,并绘制成功次数分布图。该图显示了模拟的经验结果。

我现在还被要求绘制理论结果,我最好的猜测是在 x 轴 (0.2 * 5 = 1) 上围绕 1 个成功进行正态分布图。

  1. 如何创建此图并将其显示在同一个直方图上?
  2. 如何正确显示我的图表,其中 x 轴仅从 0 到 5,两个轴都标有标签,两个直方图用图例进行颜色编码?

编辑

这是我当前尝试绘制归一化/理论曲线的函数:

function x = generate_binomial_bernoulli(n,p,m)
  % generate Bi(n, p) outcomes m times

  emperical = zeros(1,m);             % allocate array for m simulations
  for i = 1:m                         % iterate over m simulations
    successes = 0;                    % count the number of successful trials per simualtion (0-5)
    for j = 1:n                       % iterate through the n trials
      u = rand;                       % generate random nuumber from 0-1
      if (u <= p)                     % if random number is <= p
        successes++;                  % count it as a success
      endif
    end
    emperical(i) = successes;         % store the number of successful trials in this simulation
  end

  close all;                          % close any existing graphs

  x_values = [0:n];                   % array of x-axis values        
  hist(emperical, x_values, "facecolor", "r"); % plot empirical data
  xlim([-0.5 (n + 0.5)]);             % set x-axis to allow for histogram bar widths

  hold on;                            % hold current graph

  mean = n * p;                       % theoretical mean
  norm = normpdf(x_values, mean, 1);  % normalised y values
  plot(x_values, norm, "color", "b"); % plot theoretical distribution

  legend('Emprical', 'Theoretical');   

end

如下图,这条曲线只沿着y轴延伸到一个非常低的高度,但我不知道如何跨越整个数据集。

【问题讨论】:

  • holdxlimLine plot propertieslegend上查看Matlab的文档
  • @Yvon 好的,我几乎已经完成了所有工作,但是现在,我不想添加另一个标准化直方图,而是想创建一个围绕 1 x 值标准化的线图。我怎么能这样做?是否有一个简单的函数可以让我在给定 [0:5] 数据和平均值 1 的情况下绘制归一化曲线?
  • 要对数据进行归一化,你可以先找到数据的最大值,然后除以它。
  • @Yvon 我明白你在说什么,但我不知道如何绘制归一化曲线。我编辑了我的帖子以包含我更新的函数,我尝试使用 normpdf 函数绘制归一化曲线,但曲线只延伸到非常低的 y 值,我不知道为什么

标签: matlab octave probability probability-density bernoulli-probability


【解决方案1】:

获取直方图的计数和分箱:

[counts,centers] = hist(x);

标准化:

freq = counts/sum(counts);

绘制归一化直方图:

bar(centers,freq)

【讨论】:

  • 感谢您的帮助,但此解决方案似乎生成了与我的编辑中显示的相同的图形 - 归一化图(我想要一条显示归一化结果的曲线,而不是主机图)只延伸到非常低的高度。
  • freq 应该标准化为 (0,1),不是吗?
猜你喜欢
  • 2015-01-10
  • 1970-01-01
  • 1970-01-01
  • 2017-07-07
  • 2012-06-26
  • 2019-04-18
  • 2011-10-15
  • 2023-02-07
相关资源
最近更新 更多