% ***** GRADIENT DESCENT LINEAR REGRESSION ***** % % m: Number of data samples % n: Number of factors or dependent variables % p: Index value of a single sample within the set of m samples % q: Index value of an individual feature within the % ith sample's feature (or variable) vector % x: Feature vector with n+1 features, i.e. x=[x0 x1 x2 x3] & x0=1 % theta: Parameter vector with at least n+1 parameters, % i.e. theta=[theta0 theta1 theta2 theta3] % h: "Hypothesis" or prediction model, i.e. h=theta*transpose(x) % J: Cost function dependent on the parameter values in vector theta clear all close all % Data Points x=[0.1 1.2 3.0 4.6 4.9 5.7 6.2 7.0 8.4 10.1 10.3 10.9 12]; y=[1.3 4.9 3.5 3.9 5.6 4.3 6.7 4.4 7.4 7.8 9.2 11.4 10.4]; % Initialize parameters theta(1)=2; theta(2)=0.08; % Initialize the "momentum" alpha alpha=[0.1 0.001]; m=length(y); h=theta(1)+theta(2).*x; % "Hypothesis" model J(1)=(1/m)*sum((h-y).^2); e=1; count=2; frameCount=1; while (e>0.01) theta(1)=theta(1)-alpha(1).*(2/m).*sum(h-y); theta(2)=theta(2)-alpha(2).*(2/m).*sum((h-y).*x); h=theta(1)+theta(2).*x; J(count)=(1/m)*sum((h-y).^2); e=abs(J(count)-J(count-1)); count=count+1; set(gcf,'Color','white') subplot(2,1,1) plot(x,y,'bo','LineWidth',2) title('Data and Decision Boundary','fontsize',11,'fontweight','b') xlabel('Independent Variable','fontsize',10) ylabel('Dependent Variable','fontsize',10) set(gca,'XColor',[0.35,0.35,0.35]) set(gca,'YColor',[0.35,0.35,0.35]) grid on grid minor set(gca,'Color',[0.65,0.65,0.65]) axis([0 12 0 12]) hold on plot(x,h,'r','LineWidth',2) pause(0.1) hold off subplot(2,1,2) plot(1:(count-1),J(1:(count-1)),'g','LineWidth',2); title('Cost Function','fontsize',11,'fontweight','b') xlabel('Epoch Number','fontsize',10) ylabel('Cost Value','fontsize',10) grid on grid minor set(gca,'Color',[0.65,0.65,0.65]) set(gca,'XColor',[0.35,0.35,0.35]) set(gca,'YColor',[0.35,0.35,0.35]) axis([0 70 0 25]) hold off set(gcf,'Renderer','zbuffer') M(frameCount)= getframe(gcf); frameCount=frameCount+1; M(frameCount)= getframe(gcf); frameCount=frameCount+1; end movie(M) movie2avi(M,'TrivialLinearRegressionLearner.avi')%,'compression','Cinepak')