Using MATLAB, Please show codes for exponential distribution histogram, GoodFit
ID: 3828608 • Letter: U
Question
Using MATLAB, Please show codes for exponential distribution histogram, GoodFit Test, qqplot. If possible please use starter code below.
Make charts in color
clear
clc
close all
% myData=xlsread('examplevalues.xls');
% If xlsread is giving problems, save/export values to a csv file then use
% the function below to read in the data. Comment line above and remove the
% comment marker "%"
myData=csvread('examplevalues.csv');
% First lets fit the distribution. Replace myDistribution srting (i.e.
% "Normal" with whatever distribution you want to try to fit your data to)
% Option include the following:
%-----------------------------------------------------------
%-----------------------------------------------------------
% Uniform Requires min and max bounds
% Triangular Requires min, mode, and max
%-----------------------------------------------------------
%-----------------------------------------------------------
myDistributionType='Triangular'
if isequal(myDistributionType,'Uniform')
minVal=0; % Change this values as needed
maxVal=1; % Change this values as needed
myPDBestFit=makedist(myDistributionType,'lower',minVal,'upper',maxVal)
elseif isequal(myDistributionType,'Triangular')
minVal=0; % Change this values as needed
modeVal=.5
maxVal=1; % Change this values as needed
myPDBestFit=makedist(myDistributionType,'a',minVal,'b',modeVal,'c',maxVal)
end
figure(1)
subplot(1,2,1)
xPDFplot=linspace(0,max(myData),1000);
yPDFplot = pdf(myPDBestFit,xPDFplot);
yCDFplot = cdf(myPDBestFit,xPDFplot);
myxCDFplot=[0;sort(myData)];
myyCDFplot=[1:numel(myData)]/numel(myData);
myyCDFplot=[0;myyCDFplot'];
numBins=numel(myData)/10;
binEdges=linspace(min(myData),max(myData),numBins);
binCounts=histc(myData,binEdges);
binProbs=binCounts/max(binCounts)*max(yPDFplot);
bar(binEdges,binProbs), hold on
plot(xPDFplot,yPDFplot,'Color',[1 0 0],'LineWidth',2); hold on
legend('Sampled Data PDF','Best-Fit PDF')
title(['Best Fit:' myDistributionType])
subplot(1,2,2)
plot(myxCDFplot,myyCDFplot,'Color',[0 0 1]), hold on
plot(xPDFplot,yCDFplot,'Color',[1 0 0],'LineWidth',2); hold on
legend('Sampled Data CDF','Best-Fit CDF')
title(['Best Fit:' myDistributionType])
% Next KS-test on data with best-fit distribution
[h,p] = kstest(myData,'cdf',myPDBestFit)
if h==0
fprintf('UNABLE TO REJECT THE NULL HYPOTHESIS ')
else
fprintf('WE REJECT THE NULL HYPOTHESIS ')
end
% from "help kstest"
% H = 0 => Do not reject the null hypothesis at the 5% significance level.
% H = 1 => Reject the null hypothesis at the 5% significance level.
figure
qqplot(myData,myPDBestFit)
Explanation / Answer
myDistributionType='Triangular'
if isequal(myDistributionType,'Uniform')
minVal=0; % Change this values as needed
maxVal=1; % Change this values as needed
myPDBestFit=makedist(myDistributionType,'lower',minVal,'upper',maxVal)
elseif isequal(myDistributionType,'Triangular')
minVal=0; % Change this values as needed
modeVal=.5
maxVal=1; % Change this values as needed
myPDBestFit=makedist(myDistributionType,'a',minVal,'b',modeVal,'c',maxVal)
end
figure(1)
subplot(1,2,1)
xPDFplot=linspace(0,max(myData),1000);
yPDFplot = pdf(myPDBestFit,xPDFplot);
yCDFplot = cdf(myPDBestFit,xPDFplot);
myxCDFplot=[0;sort(myData)];
myyCDFplot=[1:numel(myData)]/numel(myData);
myyCDFplot=[0;myyCDFplot'];
numBins=numel(myData)/10;
binEdges=linspace(min(myData),max(myData),numBins);
binCounts=histc(myData,binEdges);
binProbs=binCounts/max(binCounts)*max(yPDFplot);
bar(binEdges,binProbs), hold on
plot(xPDFplot,yPDFplot,'Color',[1 0 0],'LineWidth',2); hold on
legend('Sampled Data PDF','Best-Fit PDF')
title(['Best Fit:' myDistributionType])
subplot(1,2,2)
plot(myxCDFplot,myyCDFplot,'Color',[0 0 1]), hold on
plot(xPDFplot,yCDFplot,'Color',[1 0 0],'LineWidth',2); hold on
legend('Sampled Data CDF','Best-Fit CDF')
title(['Best Fit:' myDistributionType])
% Next KS-test on data with best-fit distribution
[h,p] = kstest(myData,'cdf',myPDBestFit)
if h==0
fprintf('UNABLE TO REJECT THE NULL HYPOTHESIS ')
else
fprintf('WE REJECT THE NULL HYPOTHESIS ')
end
% from "help kstest"
% H = 0 => Do not reject the null hypothesis at the 5% significance level.
% H = 1 => Reject the null hypothesis at the 5% significance level.
figure
qqplot(myData,myPDBestFit)