I've written a simple linear regression algorithm in Octave, but no matter what learning rate and number of iterations I chose, and even drawing out the matrices on paper, the values for theta never converge. Can anyone see any mistakes in my code?
data = load('ex1data2.txt');
X = data(:,1:2);
y = data(:,3);
m = rows(X);
X = [ones(m,1), data(:,1:2)];
alpha = 0.01;
iterations = 5000;
n = columns(X);
theta = zeros(n,1);
for count = 1:iterations
hypo = zeros(1,m);
hypo = theta'*X';
sqr_err = (hypo-y').*(hypo-y');
sum_sqr_err = sum(sqr_err);
J = 1/(2*m)*sum_sqr_err;
for i = 1:n
theta(i) = theta(i)-(alpha/m)*((hypo-y')*X(:,i));
end
end
J
theta
Thanks.