简体   繁体   English

从表中的值制作直方图

[英]Making a histogram out of values from a table

I have defined a Mixed distribution made out of two Normal Distributions, like this我定义了一个由两个正态分布组成的混合分布,就像这样

MixDist[s_,n_]:=With[{Dist=MixtureDistribution[{.5,.5},{NormalDistribution[0,s],Normaldistribution[0.5s,s]}]},RandomVariate[Dist,n]] MixDist[s_,n_]:=With[{Dist=MixtureDistribution[{.5,.5},{NormalDistribution[0,s],Normaldistribution[0.5s,s]}]},RandomVariate[Dist,n]]

For example, MixDist[1,1000] should generate 1000 numbers distributed with a mixed distribution made out of NormalDistribution1[0,1] and NormalDistribution2[0.5,1].例如,MixDist[1,1000] 应该生成 1000 个数字,这些数字的分布由 NormalDistribution1[0,1] 和 NormalDistribution2[0.5,1] 组成的混合分布。 Now, I want to run this generator 100 times, and this is where I am stuck.现在,我想运行这个生成器 100 次,这就是我被卡住的地方。

I tried doing this我试过这样做

dist1=Table[MixDist[1,1000],100] dist1=表[MixDist[1,1000],100]

to generate a table with 100 sets of 1000 random numbers, but when trying to plot a histogram with生成一个包含 100 组 1000 个随机数的表,但是当尝试 plot 一个直方图时

histogram=Histogram[dist1,20,"ProbabilityDensity"]直方图=直方图[dist1,20,"概率密度"]

it shows a blank coordinate system.它显示了一个空白坐标系。

Can data from a table be included in a histogram?表格中的数据可以包含在直方图中吗? Or is there another way to do this (make a histogram of 100 sets of 1000 randomly generated numbers from the mixed distribution mentioned above).或者还有另一种方法可以做到这一点(从上述混合分布中制作 100 组 1000 个随机生成的数字的直方图)。

Thank you!谢谢!

Short answer简答

mixDist[s_, n_] := With[{
   dist = MixtureDistribution[
     {.5, .5},
     {NormalDistribution[0, s], NormalDistribution[0.5 s, s]}
     ]
   },
  
  RandomVariate[dist, n]
  ]
dist1 = Table[mixDist[1, 1000], 100];
histogram = Histogram[dist1, 20, "ProbabilityDensity"]

Details细节

  • The only mistake was that the second NormalDistribution was written as Normaldistribution唯一的错误是第二个NormalDistribution写成了Normaldistribution
  • Improved code style.改进的代码风格。 I am convinced that user-defined variables should start with a lowercase letter to be distinguishable from the system ones.我确信用户定义的变量应该以小写字母开头,以便与系统变量区分开来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM