10.12.2019 Views

Python for Finance

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9

var=w1**2*var1 +w2**2*var2+2*w1*w2*rho*sigma1*sigma2

if(var<portVar):

portVar=var

finalW1=w1

#print(vol)

print("min vol=",sp.sqrt(portVar), "w1=",finalW1) ('min vol=',

('min vol=', ('min vol=', 9.3132257461547852e-10, 'w1=',

0.80000000000000004)

First, the result confirms our previous result with w1=0.8 and w2=0.2. In the

program, we have 1000 pairs of w1 and w2. A small value, called tiny, is

1/1000=0.001. The first pair of two weights is 0.1% and 99.9%. A very big number

is assigned to our solution variable, that is, as an initial value. In this program,

portVar=10. Other big numbers would work perfectly, such as 100. Here is the logic:

based on the first pair of w1 and w2, we estimate the portfolio variance. If this new

portfolio variance is less than portVar, we replace portVar with this new value

and record w1 as well. If the new portfolio variance is bigger than portVar, we do

nothing. Repeat the same procedure until we finish the loop. Here is an analogy.

Assume that we want to find the tallest person among 1,000 persons. Assume that

we have a variable call tallestPerson and its initial vale is 0.1 inch. Since every

person will be taller than this value, the first person's height will replace this value. If

the next person's height is higher than this variable, we replace it. Otherwise, we go

to the next one. The procedure is repeated until the last person. In terms of efficiency,

one small trick is to estimate var1 and var2 just once.

In finance, it is a convention to use both variance and standard deviation to represent

risk, since they describe uncertainty. Usually, we use standard deviation of returns

to represent the volatility. It is a good idea to look at the impact of correlation on

the efficient frontier. First, let's learn how to generate a set of correlated random

numbers. There are two steps involved:

1. Generate two random time series, x1 and x2, with a zero-correlation.

2. Apply the following formula:

[ 293 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!