I try to solve a constrained optimisation problem, where the variable is a vector. In one of the constraints I use pinv, but this creates an error code as this:
Error using svd
First input must be single or double.
Error in pinv (line 18)
[U,S,V] = svd(A,'econ');
Error in CAPM_2 (line 21)
cons2 = (w'*r*pinv(D)*(ones(n-1,1)-beta(1:(n-1)))==mu_M);
I cannot find a way around this (happy to use other optimisation routines)
My code:
r=0.05; %exogenous parameters
mu_M=0.1;
n=2; %dimension of vector
w=cumsum([1:n])'; %creating a vector of weights
w=w.^2;
w=w/sum(w);
prob = optimproblem('ObjectiveSense','min');
beta = optimvar('beta',n);
prob.Objective = (ones(n,1)-beta)'*(ones(n,1)-beta); %objective function is minimizing the norm over beta with the below constraints
cons1 = (w'*beta==1);
D=eye(n)-beta*w'; %This creates a matrix for the second constraint. It is singular (w'*ones(n,1)=1)
D=D(1:(n-1),:); %Cuts the excess line out
cons2 = (w'*r*pinv(D)*(ones(n-1,1)-beta(1:(n-1)))==mu_M);
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
show(prob)

 채택된 답변

Walter Roberson
Walter Roberson 2021년 4월 26일

0 개 추천

pinv() and matrix division are not supported.
However, for 1 x N variables, then
PINV_D = (D./sum(D.*conj(D))).'

댓글 수: 6

Andreas Krause
Andreas Krause 2021년 4월 26일
Unfortunately, D in general has dimension n x (n-1), so this won't work, except in your special case
Walter Roberson
Walter Roberson 2021년 4월 26일
If your n does not change all that much, and if you have the Symbolic Toolbox, you could generate the general forms for the n you are likely to use, and use matlabFunction() to save them to files; do that ahead of time, and then at run-time pull out the appropriate function and invoke it on the optimization variable.
Andreas Krause
Andreas Krause 2021년 4월 26일
The n changes quite a bit (from 10 to about 5000), plus the w will be changing a lot (I just put an example in there I use for testing).
I can get an analytical expression for the first order condition of the minimum, which I then could use to solve, but this also involves pinv or inv.
Walter Roberson
Walter Roberson 2021년 4월 26일
Unfortunately your needs appear to be beyond the current capabilities of problem-based optimization; you might need to use solver-based optimization.
Andreas Krause
Andreas Krause 2021년 4월 26일
Thanks for your help. A shame it does not work this way. Off to solvers then...
Matt J
Matt J 2021년 4월 26일
For the problem you've shown, there would scarcely be an advantage to using the problem-based framework even if pinv were supported. You don't have any complicated linear constraints or a complicated partitioning of beta into sub-vectors.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

릴리스

R2020a

질문:

2021년 4월 26일

댓글:

2021년 4월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by