在 Matlab 中生成三角分布

发布于 2025-01-04 19:14:48 字数 629 浏览 4 评论 0原文

我尝试在 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:

enter image description here

Doesn't look like much of a triangle to me. What's the problem? Am I abusing rand(n)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沙沙粒小 2025-01-11 19:14:48

您可以将两个均匀分布相加,分布图进行卷积,然后得到三角形分布。

易于理解的示例:掷两个骰子,每个动作具有均匀分布,产生 1-6 的数字,组合动作具有三角形分布,产生数字 2-12

编辑:最小工作示例:

a=randint(10000,1,10);
b=randint(10000,1,10);

c=a+b;

hist(c,max(c)-min(c)+1)

edit2:查看您的再次脚本。它可以工作,但你犯了一个错误:

u = sqrt(rand(n, 1));

应该是

u = rand(n, 1);

edit3: optimization code

n = 10000000;

a = 0.2;
b = 0.7;
c = 0.5;

u = rand(n, 1);
x = zeros(n, 1);

idx = find(u < (c-a)/(b-a));
x(idx) = a + sqrt(u(idx)*(b-a)*(c-a));
idx =setdiff(1:n,idx);
x(idx) = b - sqrt((1-u(idx))*(b-a)*(b-c));
hist(x, 100);

you 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:

a=randint(10000,1,10);
b=randint(10000,1,10);

c=a+b;

hist(c,max(c)-min(c)+1)

edit2: looked in your script again. It's working but you've made one mistake:

u = sqrt(rand(n, 1));

should be

u = rand(n, 1);

edit3: optimized code

n = 10000000;

a = 0.2;
b = 0.7;
c = 0.5;

u = rand(n, 1);
x = zeros(n, 1);

idx = find(u < (c-a)/(b-a));
x(idx) = a + sqrt(u(idx)*(b-a)*(c-a));
idx =setdiff(1:n,idx);
x(idx) = b - sqrt((1-u(idx))*(b-a)*(b-c));
hist(x, 100);

隔岸观火 2025-01-11 19:14:48

此示例使用 makedist() 和 < a href="https://www.mathworks.com/help/stats/prob.normaldistribution.pdf.html" rel="nofollow noreferrer">pdf() 命令。

a = 2; m = 7; b = 10;
N = 50000;                             % Number of samples
pd = makedist('Triangular',a,m,b);     % Create probability distribution object

T = random(pd,N,1);                    % Generate samples from distribution

三角分布,下限 a = 7,众数 m = 10,上限 b = 10。
三角分布

% Plot PDF & Compare with Generated Sample
X = (a-2:.1:b+2);

figure, hold on, box on
histogram(T,'Normalization','pdf') % Note normalization-pdf option name-value pair
title([num2str(N) ' Samples'])
plot(X,pdf(pd,X),'r--','LineWidth',1.8)
legend('Empirical Density','Theoretical Density','Location','northwest')

MATLAB 引入了 makedist()。需要统计工具箱。

参考:
三角分布

This example uses the makedist() and pdf() commands.

a = 2; m = 7; b = 10;
N = 50000;                             % Number of samples
pd = makedist('Triangular',a,m,b);     % Create probability distribution object

T = random(pd,N,1);                    % Generate samples from distribution

Triangular Distribution with lowerbound a = 7, mode m = 10, and upperbound b = 10.
Triangular Distribution

% Plot PDF & Compare with Generated Sample
X = (a-2:.1:b+2);

figure, hold on, box on
histogram(T,'Normalization','pdf') % Note normalization-pdf option name-value pair
title([num2str(N) ' Samples'])
plot(X,pdf(pd,X),'r--','LineWidth',1.8)
legend('Empirical Density','Theoretical Density','Location','northwest')

MATLAB introduced makedist() in R2013a. Requires Stats toolbox.

Reference:
Triangular Distribution

横笛休吹塞上声 2025-01-11 19:14:48

更改

u = sqrt(rand(n, 1));

u = rand(n, 1);

该公式的好处是您可以使用单个随机样本来从一般三角形分布中分配样本。

Change

u = sqrt(rand(n, 1));

to

u = rand(n, 1);

The nice thing about this formula is that you can distribute a sample from a general triangle distribution with a single random sample.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文