-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathGUIcov.m
236 lines (208 loc) · 6.74 KB
/
GUIcov.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
function out = GUIcov(x,y,w)
%GUIcov shows the necessary calculations to obtain the covariance in a GUI.
%
%
%<a href="matlab: docsearchFS('GUIcov')">Link to the help function</a>
%
%
%
%
% Required input arguments:
%
% x : vector of numeric data or table. Vector or table.
% Vector containing strictly numerical data.
% If x is table the second input argument y is not necessary. In
% this case weighted covariance is computed where the weights are
% the values inside the contingency table.
% Data Types - double or table
%
% y : vector of numeric data. Vector.
% Vector containing strictly numerical data.
% This input argument is not requested if previously input
% argument x is a table.
% Data Types - double
%
% Optional input arguments:
%
% w : weights. Vector.
% Vector of the same length of x containing the weights
% (frequencies) assigned to each observation.
% Example - 1:10
% Data Types - double
%
% Output:
%
% out = detailed output to compute the index. struct.
% Structure containing the following fields.
% out.data = table with n+1 rows (where n is the length of x)
% containing what is shown in the GUI.
% Last row contains the totals.
% out.cov = scalar containing the covariance.
%
%
%
%
% See also: GUIvar, GUImad, GUIskewness
%
% References:
%
% Milioli, M.A., Riani, M., Zani, S. (2019), "Introduzione all'analisi dei dati statistici (Quarta edizione ampliata)". [MRZ]
%
% Cerioli, A., Milioli, M.A., Riani, M. (2016), "Esercizi di statistica (Quinta edizione)". [CMR]
%
% Copyright 2008-2024.
% Written by FSDA team
%
%
%<a href="matlab: docsearchFS('GUIcov')">Link to the help function</a>
%
%$LastChangedDate:: 2018-09-15 00:27:12 #$: Date of the last commit
%
% Examples:
%
%{
% Example of unweighted covariance.
% The data below are referred to monthly income of 13 families and
% their corrisponding free time expenditure (See page 223 of [MRZ]).
% x= monthly income of 13 families.
% y= free time expenditure.
x=[1330 1225 1225 1400 1575 2050 1750 2240 1225 1730 1470 2730 1380];
y=[120 60 30 60 90 150 140 210 30 100 30 270 260];
GUIcov(x,y)
%}
%{
% Example 1 of weighted covariance.
% In this example vectors x y and w are supplied. (See Covariance from Wikipedia)
x=[ 8 8 9 9];
y=[6 7 5 7];
w=[0.4000 0.1000 0.3000 0.2000];
y=GUIcov(x,y,w);
%}
%{
%% Example 2 of weighted covariance.
% In this example first input argument is a table and only this
% argument is passed. (See Covariance from Wikipedia)
N=[0 0.4 0.1
0.3 0 0.2];
colnames={'5' '6' '7'};
rownames={'8','9'};
Ntable=array2table(N,'RowNames',rownames,'VariableNames',colnames);
out=GUIcov(Ntable);
%}
%% Beginning of code
if istable(x)
unweighted=false;
xdouble=str2double(x.Properties.RowNames);
ydouble=str2double(x.Properties.VariableNames);
[r,c]=size(x);
W=table2array(x);
x=zeros(r*c,1);
y=x; w=x;
ij=1;
for i=1:r
for j=1:c
x(ij)=xdouble(i);
y(ij)=ydouble(j);
w(ij)=W(i,j);
ij=ij+1;
end
end
else
if nargin<3
unweighted=true;
else
unweighted=false;
end
x=x(:);
y=y(:);
end
lenx=length(x);
seq=(1:lenx)';
if unweighted==true % unweighted standard deviation
sumx=sum(x);
mx=sumx/lenx;
sumy=sum(y);
my=sumy/lenx;
xmmx=x-mx;
ymmy=y-my;
xmmxy=xmmx.*y;
ymmyx=ymmy.*x;
numx=sum(xmmxy);
numy=sum(ymmyx);
xmmxy=xmmx.*y;
xmmxymmy=xmmx.*ymmy;
numxy=sum(xmmxymmy);
covxy=numxy/lenx;
header={'i' 'x_i' 'y_i' '(x_i-M_X)' '(y_i-M_Y)' '(x_i - M_X )y_i' '(y_i - M_Y) x_i' '(x_i-M_X)(y_i-M_Y)'};
corpus=[seq, x,y, xmmx, ymmy, xmmxy, ymmyx, xmmxymmy];
footer=[NaN sum(x) sum(y) 0 0, numx, numy, numxy];
strtitle='Details of covariance $(cov(x,y))$ calculation';
else % weighted covariance
w=w(:);
n=sum(w);
sumxw=sum(x.*w);
mx=sumxw/n;
sumyw=sum(y.*w);
my=sumyw/n;
xmmx=x-mx;
ymmy=y-my;
xyw=xmmx.*ymmy.*w;
numxy=sum(xyw);
covxy=numxy/n;
header={'i' 'x_i' 'y_i' 'w_i' '(x_i-M_X)' '(y_i-M_Y)' '(x_i-M_X)(y_i-M_Y)w_i'};
corpus=[seq, x,y,w, xmmx, ymmy, xyw];
footer=[NaN sum(x) sum(y) n NaN NaN, numxy];
strtitle='Details of weighted covariance calculation';
end
str=strForSchool(header, corpus, footer);
% % note that there is a maximum of string size of 1200 characters for the
% % LaTeX interpreter
% startIndex=regexp(str,'\cr');
% a=ceil(length(startIndex)/2);
% b=a+1;
% toinsert=['\cr ' repmat('&',1,length(header)) ' '];
% while length(str) >1150 % 1200
% str=[str(1:startIndex(a)-2) toinsert str(startIndex(b)-1:end)];
% a=a-1;
% b=b-1;
% end
fs=14;
dim = [.02 .80 0.1 0.1];
figure('Position',[100 100 1200 600],'Units','normalized');
% Make sure that that figure is also visible inside .mlx files
scatter([],[]);
axis('off')
set(gcf,'Visible','on')
annotation('textbox',dim,'FitBoxToText','on','String',str,'Interpreter','latex','FontSize',fs);
if unweighted==true
dim = [.56 .88 0.1 0.1];
strmean=['\boldmath{$M_X$}= $\frac{' num2str(sumx) '}{' num2str(lenx) '}=' num2str(mx) '\qquad $' ...
'\boldmath{$M_Y$}= $\frac{' num2str(sumy) '}{' num2str(lenx) '}=' num2str(my) '$'];
else
dim = [.46 .90 0.09 0.09];
strmean=['\boldmath{$M_X$}=$\frac{ \sum_{i=1}^n x_i w_i}{\sum_{i=1}^n w_i}$ = $\frac{' num2str(sumxw) '}{' num2str(n) '}=' num2str(mx) '\qquad $' ...
'\boldmath{$M_Y$}=$\frac{ \sum_{i=1}^n y_i w_i}{\sum_{i=1}^n w_i}$= $\frac{' num2str(sumyw) '}{' num2str(n) '}=' num2str(my) '$'];
end
annotation('textbox',dim,'FitBoxToText','on','String',strmean,'Interpreter','latex','FontSize',fs);
dim = [.02 .9 0.1 0.1];
fs1=20;
annotation('textbox',dim,'FitBoxToText','on','String',strtitle,'Interpreter','latex','FontSize',fs1);
dim = [.01 .05 0.1 0.1];
% strfin = text at the end of the GUI
if unweighted==true
strfin=['\boldmath{$cov(x,y)$}=$\frac{\sum_{i=1}^n (x_i-M_X) (y_i-M_Y)}{n}'...
'=\frac{\sum_{i=1}^n (x_i-M_X) y_i }{n} = \frac{\sum_{i=1}^n (y_i-M_Y) x_i }{n}'...
'= \frac{' num2str(numx) '}{' num2str(lenx) '}=' ...
num2str(covxy) '$'];
else
strfin=['\boldmath{$cov(x,y)$}=$\frac{\sum_{i=1}^n (x_i-M_X) (y_i-M_Y)w_i}{\sum_{i=1}^n w_i}'...
'= \frac{' num2str(numxy) '}{' num2str(n) '}=' ...
num2str(covxy) '$'];
end
fs1=20;
annotation('textbox',dim,'FitBoxToText','on','String',strfin,'Interpreter','latex','FontSize',fs1);
out=struct;
out.data=array2table([corpus;footer],'VariableNames',header);
out.cov=covxy;
end
%FScategory:GUI