MATLAB Answers

Problems with passing array of anonymous functions to optimizer

4 views (last 30 days)
Sebastian on 15 Jun 2015
Edited: Walter Roberson on 15 Jun 2015
Dear all,
I am working on writing a flexible estimator for discrete choice data. The idea is that I can specify the function to be estimated flexible like: v = b(1)x1 + b(2)x2
where b(i) are elements of the parameter vector and xi are the variables. The routines can than be called by FUNCTION(v,[SOME OTHER STUFF])
This works fine with fminunc. However, I have great problems with this optimization routine for many models. It slows down a lot and is much more slowly for these types of problems than alternative packages. Thus, I wanted to use the econometric toolbox provided by James LeSage Here I am using the maxlik routine and I get an error message saying that my objective function is not defined for the anonymous function. I get the same issue with the minz routine by Mcliff Again the thing works for the fminlbfgs command.
Given the different structure of the commands:
[b,infoz,stat] = minz(b,'logl',infoz,v,ncs,alt,y);
result = maxlike(func,b,info,varargin);
[X,FVAL,EXITFLAG,OUTPUT,GRAD] = fminlbfgs(@(b)logl(b,v,ncs,alt,y),b,options);
[X,FVAL,EXITFLAG,OUTPUT] = fminunc(FUN,X0,...);
I am assuming that the problem is caused by varargin.
Do you have any ideas how to solve the problem?
If not, what would be an equally flexible alternative to the anonymous function approach?
Your help would be very much appreciated!
Here a very easy example code:
% v = @(b) b(1)*asc + b(2)*fr_d + b(3)*fr_n + b(4)*urge_3 + b(5)*urge_2 + b(6)*urge_1 + b(7)*sse_fluid + b(8)*sse_desire + b(9)*sse_imp + b(10)*nsse_dry + b(11)*nsse_head + b(12)*nsse_dizzy + b(13)*tablets + b(14)*cost;
% b = [0,0,0,0,0,0,0,0,0,0,0,0,0,0]
function [b,infoz,stat] = clogit(b,v,ncs,alt,y)
%result = maxlik('logl',b,[],v,ncs,alt,y);
%options = optimset('GradObj','off','Display','iter','GoalsExactAchieve',0);
%[X,FVAL,EXITFLAG,OUTPUT,GRAD] = fminlbfgs(@(b)logl(b,v,ncs,alt,y),b,options);
infoz.hess = 'dfp'
[b,infoz,stat] = minz(b,'logl',infoz,v,ncs,alt,y);
function ll = logl(b,v,ncs,alt,y)
y = reshape(y,max(alt),max(ncs));
x = reshape(exp(v(b)),max(alt),max(ncs));
p = x(y == 1)./sum(x)';
p = max(p,0.0000001);
ll = -sum(log(p));
Best wishes

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!