All right. So let's pick up from where we stopped last time, which is we had figured out that we had the expected returns vector, and we had the co-variance vector and sorry, the co-variance matrix, and so now we're going to actually try and draw the efficient frontier. Okay. So, maybe I should do that, and I'm going to say, Efficient Frontier, Part Efficient Frontier Part Two. Okay. So, let's do some standard stuff that we have to do. I'm going to just do, by now should be pretty straightforward. You've seen this probably a whole bunch of times. We're going to pull in the industry returns, we are going to generate an expected returns vector which is the actual returns that we had that we had observed over that five-year period from 1996 to 2000. So now we've got a co-variance matrix between all of these assets. We've got the expected returns. So we should be able to go off and start generating portfolios and plotting them, right?So, let's think about what exactly we're going to do. We're going to generate portfolios, and what is a portfolio? Portfolio corresponds to simply an allocation of capital, right? So if I've got $100, I'm going to put $2 in this, $20 in this, $30 in this etc. The weights are basically what determines how much money I put in each asset, right? So, the first thing we want to do is, we want to see for every possible portfolio which represents a possible allocation of weights, where do we end up on that risk return map? That's really what it is, right? So, the first thing I need to do is I need to generate the X-axis and the Y-axis point for every set of weights. So if you give me a set of weights that says I put $20 in this and $2 in this, and $4 in this, I need to be able to say; "okay. I know where you fit, I know exactly. I know which gawk you are on that efficient frontier, right? Because I can go from that weight to a return, and I can go from that weight to a volatility. Good. So let's do that. So the first thing I need to do is the weight. So let's just write a function. So def portfolio_return, right? So that's the first thing I need the return length and the volatility, and I'm going to take some weights as input, right? You've got to give me an expected return vector, right? So once I have the expected return vector, right? I'm just going to go here. Let me just, weights -> returns, that's really what I'm doing here, right? Okay. How do I do that? Well, that's really just that very simple formula that we talked about before, its weights, you take the weights, you take the weight vector, right? Then you transpose it, and then you do matrix multiplication with the returns vector. Okay. This was that W transpose R in matrix notation that I showed you in class, right? So this is just that. It's as simple as that, right? It's a direct translation of that equation. We're not quite done, we need to do a portfolio volatility as well. So let me just do that as well. That's portfolio_vol, right? That maps a set of weights along with a co-variance matrix, right? So, you give me weights and you tell me what the co-variance matrix was and I can then tell you exactly what the volatility of that portfolio would have been. So let me just do this for now, to vol, right? How do I do that? Well, what do I return? It's the same weights vector right, transpose, and then you do matrix multiply with the co-variance matrix, and you matrix multiply that with the weights, right? This is the expression for what? This is the expression for the variance of the portfolio because that's the variance co-variance matrix. So, let me just compute the volatility from that and that's nothing more than the square root of that. So all I have to do is that. Raise that to the 0.5 power. Okay. So let's see if that really works. Let's take a very simple example, right? So I'm going to say, let's say my assets that I am going to invest in are something like, let's do food, beer, and smoke, tobacco and I don't know coal, right? So, first let's look at what did that return vector for that looks like. Okay. So that's good. So that's basically what an expected return vector looks like. Okay. What is the co-variance matrix for that look like? So I can do cov, right? Remember I'm using er and cov because I just built it up here, er and cov, okay. Lets see. Now, what I'm going to do here, I'm going to just index into that. So you're going to do.loc,.loc gives you,.iloc gives you the index location by integers, whereas I'm looking at the index location by this index. I'm going to index it by the column index here. So let's do ll. All right. So that's the row index and that's the column index. Okay. You should see a co-variance matrix similar to what we saw but this is just a subset for us. Okay. So good. So we're done with all of that. So what I'm going to do now is, this looks okay. So since this looks alright, I'm going to move this now into my risk kit. So let's do that before we forget. Okay. I'm going to save that, go back in here, and let's make sure that this works. So, how do I do this? Now I'm going to erk.portfolio_return, right? Exactly the same thing that we did before, except I'm going to just pull it from my kit here. Exactly the same thing, right? Portfolio_return. So, let's give it some vector like erl. I'm sorry. I have to give it some weights. So let's say weights is, let's say this four assets, so let's do something like np.repeat. So I'm going to generate an array which is just one fourth in each asset, and four entries. All right. So those are my weights. Okay. So let me just make sure you understand what that looks like. So I'm just going to do that. I didn't do np did I? So, import numpy as np, and there you go, right? My weights vector should look like that, which looks exactly as I wanted, and so now I can go ahead and do this. So, if you look at that, I go to a 19.5 percent return of what that's telling me. Okay. If I look at erk.portfolio volatility, right, for that same weights, and I give it the co-variance matrix which is.lockll, just like we had before it, that's just exactly this right? So I'm giving it that co-variance matrix for my four assets that I've decided to invest in, and you get an answer. Okay. So there you go, you've got some sort of a risk and a return metric. So, if we were plotting the efficient frontier, we could put a dot where this portfolio that is represented by those weights so we know exactly where that would go. Okay. So, now the only question that's left is, let's just chart out a whole bunch of portfolios and since the simplest way to do this is to start with a two-asset case, I'm going to do that. So let's just do two asset frontier. Okay so.. Okay. So what does the 2 Asset frontier look like? All right, to be able to do this, I'm now going to introduce a new Python concept that you may not have seen before. So let's think about what we want to do. What we want to do is we want to basically generate a set of dots and then connect them all. Each dot is going to represent a portfolio. The first portfolio is going to be a 100 percent in asset A, and the second one is going to be a 100 percent in asset B. So in other words I'm going to go, let's pick our assets now. So for example, I might say let's see, games and let's not forget our own portfolio, our own industry. So this is going to be the assets I'm going to play with. What I'm going to do now is I'm going to generate a set of weights. I'm going to generate a sequence of weights. The first weight vector is going to be a 100 percent in this and the last weight vector is going to be a 100 percent in this, and everything in between is going to be somewhere in between. So let's figure out how that works. Let's say the number of points I want, each of these is going to be a point in that curve. So let's say my number of points is 20 points. Now, I'm going to generate the set of weights. This, I could do in a loop, a for-loop literally in a for loop, but I'm going to generate something called a list comprehension. So a list comprehension is a way of generating a list. Let's start by doing that, so that's a list right there. What does each element of the list going to look like? Each element of the list is basically nothing more than a weight vector, this is a list of weight vectors. So each one of this had better be a weight vector, and a weight vector we already know is just nothing more than an array of weights. So let's do np.array, so that's what each one of these things is going to look like. What is this going to look like? What is each one of these elements going to look like? It's going to have two things to it, why? In this case, we had four because we had four assets. Well, here I'm going to have two assets. So let's just call it W in one asset and therefore one minus W in the other, that makes sense? So W is something like 10 percent. If I put 10 percent in one asset I'm going to put 90 in the other. Now, then if I say put 20 percent in this asset, in the first asset, I'm going to put 80 in the other. Okay, good. So now where am I going to get this W4 from? So that's where this list comprehension is really nice. It looks a lot like a for-loop, so you can say, you're going to generate this element for W in and then you give it something that you can iterate over or something that you can loop over. All right. So what we're going to do is we're going to generate a sequence of numbers which are the asset weights for the first asset. You can do that with something called linspace in NumPy and linspace is just linearly spaced points, that's all it is between two numbers. So I want to go linearly spaced, equally spaced, from zero to one and I want endpoints. So this is a very nice construct in Python, It does take some time to get used to it if you've never seen it. The sequences vary, the formula for it is the recipe for it if you will, is really quite straightforward if you see that tells you right away that's a list comprehension. This is what each element in that list is going to look like and it's going to have as many elements as this for loop is going to run. This for-loop is basically for W in linspace zero to one endpoints. So far so good, let's do that and let's see what this weight's vector look like. Let me get rid of those, we don't need this. Okay. So you can see that the first week vector is zero one, the next one is five percent, 5.2 percent and 94.7 percent and so on and so forth. Let's look at this for example. So you have 20 points, that's exactly what we wanted. Okay. Good. So now we've got a sequence of weights, and all we have to do is corresponding to every weight. We've got to figure out what is the return corresponding to the rate and what is the volatility corresponding to that weight and boom, there you go, we've got the frontier. So let's look at our L, those are the assets where you'd invest in. So now, all I have to do is generate two more lists. The list corresponding to the returns for every one of these weights and the second is the list corresponding to all the volatilities corresponding to that weight. So let's do that, we can use the rets comprehension idea again. So what are the returns? The returns are erk.portfolio return, that's the portfolio return. For what? Of some weight vector, given these asset as my return. So remember this is my 2 Asset portfolio, for what? For W in weights. So for each weight that I get, I'm going to compute a portfolio return and I'm going to stick that altogether in one list, that's really what that saying. The next one is going to be almost identical so I'm just going to copy and paste that, except this is going to be vols and the vols are not the return here, but it's going to be what? It's going to be the portfolio vol, and instead of giving it the return vector, what do I need? I need cov, I need to give the covariance vector. So now you're done, and what I'm going to do is I'm just going to package that in to these. So these are all the points that define the efficient frontier, I'm just going to package them into a DataFrame. So that it looks nice when I draw it. Let's do it this way. So I'm going to give it a dictionary, so this is a two column DataFrame. So what is the title for the task. First column I'm going to call it R and we'll leave rets there, and then the next one is going to let's call it vol and those are the vols. Good. All right. We're done, because now we have everything we need. All we need to do is plot it, so let's do ef.plot and I'm going to plot it as a scatterplot for now. For a scatterplot you give it an x and a y. So let's say your x axis, I'm just going to say that the x-axis is going to be the vol and the y-axis is going to be, what I just call R in that dataset. All right, let's see what it looks like. Of course, we didn't import that, so import pandas as pd, and there you go. Beautiful, efficient Frontier plotted. So let's take all the stuff that we did all these kind of things, let's put that into our toolkit because we're going to be plotting. It's nice to be able to plot these things, let's put it right here. Okay, so I'm just going to copy it in because I don't want to have you sit and watch me type all this stuff in. I'll make some small changes and I'll tell you what I'm doing as I'm going. So let's do this. So I'm going to call this function plot EF2. You have to give it the number of points that you wanted to plot. You have to give it your expected return vector and you have to give it the covariance matrix. Then it's going to do some checking here and then it's going to do exactly what we did. It's going to generate a sequence of weights, then it's going to compute the return for each one of those weights, is going to compute the volatility for each one of those weights. It's going to package them into a DataFrame and then it's going to plot it. I decided to plot it as a line this time just for fun. Let's see what this looks like. Okay. You can see here you've got this thing called style and that's the line style. So this is a way you can tell Pandas what kind of a lifestyle you want when you want to draw the line, and you'll see in a second what that style of dot dash does. In fact it's running it. What I'm going to do is I'm going to change things around a little bit. Let's plot something different. Let's do fin, always fin, and what else? May be beer. Now I'm going to do erk.plot EF2. So what do I have to give it? I have to get how many points? I'm going to do is say, 25 points this time, and what do I have to do? I have to give it the expected returns vector which we have as that, and I also have to give it the covariance matrix. So cov is what we call that and we want to pull out just the assets that we were interested in, and there you go. Good. So that's the efficient frontier right there. That's what the line style did by the way. It basically puts, when you say dot dash like that, that means foot markers and connect line. All right, that's what that means. So you can try other line style, in fact what you can do is here. Let's do this. Let's pass in a style, and I'm going to give it that, default.dash and then let's maybe fix that here and just say style. So I'll save that and let's do this again except that I'm going to pass in a style as equal to, you can do just dot dot, dash line, you can do style of dotted line, all kinds of things like that. So we've been able to plot the efficient frontier for a 2 asset portfolio. I think we're ready to try this for a multi-asset portfolio. It's a little bit more complicated but nothing we can't handle. All right, we'll see you next time.