Main Content

portopt Migration to Portfolio Object

Migrate portopt Without Output Arguments

This example shows how to migrate portopt without output arguments to a Portfolio object.

The basic portopt functionality is represented as:

ExpReturn = [ 0.0054; 0.0531; 0.0779; 0.0934; 0.0130 ];

ExpCovariance = [ 0.0569,  0.0092,  0.0039,  0.0070,  0.0022;
	0.0092,  0.0380,  0.0035,  0.0197,  0.0028;
	0.0039,  0.0035,  0.0997,  0.0100,  0.0070;
	0.0070,  0.0197,  0.0100,  0.0461,  0.0050;
	0.0022,  0.0028,  0.0070,  0.0050,  0.0573 ];

NumPorts = 10;

portopt(ExpReturn, ExpCovariance, NumPorts);

To migrate a portopt syntax without output arguments to a Portfolio object:

ExpReturn = [ 0.0054; 0.0531; 0.0779; 0.0934; 0.0130 ];

ExpCovariance = [ 0.0569,  0.0092,  0.0039,  0.0070,  0.0022;
	0.0092,  0.0380,  0.0035,  0.0197,  0.0028;
	0.0039,  0.0035,  0.0997,  0.0100,  0.0070;
	0.0070,  0.0197,  0.0100,  0.0461,  0.0050;
	0.0022,  0.0028,  0.0070,  0.0050,  0.0573 ];

NumPorts = 10;

p = Portfolio;
p = setAssetMoments(p, ExpReturn, ExpCovariance);
p = setDefaultConstraints(p);

plotFrontier(p, NumPorts);

Without output arguments, portopt plots the efficient frontier. The Portfolio object has similar behavior although the Portfolio object writes to the current figure window rather than create a new window each time a plot is generated.

Migrate portopt with Output Arguments

This example shows how to migrate portopt with output arguments to a Portfolio object.

With output arguments, the basic functionality of portopt returns portfolio moments and weights. Once the Portfolio object is set up, moments and weights are obtained in separate steps.

ExpReturn = [ 0.0054; 0.0531; 0.0779; 0.0934; 0.0130 ];

ExpCovariance = [ 0.0569,  0.0092,  0.0039,  0.0070,  0.0022;
	0.0092,  0.0380,  0.0035,  0.0197,  0.0028;
	0.0039,  0.0035,  0.0997,  0.0100,  0.0070;
	0.0070,  0.0197,  0.0100,  0.0461,  0.0050;
	0.0022,  0.0028,  0.0070,  0.0050,  0.0573 ];

NumPorts = 10;

[PortRisk, PortReturn, PortWts] = portopt(ExpReturn, ExpCovariance, NumPorts);

display(PortWts);
PortWts =

    0.2103    0.2746    0.1157    0.1594    0.2400
    0.1744    0.2657    0.1296    0.2193    0.2110
    0.1386    0.2567    0.1436    0.2791    0.1821
    0.1027    0.2477    0.1575    0.3390    0.1532
    0.0668    0.2387    0.1714    0.3988    0.1242
    0.0309    0.2298    0.1854    0.4587    0.0953
         0    0.2168    0.1993    0.5209    0.0629
         0    0.1791    0.2133    0.5985    0.0091
         0    0.0557    0.2183    0.7260         0
         0         0         0    1.0000         0

To migrate a portopt syntax with output arguments:

ExpReturn = [ 0.0054; 0.0531; 0.0779; 0.0934; 0.0130 ];

ExpCovariance = [ 0.0569,  0.0092,  0.0039,  0.0070,  0.0022;
	0.0092,  0.0380,  0.0035,  0.0197,  0.0028;
	0.0039,  0.0035,  0.0997,  0.0100,  0.0070;
	0.0070,  0.0197,  0.0100,  0.0461,  0.0050;
	0.0022,  0.0028,  0.0070,  0.0050,  0.0573 ];

NumPorts = 10;

p = Portfolio;
p = setAssetMoments(p, ExpReturn, ExpCovariance);
p = setDefaultConstraints(p);

PortWts = estimateFrontier(p, NumPorts);
[PortRisk, PortReturn] = estimatePortMoments(p, PortWts);

display(PortWts);
PortWts =

    0.2103    0.1744    0.1386    0.1027    0.0668    0.0309         0         0         0         0
    0.2746    0.2657    0.2567    0.2477    0.2387    0.2298    0.2168    0.1791    0.0557         0
    0.1157    0.1296    0.1436    0.1575    0.1714    0.1854    0.1993    0.2133    0.2183         0
    0.1594    0.2193    0.2791    0.3390    0.3988    0.4587    0.5209    0.5985    0.7260    1.0000
    0.2400    0.2110    0.1821    0.1532    0.1242    0.0953    0.0629    0.0091         0         0

The Portfolio object returns PortWts with portfolios going down columns, not across rows. Portfolio risks and returns are still in column format.

Migrate portopt for Target Returns Within Range of Efficient Portfolio Returns

This example shows how to migrate portopt target returns within range of efficient portfolio returns to a Portfolio object.

portopt can obtain portfolios with specific targeted levels of return but requires that the targeted returns fall within the range of efficient returns. The Portfolio object handles this by selecting portfolios at the ends of the efficient frontier.

ExpReturn = [ 0.0054; 0.0531; 0.0779; 0.0934; 0.0130 ];

ExpCovariance = [ 0.0569,  0.0092,  0.0039,  0.0070,  0.0022;
	0.0092,  0.0380,  0.0035,  0.0197,  0.0028;
	0.0039,  0.0035,  0.0997,  0.0100,  0.0070;
	0.0070,  0.0197,  0.0100,  0.0461,  0.0050;
	0.0022,  0.0028,  0.0070,  0.0050,  0.0573 ];

NumPorts = 10;

TargetReturn = [ 0.05; 0.06; 0.07; 0.08; 0.09 ];

[PortRisk, PortReturn, PortWts] = portopt(ExpReturn, ExpCovariance, [], TargetReturn);

disp(' Efficient    Target');
disp([PortReturn, TargetReturn]);
 Efficient    Target
    0.0500    0.0500
    0.0600    0.0600
    0.0700    0.0700
    0.0800    0.0800
    0.0900    0.0900

To migrate a portopt syntax for target returns within range of efficient portfolio returns to a Portfolio object:

ExpReturn = [ 0.0054; 0.0531; 0.0779; 0.0934; 0.0130 ];

ExpCovariance = [ 0.0569,  0.0092,  0.0039,  0.0070,  0.0022;
	0.0092,  0.0380,  0.0035,  0.0197,  0.0028;
	0.0039,  0.0035,  0.0997,  0.0100,  0.0070;
	0.0070,  0.0197,  0.0100,  0.0461,  0.0050;
	0.0022,  0.0028,  0.0070,  0.0050,  0.0573 ];

NumPorts = 10;

TargetReturn = [ 0.05; 0.06; 0.07; 0.08; 0.09 ];

p = Portfolio;
p = setAssetMoments(p, ExpReturn, ExpCovariance);
p = setDefaultConstraints(p);

PortWts = estimateFrontierByReturn(p, TargetReturn);
[PortRisk, PortReturn] = estimatePortMoments(p, PortWts);

disp(' Efficient    Target');
disp([PortReturn, TargetReturn]);
 Efficient    Target
    0.0500    0.0500
    0.0600    0.0600
    0.0700    0.0700
    0.0800    0.0800
    0.0900    0.0900

Migrate portopt for Target Return Outside Range of Efficient Portfolio Returns

This example shows how to migrate portopt target returns outside of range of efficient portfolio returns to a Portfolio object.

When the target return is outside of the range of efficient portfolio returns, portopt generates an error. The Portfolio object handles this effectively by selecting portfolios at the ends of the efficient frontier.

ExpReturn = [ 0.0054; 0.0531; 0.0779; 0.0934; 0.0130 ];

ExpCovariance = [ 0.0569,  0.0092,  0.0039,  0.0070,  0.0022;
	0.0092,  0.0380,  0.0035,  0.0197,  0.0028;
	0.0039,  0.0035,  0.0997,  0.0100,  0.0070;
	0.0070,  0.0197,  0.0100,  0.0461,  0.0050;
	0.0022,  0.0028,  0.0070,  0.0050,  0.0573 ];

NumPorts = 10;

TargetReturn = [ 0.05; 0.06; 0.07; 0.08; 0.09; 0.10 ];

[PortRisk, PortReturn, PortWts] = portopt(ExpReturn, ExpCovariance, [], TargetReturn);

disp(' Efficient    Target');
disp([PortReturn, TargetReturn]);
> In portopt at 85 
Error using portopt (line 297)
One or more requested returns are greater than the maximum achievable return of 0.093400.

To migrate a portopt syntax for target returns outside of the range of efficient portfolio returns to a Portfolio object:

ExpReturn = [ 0.0054; 0.0531; 0.0779; 0.0934; 0.0130 ];

ExpCovariance = [ 0.0569,  0.0092,  0.0039,  0.0070,  0.0022;
	0.0092,  0.0380,  0.0035,  0.0197,  0.0028;
	0.0039,  0.0035,  0.0997,  0.0100,  0.0070;
	0.0070,  0.0197,  0.0100,  0.0461,  0.0050;
	0.0022,  0.0028,  0.0070,  0.0050,  0.0573 ];

NumPorts = 10;

TargetReturn = [ 0.05; 0.06; 0.07; 0.08; 0.09; 0.10 ];

p = Portfolio;
p = setAssetMoments(p, ExpReturn, ExpCovariance);
p = setDefaultConstraints(p);

PortWts = estimateFrontierByReturn(p, TargetReturn);
[PortRisk, PortReturn] = estimatePortMoments(p, PortWts);

disp(' Efficient    Target');
disp([PortReturn, TargetReturn]);
Warning: One or more target return values are outside the feasible range [
0.0427391, 0.0934 ].
	Will return portfolios associated with endpoints of the range for these
    values. 
> In Portfolio/estimateFrontierByReturn (line 106) 
 Efficient    Target
    0.0500    0.0500
    0.0600    0.0600
    0.0700    0.0700
    0.0800    0.0800
    0.0900    0.0900
    0.0934    0.1000

Migrate portopt Using portcons Output for ConSet

This example shows how to migrate portopt when the ConSet output from portcons is used with portopt.

portopt accepts as input the outputs from portcons, pcalims, pcglims, and pcgcomp. This example focuses on portcons. portcons sets up linear constraints for portopt in the form A*Port <= b. In a matrix ConSet = [ A, b ] and break into separate A and b arrays with A = ConSet(:,1:end-1); and b = ConSet(:,end);. In addition, to illustrate default problem with additional group constraints, consider three groups. Assets 2, 3, and 4 can constitute up to 80% of portfolio, Assets 1 and 2 can constitute up to 70% of portfolio, and Assets 3, 4, and 5 can constitute up to 90% of portfolio.

ExpReturn = [ 0.0054; 0.0531; 0.0779; 0.0934; 0.0130 ];

ExpCovariance = [ 0.0569,  0.0092,  0.0039,  0.0070,  0.0022;
	0.0092,  0.0380,  0.0035,  0.0197,  0.0028;
	0.0039,  0.0035,  0.0997,  0.0100,  0.0070;
	0.0070,  0.0197,  0.0100,  0.0461,  0.0050;
	0.0022,  0.0028,  0.0070,  0.0050,  0.0573 ];

NumPorts = 10;

Groups = [ 0 1 1 1 0; 1 1 0 0 0; 0 0 1 1 1 ];
GroupBounds = [ 0, 0.8; 0, 0.7; 0, 0.9 ];

LowerGroup = GroupBounds(:,1);
UpperGroup = GroupBounds(:,2);

ConSet = portcons('default', 5, 'grouplims', Groups, LowerGroup, UpperGroup);

[PortRisk, PortReturn, PortWts] = portopt(ExpReturn, ExpCovariance, NumPorts, [], ConSet);

disp([PortRisk, PortReturn]);
Error using portopt (line 83)
In the current and future releases, portopt will no longer accept ConSet or varargin arguments.
'It will only solve the portfolio problem for long-only fully-invested portfolios.    
To solve more general problems, use the Portfolio object.
See the release notes for details, including examples to make the conversion.

To migrate portopt to a Portfolio object when the ConSet output from portcons is used with portopt:

ExpReturn = [ 0.0054; 0.0531; 0.0779; 0.0934; 0.0130 ];

ExpCovariance = [ 0.0569,  0.0092,  0.0039,  0.0070,  0.0022;
	0.0092,  0.0380,  0.0035,  0.0197,  0.0028;
	0.0039,  0.0035,  0.0997,  0.0100,  0.0070;
	0.0070,  0.0197,  0.0100,  0.0461,  0.0050;
	0.0022,  0.0028,  0.0070,  0.0050,  0.0573 ];

NumPorts = 10;

Groups = [ 0 1 1 1 0; 1 1 0 0 0; 0 0 1 1 1 ];
GroupBounds = [ 0, 0.8; 0, 0.7; 0, 0.9 ];

LowerGroup = GroupBounds(:,1);
UpperGroup = GroupBounds(:,2);

ConSet = portcons('default', 5, 'grouplims', Groups, LowerGroup, UpperGroup);

A = ConSet(:,1:end-1);
b = ConSet(:,end);

p = Portfolio;
p = setAssetMoments(p, ExpReturn, ExpCovariance);
p = setInequality(p, A, b);					% implement group constraints here

PortWts = estimateFrontier(p, NumPorts);
[PortRisk, PortReturn] = estimatePortMoments(p, PortWts);

disp([PortRisk, PortReturn]);
0.1288    0.0427
0.1292    0.0465
0.1306    0.0503
0.1328    0.0540
0.1358    0.0578
0.1395    0.0615
0.1440    0.0653
0.1504    0.0690
0.1590    0.0728
0.1806    0.0766

The constraints are entered directly into the Portfolio object with the setInequality or addInequality functions.

Integrate Output from portcons, pcalims, pcglims, and pcgcomp with a Portfolio Object

This example shows how to integrate output from pcalims, pcalims, pcglims, or pcgcomp with a Portfolio object implementation.

portcons, pcalims, pcglims, and pcgcomp setup linear constraints for portopt in the form A*Port <= b. Although some functions permit two outputs, assume that the output is a single matrix ConSet. Break into separate A and b arrays with:

  • A = ConSet(:,1:end-1);

  • b = ConSet(:,end);

In addition, to illustrate default problem with additional group constraints, consider three groups:

  • Assets 2, 3, and 4 can constitute up to 80% of portfolio.

  • Assets 1 and 2 can constitute up to 70% of portfolio.

  • Assets 3, 4, and 5 can constitute up to 90% of portfolio.

Groups = [ 0 1 1 1 0; 1 1 0 0 0; 0 0 1 1 1 ];
GroupBounds = [ 0, 0.8; 0, 0.7; 0, 0.9 ];

To integrate the ConSet output of portcons with a Portfolio object implementation:

ExpReturn = [ 0.0054; 0.0531; 0.0779; 0.0934; 0.0130 ];

ExpCovariance = [ 0.0569,  0.0092,  0.0039,  0.0070,  0.0022;
	0.0092,  0.0380,  0.0035,  0.0197,  0.0028;
	0.0039,  0.0035,  0.0997,  0.0100,  0.0070;
	0.0070,  0.0197,  0.0100,  0.0461,  0.0050;
	0.0022,  0.0028,  0.0070,  0.0050,  0.0573 ];

NumPorts = 10;

Groups = [ 0 1 1 1 0; 1 1 0 0 0; 0 0 1 1 1 ];
GroupBounds = [ 0, 0.8; 0, 0.7; 0, 0.9 ];

LowerGroup = GroupBounds(:,1);
UpperGroup = GroupBounds(:,2);

ConSet = portcons('default', 5, 'grouplims', Groups, LowerGroup, UpperGroup);

A = ConSet(:,1:end-1);
b = ConSet(:,end);

p = Portfolio;
p = setAssetMoments(p, ExpReturn, ExpCovariance);
p = setDefaultConstraints(p);				% implement default constraints here
p = setInequality(p, A, b);					% implement group constraints here

PortWts = estimateFrontier(p, NumPorts);
[PortRisk, PortReturn] = estimatePortMoments(p, PortWts);

disp([PortRisk, PortReturn]);
    0.1288    0.0427
    0.1292    0.0465
    0.1306    0.0503
    0.1328    0.0540
    0.1358    0.0578
    0.1395    0.0615
    0.1440    0.0653
    0.1504    0.0690
    0.1590    0.0728
    0.1806    0.0766

To integrate the output of pcalims and pcglims with a Portfolio object implementation:

ExpReturn = [ 0.0054; 0.0531; 0.0779; 0.0934; 0.0130 ];

ExpCovariance = [ 0.0569,  0.0092,  0.0039,  0.0070,  0.0022;
	0.0092,  0.0380,  0.0035,  0.0197,  0.0028;
	0.0039,  0.0035,  0.0997,  0.0100,  0.0070;
	0.0070,  0.0197,  0.0100,  0.0461,  0.0050;
	0.0022,  0.0028,  0.0070,  0.0050,  0.0573 ];

NumPorts = 10;

Groups = [ 0 1 1 1 0; 1 1 0 0 0; 0 0 1 1 1 ];
GroupBounds = [ 0, 0.8; 0, 0.7; 0, 0.9 ];

LowerGroup = GroupBounds(:,1);
UpperGroup = GroupBounds(:,2);

AssetMin = [ 0; 0; 0; 0; 0 ];
AssetMax = [ 0.8; 0.8; 0.8; 0.8; 0.8 ];

[Aa, ba] = pcalims(AssetMin, AssetMax);
[Ag, bg] = pcglims(Groups, LowerGroup, UpperGroup);

p = Portfolio;
p = setAssetMoments(p, ExpReturn, ExpCovariance);
p = setDefaultConstraints(p);				% implement default constraints first
p = addInequality(p, Aa, ba);				% implement bound constraints here
p = addInequality(p, Ag, bg);				% implement group constraints here

PortWts = estimateFrontier(p, NumPorts);
[PortRisk, PortReturn] = estimatePortMoments(p, PortWts);

disp([PortRisk, PortReturn]);
0.1288    0.0427
0.1292    0.0465
0.1306    0.0503
0.1328    0.0540
0.1358    0.0578
0.1395    0.0615
0.1440    0.0653
0.1504    0.0690
0.1590    0.0728
0.1806    0.0766

See Also

| | | | | | | | | | | |

Related Topics