% ***** GRADIENT DECENT LOGISTIC 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 % DATA POINTS x=[1 1 1 1 1; 5 1 7 8 2; 8 1 8 8 1]; y=[1 0 1 1 0]; % MODEL PARAMETERS % Note: The length of the array "theta" needs to % equal the number of independent variables % (i.e. length(theta)=n) % Initialize theta theta=[-4 1 1]'; % Initialize alpha alpha=1.4; n=size(x,1); m=size(x,2); for count=1:10 h=1./(1+exp(-theta'*x)); theta(1)=theta(1)-alpha.*(1/m).*sum((h-y)*x(1,:)'); theta(2)=theta(2)-alpha.*(1/m).*sum((h-y)*x(2,:)'); theta(3)=theta(3)-alpha.*(1/m).*sum((h-y)*x(3,:)'); J=(1/m).*sum(y'*log(1./(1+exp(-theta'*x)))-(1-y)'*log(1-(1./(1+exp(-theta'*x))))); plot([1:m],J,'r') hold on end % J is the Maximum Likelihood Logistic Regression cost function % J=-(1/m).*sum(y.*log(h)-(1-y).*log(1-h)); % Regularize with lambda*sum(theta)