Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: fminunc for function with two parameter inputs
Replies: 1   Last Post: Jan 21, 2013 12:07 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View  
Alan Weiss

Posts: 1,253
Registered: 11/27/08
Re: fminunc for function with two parameter inputs
Posted: Jan 21, 2013 12:07 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.