-
Notifications
You must be signed in to change notification settings - Fork 1
/
quantilePlot.m
71 lines (56 loc) · 2.4 KB
/
quantilePlot.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function varargout = quantilePlot(X,varargin)
% Plot a quantile-quantile plot of the data set X to test for normality of
% the data.
%
% Mandotory inputs
% X: Cell array. Each entry of X is the data set to test for normality.
% cell array of size 1 X M. Each element is an array of varying size,
% X_i x 1
%
%
% Optional input: Create a structure variable with the following field:
% disp: Set disp to 'off' to avoid plotting. If 'off' then read the
% optional ouput and look at the corelation coefficient value > 0.8 for
% nomality.
%
% Optional output
% varargout: A structure variable with the results of qq analaysis with
% fields
% xSort: Sorted (ascending) measured data set
% idxSort: Indices of sorted measured datapoints
% z: Theoretical data points
% corrCoeff: Sample correlation coefficient between measured and
% theoretical data
%
% W.D . Widanage 04-03-2016 (Deal with the Four Horsemen!!)
parObj = inputParser;
% Create variable names and assign default values after checking the value
addRequired(parObj,'x');
% Optional parameteres
addParameter(parObj,'disp','on');
% Re-parse parObj
parse(parObj,X,varargin{:})
for xx = 1:length(X)
x = X{xx};
nX = length(x); % Number of samples
mX = mean(x); % Sample mean
stdX = std(x); % Sample std
q = ([1:nX]-0.5)/nX; % Calculate quantiles
zTmp = norminv(q,0,1); % Calculate theoretical values based on inverse standard normal distribution
z = zTmp'*stdX + mX; % Translate theoretical data with sample mean and std
[xSort,idxSort] = sort(x); % Sort data for plotting and correlation of coefficient calculation
corrCoeff = (mean(xSort.*z)-mean(xSort)*mean(z))/(std(xSort)*std(z)); % Correlation of coefficient
titleMsg = sprintf('Correlation coefficient r: %.2f%',corrCoeff);
if strcmpi(parObj.Results.disp,'on')
figure()
plot(z,z,'r',z,xSort,'x')
xlabel('Expected data'); ylabel('Measured data')
title(titleMsg)
end
qqResults.(['DataSet',num2str(xx)]).xSort = xSort;
qqResults.(['DataSet',num2str(xx)]).z = z;
qqResults.(['DataSet',num2str(xx)]).corrCoeff = corrCoeff;
qqResults.(['DataSet',num2str(xx)]).idxxSort = idxSort;
end
varargout{1} = qqResults;
end