Using fminsearch for solving a problem with constraints

12 visualizaciones (últimos 30 días)
Andrew
Andrew el 20 de Jul. de 2013
Respondida: Divyam el 5 de Nov. de 2024 a las 9:52
I have an optimization problem that is suitable for fmincon but fmincon fails to converge. I have to increase the tolerances to absurd levels (22%) to make it work.
I am trying to find an optimum portfolio from a set of 8 assets. The program will determine optimal percentage weight (W) of each asset in the portfolio.
I would like to try fminsearch and therefore have to come up with ways to make sure that:
the weights (variable) don't go negative
and that the weights of all assets add up to 1.
Is there a trick to put either or both of these constraints somewhere (either in the functions or in the optimization tool)?
I have put these constraints in the function and the function by itself works but fminsearch does not work on this function. It stops at zero iteration and gives the following error mesasge:
Output argument "RARV" (and maybe others) not assigned during call to "C:\Users\ ....
The code I am using is the following:
function RARV=RARvariance2(A,B,C,D,E,F,G,H,W)
T=length(A); %Time period, the same for all assets
SW=sum(W);
if abs(SW-1)<.00000001
if W(1,:)>0
DD=[A-mean(A), B-mean(B), C-mean(C), D-mean(D), E-mean(E), F-mean(F), G-mean(G), H-mean(H)]; %Temporal, needed for covariances
Cov=DD'*DD/T;
V=W(1,:)*Cov*W(1,:)';
%Portfolio expected returns
ER=[mean(A), mean(B), mean(C), mean(D), mean(E), mean(F), mean(G), mean(H)]*W(1,:)';
%(Negative) risk adjusted return
RARV=-(ER)/sqrt(V); %Change V to sqrt(V) for semi-standard deviation
else
end
end
Please help. Thanks.

Respuestas (1)

Divyam
Divyam el 5 de Nov. de 2024 a las 9:52
Hi @Andrew,
You can perform the softmax transformation on your unconstrained variables in the code itself. The softmax transformation will ensure that your weights are non-negative and sum to 1. You can then call the "fminsearch" on this transformed objective function and let the "fminsearch" handle everything else on its own. Here is a sample code to help you with the same:
% Define sample asset returns (e.g., monthly returns) or read your data
% Initial guess for the unconstrained variables
x0 = zeros(1, 8);
% Objective function with constraints handled internally
objectiveFunction = @(x) RARvariance2_unconstrained(x, A, B, C, D, E, F, G, H);
% Call fminsearch
options = optimset('Display', 'iter'); % Display iteration information
[x_opt, fval] = fminsearch(objectiveFunction, x0, options);
% Transform the optimal unconstrained variables to weights
expX_opt = exp(x_opt);
W_opt = expX_opt / sum(expX_opt);
% Function definition
function RARV = RARvariance2_unconstrained(x, A, B, C, D, E, F, G, H)
% Transform the unconstrained variables to constrained weights using softmax
expX = exp(x);
W = expX / sum(expX);
T = length(A); % Time period, the same for all assets
DD = [A - mean(A); B - mean(B); C - mean(C); D - mean(D); ...
E - mean(E); F - mean(F); G - mean(G); H - mean(H)]; % Temporal, needed for covariances
Cov = (DD * DD') / T;
V = W * Cov * W';
% Portfolio expected returns
ER = [mean(A), mean(B), mean(C), mean(D), mean(E), mean(F), mean(G), mean(H)] * W';
% (Negative) risk adjusted return
RARV = -(ER) / sqrt(V); % Change V to sqrt(V) for semi-standard deviation
end
For more information regarding the "fminsearch" function, refer to the following documentation: https://www.mathworks.com/help/matlab/ref/fminsearch.html
For more information regarding softmax transformation on data, refer to this article: https://nhigham.com/2021/01/12/what-is-the-softmax-function/

Categorías

Más información sobre Systems of Nonlinear Equations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by