Trying to find empirical cdf

1 次查看(过去 30 天)
Kylenino Espinas
Kylenino Espinas 2022-5-2
回答: Gyan Vaibhav 2023-10-10
%Sn = X1 + X2 ... + Xn
%Xi's are independent random variables
%uniform on the interval [-a,a]
a = 1;
n1 = 4;
n2 = 20;
n3 = 50;
x1n1 = -a + (a - (-a)) * (rand(n1,1));
x1n2 = -a + (a - (-a)) * (rand(n2,1));
x1n3 = -a + (a - (-a)) * (rand(n3,1));
sort(x1n1);
sort(x1n2);
sort(x1n3);
y1n1 = (1/2)*(1 + erf( (x1n1-n1*mean(x1n1)) / (sqrt(n1)*var(x1n1)) ));
y1n2 = (1/2)*(1 + erf( (x1n2-n2*mean(x1n2)) / (sqrt(n2)*var(x1n2)) ));
y1n3 = (1/2)*(1 + erf( (x1n3-n3*mean(x1n3)) / (sqrt(n3)*var(x1n3)) ));
hold on
stairs(x1n1,y1n1)
stairs(x1n2,y1n2)
stairs(x1n3,y1n3)
grid on
title('a=1')
xlabel('x')
ylabel('CDF')
hold off
Attempting to find the empircal cdf of a function with a different amount of random variables. But the function looks nothing like the typical empirical cdf graph. I even used the mean() and var() function yet I can't seem to find the error.
  2 个评论
Star Strider
Star Strider 2022-5-2
If you have the Statistics and Machine Learning Toolbox, consider using the ecdf function.
Torsten
Torsten 2022-5-2
You are aware that you don't sum the x1ni anywhere, aren't you ?

请先登录,再进行评论。

回答(1 个)

Gyan Vaibhav
Gyan Vaibhav 2023-10-10
Hi Kylenino,
So the problem here is with your computation which I think is wrong probably due to the formula.
If you have the Statistics and Machine Learning Toolbox, consider using the ecdf function as stated earlier by Star Strider. It can be simply done by replacing the corresponding lines in your code.
[y1n1, x1n1] = ecdf(x1n1);
[y2n2, x1n2] = ecdf(x1n2);
[y3n3, x1n3] = ecdf(x1n3);
Here is the documentation link for the “ecdf” function:
Alternatively, you can code it as follows:
% Calculate empirical CDF
y1n1 = zeros(size(x1n1));
y1n2 = zeros(size(x1n2));
y1n3 = zeros(size(x1n3));
for i = 1:length(x1n1)
y1n1(i) = sum(x1n1 <= x1n1(i)) / n1;
end
for i = 1:length(x1n2)
y1n2(i) = sum(x1n2 <= x1n2(i)) / n2;
end
for i = 1:length(x1n3)
y1n3(i) = sum(x1n3 <= x1n3(i)) / n3;
end
Hope this helps.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by