21
Financial Optimization using MATLAB’s Financial Toolbox Sonal Varshney, Arnab Sarkar Advantages of using Financial Toolbox The Financial toolbox includes a set of portfolio construction and optimization functions designed to build an optimal portfolio that optimize risk adjusted return. MATLAB uses matrices for calculations. Storing of data is done in matrices and so it becomes easy and efficient to work in MATLAB. Writing MATLAB programs are easy and very much similar to C. There is a C++ compiler built in which can run already existing applications and programs in MATLAB. MATLAB enables user to import data from external files like spreadsheets. It is possible to download data from Bloomberg also. The downloading of data can be done in the form of matrices and then number crunching computation can be performed on them. It is possible to integrate CPLEX (a more efficient optimization package) to integrate with MATLAB.

Optimization in Finance

Embed Size (px)

DESCRIPTION

optimization

Citation preview

Page 1: Optimization in Finance

Financial Optimization using MATLAB’s Financial Toolbox

Sonal Varshney, Arnab Sarkar

Advantages of using Financial Toolbox

The Financial toolbox includes a set of portfolio construction and optimization

functions designed to build an optimal portfolio that optimize risk adjusted return.

MATLAB uses matrices for calculations. Storing of data is done in matrices and

so it becomes easy and efficient to work in MATLAB.

Writing MATLAB programs are easy and very much similar to C.

There is a C++ compiler built in which can run already existing applications and

programs in MATLAB.

MATLAB enables user to import data from external files like spreadsheets. It is

possible to download data from Bloomberg also. The downloading of data can be

done in the form of matrices and then number crunching computation can be

performed on them.

It is possible to integrate CPLEX (a more efficient optimization package) to

integrate with MATLAB.

All these features enable a financial analyst and portfolio manager to work in MATLAB.

There are other in built functions in MATLAB for calculating yield of a bond, options

and other valuations models which can make overall life of a financial analyst simpler. A

financial analyst can perform a time-series analysis on a stock, he can check on the

pricing of options and other derivatives. These features provide a common working

platform for the financial analyst.

MATLAB provides lot of inbuilt functions for Portfolio Analysis and Optimization. A

Portfolio Managers faces a lot of constraints while constructing the portfolio, to meet the

objectives of the variety of investors. Regulations in the industry, different risk appetite

of the investors etc. result in constraints that mathematically take the form or linear

Page 2: Optimization in Finance

inequalities which can then be used to realize an optimal portfolio. Portfolio’s that

maximize the risk for a given risk, or minimize the risk for a given return are called

optimal portfolios.

SOME INFO ABOUT MATLAB

All of the toolbox functions are MATLAB M-files, made up of MATLAB statements that

implement specialized optimization algorithms. You can view the MATLAB code for

these functions using the statement

type function_name

and the whole code can is displayed in the command window.

If you need more information about a particular function type

help function_name

and all details about the function is displayed on the Command Window itself. MATLAB

help is pretty detailed and explanatory. You can go into the help section and do a search

to get detailed information about the command.

MATLAB interface:

Page 3: Optimization in Finance

A typical screenshot of the MATLAB window looks like this.

It is subdivided into three sub spaces.

Command Window – the window in which you work. It display the commands that are

typed and the output of those commands.

Command History – keeps a track of the history of the commands.

Present Working Directory or Workspace – Workspace gives the matrices that are

presently existing and the dimension of the matrices. The contents of these can be

viewed by double clicking on it.

Once can change the interface by going to the windows section and select which one is

required. If a function is preformed that makes a graph then another window with the

graph comes up.

It is possible to make M-files and then directly run them. There is an editor for making

and debugging the M-files. M-file editor can be opened by clicking “new” under the file

tab and then select M-file.

Page 4: Optimization in Finance

Portfolio Construction, Optimization and Analysing inbuilt Functions in MATLAB

Before getting into actual computation of portfolio optimization it will be good to have a

look at what inbuilt functions MATLAB has.

A word of caution: Make sure that the matrix and vectors that are fed as input to

the functions are of compatible sizes. (Mean that when you pass vectors to a function the

follow matrix algebra. – e.g Matrix A with dimension n*1 can be added to Matrix B with

the same dimension.)

Efficient Frontier Computation: Optimal portfolios define a line in the risk/return

plane called efficient frontier. Anything below it is not efficient and anything

above it is not possible with the given set of constraints.

(MGT 6078- Untitled - qcf-introclass-fall04.pdf (Pg-39)

https://banner.webct.gatech.edu/SCRIPT/83562200408/scripts/serve_home

frontcon:

It computes portfolio along the efficient frontier for a given group of

assets. Maximum and minimum bounds on the weights for each asset can

be specified by passing arguments when the function is called.

syntax:

[PortRisk, PortReturn, PortWts] = frontcon(ExpReturn,...

ExpCovariance, NumPorts, PortReturn, AssetBounds, Groups,

GroupBounds)

The input arguments are in the form of matrix on the Expected Return on

each asset, the variance-covariance matrix, number of portfolios one wants

to analyze along the efficient frontier. If a target expected return is

expected it can be specified in the PortReturn vector. If PortReturn is not

entered or [], NumPorts gives returns equally spaced between the

minimum and maximum values used.

Page 5: Optimization in Finance

The weight on individual assets can also be given as an input argument

specifying the maximum and minimum weights to be allocated to the

individual security. A portfolio Managers faces these kinds of constraints

day and night. e.g there are only specific securities which he can short

sell, or he might have a maximum bound on how much an individual

security can be as a part of the whole portfolio.

One also faces constraints on the portfolio based not only on the individual

security but also on a group of securities. e.g technology sector cannot be

more than 40% of the whole portfolio. or 20% of the portfolio consist of

bonds. Such kind of constraints can also be passed into frontcon.

Identification of the groups is passed in the form of a matrix and then

bounds on the group can be put. It will become easier once we see with an

example on how to input groups and group bonds.

If output argument are not specified (PortRisk, PortReturn, PortWts) then

frontcon outputs a graph of the efficient frontier.

Example to use frontcon to get the efficient frontier

e.g. as in the notes of Prof. Khorana - pg38.

Expected returns on assets A and B and the risk (Standard Deviation) associated

with them are given as

Ra = 0.12

Rb =0.16

SIGMAa = 0.1

SIGMAb = 0.2

Correlation coefficient: rho= 1

Covariance can be calculated as:

cov(x,y)=cov(y,x)=SIGMAa*SIGMAb*rho

Page 6: Optimization in Finance

A covariance matrix can be build using the above formula.

Matlab Code:

>> exp_return = [0.12 0.16];

>> exp_covariance = [0.01 0.02; 0.02 0.04];

>> frontcon(exp_return,exp_covariance);

Fig.1

When rho=0

>> exp_covariance = [0.01 0; 0 0.04];

>>frontcon(exp_return,exp_covariance);

The graph of the output is shown in Fig2.

When rho=-1

>> exp_covariance = [0.01 -.02;-.02 0.04];

Page 7: Optimization in Finance

>>frontcon(exp_return,exp_covariance);

The graph of the output is shown in Fig3.

Fig.2

Page 8: Optimization in Finance

Fig.3

Calling frontcon( ) without specifying any output arguments, a graph as shown in fig1 is

popped which represents the efficient frontier curve.

If the number of efficient portfolios generated along the efficient frontier is not specified

then it outputs 10 values as default. This will get clearer when output arguments are

specified.

Calling frontcon( ) while specifying the output arguments returns value of the

corresponding vectors. e.g in the pervious example with rho=1

>> [port_risk, port_return, port_wts] = frontcon(exp_return,exp_covariance)

gives the result as:

Page 9: Optimization in Finance

port_risk =

0.1000

0.1111

0.1222

0.1333

0.1444

0.1556

0.1667

0.1778

0.1889

0.2000

port_return =

0.1200

0.1244

0.1289

0.1333

0.1378

0.1422

0.1467

0.1511

0.1556

0.1600

port_wts =

1.0000 0

0.8889 0.1111

0.7778 0.2222

0.6667 0.3333

0.5556 0.4444

0.4444 0.5556

0.3333 0.6667

0.2222 0.7778

0.1111 0.8889

0 1.0000

Note: Each output argument portfolio risk, portfolio return and portfolio weights are

identified as corresponding rows in the vectors and matrix.

For portfolio 2 the vectors can be read as: To get an expected return of 13.54% the risk

that is computed along the efficient frontier is 0.2774 and the weights that have to be

assigned to securities A and B are 0.6154 and 0.3846 respectively.

If the number of efficient portfolios is specified say (numb_port) = 15

>> [port_risk, port_return, port_wts] = frontcon(exp_return,exp_covariance,num_port)

Then it outputs 15 row matrixes just as it showed for 10 portfolios.

Portfolio Optimization:

portopt:

portopt also construct portfolio’s on the efficient frontier. It is quite similar

frontcon but in portopt instead of specifying bunch of arguments like

group and group bounds it is easier to specify constraints in the form of a

matrix and input it into the optimization problem.

Page 10: Optimization in Finance

syntax:

[PortRisk, PortReturn, PortWts] = portopt(ExpReturn, ExpCovariance,

NumPorts, PortReturn, ConSet)

The difference between the input arguments of frontcon and portopt is that

of conset. conset is basically a matrix of constraints set. Constraints in a

portfolio optimization problem grow very fast. It may be possible that in

frontcon call the constraints cannot be specified.

ConSet can be specified by using portcons function (called portfolio

constraints-explained later) and there are some other ways to get around it.

It is generally in the form of Ax<=b form.

It however returns the same output arguments as the frontcon. If portopt is

invoked without output arguments it outputs a plot of efficient frontier

(similar to frontcon).

Allocation of assets in a Portfolio:

portalloc:

portalloc is an inbuilt function which returns optimal capital allocation to

the individual securities along the efficient frontier. The difference of

portalloc with frontcon / portopt is that portalloc divides the whole

portfolio into riskfree investment and risky investment depending upon the

risk appetite of the investor. And then it calculates weights to the different

securities in the portfolio along the efficient frontier. This can be thought

as a capital allocation decision between the risky and the non risky asset.

Where the capital allocation line intersects the efficient frontier lie the

asset allocation decision which assigns the weights to different securities

(Explained later with a figure).

It outputs data for the riskfree, riskless and the overall portfolio

characteristics.

syntax:

[RiskyRisk, RiskyReturn, RiskyWts, RiskyFraction, OverallRisk,

Page 11: Optimization in Finance

OverallReturn] = portalloc(PortRisk, PortReturn, PortWts,

RisklessRate, BorrowRate, RiskAversion)

portalloc takes input arguments as PortRisk, PortReturn, PortWts which

are outputs of the frontcon or portopt. The other input arguments are the

risk free rate, borrowing rate and the risk aversion of the investor. If

borrowing is not desirable option it is set to NaN [not-a-number]. If not

specified it is taken as default.

The output arguments are set of values associated with the optimal

portfolio. It gives the weights of individual securities, the expected return,

the risk of the portfolio, and how much to borrow.

Note: If borrowing is not done & RiskyFraction=1 then

RiskyRisk = OverallRisk &

RiskyReturn = OverallReturn &

If borrowing is done than RiskyFraction is more than 1.

e.g RiskyFraction = 1.5 implies that 50% of the money can be borrowed.

RiskyFraction = 0.8 implies that 80 of the capital allocation has been done

in the Risky part of the portfolio and 20% of capital allocation has been

done in the risk free securities at the risk free rate.

OverallRisk is the weighted average of the RiskyRisk and the Riskfree

allocation and so is the OverallReturn.

Risk free rate is specified in decimal (15% as 0.15)

Risk Aversion: It’s the coefficient of investor’s degree of risk aversion. It

basically establishes the increment in return that a particular investor will

require in order to make an increment in risk worthwhile. Typical values

of risk aversion coefficient range between 2.0 and 4.0, the higher value

representing lesser tolerance to risk.

Default: 3

Page 12: Optimization in Finance

The level of risk aversion is characterized by defining investor’s

indifference curve. The equation used to present risk aversion in the

Financial Toolbox is

U = E(r) – 0.005*A*sig^2

U: Utility Value E(r): Expected Return

sig:standard dev. A: index of investor’s aversion

A typical figure for portfolio optimization is shown below.

U (A) and U (B) are utility curves for investors A and B. We can see that

investor A is more risk averse than investor B. When we execute the portopt

then the point C gives the allocation of the capital into the risky and the risk

free assets for investor A and C’ for investor B. Beyond point P on the

capital allocation line the risky fraction will be greater than 1. (for more

details see Dr. Khorana Notes).

Specifying large constraints on the portfolio:

portcons:

Page 13: Optimization in Finance

portcons is an auxiliary function used to create the matrix of constraints.

One can specify all the data constraints at once, while specific portfolio

constraint functions only allow constraints to grow incrementally.

Computing the Risk and Return based on the weights of the assets.

portstats:

computes a portfolio’s risk and return

syntax:

[PortRisk, PortReturn] = portstats(ExpReturn, ExpCovariance,

PortWts)

Example using Portfolio Optimization function (portopt) by inputting some linear

constraints:

The auxiliary function portcons can be used to create the matrix of constraints with each

row representing an inequality in the form A*wts<=b, where A is a matrix and b is a

vector and wts is row vector of asset allocations.

A fund manager is bounded in his exposure to different markets around the world and is

given in the table.

Group Minimum Exposure Maximum Exposure

North America 0.3 0.75

Europe 0.10 0.55

Latin America 0.20 0.50

Asia 0.50 0.50

Table.1

Group Fund 1 Fund 2 Fund 3

North America X X

Europe X

Latin America X

Asia X X

Table.2

Page 14: Optimization in Finance

weights representing the exposure to each fund in a portfolio is

wts = [w1 w2 w3]

Using Table 1 and Table 2 we can formulate the following linear constraints:

1. w1 + w2 >= 0.30

2. w1 + w2 <=0.75

3. w3 >= 0.10

4. w3 <= 0.55

5. w1 >= 0.20

6. w1 <=.0.50

7. w2 + w3 =0.50

We have to input the following constraints in the form A*wts<=b;

The above constraints take the form

1. -w1 - w2 <= -0.30

2. w1 + w2 <= 0.75

3. -w3 <= -0.10

4. w3 <= 0.55

5. -w1 <= -0.20

6. w1 <= 0.50

7. -w2 - w3 <= -0.50

8. w2 + w3 <= 0.50

Converting them into matrix format we get:

A = [ -1 -1 0; 1 1 0; 0 0 -1; 0 0 1;-1 0 0; 1 0 0; 0 -1 -1;

Page 15: Optimization in Finance

0 1 1 ]

b = [ -0.30; 0.75;-0.10; 0.55;-0.20; 0.50;-0.50; 0.50 ]

Building the constraint matrix ConSet by concatenating the matrix A to the vector b.ConSet = [A,b];

When we run in MATLAB>> ExpReturn = [.1 .2 .15]>> ExpCovariance = [.005 -.010 .004;-0.01 .04 -.002; .004 -.002 .023]>> [PortRisk, PortReturn, PortWts] = portopt (ExpReturn, ExpCovariance,[],[],ConSet)

output:

port_risk =

0.0569

0.0569

0.0569

0.0570

0.0570

0.0571

0.0572

0.0573

0.0574

0.0586

port_return =

0.1169

0.1192

0.1215

0.1238

0.1261

0.1283

0.1306

0.1329

0.1352

0.1375

port_wts =

0.2950 0.2482 0.2518

0.3156 0.2528 0.2472

0.3361 0.2574 0.2426

0.3567 0.2620 0.2380

0.3773 0.2666 0.2334

0.3979 0.2712 0.2288

0.4185 0.2757 0.2243

0.4391 0.2803 0.2197

0.4597 0.2849 0.2151

0.5000 0.2500 0.2500

Note: The overall portfolio weights w1+w2+w3=1 was not a constraint.

If you add the constraint then we add two more constraints

w1+w2 + w3 <= 1 &

-w1-w2 - w3 <= -1 &

Doing the corresponding changes in A and b gives the following output

Page 16: Optimization in Finance

port_risk =

0.0586

port_return =

0.1375

port_wts =

0.5000 0.2500 0.2500