So let's do that. Let's plot it. Let's say risky_wealth.plot. Let me just make sure that it works as we expected. Yeah. So I'm starting with $1,000 and then I'm just compounding it over time. So this is what I would get if I did none of the CPPI stuff. All I'm doing is starting with $1,000 and then compounding it overtime. Good. So what I'm going to do now is, I'm actually going to, and I'm going do this one at a time. So I've got my account history of beer. Let me do this thing that I've done before. I'm going to plot these things on the same plot. So I'm going to get the axis object which is what I get when I do a plot. Then I'm going to plot my risky_wealth for beer on that same plot. So I'm going to say, plot the risky_wealth for beer. That is in other words, the value of the account if I had just invested in the beer portfolio, and plot it on that same axis, that same pair of axis. Let me make this bigger. Just to make it different, look different, I am going to say, let's put that as let's say style is k:. So what that's saying is, plot it and use a line style, k is just a code for black, and that means dotted lines. It's such a fancy short compact notation which you'll see a lot for setting a line style. Is there anything else we want to do? Let's also while we're at it, since we have access to this, let's also set the floor values so that we can make sure that we never actually breached the floor. So let's look at that, axhline, that's what we call it. So access horizontal line. Let's draw a horizontal line at the floor value. There you go, floor value. So at $800. Let's do that in red. So this is another way of doing it and line style is. Just different ways of doing the same thing. Let's see what that looks like. So you see there. This blue line is the main series that we plotted. That's the account history of beer. That's the CPPI account history of beer. Then there's the dotted line, which is the risky_wealth. So if I just invested in beer alone, you see that it would have had more variation, and of course, that makes sense because when you're investing in the blue asset, it's some dynamic mix of beer and the safe asset, of the beer portfolio and the safe asset. So the safe asset obviously is very much less volatile. So let's look at that because we have the history. So let's look at the risky with history. I'm just going to plot that over time. You see here that these weights have varied quite a bit. In the case of beer, you started somewhere in the middle and there was a lot of variation. So that means we did have allocations. So eventually, it ended up with 100 percent allocation to the risky asset. So at some point in time, it just becomes, grows along with the risky asset. It grows, both of them grow. You can see the correlation between them is almost what? The correlation is 100 percent because at some point in time, the blue line is just getting exactly the same returns as that dotted line. But over here in the early days, you can see that the variation in the blue line is less than the variation in the dotted line and that's because some of the allocation is to the safe asset at that time. Now, you can see immediately by the way right here that the CPPI stuff just setting an absolute floor like that, in this particular case, very quickly becomes a non-issue. In other words, once you've gotten to a value somewhere around here, so let's look at that. The beer line is here. So once you've gotten to about 2009 which is around here, you are so far away from the floor that you're 100 percent in that asset and the chances that you're going to get anywhere close to your floor, you're already at a decent value. The chances that you're going to get back to that floor is so small because you've got such a large account value that your cushion is so large that you're just going to be 100 percent in. The floor is irrelevant because you're just going to be 100 percent no matter what happens, because the chances that you're going to go from that value somewhere there, and crash all the way down to 800 is very, very small. Makes sense? All right. So now you see why actually in real life, nobody would actually just set that flow. What they would typically do is update that floor when you have that account value. So let's say you set your floor value at 800 at that point and you're sitting at a $3,000 account, in real life, any investor would say, "Why am I setting my floor at $800 when I already have $3,000? I should probably set my floor at maybe 2,000 or 2,500." Well, that's implicitly implementing a drawdown. So that's exactly what the drawdown is. It's constantly updating your floor, but we'll get to that shortly. I want to do a few other things before then. So for example, let's look at the finances. Let's do the exact same thing. So here, let me just make this a little bit easier to use. So I'm going to say ind is finance, then I'm just going to do this for the finance industry. So it's just a little bit easier so that I can copy and paste the next time around for steel. So let's look at that. Well, you can see here CPPI has been far more important and far more effective because you started out here, everything was alright, finance was doing really well, you weren't getting all of the benefits so your blue line was below the dotted line. Then in 2008, finance just crashed. If you had been invested in finance, you would have ended up piercing your floor. Think about somebody who wants to either retire at that point in time or who has a kid who wants to go to college at that point in time, that would have been brutal. Instead, what you can see here is the blue line is very nicely deftly bouncing off this. That's because there was no violation of the floor here because there was so little risk budget at that point. Now, the disadvantage of that of course is when it bounces back, you only have a small exposure to that portfolio and as a result, you don't enjoy all the upside. Makes sense? Good. Lets do this again in steel. That isn't what I wanted to do because I wrote all that stuff there so that I can copy it and paste it. Let me see what happens. Let's see what happened in the steel industry. So in the steel industry, it's even more dramatic because the steel industry just tanked right away, right over there. If you had been invested in the steel industry, oh my God, that would have been brutal, that would've been completely brutal. You can see that CPPI there is really bailing you out and for most of the life of that thing, CPPI is by far the place you want to be. It's far less volatile and it protects you from breaching your floor. Good. So I think you've got a pretty good idea of how this works. So let's take all the stuff we've done and I'm just going to actually take all this code that we wrote and I'm going to just put it into our toolkit. So here, I'll just copy paste that for now because you don't want to sit and watch me type. Let's go into our EDHEC-Risk toolkit, let's go all the way to the bottom, and I'm going to add my cppi code there. So let me quickly walk you through it. Nothing here that you haven't seen, so my function I'm going to call it runcppi. I'm going to give it a risky asset, if you don't give me a safe asset I'm just going to use the risk-free rate of 0.03, and all the stuff is exactly what we've seen before. All I did was add in some comments and things like that. I added this to initialize the risky return. I initialize it as a data frame if you give me a series, just so it works with series as well. I've got the safe value here, and all the stuff is exactly the same. It's just nothing more than the same code that we just typed in and that's it. So that's the fore-loop. What else am I going to do here? I have to compute the risky wealth, so let me add that. Risky wealth is the start value times one plus the risky return, and I said we're going to do a cumprod on that. Then what else do we do? I'm actually done, I just have to return the result of this back-test. So what I'm going to do is take all of these things that we've got here, all the things that I was looking at like the cushion history, and the risky wage history, and all that stuff, and I'm going to just pack them into a nice little dictionary, so that I can access it on the other end, or when I call it, and I'm just going to say "return backtest result". While I'm at it, since I'm going to be looking at backtest results, I'm also going to add this little convenience function. This is something I should have done earlier but let's do it now. Let me copy and paste this so you don't have to watch me type all this stuff. I'm just going to define a function called summary stats, and if you give it a set of returns, it will just take all the stuff we've done so far like the annualized return, the volatility, the sharpe ratio, the draw-down, the skewness, the kurtosis, the cornish-fisher var, the historic c-var and it's just going to package all of these and put it nicely in a little data frame so that you can print it. So let me save that. Let me make sure that I didn't do anything weird in that stuff. So let me just take an example, summary stats, and let's just run it on the Risky Returns just to make sure the code works. That is not a markdown cell, that it is a code cell. This is what that code does. All it does is, for every one of the columns here, it just produces a nice laid out all the things that we might care about. You can see here right away that the draw-downs for all of these over this period of time, this risky returns for these three portfolios were brutal. Obviously steel was the worst, but they had some significant draw-downs here as well. Now let's run a backtest. So I'm going to do backtest results is erk.run_cppi, here you go. runcppi. I'm going to give it the risky returns. Well, what else do we need to do? Anything else? I think we'll just use all the defaults. The starting value is 1,000 and for this thing. Okay, we're good. Now let me just do erk.eummary_stats of the btr. So what am I doing? So btr is the back-test results. I want to take the backtest results and convert them back into returns. If you'd remember, let's go back here, I actually didn't return, I only returned the wealth. Well, but it's not a big deal because I know very easily how to convert wealth back. So I'm only getting the wealth here, but that's not a big deal because I can just do percent change on that, that gives me the returns. The percentage change of the wealth is just the returns. So let me just make sure I did that right. Oh, except that percent change has this one caveat which is the first line as an NA, so it's traditional to just do this, percentagechange.dropna. It has no object wealth, yes of course it doesn't. Okay, good. So let's compare these things. So if we invest in the steel industry, you still have drawdowns but these drawdowns are much, much more palatable than we had before. You look at that? Was this Steel? Yeah, this was Steel, right? So you started at $1,000, you still ended up below where you had started. So if you look at the terminal value there it's just below 1,000. That's the ending value here which is why you have a negative return here. But in all of those cases, you really did reduce the downside risk, and that's really what this insurance nature of the stuff is. It's not really meant to help you on the upside, it's meant to help you on the downside. So let's look at a few other things here. Let's run this algorithm on the market index. Just for fun, let's run it right before the market tanked. So erk.run_cppi, and I'm going to run it on My Total Market Return. So I'm going to invest right before everything goes to pot. Now what am I going to do? I'm going to plot the wealth, so btr Wealth, then I'm going to plot it. Let's give it a fig-size of let's say, 12 five or 12 six. Let me turn off the Legend because it doesn't make sense because all I'm going to do is plot a single series. Then of course, I want to do btr Risky Wealth. I want to plot that as well on the same plot. So that's why I do plot ax equal ax. I want to give it a different style, let's do k dash dash. So dash line. Again, no Legend here. So what do we got? So there you are, you can see very much how this behavior has helped at least around here. So you don't have this huge draw down between these two things. So let's look at whether it really helped. So let's compare, so I'm going to do erk.summary_stats and let's compare the Back-test results Risky Wealth. Again, I'll do the same trick that I did before, percentage.change.dropna. Yeah, that's the Risky Volt. I'm going to compare that with just the wealth, that is the cppi Wealth. Look at that. The drawdown here in the case of if just invested in the market was about a 50 percent drawdown. Our total market index fell by about 50 percent. The drawdown here was about 22 percent. The really interesting thing is, I'm not actually trying to fix drawdown here. It worked so well because I happen to know that by setting it at 800, I knew that was a drawdown coming, of course, I wouldn't have known in real life but I will just illustrating the point that, if I had invested at the start of 2000, I would not have suffered the drawdown of 2008. But now let's actually address the drawdown situation right away. So let's say I want to impose a drawdown constraint. So now we're doing the last thing that I said I was going to do here which is to actually improve my CPPI algorithm to support a drawdown constraint. I'm going to assume that by default there's no drawdown constraint. Now, let's look about what should we do if there is a drawdown constraint. So let's look at what's going on here. The only thing that changes if there's a drawdown constraint is that your floor value is not the static value anymore, it's actually a dynamic value that changes over time as your peak changes over time. So in other words what I'm saying is this, if drawdown is not known. So let's say I had set my drawdown at let's say 20 percent, all I have to do is I have to update my floor value. My floor value is no longer a static number. My floor value is what? It is whatever was my previous peak. So let's say my previous peak because, let's say my previous peak was at $1,000 and my drawdown constraint is 20 percent, then my floor value is 20 percent, so 80 percent of my previous peak. So that's one minus drawdown. If my drawdown constraint is let's say 40 percent, then one minus drawdown is six. So it means, I have to my floor value is 60 percent of my previous peak. What is my previous peak by the way? It's nothing more than the maximum of everything I've seen so far. So I'm going to do something like np.maximum and there are other ways of doing this but let's keep it simple maximum of what the peak I've seen so far and my account value. In other words, it is the higher of my previous peak and the current account value, and of course, I have to set up my previous peak. So let me start my previous peak. So let me do that here, peak is the start value. So I'm going to start it out at the start value. My previous to speak of course is the start value. Good. That's actually all there is. Unless I've made a mistake which I have been known to do in my typos. I think at this point, we can actually run the exact same algorithm but let's give it a drawdown. So let's do a back test of, so in fact,. Let me just make sure I'm doing the same thing here. So drawdown constraint. All right. So how do I do this? Very simple, what I do BTR is erk.cppi just like exactly like we had before. Now, let's do the exact same thing we did before. Let's go from 2007 onwards. So I think we did 2007 yes. So let's run it on the industry portfolios. First, say 2007 onwards and let's run it on our favorite portfolios. So I don't know steel oops finance and beer, and let's put a drawdown constraint on it now. Let's say 25 percent drawdown. Of course, that is what I meant to do. So now I've got my backtest results. So now let's look at it again. So first let's maybe we should just plot it as a first step. So what do I do? AX is BTR, right? Wealth.plot and let's just give it a fixed size of 12, 6 let's say and let's plot the risky wealth of that same back-test.plot and x equal x and, of course, let's do style equal. So remember I'm doing this for all the portfolios here. So you can see here that all of these things have run starting at 1,000 and let's look at steel, for example, that's the portfolio for steel. It's been remarkably stable considering that the dashed red that very violent red line is actually the steel portfolio itself. So let's try something like looking at summary stats for all these things. Because it's a little easier to see the summary stats, so erk.summary starts and let's do it on the risky wealth first just as that because that's our sort of benchmark, risky wealth dot remember the sticks returns not a walton next. So I have to do my usual percentage change dropna and let's look at that. Now let's do the exact same thing but do it for the wealth itself. That is the actual CPPI. Okay. So what should you expect to see? We should expect to see lower drawdowns. What should the drawdowns be? Well, the drawdowns should be less than 25 percent. That was the constraint we give it and as you can see that the maximum drawdown that we ever saw was just about 24.8 percent. You can see that in all of these cases the drawdown was cut. So this is the magic of CPPI. I really encourage you to play with this stuff much more CPPI is far more important than people give it credit for because it really is the basis for a lot of downside protection algorithms that are now finding their way into the marketplace. So CPPI is just the core algorithm, no one implements CPPI quite the way we just did in a very literal sense, there were always some sophisticated sort of twists that are implemented alongside it. For example, the reason we haven't had any violations here is because in this example in this back-test, we re-balancing literally every single month, in reality you wouldn't want to do that. So you typically will do something to have boundaries or sort of buffers around your training limits. So there are things that you would obviously have to adjust for to limit trading costs and things like that. But the basic underlying algorithm is really quite straightforward and it is like I said finding its way into sort of what some people are calling defined outcome type investing or what we call Safety Net type investing where, for example, you want to invest in the SNP 500 but you don't want to take a drawdown of more than say 30 percent or 25 percent. It is also being used to do the same sort of things that principle protected product or a structured product used to do in the old days, but the thing is that these principle protected products were structured products that were very opaque and very difficult to get out of if you ever got into them. Well, with a secret behind CPPI type algorithm, you can see that you can literally implement CPPI with just two assets. You buy the SNP 500 and you buys some sort of a bond index, a short-term bond index and you could do CPPI and you could implement sort of the equivalent of a structure product in your personal bank your personal law brokerage account. So as transaction costs are getting lower, these kinds of algorithms are finding their way into retail accounts and I think it's really important for you to understand these you're going to see more and more of these kinds of algorithms find its way into the marketplace and I think it's good for you to know how it all works. Thank you. I think this has been a long enough session. I will stop here, I hope you enjoyed it. See you next time.