Markowitz Whitepaper

March 25, 2018 | Author: Wen Bin | Category: Variance, Tangent, Covariance Matrix, Modern Portfolio Theory, Quadratic Equation


Comments



Description

ILLUSTRATING THE LINK BETWEEN MODERN PORTFOLIOTHEORY AND KELLY TOM STARKE 0 there is an optimum fraction you can bet to maximise your profits. which will hopefully help you to get a sense of how to use modelling and simulation to improve your understanding of the theoretical concepts. The Kelly formula works on the assumption that. I hope you enjoy it and get a little more enlightened in the process. so thanks Ernie for providing us with so much insight. So let‘s start with importing a few modules. He has a sizeable section dedicated to portfolio management and describes how to use the Kelly formula to apply the optimum leverage in order to maximise returns. In this post I will walk you through his work and provide you with code examples that illustrate the concepts. such a strategy can also suffer from enormous drawdowns and anyone who experienced the darker side of trading psychology knows what that means.blogspot. In this post I used random data rather than actual stock data.pdf 3 http://epchan. Don‘t forget that the skill of an algo-trader is to put mathematical models into code and this example is great practise.com. before you read on. Markowitz is generally regarded as a ’serious’ academic theory whereas Thorpe was just a mathematically-minded Black-Jack player who also made a couple of hundred millions on the stock market.harvard. Even though we may maximise our profits with straight-Kelly. both theories work on the (mostly inaccurate) assumption that returns are normally distributed. Recently. However. As a final note I would like to point out that from my experience there are a lot of people with IT experience reading these articles who do not always have the in-depth mathematical background. If you are active in the algorithmic trading field you will have come across Ernie Chan’s books1.com.eecs. Ernie has posted a very interesting piece of work on his blog3 which unifies the portfolio management approaches of Kelly and Markowitz and his modern portfolio theory. which we need later and produce a series of normally distributed returns. If you had a look at his post you may have realised that there is a lot of algebra happening and some of you may want to know how they can use this in practical terms.html 2 . This post is based on his work. In reality we often have fatter tails.edu/cs286r/courses/fall12/papers/Thorpe_ KellyCriterion2007. with respect to your bankroll. CVXOPT is a convex solver which you can easily download with sudo pip install cvxopt.blogspot.ILLUSTRATING THE LINK BETWEEN MODERN PORTFOLIO THEORY AND KELLY 1 1. which means that we should at least reduce the amount of leverage that we use. Introduction This article was hugely inspired by Ernie Chan’s work.au http://www. This approach was first applied and popularised by Thorpe2.au/2014/08/kelly-vs-markowitz-portfolio. please have a look at his post now. 1 http://epchan. For that reason I try not to jump too many steps to increase the educational value. You can also see that there is a filter that only allows to plot portfolios with a standard deviation of < 1. In this example I introduce an upward trending bias by adding 0. append ( dot ( m a t r i x ( pbar ) . 01 ) pbar . append ( s q r t ( dot ( m a t r i x (w) . x l a b e l . append ( random . r a n d w e i g h t ) p l o t ( s t d s . In the code you will notice the calculation of the return with: (1) R = pT w . These return series can be used to create a wide range of portfolios. def rand weight (n) : k = random .2 for better illustration. s o l v e r s im por t qp im po rt numpy a s np from s c i p y i mpo rt o p t i m i z e n = 5 # number o f a s s e t s i n t h e p o r t f o l i o pbar = [ ] . ’o ’ ) y l a b e l ( ’ mean ’ ) xlabel ( ’ std ’ ) Listing 2. Producing normally distributed return series.2 TOM STARKE from numpy i mp ort ∗ from p y l a b im por t p l o t . 2 : means . show . means . m a t r i x (w) ) ) s t d s . where we have the lowest variance for a given expected return as shown in Figure 1. T∗w) < 1 . s t d s means . f ) : means = [ ] .101 . Upon plotting those you will observe that they form a characteristic parabolic shape called the ‘Markowitz bullet‘ with the boundaries being called the ‘efficient frontier‘. n . m a t r i x ( cov ( a r r a y ( r e t v e c ) ) ) ∗ m a t r i x (w) ) ) ) r e t u r n means . b l a s i mpo rt dot from cv xo pt . which makes things easier for illustration but you can set it to zero and see what happens. append ( mean ( r e t v e c [ − 1 ] ) ) Listing 1. r e t v e c = [ ] f o r i in range (n) : r e t v e c . s t d s = c a l c m e a n r e t ( r e t v e c .01. Producing normally distributed return series. s t d s = [ ] f o r i in range (3000) : w = f (n) i f s t d ( a r r a y ( r e t v e c ) . r a n d i n t ( −101 . n ) r e t u r n k/ f l o a t ( sum ( k ) ) def calc mean ret ( ret vec . which all have different returns and risks (standard deviation). We can produce a wide range of random weight vectors and plot those portfolios. y l a b e l from cv xo pt imp ort m a t r i x from cv xo pt . randn ( 1 0 0 0 ) + 0. n . By using ordinary std() we effectively only regard the diagonal and miss the rest. In the covariance matrix. pT is the transpose of the vector for the mean returns for each time series and w is the weight vector of the portfolio. p is a Nx1 column vector. This is done by minimising (3) wT Cw . This is equivalent to the dot product used in the code. Simulation of portfolios with varying weight. we calculate the standard deviation with √ (2) σ = wT Cw where C is the covariance matrix of the returns which is a NxN matrix.T*w) we would get a slightly different ’bullet’. the values of the diagonal represent the simple variances of each asset while the off-diagonals are the variances between the assets. plot of the efficient frontier and the Kelly portfolios with varying θ. Figure 1. Keep in mind that Python has a reversed definition of rows and columns and the accurate Numpy version of the previous equation would be R = matrix(w)*matrix(p). Once we have a good representation of our portfolios as the blue dots in Figure 1 show we can calculate the efficient frontier Markowitz-style.T Next. This is because the simple standard deviation calculation would not take covariances into account. Please note that if we simply calculated the simple standard deviation with the appropriate weighting using std(array(ret_vec). A small but significant difference.ILLUSTRATING THE LINK BETWEEN MODERN PORTFOLIO THEORY AND KELLY 3 where R is the expected return. so pT turns into a 1xN row vector which can be multiplied with the Nx1 weight (column) vector w to give a scalar result. which can safely be extrapolated for higher values. They are simply needed to set up the problem. G. Conveniently. A. n ) ) G [ : : n+1] = −1.html . n ) ) b = matrix ( 1 . The mus vector produces a series of expected return values µ in a non-linear and more appropriate way. b ) [ ’ x ’ ] f o r mu i n mus ] ## CALCULATING RISKS AND RETURNS FOR FRONTIER r e t s = [ dot ( pbar . For more information please have a look at the cvxopt example.minimize but we have to define quite a complex problem with bounds. You can see the result in Figure 1 as the parabolic fitting curve to the Markowitz bullet.org/examples/book/portfolio. So far. We will see later that we don‘t need to calculate a lot of these as they perfectly fit a parabola. the cvxopt package. 0 ∗ t /N−1. ## CONVERSION TO MATRIX k = array ( r e t v e c ) S = m a t r i x ( cov ( k ) ) pbar = m a t r i x ( pbar ) ## CONDITIONING OPTIMIZER G = matrix ( 0 . You will notice that there are some conditioning expressions in the code. a convex solver. 0 ) ## CALCULATING EXPECTED RETURNS N = 100 mus = [ 1 0 ∗ ∗ ( 5 . r e t s .0 h = matrix ( 0 . ’ y−o ’ ) Listing 3. ( 1 . there is no magic here. 0 . h . (n . This can be done with scipy. does that all for us.4 TOM STARKE for w on the expected portfolio return RT w whilst keeping the sum of all the weights equal to 1: X (4) wi = 1 i Here we parametrically run through RT w = µ and find the minimum variance for different µ‘s. S∗x ) ) f o r x i n p o r t f o l i o s ] p l o t ( r i s . 0 . (n . But now comes the interesting part: Ernie Chan shows in his article after some interesting algebra that the Markowitz optimal portfolio is proportional to the Kelly optimal leverage portfolio by a factor which 4http://cvxopt. I used one of their examples4 with some modifications as shown below.0) f o r t i n r a n g e (N) ] ## CALCULATING THE WEIGHTS FOR EFFICIENT FRONTIER p o r t f o l i o s = [ qp (mu∗S . constraints and a Lagrange multiplier. x ) f o r x i n p o r t f o l i o s ] r i s = [ s q r t ( dot ( x . −pbar .optimise. 0 . 1 ) ) A = matrix ( 1 . Calculating the efficient frontier. When I read this article I was intrigued and wanted to see this for myself. 0 5 . x ) ## FITTING THE KELLY WEIGHTS m2 = p o l y f i t ( r e s .max( r e t s ) ∗ 2 . The black line with the negative slope can also be a Kelly-tangent since the tangent to the efficient frontier has actually two solutions as we will see below. dotted lines in Figure 1 and it can be seen that the upper line is exactly the tangent of our efficient frontier. In order to work that out we first have to extend our efficient frontier a little by doing a second-order polynomial fit as shown in the code example below. 1 ) . Calculation of optimal Kelly portfolio. a r r a y ( r k s ) ∗ −1 . m a r k e r s i z e =3) p l o t ( r e s . Once we know the optimal Kelly portfolio. r k s . we can readily find the weights for Markowitz with Pwwi . An example of these fits is shown in Figure 2. res = [ ] . However. 2 0 . ’ ko ’ . r i s . 2 ) x = l i n s p a c e ( min ( r e t s ) . append ( dot ( pbar . This is very simply done by the equation: w = C −1 R (5) which in pythonic form look like this: ww = np . Since we know that all the points on the efficient frontier have a leverage of 1 we can see that Ernie Chan is indeed correct with his calculations. So let‘s start with calculating the optimal Kelly portfolio. i n v ( np . In my little example below I actually plotted the lines for both θ and −θ.ILLUSTRATING THE LINK BETWEEN MODERN PORTFOLIO THEORY AND KELLY 5 I will call θ. Calculation of the Kelly tangent. r k s . append ( s q r t ( dot ( m a t r i x (w) . m a t r i x ( pbar ) Listing 4. 0 0 0 1 ) : w = ww/ t h e t a r k s . rks = [ ] . which gave me the motivation for this article. which is quite interesting. ’ ko ’ . 0 . m a r k e r s i z e =3) show ( ) Listing 5. ## FITTING THE MARKOWITZ PARABOLA m1=p o l y f i t ( r e t s . For ease of calculation I have reversed the axis here. S∗ m a t r i x (w) ) ) ) p l o t ( r e s . Now all we have to do is to apply a factor θ and see how we go with this. l i n a l g . Bingo! By getting that plot we can see that somewhere along the black Kelly line we will find the optimal Markowitz portfolio. m a t r i x (w) ) ) r e s . At the same time we are also fitting the line for the Kelly portfolios. f o r theta in arange ( 0 . The result of this can be seen as the black. m a t r i x ( S ) ) ∗np . the diligent investigator may wonder how accurate i that Kelly-derived weight vector actually is. 1 0 0 0 ) y = p o l y v a l (m1 . The equation for the efficient frontier can be modelled by a simple polynomial: (6) f (x) = ax2 + bx + c where the slope of the tangent is: (7) f 0 (x) = 2ax + b Since we know that our tangent goes through the origin. The actual tangent to the efficient frontier is shown as solid black line.6 TOM STARKE Listing 6. it‘s equation will be t(x) = xf 0 (x) which gives us (8) t(x) = (2ax + b)x We set this equal to the efficient frontier: (9) (2ax + b) = ax2 + bx + c and after some algebra we can calculate the x-value for the intercept: r c (10) x=± a . Note that the axis of risk and return are reverse to Figure 1 Next. we calculate the tangent to the efficient frontier and the point where tangent and parabola meet. Fitting the efficient frontier. Fitting the efficient frontier and the Kelly-tangent (blue crosses) and plotting the optimum portfolio determined with Kelly (green) and the and using the tangent calculation (black). Figure 2. You can easily calculate this yourself knowing that the derivative of the point where both lines touch is equal to the slope of the tangent. G. m a t r i x ( wt ) ) . m a r k e r s i z e =10) Listing 8. ’ ko ’ . we compare the optimum portfolio point obtained from the real tangent with that from the Kelly-tangent. if you have followed me up to this point. p l o t ( x . x l a b e l . h . s o l v e r s im por t qp im po rt numpy a s np ## NUMBER OF ASSETS n = 4 ## PRODUCING RANDOM WEIGHTS def rand weight (n) : k = random . I hope you got a little bit enlightened and please let me know if you have any questions or suggestions. Calculating the interception point based on the Kellytangent. s q r t ( dot ( m a t r i x ( wt ) . For this we use cvxopt again to calculate the weights at the optimum Kelly point are a bit different but relatively close. Finally. [ y1 ] . However. Plotting tangent and interception point. wt = qp ( x1 ∗S . A. Well done. n ) . When we compare the slopes of the Kelly and the real tangent to the parabola we find that they are slightly different. from numpy imp ort ∗ from p y l a b im por t p l o t . ’ go ’ . ( 2 ∗ ( s q r t (m1 [ 0 ] ∗ m1 [ 2 ] ) )+m1 [ 1 ] ) ∗x . I do not have a satisfying explanation for this right now other then attributing it to rounding errors. particularly when we are dealing with portfolios where most of the expected returns are close to zero. If you change the bias on the returns to negative you will notice that the bullet will also shift towards the negative tangent and you can calculate the solutions for this case. in most cases the curves are close enough to conclude that Ernie Chan‘s derivations are correct.101 . show . But remember. −pbar . r a n d i n t ( −101 . S∗ m a t r i x ( wt ) ) ) . To make it really easy for you I finally put it all together in one big block of code. m a r k e r s i z e =10) Listing 7. b l a s i mpo rt dot from cv xo pt . ’ k ’ ) x1 = s q r t (m1 [ 2 ] / m1 [ 0 ] ) y1 = m1 [ 0 ] ∗ x1 ∗∗2 + m1 [ 1 ] ∗ x1 + m1 [ 2 ] p l o t ( [ x1 ] .ILLUSTRATING THE LINK BETWEEN MODERN PORTFOLIO THEORY AND KELLY 7 and the slope of the tangent at this point: √ (11) m = ±2 ca + b Notice that the for the tangent we obtain 2 solutions and my code is written to obtain the positive one. y l a b e l from cv xo pt imp ort m a t r i x from cv xo pt . b ) [ ’ x ’ ] p l o t ( dot ( pbar . just running the code does not help to understand the depth of the problem described here. 0 . S∗x ) ) f o r x i n p o r t f o l i o s ] p l o t ( r i s . A = matrix ( 1 . S∗ m a t r i x (w) ) ) ) . A. 1 ) ) (1 . s t d s ## RUNNING AND PLOTTING PORTFOLIOS means . i n v ( np . r e t s . m a t r i x (w) ) ) r e s . b ) [ ’ x ’ ] f o r mu i n mus ] ## CALCULATING RISKS AND RETURNS FOR FRONTIER r e t s = [ dot ( pbar .0) f o r t i n r a n g e (N) ] ## CALCULATING THE WEIGHTS FOR EFFICIENT FRONTIER p o r t f o l i o s = [ qp (mu∗S . h .0 h = matrix ( 0 . append ( s q r t ( dot ( m a t r i x (w) . 01 ) pbar . m a t r i x ( S ) ) ∗np . 2 0 . 0 ∗ t /N−1. means . l i n a l g . n . x ) f o r x i n p o r t f o l i o s ] r i s = [ s q r t ( dot ( x . 0 5 . n) ) (n . m a t r i x ( pbar ) rks = [ ] . G [ : : n+1] = −1. ’o ’ ) y l a b e l ( ’ mean ’ ) xlabel ( ’ std ’ ) ## CONVERSION TO MATRIX k = array ( r e t v e c ) S = m a t r i x ( cov ( k ) ) pbar = m a t r i x ( pbar ) ## CONDITIONING G = matrix ( 0 .8 TOM STARKE r e t u r n k/ f l o a t ( sum ( k ) ) ## PRODUCING NORMALLY DISTRIBUTED RETURNS pbar = [ ] . 0 ) OPTIMIZER (n .n) ) ## CALCULATING EXPECTED RETURNS N = 100 mus = [ 1 0 ∗ ∗ ( 5 . r a n d w e i g h t ) p l o t ( s t d s . n . T∗w) < 1 . append ( dot ( pbar . 0 . r e t v e c = [ ] f o r i in range (n) : r e t v e c . append ( s q r t ( dot ( m a t r i x (w) . ’ y−o ’ ) ## PLOTTING THE KELLY PORTFOLIOS ww = np . b = matrix ( 1 . 0 . −pbar . f o r i in arange ( 0 . 0 0 0 1 ) : w = ww/ i r k s . res = [ ] . append ( mean ( r e t v e c [ − 1 ] ) ) ## CREATING PORTFOLIOS FUNCTION def calc mean ret ( ret vec . G. append ( dot ( m a t r i x ( pbar ) . s t d s = [ ] f o r i in range (3000) : w = f (n) i f s t d ( a r r a y ( r e t v e c ) . m a t r i x (w) ) ) s t d s . 0 . f ) : means = [ ] . s t d s = c a l c m e a n r e t ( r e t v e c . m a t r i x ( cov ( a r r a y ( r e t v e c ) ) ) ∗ m a t r i x (w) ) ) ) r e t u r n means . randn ( 1 0 0 0 ) + 0. append ( random . 2 : means . max( r e t s ) ∗ 2 . 1 0 0 0 ) y = p o l y v a l (m1 . r i s .ILLUSTRATING THE LINK BETWEEN MODERN PORTFOLIO THEORY AND KELLY 9 ## PLOTTING THE KELLY PORTFOLIOS FOR DIFF LEVERAGE p l o t ( r e s . r k s . y) ## x1 x2 y1 y2 FINDING OPTIMAL MARKOWITZ POINTS = s q r t (m1 [ 2 ] / m1 [ 0 ] ) = −s q r t (m1 [ 2 ] / m1 [ 0 ] ) = m1 [ 0 ] ∗ x1 ∗∗2 + m1 [ 1 ] ∗ x1 + m1 [ 2 ] = m1 [ 0 ] ∗ x2 ∗∗2 + m1 [ 1 ] ∗ x2 + m1 [ 2 ] ## PLOTTING THE OPTIMUM MARKOWITZ POINT p l o t ( [ x1 ] . x ) ## FITTING THE KELLY WEIGHTS m2 = p o l y f i t ( r e s . s q r t ( dot ( m a t r i x ( wt ) . A. r k s . m a r k e r s i z e =10) show ( ) Listing 9. a r r a y ( r k s ) ∗ −1 . . 1 ) ## m1 I S THE PARABOLIC FIT AND m2 THE LINEAR FIT p r i n t m1 . Putting it all together. r i s . ’ ko ’ . S∗ m a t r i x ( wt ) ) ) . res . m a r k e r s i z e =10) x l a b e l ( ’ mean ’ ) ylabel ( ’ std ’ ) ## FINDING OPTIMUM PORTFOLIO WITH CVXOPT wt = qp ( x1 ∗S . b ) [ ’ x ’ ] p l o t ( dot ( pbar . h . ’ k ’ ) ## PLOTTING THE KELLY PORTFOLIOS FOR DIFFERENT FACTORS p l o t ( rks . −pbar . 2 ) x = l i n s p a c e ( min ( r e t s ) . [ y1 ] . ’ ko ’ . m a t r i x ( wt ) ) . m2 . ’ r−o ’ ) ## PLOTTING THE FITTING FUNCTION FOR THE MARKOWITZ PARABOLA plot (x . ’ ko ’ . m a r k e r s i z e =3) p l o t ( r e s . ( 2 ∗ ( s q r t (m1 [ 0 ] ∗ m1 [ 2 ] ) )+m1 [ 1 ] ) ∗x . ’ x ’ ) ## PLOTTING THE MARKOWITZ PARABOLA p l o t ( r e t s . G. m a r k e r s i z e =3) show ( ) ## FITTING THE MARKOWITZ PARABOLA m1=p o l y f i t ( r e t s . ww ## PLOTTING THE TANGENT TO THE PARABOLA p l o t ( x . ’ go ’ .
Copyright © 2024 DOKUMEN.SITE Inc.