I run the code below and expect to get a uniform histogram. It's not. I don't understand why not.

3 views (last 30 days)
% I have a two column array, the first column is time and the second column is state. I ran interp1 to observe my data at uniform time points. The results were unexpected. In trying to identify the problem, I have run this simpler code expecting the deterministic uniformly spaced points to be uniformly distributed. They are not. Can anyone explain why not? Thanks.
% Parameters
thismesh = 100; % Number of bins
deltat = 1 / thismesh;
lasttime = 1000; % Example last time
% Generate uniform time steps
xx = 0:deltat:lasttime;
% Calculate fractional times
fractime = (xx - floor(xx)).';
% Plot histogram to check uniformity
figure;
histogram(fractime, thismesh);
title('Histogram of fractional times');
xlabel('Fractional time');
ylabel('Count');
% Display counts in each bin
[counts, edges] = histcounts(fractime, thismesh);
disp('Counts per bin:');
Counts per bin:
disp(counts);
Columns 1 through 15 1907 918 680 1172 884 936 1313 903 562 1095 753 894 1937 793 806 Columns 16 through 30 1141 578 918 1300 794 863 1110 819 957 968 1906 918 680 1172 882 Columns 31 through 45 938 1313 903 562 1093 755 894 1937 793 806 1141 578 918 1300 794 Columns 46 through 60 862 1111 815 961 968 1906 918 680 1172 882 938 1313 903 562 1093 Columns 61 through 75 754 895 1937 793 806 1140 579 918 1299 793 857 1117 816 960 969 Columns 76 through 90 1906 917 681 1172 882 938 1313 903 562 1093 754 894 1938 793 806 Columns 91 through 100 1140 579 917 1300 793 857 1116 817 960 969
% Expected count per bin
expected_count = length(fractime) / thismesh;
disp('Expected count per bin:');
Expected count per bin:
disp(expected_count);
1.0000e+03
% Plot actual counts vs expected uniform count
figure;
bar(counts);
hold on;
yline(expected_count, 'r', 'LineWidth', 2);
hold off;
title('Actual counts vs Expected uniform count');
xlabel('Bin');
ylabel('Count');
legend('Actual counts', 'Expected count');
[SL: formatted code as code and ran that code]

Accepted Answer

the cyclist
the cyclist on 30 May 2024
It's because of your choice of 100 bins is tuned to the periodicity of your function in a way that makes it the counts chaotic near the bin edges. Here is a better choice for binning, that gives what you expect.
% Parameters
thismesh = 100; % Number of bins
deltat = 1 / thismesh;
lasttime = 1000; % Example last time
% Generate uniform time steps
xx = 0:deltat:lasttime;
% Calculate fractional times
fractime = (xx - floor(xx)).';
%%
% Plot histogram to check uniformity
figure;
histogram(fractime, -0.005 : 0.01 : 1.005);
title('Histogram of fractional times');
xlabel('Fractional time');
ylabel('Count');

More Answers (1)

Barbara Margolius
Barbara Margolius on 30 May 2024
in answer to my own question, I can define
bin_edges=-0.005:.01:.995;
and then
hh=histogram(fractime, bin_edges);
Unrecognized function or variable 'fractime'.
and the result is uniform. This feels very jerry-rigged. I guess when I run my code (kept as text here because the rest of the code is not provided):
lasttime = cumRV(end,1);
deltat = 1/thismesh;
xx = 0:deltat:lasttime;
ph = interp1(cumRV(:,1),cumRV(:,2),xx,'previous').';
I could change the definition of xx so that the interpolation points are in the center of the bars. This seems awkward, and I would appreciate other thoughts on how better to do this. Thanks.
% Parameters
thismesh = 100; % Number of bins
deltat = 1 / thismesh;
lasttime = 1000; % Example last time
% Generate uniform time steps
xx = 0:deltat:lasttime;
% Calculate fractional times
fractime = (xx - floor(xx)).';
% Plot histogram to check uniformity
figure;
histogram(fractime, thismesh);
title('Histogram of fractional times');
xlabel('Fractional time');
ylabel('Count');
% Display counts in each bin
[counts, edges] = histcounts(fractime, thismesh);
disp('Counts per bin:');
disp(counts);
% Expected count per bin
expected_count = length(fractime) / thismesh;
disp('Expected count per bin:');
disp(expected_count);
% Plot actual counts vs expected uniform count
figure;
bar(counts);
hold on;
yline(expected_count, 'r', 'LineWidth', 2);
hold off;
title('Actual counts vs Expected uniform count');
xlabel('Bin');
ylabel('Count');
legend('Actual counts', 'Expected count');
% corrections below here
bin_edges=-0.005:.01:.995;
hh=histogram(fractime, bin_edges);

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!