Portfolio optimization -- estimateCu​stomObject​ivePortfol​io equivalent in R2021b?

조회 수: 9 (최근 30일)
Hello, Matlab community. I am working on a portfolio optimization exercise, and I'm looking for a way to incorporate a user-defined objective function in the usual Portfolio object workflow. The "estimateCustomObjectivePortfolio" function seems to be exactly what I need, but unfortunately it was introduced in R2022b. I am using a corporate R2021b Matlab license, so my IT department may not let me just upgrade to get the new functionality (I'm checking on that as well).
Assuming I have to live with R2021b for now, I'll give you an example of what I'm trying to accomplish. Consider a fixed-income-only universe of asset classes, and durations for each:
- IG corporate duration = 7 years
- high yield corporate duration = 3 years
- treasury duration = 10 years
- etc.
In the portfolio optimization, I'd like to be able to have a stated duration target for the resulting portfolio. But, the duration of any hypothetical portfolio depends on the portfolio weights the optimizer is considering in any given iteration, and I don't see a way (in my version of Matlab) to state that duration target as a constraint or objective.
Thanks in advance for any thoughts you may have.

채택된 답변

MULI
MULI 2024년 6월 18일
Hi Jeremy,
I understand that you need to perform portfolio optimization without using "estimateCustomObjectivePortfolio" function. This can be achieved using `fmincon` function where,
  • The Objective Function is defined inline within the fmincon call as, which minimizes the portfolio variance.
  • The Nonlinear Constraint is also defined inline within the fmincon call which ensures that the difference between the portfolio's duration and the target duration is zero (an equality constraint).
Below is the MATLAB Code that helps in achieving the requirement:
% Example Data
numAssets = 3; % Adjust based on your actual number of assets
covMatrix = rand(numAssets)*0.01; % Replace with your actual covariance matrix
covMatrix = covMatrix + covMatrix' - diag(diag(covMatrix)); % Making symmetric
assetDurations = [7; 3; 10]; % Example durations for each asset
targetDuration = 5; % Target portfolio duration
% Initial guess for the weights
initialWeights = ones(numAssets, 1) / numAssets;
% Bounds for the weights
lb = zeros(numAssets, 1); % Lower bound of 0
ub = ones(numAssets, 1); % Upper bound of 1
% Equality constraint for the sum of weights
Aeq = ones(1, numAssets);
beq = 1;
% Optimization options
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp');
% Solve the optimization problem
[optimalWeights, optimalValue] = fmincon(@(weights) weights' * covMatrix * weights, ...
initialWeights, [], [], Aeq, beq, lb, ub, ...
@(weights) deal([], sum(weights .* assetDurations) - targetDuration), options);
% Display the optimal weights
disp('Optimal Weights:');
disp(optimalWeights);
You may refer to this documentation link for more information related to fmincon function.
Hope this answers your query!
  댓글 수: 1
jeremy gogos
jeremy gogos 2024년 6월 26일
Muli, thank you so much for the guidance! Your illustration of how to run this problem through the optimizer was exactly what I needed.

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

추가 답변 (1개)

Alejandra Pena-Ordieres
Alejandra Pena-Ordieres 2025년 4월 8일
Hello,
Since target duration can be added as a linear equality constraint, you can still use the Portfolio object. You won't need the estimateCustomObjectivePortfolio function. Here is the code that you'd need to include the target duration constraint:
% Initialize Portfolio object
p = Portfolio;
% Set moments and problem constraints (other than target duration)
% ...
% Set target duration constraint
assetDurations = [7; 3; 10]; % Example durations for each asset
targetDuration = 5; % Target portfolio duration
p = setEquality(p,assetDurations',targetDuration);
% Get efficient frontier
plotFrontier(p)
Hope this helps!

카테고리

Help CenterFile Exchange에서 Solver-Based Nonlinear Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by