I give below the changes in the codes that I made for questions 2-4. Of course, there are other ways to do these tings, and so my codes are just suggestions. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Question 2: Alternative Specific Constants ADDED TO logit.m: %Do you want to include alternative specific constants for all except the last alt? %ASC=1 for yes, and ASC=0 for no ASC=1; ADDED to doit.m: %Create ascs if ASC == 1; nalt=max(XMAT(:,2)); startasc=zeros(1,nalt-1); for n=1:(nalt-1); newvar=XMAT(:,2)==n; VARS=[VARS newvar]; NAMES=[NAMES num2str(n)]; end; end; if ASC==0; param=[B]'; %starting values: take transpose since must be col vector else; param=[B startasc]'; end; @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Question 3: Scale ADDED TO logit.m: global WANTSC SCGROUP %Do you want to estimate the scale for a group? % WANTSC=1 for yes, WANTSC=0 for no WANTSC=1; %create a variable that identifies the group with an estimated scale. %SCGROUP must have as NROWS rows and 1 col, with elements of 1 for in group, 0 not SCGROUP=XMAT(:,11)+XMAT(:,12); ADDED TO doit.m: if WANTSC==1; B=[B ones(1,1)]; NAMES=[NAMES 'scale']; end; ADDED TO loglik.m: global WANTSC SCGROUP AND replaced "v=VARS*coef" with this: if WANTSC==0; v=VARS*coef; else; v=VARS*coef(1:end-1,1); v(SCGROUP==1,1)=v(SCGROUP==1,1).*coef(end,1); %Multiplicative scale for group end; @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Question 4: Gradient CHANGED "options" and fminunc" commands in doit.m to: options=optimset('LargeScale','off','Display','iter','GradObj','on',... 'MaxFunEvals',10000,'MaxIter',MAXITERS,'TolX',PARAMTOL,'TolFun',LLTOL,'DerivativeCheck','on'); [paramhat,fval,exitflag,output,grad,hessian]=fminunc(@loglikgr,param,options); HERE is the entire loglikgr.m code: %Inputs: coef is Kx1 where K is number of explanatory variables % %Globals: NCS: scale number of choice situations % IDCASE: NROWSx1 vector identifying the rows for each choice situation (1-NCS), where NROWS is number of alternatives % in all choice situations combined % IDDEP: NROWSx1 vector identifying the chosen alternative (1=chosen, 0=nonchosen) % VARS: NROWSxK matrix of explanatory variables function [ll,gr]=loglikgr(coef); global NCS IDCASE IDDEP VARS p=zeros(NCS,1); k=size(coef,1); g=zeros(NCS,k); v=VARS*coef; for n=1:NCS vv=v(IDCASE==n,1); vy=v(IDCASE==n & IDDEP==1,1); %Create gradient here pp=exp(vv)./sum(exp(vv),1); %Prob for each alt gg=IDDEP(IDCASE==n,1)-pp; g(n,:)=gg'*VARS(IDCASE==n,:); %Done with gradient vv=vv-repmat(vy,size(vv,1),1); p(n,1)=1/sum(exp(vv)); end p=max(p,0.00000001); %As a precaution ll=-sum(log(p),1); %Negative since neg of ll is minimized gr=-sum(g)'; %Transpose since fminunc needs col vector