FInished regression chapter for now.

This commit is contained in:
2015-11-12 10:30:45 +01:00
parent 497b17227a
commit 6b3f3e4abb
7 changed files with 108 additions and 86 deletions

View File

@@ -1,4 +1,3 @@
clear
load('lin_regression.mat')
ms = -1:0.5:5;
@@ -23,9 +22,9 @@ hold on
%surface(M,N, error_surf, 'FaceAlpha', 0.5);
contour(M,N, error_surf, 50);
quiver(M,N, gradient_m, gradient_n)
xlabel('slope')
ylabel('intercept')
zlabel('error')
set(gcf, 'paperunits', 'centimeters', 'papersize', [15, 12.5], ...
'paperposition', [0., 0., 15, 12.5])
xlabel('Slope m')
ylabel('Intercept b')
zlabel('Mean squared error')
set(gcf, 'paperunits', 'centimeters', 'papersize', [15, 10.5], ...
'paperposition', [0., 0., 15, 10.5])
saveas(gcf, 'error_gradient', 'pdf')

View File

@@ -6,7 +6,7 @@ intercepts = -30:1:30;
error_surf = zeros(length(slopes), length(intercepts));
for i = 1:length(slopes)
for j = 1:length(intercepts)
error_surf(i,j) = lsqError(x, y, [slopes(i), intercepts(j)]);
error_surf(i,j) = lsqError([slopes(i), intercepts(j)], x, y);
end
end

View File

@@ -1,13 +1,13 @@
function error = lsqError(x, y, parameter)
function error = lsqError(parameter, x, y)
% Objective function for fitting a linear equation to data.
%
% Arguments: x, the input values
% y, the corresponding measured output values
% parameter, vector containing slope and intercept
% Arguments: parameter, vector containing slope and intercept
% as the 1st and 2nd element
% x, vector of the input values
% y, vector of the corresponding measured output values
%
% Returns: the estimation error in terms of the mean sqaure error
y_est = x .* parameter(1) + parameter(2);
error = meanSquareError(y, y_est);
y_est = x .* parameter(1) + parameter(2);
error = meanSquareError(y, y_est);
end

View File

@@ -1,17 +1,17 @@
function gradient = lsqGradient(x, y, parameter)
function gradient = lsqGradient(parameter, x, y)
% The gradient of the least square error
%
% Arguments: x, the input values
% y, the corresponding measured output values
% parameter, vector containing slope and intercept
% Arguments: parameter, vector containing slope and intercept
% as the 1st and 2nd element
% x, vector of the input values
% y, vector of the corresponding measured output values
%
% Returns: the gradient as a vector with two elements
h = 1e-6; % stepsize for derivatives
partial_m = (lsqError(x, y, [parameter(1)+h, parameter(2)]) - lsqError(x, y, parameter))/ h;
partial_n = (lsqError(x, y, [parameter(1), parameter(2)+h]) - lsqError(x, y, parameter))/ h;
partial_m = (lsqError([parameter(1)+h, parameter(2)], x, y) - lsqError(parameter, x, y))/ h;
partial_n = (lsqError([parameter(1), parameter(2)+h], x, y) - lsqError(parameter, x, y))/ h;
gradient = [partial_m, partial_n];
end