[regression] further improved the chapter

This commit is contained in:
2019-12-11 09:48:19 +01:00
parent 9cc839de94
commit 4273a953f6
7 changed files with 91 additions and 107 deletions

View File

@@ -1,22 +1,13 @@
% x, y, slopes, and intercepts from exercise 8.3
slopes = -5:0.25:5;
intercepts = -30:1:30;
error_surface = zeros(length(slopes), length(intercepts));
for i = 1:length(slopes)
for j = 1:length(intercepts)
error_surf(i,j) = meanSquaredError([slopes(i), intercepts(j)], x, y);
end
end
error_surface = zeros(length(slopes), length(intercepts));
gradient_m = zeros(size(error_surface));
gradient_b = zeros(size(error_surface));
for i = 1:length(slopes)
for j = 1:length(intercepts)
error_surface(i,j) = meanSquaredError([slopes(i), intercepts(j)], x, y);
grad = meanSquaredGradient([slopes(i), intercepts(j)], x, y);
error_surface(i,j) = meanSquaredError(x, y, [slopes(i), intercepts(j)]);
grad = meanSquaredGradient(x, y, [slopes(i), intercepts(j)]);
gradient_m(i,j) = grad(1);
gradient_b(i,j) = grad(2);
end

View File

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

View File

@@ -9,8 +9,8 @@ errors = [];
count = 1;
eps = 0.01;
while isempty(gradient) || norm(gradient) > 0.1
gradient = meanSquaredGradient(position, x, y);
errors(count) = meanSquaredError(position, x, y);
gradient = meanSquaredGradient(x, y, position);
errors(count) = meanSquaredError(x, y, position);
position = position - eps .* gradient;
count = count + 1;
end

View File

@@ -1,10 +1,10 @@
function mse = meanSquaredError(parameter, x, y)
function mse = meanSquaredError(x, y, parameter)
% Mean squared error between a straight line and data pairs.
%
% Arguments: parameter, vector containing slope and intercept
% as the 1st and 2nd element, respectively.
% x, vector of the input values
% Arguments: x, vector of the input values
% y, vector of the corresponding measured output values
% parameter, vector containing slope and intercept
% as the 1st and 2nd element, respectively.
%
% Returns: mse, the mean-squared-error.

View File

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