IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
calculate_scatters.m
Go to the documentation of this file.
1 %>@ingroup maths
2 %>@file
3 %>@brief Calculates scatter matrices from X and classes
4 %
5 %> @param X [no]x[nf] matrix
6 %> @param classes [no]x[1] matrix
7 %> @param flag_modified_s_b if 1, S_B will be calculated in an alternative way which is class terms will not be weighet by class sample size, which
8 %> will cause all classes to have equal importance.
9 %> @param P penalty matrix to be added to @c S_W
10 %> @return <em>[S_B, S_W]</em> Respectively "inter-class scatter matrix" and "within-class scatter matrix"
11 function [S_B, S_W] = calculate_scatters(X, classes, flag_modified_s_b, P)
12 
13 [no, nf] = size(X);
14 
15 if ~exist('flag_modified_s_b', 'var')
16  flag_modified_s_b = 0;
17 end;
18 
19 if ~exist('P', 'var')
20  P = 0;
21 else
22  [q, w] = size(P);
23  if q ~= nf || w ~= nf
24  % assumes the coefficients have been passed
25  P = no*penalty_matrix(nf, P);
26  else
27  end;
28 end;
29 
30 m = mean(X); % total mean vector
31 
32 no_classes = max(classes)+1;
33 
34 
35 S_W = zeros(nf, nf);
36 S_B = S_W;
37 for i = 1:no_classes
38  Xi = X(classes == i-1, :);
39  if size(Xi, 1) > 0
40  m_i = mean(Xi); % vector containing the means for each column/feature.
41 
42  Xi = Xi-repmat(m_i, size(Xi, 1), 1); % centers each column to its mean
43 
44  n_i = size(Xi, 1); % number of observations for class i
45 
46  scatter = Xi'*Xi; % scatter matrix. This is S_i in the reference
47  if flag_modified_s_b
48  S_B = S_B+(m_i-m)'*(m_i-m); % last term is a rank-1 matrix (MATLAB's default is row vector)
49  else
50  S_B = S_B+n_i*(m_i-m)'*(m_i-m); % last term is a rank-1 matrix (MATLAB's default is row vector)
51  end;
52  S_W = S_W+scatter;
53  end;
54 end;
55 
56 if 0
57  S_B = S_B-P;
58 else
59  % I have found empirically that adding P to S_W is equivalent to adding
60  % P to X'*X in canonical correlation analysis
61 
62  S_W = S_W+P;
63 
64 % S_W = eye(nf, nf);
65 end;
function calculate_scatters(in X, in classes, in flag_modified_s_b, in P)
function penalty_matrix(in nf, in dcoeff)