How to create function handle using random output
3 次查看(过去 30 天)
显示 更早的评论
Hi all,
I am trying to implement something called a "Tailored Randomized Block-MH" algorithm, which requires me to take a likelihood function, change a subset of parameters while fixing the others through the optimization step.
Suppose I have a function f([p,q,r,x,y,z]) and a randomization step tells me to hold [p,q,r] at [p_bar,q_bar,r_bar] fixed while changing [x,y,z]. I can type manually something like:
maximand = @(x) f([p_bar,q_bar,r_bar,[x_1,x_2,x_3])
how would I do something like this using just the output from the output of, say, randperm(3)?
6 个评论
Walter Roberson
2023-2-23
Which specific optimizer are you using? Some of them make it easier than others.
回答(3 个)
Jan
2023-2-23
Maybe:
maximand = @(x) f([p_bar, q_bar, r_bar, x(randperm(3, 3))])
2 个评论
Walter Roberson
2023-2-23
That potentially returns one of six different values, depending on which permutation occurs. It might possibly make sense to me to deliberately try all permutations of x, six calls to f, but I am having difficulty thinking of a context in which randomly selecting would be useful.
Walter Roberson
2023-2-23
For fmincon and simulannealbnd the easiest way to handle this is to use the same function call in each case, but set the ub and lb the same for the entries that are to be fixed for this run.
For example,
rvars = sort(randperm(numel(UB),NumFixedVars));
fixedvals = LB(rvars) + rand(1,NumFixedVars) .* (UB(rvars) - LB(rvars));
LB(rvars) = fixedvals; UB(rvars) = fixedvals;
x0(rvars) = fixedvals;
[bestx, fvals, exitflag] = fmincon(fun, x0, A, b, Aeq, beq, LB, UB, nonlcon, opts);
This code picks random variable indices. Then it picks random values between the lower and upper bound for those variables, and sets the lower and upper bound to be the same for those variables, and proceeds to run the optimization.
You might, of course, have had completely different logic in mind as to how to choose the fixed values for the variables, so alter the fixedvals assignment as appropriate for your situation.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!