function [ w_i, data ] = prob_4_3(Niter,method) % PROB_4_3 - % % Written by: % -- % John L. Weatherwax 2005-08-04 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- data = [ 1 1 -1 0 2; 0 0 1 2 0; -1 -1 1 1 0; 4 0 1 2 1; -1 1 1 1 0; -1 -1 -1 1 0; -1 1 1 2 1 ]; class = [ 2 1 2 1 1 1 2 ]; %Niter = 100; d = 0; %d = 0.1; % Method used in computing epsilon_i (1-3): %method = 2; % Value used in fractional correction: lambda = 2; w_i = [ 1 1 1 -1 -1 1 ]; % % Code begins: %--- % Append +1 to all data: data = [ data, ones(size(data,1),1) ]; % Negate the feature vector from class 2: ind = find( class==2 ); data(ind,:) = -data(ind,:); clear ind; w_history = zeros( Niter, length(w_i) ); for i=0:Niter+1, sampleIndex = mod( i, size(data,1) )+1; y_i = data( sampleIndex, : ).'; v_i = y_i; switch( method ) case 1 % Fixed increment: e_i = 1; case 2 % absolute correction: e_i = abs( w_i * v_i - d )/( norm( v_i, 2 )^2 ); case 3 e_i = lambda*abs( w_i * v_i - d )/( norm( v_i, 2 )^2 ); otherwise error( 'Unknown value of method' ); end if( w_i * y_i < d ) % This element is INCORRECTLY classified w_ip1 = w_i + e_i * y_i.'; else w_ip1 = w_i; % This element is CORRECTLY classified end % Save w iterates: w_history( i+1, : ) = w_i; % Prepare for next iteration: w_i = w_ip1; end % Find the points that are MISS-classified % (evaluate the sample error rate): % data * ( w_i.' ) - d J = find( data * (w_i.') < d ); J w_history(end-4:end,:) % $$$ % Plot results: % $$$ w_plot = w_history( 1:10:end, : ); % $$$ figure; plot( w_plot, '-o' );