在 Matlab 中生成三角分布
我尝试在 Matlab 中生成三角概率分布,但没有成功。我使用了 http://en.wikipedia.org/wiki/Triangle_distribution 中的公式。
n = 10000000;
a = 0.2;
b = 0.7;
c = 0.5;
u = sqrt(rand(n, 1));
x = zeros(n, 1);
for i = 1:n
U = u(i);
if U < (c-a)/(b-a)
X = a + sqrt(U*(b-a)*(c-a));
else
X = b - sqrt((1-U)*(b-a)*(b-c));
end
x(i) = X;
end
hist(x, 100);
直方图看起来像这样:
对我来说看起来不太像三角形。有什么问题吗?我是否滥用了rand(n)
?
I have attempted to generate a triangular probability distribution in Matlab, but was not successful. I used the formula at http://en.wikipedia.org/wiki/Triangular_distribution.
n = 10000000;
a = 0.2;
b = 0.7;
c = 0.5;
u = sqrt(rand(n, 1));
x = zeros(n, 1);
for i = 1:n
U = u(i);
if U < (c-a)/(b-a)
X = a + sqrt(U*(b-a)*(c-a));
else
X = b - sqrt((1-U)*(b-a)*(b-c));
end
x(i) = X;
end
hist(x, 100);
The histogram looks like so:
Doesn't look like much of a triangle to me. What's the problem? Am I abusing rand(n)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将两个均匀分布相加,分布图进行卷积,然后得到三角形分布。
易于理解的示例:掷两个骰子,每个动作具有均匀分布,产生 1-6 的数字,组合动作具有三角形分布,产生数字 2-12
编辑:最小工作示例:
edit2:查看您的再次脚本。它可以工作,但你犯了一个错误:
应该是
edit3: optimization codeyou can add up two uniform distributions, the distribution graphs convolve, and you get a triangular distribution.
easy-to-understand example: rolling two dice, each action has uniform distribution to result in a number from 1-6, combined action has triangular distribution to result in a number 2-12
edit: minimal working example:
edit2: looked in your script again. It's working but you've made one mistake:
should be
edit3: optimized code此示例使用
makedist()
和 < a href="https://www.mathworks.com/help/stats/prob.normaldistribution.pdf.html" rel="nofollow noreferrer">pdf()
命令。三角分布,下限 a = 7,众数 m = 10,上限 b = 10。

MATLAB 引入了
makedist()
。需要统计工具箱。参考:
三角分布
This example uses the
makedist()
andpdf()
commands.Triangular Distribution with lowerbound a = 7, mode m = 10, and upperbound b = 10.

MATLAB introduced
makedist()
in R2013a. Requires Stats toolbox.Reference:
Triangular Distribution
更改
为
该公式的好处是您可以使用单个随机样本来从一般三角形分布中分配样本。
Change
to
The nice thing about this formula is that you can distribute a sample from a general triangle distribution with a single random sample.