Date: Jan 21, 2013 12:07 PM
Author: Alan Weiss
Subject: Re: fminunc for function with two parameter inputs
On 1/21/2013 10:15 AM, Sebastian wrote:

> Dear all,

>

> I am looking for a way to minimize a function with two inputs:

>

> function ll=loglik(coef,coef2)

>

> p = zeros(NCS,NCLASS);

> for i = 1:NCLASS;

> v = VARS*coef(:,i);

> for n = 1:NCS

> vv = v(IDCASE==n,1);

> vy = v(IDCASE==n & IDDEP==1,1);

> vv = vv-repmat(vy,size(vv,1),1);

> p(n,i) = 1/sum(exp(vv));

> end;

> end;

>

> zg = RISK'*coef2;

> h = zeros(NCLASS,NCS);

> for i = 1:NCS;

> for n = 1:NCLASS;

> v = exp(zg(i,n));

> vv = sum(exp(zg(i,1:end)));

> h(n,i) = v/vv;

> end;

> end;

> h = max(h,0.00000001);

>

> % FUNCTION TO BE MINIMIZED

> ll=-sum(log(diag(p*h)));

>

>

> The problem is, that the inputs do not have the same dimensions.

>

> Example:

> B = [0 0 0; 0 0 0; 0 0 0];

> G = 0;

>

> where B are the initial values for coef and G the ones of coef2

> I tried:

> [paramhat,fval,exitflag,output,grad,hessian]=fminunc(@loglik,[B',G],options)

>

> Error (obviously):

> CAT arguments dimensions are not consistent.

>

> [paramhat,fval,exitflag,output,grad,hessian]=fminunc(@(B)loglik(B',G),options,@(G)loglik(B',G))

>

> Error:

> FMINUNC only accepts inputs of data type double.

>

> What is the correct way of solving this?

>

> Best Regards

> Sebastian

You have two problems: fminunc wants all the variables it controls to be

in one vector or array, and it also wants the initial point to be a

single vector or array. If you want fminunc to change the values of both

coef and coef2, well, you have to get those values into one vector or array.

If coef2 is a constant, then you can follow the prescriptions in

http://www.mathworks.com/help/optim/ug/passing-extra-parameters.html#bskkr4z

For example, you could write

fun = @(coef)loglik(coef,coef2)

and just pass the initial value of coef as x0.

But if, as I suspect, both coef and coef2 are variable, then you need to

think of

x = [coef(:);coef2(:)]

In other words, make a single vector out of all the entries in coef and

coef2.

You can then take

function u = loglik2(x)

N = 15; % instead of 15 put in the number of elements of coef

coef = x(1:N);

coef = reshape(coef,mm,nn); % put in the correct values for mm and nn

coef2 = x(N+1:end);

% reshape coef2 if necessary

u = loglik(coef,coef2)

Do the same trick for x0.

x0 = [B'(:);G(:)];

Then call fminunc:

[paramhat,fval,exitflag,output,grad,hessian]=fminunc(@loglik2,x0,options)

Alan Weiss

MATLAB mathematical toolbox documentation