% script Logistic_regression % performs a logistic regression for the % categorial values in y. % the output parameters are mu and s such that % cfit(1) = mu, cfit(2) = s % for the logistic function in standard form x = 0.5:0.25:5.5; x(20)=[]; y = [0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1]; plot(x,y,'.r','markersize',12) % the logistic (probability distribution) function p = @(c,v) 1./(1+exp(-(v-c(1))/c(2))); % the loss function to be minimized, % i.e. the cross entropy of the predicted distribution loss = @(c) sum(-y.*log(p(c,x))-(1-y).*log(1-p(c,x))); cfit = fminsearch(loss,[2 1]); % display the fitted parameters of the logistic function cfit hold on title('Logistic regression') xx = linspace(x(1),x(end),200); yy = p(cfit,xx); % plot the best (fitted)logistic (probability distribution) function plot(xx,yy,'b','linewidth',2) line([cfit(1) cfit(1) 0],[0 0.5 0.5],'Color','green') hold off