How to find argmax for the function below?
21 views (last 30 days)
Show older comments
Below are my codes. The LLF function has two unknowns, PD and rho, I make them into x variable that has x(1) and x(2) in the function. I tried to use fminsearch(-LLF) to find the argmax but it did not work. I used the fzero instead, but it gives me an error: Error using fzero (line 246)
FZERO cannot continue because user-supplied function_handle ==>...........
Index exceeds the number of array elements (1).
% x(1) = PD, x(2) = rho
r1 = 0.051;
r2 = 0.27;
r3 = 0.037;
r4 = 0.116;
r5 = 0.222;
r6 = 0.121;
r7 = 0.026;
r8 = 0.025;
r9 = 0.02;
r10 = 0.14;
LLF = @(x) log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r1))).*normpdf((sqrt(1-x(2)).*norminv(r1)-norminv(x(1)))./sqrt(x(2)))) +...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r2))).*normpdf((sqrt(1-x(2)).*norminv(r2)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r3))).*normpdf((sqrt(1-x(2)).*norminv(r3)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r4))).*normpdf((sqrt(1-x(2)).*norminv(r4)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r5))).*normpdf((sqrt(1-x(2)).*norminv(r5)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r6))).*normpdf((sqrt(1-x(2)).*norminv(r6)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r7))).*normpdf((sqrt(1-x(2)).*norminv(r7)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r8))).*normpdf((sqrt(1-x(2)).*norminv(r8)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r9))).*normpdf((sqrt(1-x(2)).*norminv(r9)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r10))).*normpdf((sqrt(1-x(2)).*norminv(r10)-norminv(x(1)))./sqrt(x(2))));
x0 = [0,0];
x = fzero(LLF,x0);
3 Comments
Answers (1)
Walter Roberson
on 29 Jan 2020
r1 = 0.051;
r2 = 0.27;
r3 = 0.037;
r4 = 0.116;
r5 = 0.222;
r6 = 0.121;
r7 = 0.026;
r8 = 0.025;
r9 = 0.02;
r10 = 0.14;
LLF = @(x) log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r1))).*normpdf((sqrt(1-x(2)).*norminv(r1)-norminv(x(1)))./sqrt(x(2)))) +...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r2))).*normpdf((sqrt(1-x(2)).*norminv(r2)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r3))).*normpdf((sqrt(1-x(2)).*norminv(r3)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r4))).*normpdf((sqrt(1-x(2)).*norminv(r4)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r5))).*normpdf((sqrt(1-x(2)).*norminv(r5)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r6))).*normpdf((sqrt(1-x(2)).*norminv(r6)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r7))).*normpdf((sqrt(1-x(2)).*norminv(r7)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r8))).*normpdf((sqrt(1-x(2)).*norminv(r8)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r9))).*normpdf((sqrt(1-x(2)).*norminv(r9)-norminv(x(1)))./sqrt(x(2)))) + ...
log(sqrt(1-x(2))./(sqrt(x(2)).*normpdf(norminv(r10))).*normpdf((sqrt(1-x(2)).*norminv(r10)-norminv(x(1)))./sqrt(x(2))));
x0 = [0,0];
LLFmax = @(x) -LLF(x);
options = optimset('MaxFunEvals', 1e6, 'MaxIter', 1e6);
[bestx, fval] = fminsearch(LLFmax, x0, options)
when it eventually stops complaining it ran out of iterations, it will have fval of Inf and the first element of the output will be 0. Tis reflects that if you set x(1) to be 0 then you get out complex infinities, or NaN. The maximum is not well defined unless you add constraints.
0 Comments
See Also
Categories
Find more on Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!