SLIDE 10 fprintf(’Add threshold : %7 .2f\n’,Ta); fprintf(’SSE Reduction : %7 .2f\n’ ,(SSE -SSEb )/ SSE ); fprintf(’\n’); SSE = SSEb; ASE = ASEb; chg = 1; % Set the change flag else % Else , leave xi alone fprintf(’Step %d - Add\n’,stp ); fprintf(’------------------------\n’); fprintf(’Added variable : None\n’); fprintf(’F Statistic : %7 .2f\n’,Fb); fprintf(’Add threshold : %7 .2f\n’,Ta); fprintf(’SSE Reduction : %7 .2f\n’ ,(SSE -SSEb )/ SSE ); fprintf(’\n’); SSE = SSEb; end; % Else , leave xi alone % --------------------------------- % Drop variable loop % --------------------------------- Fb = inf; % Initialize best F statistic for cnt = 1: length(xi), xin = [xi (1:cnt -1); xi(cnt +1: length(xi ))]; % New (test) xi An = A(:,xin ); % New A matrix (with extra input) yh = An*inv(An ’*An)*An ’*y; % Model
SSEn = sum ((y -yh).^2); % New error sum of squares ASEn = SSEn /(N- length(xin )); % New averaged squared error Fn = (SSEn - SSE )/ ASE; % New F statistic if Fn <Fb , xib = xin; % Best set of indices di = xi(cnt ); % Drop index
Portland State University ECE 4/557 Regularization
39
Example 6: MATLAB Code
function [] = StepwiseRegression (); N = 500; P = 6; x6 = randn(N ,1); x5 = randn(N ,1); x4 = x6 + 0.5*randn(N ,1); x3 = x5 + 0.5*randn(N ,1); x2 = x4 + x3 + 0.5*randn(N ,1); x1 = x6 + x4 + 0.5*randn(N ,1); y = 5*x6 + 5*x5 + randn(N ,1); y = y - mean(y); A = [x1 x2 x3 x4 x5 x6]; A = A - ones(N ,1)* mean(A); % Center: Make the inputs zero mean Ta = 4.0; % Add threshold Td = 3.9; % Drop threshold xi = []; % Indices of variables in the model di = []; % Index of variable dropped from model chg = 1; % Was there a change in the model? 1 = yes , 0 = no stp = 1; % Step Ab = []; yh = 0; % Model
SSE = sum ((y-yh).^2); % Best SSE so far with xi inputs ASE = SSE /(N-length(xi)); % Average squared error
Portland State University ECE 4/557 Regularization
37
Fb = Fn; % Best F statistic end; end; if Fb <Td , % If Fb is less than drop threshold , xi = xib; % Add the variable Ab = A(:,xi); % Best A matrix so far if isempty(Ab), yhb = 0; else yhb = Ab*inv(Ab ’*Ab)*Ab ’*y; % Model
end; SSEb = sum ((y-yhb).^2); % Best SSE so far with xi inputs ASEb = SSE /(N-length(xi )); % Average squared error fprintf(’Step %d - Drop\n’,stp ); fprintf(’------------------------\n’); fprintf(’Removed variable: %7d\n’,di); fprintf(’F Statistic : %7 .2f\n’,Fb); fprintf(’Drop threshold : %7 .2f\n’,Td); fprintf(’SSE Increase : %7 .2f\n’ ,(SSE -SSEb )/ SSE ); fprintf(’\n’); SSE = SSEb; ASE = ASEb; chg = 1; % Set the change flag else % Else , leave xi alone fprintf(’Step %d - Drop\n’,stp ); fprintf(’------------------------\n’); fprintf(’Removed variable: None\n’); fprintf(’F Statistic : %7 .2f\n’,Fb); fprintf(’Drop threshold : %7 .2f\n’,Td); fprintf(’SSE Increase : %7 .2f\n’ ,(SSE -SSEb )/ SSE ); fprintf(’\n’);
Portland State University ECE 4/557 Regularization
40
while chg , chg = 0; % Intialize flag as no change this loop % --------------------------------- % Add variable loop % --------------------------------- Fb = 0; % Initialize best F statistic for cnt = 1:P, if ¬isempty(xi) & any(xi== cnt), % If variable is already in model , skip continue ; end; xin = [xi;cnt ]; % New (test) xi An = A(:,xin ); % New A matrix (with extra input) yh = An*inv(An ’*An)*An ’*y; % Model
SSEn = sum ((y -yh).^2); % New error sum of squares Fn = (SSE - SSEn )/ ASE; % New F statistic if Fn >Fb , xib = xin; % Best set of indices Fb = Fn; % Best F statistic SSEb = SSEn; end; end; if Fb >Ta , % If Fb is greater than add threshold , xi = xib; % Add the variable Ab = A(:,xi); % Best A matrix so far yhb = Ab*inv(Ab ’*Ab)*Ab ’*y; % Model
ASEb = SSE /(N-length(xi )); % Average squared error fprintf(’Step %d - Add\n’,stp ); fprintf(’------------------------\n’); fprintf(’Added variable : %7d\n’,xi(length(xi ))); fprintf(’F Statistic : %7 .2f\n’,Fb);
Portland State University ECE 4/557 Regularization
38