% uses the last column as the target and randomly holds % out test_frac of the rows for evaluating solution robustness function compare_qlsqr(A, epsilon, test_frac) if(nargin < 2), epsilon = 0.01; end if(nargin < 3), test_frac = 0.2; end [baseX, baseY, testX, testY] = split_data(A, test_frac); % time the qlsqr solution tic; bQ = qlsqr(baseX, baseY, epsilon); qTime = toc; % eval error of qlsqr solution on held-out rows errQ = norm(testY - testX*bQ, 'fro') / sqrt(length(testY)); fprintf('\tqlsqr: %.4f sec, error on held-out rows: %.4f rmse\n', qTime, errQ); % time the exact solution tic; bEx = baseX \ baseY; exactTime = toc; % eval error of exact solution on held-out rows errEx = norm(testY - testX*bEx, 'fro') / sqrt(length(testY)); fprintf('\tmatlab: %.4f sec, error on held-out rows: %.4f rmse\n', exactTime, errEx); fprintf('\tqlsqr speedup: %.2f\n', exactTime/qTime); end % randomly splits rows into fractions f and 1-f, % then splits final column from each for use as a linear system target function [base_x, base_y, test_x, test_y] = split_data(A, test_frac) [m,n] = size(A); perm_i = randperm(m); % permute rows test_size = ceil(test_frac*m); base_i = perm_i(1:(m-test_size)); test_i = perm_i((m-test_size+1):m); base_x = A(base_i, 1:(n-1)); base_y = A(base_i, n); test_x = A(test_i, 1:(n-1)); test_y = A(test_i, n); end