How to fit an uniform distribution to a histogram?

조회 수: 23 (최근 30일)
hmhuang
hmhuang 2022년 1월 12일
댓글: Jeff Miller 2022년 1월 15일
I have a set of data that is generated from an uniform distribution. Now I want to fit the corresponding histogram to an uniform distribution, such that there is a 'ㄇ' shape of line plotted on that histogram. I tried to fit it by using the MATLAB built-in function histfit, but there is no such an option of uniform distribution for histfit... As a workaround, I would like to do it manually but I have no ideas. How can I do it?
data = unifrnd(-100,100,1000,1);
%% MATLAB built-in function: 'histfit'
figure(1);
hh = histfit(data); % No options for 'histfit' to fit data to an uniform distribution
%% Manually fitting a histogram to an uniform distribution
figure(2);
numBars = length(hh(1).XData);
histogram(data, numBars);
% TODO: How to do next to plot a line that fits the data to an uniform distribution?
  댓글 수: 4
hmhuang
hmhuang 2022년 1월 12일
@Torsten min(data) and max(data)?
Torsten
Torsten 2022년 1월 13일
편집: Torsten 2022년 1월 13일
They are not free, they are known.

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

채택된 답변

John D'Errico
John D'Errico 2022년 1월 13일
If you have histfit, then you would also have unifit.
data = unifrnd(-100,100,1000,1);
[AHAT,BHAT,ACI,BCI] = unifit(data)
AHAT = -99.8483
BHAT = 99.9082
ACI = 2×1
-100.4476 -99.8483
BCI = 2×1
99.9082 100.5076
histogram(data,'norm','pdf')
hold on
fplot(@(x) unifpdf(x,AHAT,BHAT))

추가 답변 (2개)

Jeff Miller
Jeff Miller 2022년 1월 12일
histogram(data, numBars);
avgHeight = numel(data)/numBars;
hold on;
plot([min(data), max(data)],[avgHeight, avgHeight])
  댓글 수: 4
Image Analyst
Image Analyst 2022년 1월 14일
Yes, the blue bars are from a limited number of points drawn from a random, uniform distribution, and the thin orange line in John's plot is the height the bars would be at if the sample distribution was perfectly flat, like if you had no randomness and an infinite number of points.
Jeff Miller
Jeff Miller 2022년 1월 15일
Apparently the OP wanted the histogram's y-axis scaled in terms of in terms of probability density, as in John's answer, rather than in terms of counts, as in the original posted figure.

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


Walter Roberson
Walter Roberson 2022년 1월 12일
Calculate the 98th prctile of the counts; that corresponds to 2 standard deviations. Min and max of the x gives you the other bounds. You might also want to mark the mean of the counts.
If you record the handle returned by histogram() then the counts is the BinCounts property of the handle.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by