Plot ECDF and histogramm of distribution

조회 수: 5 (최근 30일)
Alex Milns
Alex Milns 2023년 9월 17일
댓글: Alex Milns 2023년 9월 17일
I try to plot custom empirical CDF in matlab for cauchy distribution. Below I present my code:
clear
clc
format long
% Parameters of the Cauchy distribution
location = 17; % Location parameter (median)
scale = 2; % Scale parameter (interquartile range)
% Number of data points to generate
num_samples = 20;
% Generate data from a Cauchy distribution
y = location + scale * trnd(1, num_samples, 1);
stats = bootstrp(20, @(x)[mean(x), median(x)], y);
% Extract the first column of stats
first_column = stats(:, 2);
% Compute the histogram without plotting it
[counts, binEdges] = histcounts(first_column, 10);
% Normalize the histogram counts to have a maximum value of 1
max_count = max(counts);
normalized_counts = counts / max_count;
% Compute bin centers from edges
binCenters = (binEdges(1:end-1) + binEdges(2:end)) / 2;
% Create a figure for plotting
figure;
% Plot the normalized histogram with heights scaled to 1
bar(binCenters, normalized_counts, 'hist');
hold on; % This will allow you to plot the ECDF on the same graph
grid on
grid minor
% Compute the ECDF values for the first column
ecdf_values = sort(first_column);
ecdf = (1:numel(ecdf_values)) / numel(ecdf_values);
% Normalize the ECDF values to have a maximum value of 1
ecdf = ecdf / max(ecdf);
% Plot the ECDF as a stairs plot
stairs(ecdf_values, ecdf, 'r-', 'LineWidth', 2);
% Add a horizontal line at y=0 before the minimum data value
line([min(ecdf_values)-10, min(ecdf_values)], [0, 0], 'Color', 'r', 'LineStyle', '-', 'LineWidth', 2);
% Find the first minimal value of custom ECDF
first_minimal_value = min(ecdf(ecdf > 0));
% Find the corresponding x-value
x_of_first_minimal = ecdf_values(find(ecdf == first_minimal_value, 1));
% Add a vertical line at the point of growth where height equals the first minimal value
line([x_of_first_minimal, x_of_first_minimal], [0, first_minimal_value], 'Color', 'r', 'LineStyle', '-', 'LineWidth', 2);
% Add horizontal lines at y=1 after the maximum data value
plot([ecdf_values(end), ecdf_values(end) + 10], [1, 1], 'r-', 'LineWidth', 2);
% Set y-axis limits
ylim([-0.05 1.05]);
% Set x-axis limits (for example, from -2 to 22)
% xlim([-2 22]);
% Add labels and a legend
xlabel('Value');
ylabel('Normalized Probability');
% legend('Normalized Histogram', 'Normalized ECDF');
title('Normalized Histogram and ECDF for First Column of stats');
hold off;
My problem: CDF also have values y = 0 and y = 1 before start my custom stairs and after end of my custom stairs. How to plot this values in thius case?
I understand, that this solution not correct and optimal, but work -- how to improve ecdf visualization in this case?

채택된 답변

the cyclist
the cyclist 2023년 9월 17일
Do you mean you want to avoid drawing the additional horizontal red lines?
I think you'll need to do something "manually", because MATLAB does not know you have data there. The following seems a bit more natural than what you are doing:
clear
clc
format long
% Parameters of the Cauchy distribution
location = 17; % Location parameter (median)
scale = 2; % Scale parameter (interquartile range)
% Number of data points to generate
num_samples = 20;
% Generate data from a Cauchy distribution
y = location + scale * trnd(1, num_samples, 1);
stats = bootstrp(20, @(x)[mean(x), median(x)], y);
% Extract the first column of stats
first_column = stats(:, 2);
% Compute the histogram without plotting it
[counts, binEdges] = histcounts(first_column, 10);
% Normalize the histogram counts to have a maximum value of 1
max_count = max(counts);
normalized_counts = counts / max_count;
% Compute bin centers from edges
binCenters = (binEdges(1:end-1) + binEdges(2:end)) / 2;
% Create a figure for plotting
figure;
% Plot the normalized histogram with heights scaled to 1
bar(binCenters, normalized_counts, 'hist');
hold on; % This will allow you to plot the ECDF on the same graph
grid on
grid minor
% Compute the ECDF values for the first column
ecdf_values = sort(first_column);
ecdf = (1:numel(ecdf_values)) / numel(ecdf_values);
% Normalize the ECDF values to have a maximum value of 1
ecdf = ecdf / max(ecdf);
% Append values to extend ECDF range
ecdf_values = [min(ecdf_values)-10; ecdf_values; max(ecdf_values)+10];
ecdf = [0 ecdf 1];
% Plot the ECDF as a stairs plot
stairs(ecdf_values, ecdf, 'r-', 'LineWidth', 2);
% Find the first minimal value of custom ECDF
first_minimal_value = min(ecdf(ecdf > 0));
% Find the corresponding x-value
x_of_first_minimal = ecdf_values(find(ecdf == first_minimal_value, 1));
% Add a vertical line at the point of growth where height equals the first minimal value
line([x_of_first_minimal, x_of_first_minimal], [0, first_minimal_value], 'Color', 'r', 'LineStyle', '-', 'LineWidth', 2);
% Set y-axis limits
ylim([-0.05 1.05]);
% Set x-axis limits (for example, from -2 to 22)
% xlim([-2 22]);
% Add labels and a legend
xlabel('Value');
ylabel('Normalized Probability');
% legend('Normalized Histogram', 'Normalized ECDF');
title('Normalized Histogram and ECDF for First Column of stats');
hold off;
Note that I added the lines
% Append values to extend ECDF range
ecdf_values = [min(ecdf_values)-10; ecdf_values; max(ecdf_values)+10];
ecdf = [0 ecdf 1];
after you calculate the ECDF.
  댓글 수: 1
Alex Milns
Alex Milns 2023년 9월 17일
Thanks a lot, exactly what is needed!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by