How to use fmincon as fminbd with two different variables

7 次查看(过去 30 天)
Hi there,
While trying to test different RL models, I've come to a concern, we define the model through a function "loglik" where I define the different input vectors and values through an iteration of different subjects (i) and I use fminbnd to search for the min value, being 0 and 1 the min and max limits. U and UU simply relates to two different vectors of data from two different agents.
hh = @(alpha)loglik_modelA(alpha,U,UU);
aa(i) = fminbnd(hh,0,1);
LL(i)=loglik_modelA(aa(i),U,UU);
This simply works perfectly fine, but the problem is when our model has more parameters, let's say for instance two alphas. I know fminbnd is thought for a single variable function, and I understand fmincon is the function that would help me with multiple variables. I've tried this piece of code based on the previous one, but it does not seem to work as I understand the logic behind fmincon might be slightly different.
hh = @(alpha1,alpha2)loglik_modelB(alpha1,alpha2,U,UU);
aa(i) = fmincon(hh,alpha1,alpha2,0,1); %I would expect aa(i) to be a 2D matrix
LL(i)=loglik_modelB(aa(i,1),aa(i,2),U,UU);
As usual, alphas are parameters associated to different prediction errors, and range from 0 to 1.
Any insight would be highly appreciated!
  4 个评论
Matt J
Matt J 2021-7-9
with my level I don't see any example that is similar to what I am looking for
How are they dissimilar? Your question is about how to process a problem with two unknowns instead of one. Every single example in the fmincon documentation is a problem with 2 unknowns.
Unai Vicente
Unai Vicente 2021-7-9
Your question and your tone are not helping me, maybe making me feel small, but thanks. If it makes you feel good, well, good for you.

请先登录,再进行评论。

采纳的回答

Alan Weiss
Alan Weiss 2021-7-9
I think that you need to understand that fmincon minimizes a function of a single argument, usually called x. The x argument can have as many entries as you like. So, for example, if your objective function is naturally a function of three variables, var1, var2, and var3, and var1 is a scalar (single real argument), var2 is a column vector of 25 entries, and var3 is a 25-by-25 matrix, then your objective function of x would look something like this:
function val = myfun(x)
var1 = x(1);
var2 = x(2:26);
var2 = var2(:); % Ensure that var2 is a column vector
var3 = x(27:end);
var3 = reshape(var3,25,25);
% Now do the calculations in terms of var1, var2, and var3 that give val
% When you give x0, the initial point, it should be a column vector of 1 +
% 25 + 25*25 entries
end
Yes, this can be confusing. That is why recent MATLAB versions have another approach, the Problem-Based Optimization Workflow. Using the problem-based approach, you define problem variables in their natural sizes.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
  2 个评论
Unai Vicente
Unai Vicente 2021-7-9
Hi Alan, thanks very much for your input. From what I understand the key here is how I introduce the vectors into the function, this is actually a great input, maybe I was too narrowly approaching how the data was introduced into the function. I'll try to implement it and will post my code back when I get it. Thank you.
Unai Vicente
Unai Vicente 2021-7-12
This was a key answer, the way how the data is introduced in the function has been key to its functionning, I ended up changing the function, to have less vectors.
Thanks @Alan Weiss for your response.
[x,fval]=fmincon('loglik_modelA',[0.5;0.5],[],[],[],[],[0;0],[1;1])

请先登录,再进行评论。

更多回答(1 个)

Kapil Gupta
Kapil Gupta 2021-7-9
I assume you want to use multiple inputs for fminbnd. The following links have similar queries, you can check these out:
  1 个评论
Unai Vicente
Unai Vicente 2021-7-9
编辑:Unai Vicente 2021-7-9
Thanks for your input, @Kapil Gupta. Not exactly, I want to find the alpha that minimizes the loglikelihood function of a reinforcement learning algorithm, which has two alphas in it, and comes out of loglik_modelB function, with the two alphas and U, UU as data inputs. What I want to do is test with fminbnd, in a range from 0 to 1, to try for every one of these alphas to reach the minimum and write it into aa(i) matrix (the code would be inside an iterative loop where i is every subject in the for loop). In the first case, with one parameter, it is perfectly fine, it does the job and aa(i) writes as expected the minimum of the alpha for the max loglikelihood (as before from function loglik_modelA), but I don't get to do the same with two or more parameters, to minimize for every one of the extra parameters, in the case I posted "alpha1" and "alpha2", but they could be more.
I would expect a(ii) to be a 2 dimensional matrix so I can take it to the next step as input parameters again to my function.
After reviewing these links as well as the help from fmibnd and fmincon I haven't searched a single example that comes close to what I want. It is of course possible I am tunnel vissioning this whole thing and I should look at this thing differently, but I guess I simply lack expertise on programming.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surrogate Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by