[regression] fixed m code
This commit is contained in:
parent
4273a953f6
commit
14160e2f8d
@ -1,13 +1,12 @@
|
|||||||
% x, y, slopes, and intercepts from exercise 8.3
|
% x, y, slopes, intercepts, ii, ss, and error_surface from exercise 8.3
|
||||||
|
|
||||||
error_surface = zeros(length(slopes), length(intercepts));
|
qslopes = 1.0:0.5:4.0;
|
||||||
gradient_m = zeros(size(error_surface));
|
qintercepts = -150:50:150;
|
||||||
gradient_b = zeros(size(error_surface));
|
gradient_m = zeros(length(qintercepts), length(qslopes));
|
||||||
|
gradient_b = zeros(length(qintercepts), length(qslopes));
|
||||||
for i = 1:length(slopes)
|
for i = 1:length(qintercepts)
|
||||||
for j = 1:length(intercepts)
|
for j = 1:length(qslopes)
|
||||||
error_surface(i,j) = meanSquaredError(x, y, [slopes(i), intercepts(j)]);
|
grad = meanSquaredGradient(x, y, [qslopes(j), qintercepts(i)]);
|
||||||
grad = meanSquaredGradient(x, y, [slopes(i), intercepts(j)]);
|
|
||||||
gradient_m(i,j) = grad(1);
|
gradient_m(i,j) = grad(1);
|
||||||
gradient_b(i,j) = grad(2);
|
gradient_b(i,j) = grad(2);
|
||||||
end
|
end
|
||||||
@ -15,13 +14,12 @@ end
|
|||||||
|
|
||||||
figure()
|
figure()
|
||||||
hold on
|
hold on
|
||||||
[N, M] = meshgrid(intercepts, slopes);
|
contour(ss, ii, error_surface, 70);
|
||||||
%surface(M, N, error_surface, 'FaceAlpha', 0.5);
|
[qss, qii] = meshgrid(qslopes, qintercepts);
|
||||||
contour(M, N, error_surface, 50);
|
quiver(qss, qii, gradient_m, gradient_b, 0.01)
|
||||||
quiver(M, N, gradient_m, gradient_b)
|
|
||||||
xlabel('Slope m')
|
xlabel('Slope m')
|
||||||
ylabel('Intercept b')
|
ylabel('Intercept b')
|
||||||
zlabel('Mean squared error')
|
zlabel('Mean squared error')
|
||||||
set(gcf, 'paperunits', 'centimeters', 'papersize', [15, 10.5], ...
|
%set(gcf, 'paperunits', 'centimeters', 'papersize', [15, 10.5], ...
|
||||||
'paperposition', [0., 0., 15, 10.5])
|
% 'paperposition', [0., 0., 15, 10.5])
|
||||||
saveas(gcf, 'error_gradient', 'pdf')
|
%saveas(gcf, 'error_gradient', 'pdf')
|
||||||
|
@ -1,27 +1,27 @@
|
|||||||
% generate data:
|
% generate data:
|
||||||
m = 0.75;
|
m = 3.0;
|
||||||
b = -40.0;
|
b = -40.0;
|
||||||
n = 20;
|
n = 20;
|
||||||
x = 120.0*rand(n, 1);
|
x = 120.0*rand(n, 1);
|
||||||
y = m*x + b + 15.0*randn(n, 1);
|
y = m*x + b + 15.0*randn(n, 1);
|
||||||
|
|
||||||
% compute mean squared error for a range of slopes and intercepts:
|
% compute mean squared error for a range of slopes and intercepts:
|
||||||
slopes = -5:0.25:5;
|
slopes = 0.0:0.1:5.0;
|
||||||
intercepts = -30:1:30;
|
intercepts = -200:10:200;
|
||||||
error_surface = zeros(length(slopes), length(intercepts));
|
error_surface = zeros(length(intercepts), length(slopes));
|
||||||
for i = 1:length(slopes)
|
for i = 1:length(intercepts)
|
||||||
for j = 1:length(intercepts)
|
for j = 1:length(slopes)
|
||||||
error_surf(i,j) = meanSquaredError(x, y, [slopes(i), intercepts(j)]);
|
error_surface(i,j) = meanSquaredError(x, y, [slopes(j), intercepts(i)]);
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
% plot the error surface:
|
% plot the error surface:
|
||||||
figure()
|
figure()
|
||||||
[N,M] = meshgrid(intercepts, slopes);
|
[ss, ii] = meshgrid(slopes, intercepts);
|
||||||
surface(M, N, error_surface);
|
surface(ss, ii, error_surface);
|
||||||
xlabel('slope', 'rotation', 7.5)
|
xlabel('slope', 'rotation', 7.5)
|
||||||
ylabel('intercept', 'rotation', -22.5)
|
ylabel('intercept', 'rotation', -22.5)
|
||||||
zlabel('error')
|
zlabel('mean squared error')
|
||||||
set(gca,'xtick', (-5:2.5:5))
|
set(gca,'xtick', (-5:2.5:5))
|
||||||
grid on
|
grid on
|
||||||
view(3)
|
view(3)
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
% x, y from exercise 8.3
|
% x, y from exercise 8.3
|
||||||
|
|
||||||
% some arbitrary values for the slope and the intercept to start with:
|
% some arbitrary values for the slope and the intercept to start with:
|
||||||
position = [-2. 10.];
|
position = [-2.0, 10.0];
|
||||||
|
|
||||||
% gradient descent:
|
% gradient descent:
|
||||||
gradient = [];
|
gradient = [];
|
||||||
errors = [];
|
errors = [];
|
||||||
count = 1;
|
count = 1;
|
||||||
eps = 0.01;
|
eps = 0.0001;
|
||||||
while isempty(gradient) || norm(gradient) > 0.1
|
while isempty(gradient) || norm(gradient) > 0.1
|
||||||
gradient = meanSquaredGradient(x, y, position);
|
gradient = meanSquaredGradient(x, y, position);
|
||||||
errors(count) = meanSquaredError(x, y, position);
|
errors(count) = meanSquaredError(x, y, position);
|
||||||
|
@ -8,9 +8,9 @@ function gradient = meanSquaredGradient(x, y, parameter)
|
|||||||
%
|
%
|
||||||
% Returns: the gradient as a vector with two elements
|
% Returns: the gradient as a vector with two elements
|
||||||
|
|
||||||
h = 1e-6; % stepsize for derivatives
|
h = 1e-5; % stepsize for derivatives
|
||||||
mse = meanSquaredError(x, y, parameter);
|
mse = meanSquaredError(x, y, parameter);
|
||||||
partial_m = (meanSquaredError(x, y, [parameter(1)+h, parameter(2)]) - mse)/h;
|
partial_m = (meanSquaredError(x, y, [parameter(1)+h, parameter(2)]) - mse)/h;
|
||||||
partial_n = (meanSquaredError(x, y, [parameter(1), parameter(2)+h]) - mse)/h;
|
partial_b = (meanSquaredError(x, y, [parameter(1), parameter(2)+h]) - mse)/h;
|
||||||
gradient = [partial_m, partial_n];
|
gradient = [partial_m, partial_b];
|
||||||
end
|
end
|
||||||
|
@ -16,6 +16,14 @@
|
|||||||
|
|
||||||
\include{regression}
|
\include{regression}
|
||||||
|
|
||||||
|
\subsection{Start with one-dimensional problem!}
|
||||||
|
\begin{itemize}
|
||||||
|
\item Just the root mean square as a function of the slope
|
||||||
|
\item 1-d gradient
|
||||||
|
\item 1-d gradient descend
|
||||||
|
\item Homework is to do the 2d problem!
|
||||||
|
\end{itemize}
|
||||||
|
|
||||||
\subsection{Linear fits}
|
\subsection{Linear fits}
|
||||||
\begin{itemize}
|
\begin{itemize}
|
||||||
\item Polyfit is easy: unique solution!
|
\item Polyfit is easy: unique solution!
|
||||||
|
Reference in New Issue
Block a user