%% this subroutine calculates the gradient %% of the logit log likelikood %% inputs are y,x,beta and the function %% returns a (kx1) vector of 1st derivatives function [grad]=logitgrad(y,x,beta) nk1=size(x); %% get dimensions of x k=nk1(2); %% parameters xbeta=x*beta; exp_xbeta=exp(xbeta); prob=exp_xbeta./(1+exp_xbeta); constant=y-prob; itt=ones(1,k); derb=kron(itt,constant).*x; grad=sum(derb)'; end