Skip to content
Permalink
3a174d1cb0
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
37 lines (24 sloc) 726 Bytes
function [J,grad]=cost_logistic(a,x,y)
N=length(y);
J=0;
grad=0;
t=a(1)+a(2).*x;
sigm=1./(1+exp(-t));
J=sum(-y.*log(sigm)- (1-y).*log(1-sigm));
costFun=@ (a) sum(-y.*log((1./(1+exp(-(a(1)+a(2).*x)))))- (1-y).*log(1-(1./(1+exp(-(a(1)+a(2).*x))))));
grad=(1/length(x))*sum((sigm - y).*t);
initial=[0 0];
% Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(a)(costFunction(a, x, y)), initial_a, options);
tt=theta(1)+theta(2).*x;
sigmm=1./(1+exp(-tt));
setdefaults
plot(x,y,'o', x, sigmm);
title('Best fit regression')
xlabel('Temp (F)')
ylabel('Pass/Fail (0,1)')
end