Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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)

Service Time 02:05 03:02 02:07 00:20 00:22 02:04 01:00 01:04 00:56 01:51 01:14 01:09 00:57 01:16 01:12 00:53 00:20 00:50 00:57 01:22 00:22 00:44 00:02 02:13 00:43 01:21 01:04 01:31 00:50 01:22 00:40 00:34 01:53 02:10 01:12 03:20 00:19 00:03 00:18 01:42 01:35 02:56 00:53 01:36 01:16 00:13 00:19 02:28 03:09 01:15 01:53 01:03 01:09 00:43 01:18 00:21 00:20 01:20 02:50 00:05 00:12 00:55 00:04 00:24 03:00 01:02 00:36 00:58 01:29 00:25 00:31 01:23 00:56 01:07 00:49 00:42 00:44 00:31 01:09 00:55 01:50 01:08 00:46 01:13 01:07 00:29 00:49

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)