Okay. Welcome back. Well, we have a lot of work to do in this session, so let's get started. What we're going to do here is we're going to now study what happens when interest rates change, when the interest rates fluctuate over time. We've already seen it has enormous impact on our liabilities, and therefore, our funding ratios, and so it makes sense to try and make sure that we understand how our portfolio is going to behave under different interest rates, when interest rates evolve. The model we're going to use to do this is this model which is called the CIR model. You might have heard about the Vasicek model and the Vasicek model is very closely related to this. So let me explain how this equation works, and then how that's different from the Vasicek model and then we'll start coding it. So let's get started. The way to read this is this differential equation, which is very similar to perhaps what you've already seen for the Brownian motion, sorry. The way it works is similar in the sense that it models changes in the interest rate. So DRT. You should read it as small changes to interest rate, and the small change that happens to the interest rate at time t is defined by this equation. What is that on the right-hand side? It is some parameter a times b minus r_t. So r_t is the current interest rate, and b minus r_t is the difference between the interest rate today and some long-term mean for the interest rate. So that gap between where interest rates today are, and where they generally are in the long-term, and that's a parameter of the model, what is the long-term interest rate. The difference between those two shows you how far you are away from your long-term mean interest rate. So what a here is a parameter that says, how fast do you revert to the long-term interest rate? So we've already seen here that the model requires you to supply at least these two parameters, and I'm going to show you a third one in a second. But the two new parameters that we haven't seen before in these kinds of models are b is the long-term mean interest rate, and a is the mean reversion speed. So if you imagine that a is 0, then this whole term goes away, and therefore, the long-term mean reversion- sorry the long-term mean rate has no role. The d and r_t, that is the change in r_t, does not depend on the mean. On the other hand, on the long-term mean. On the other hand, if you have some a, which is non-zero, then the bigger it is, the faster the observed interest rate will converge back to the mean, the long-term mean. So this is a mean reverting model where a represents the speed of mean reversion. If a is 0, then it generally will not revert to the mean. If a gets larger, it gets faster and faster where it will revert back to the mean sooner than it would if a was small. So this whole term here is really representing the fact that changes in the interest rate depend on the long-term rate, or rather the distance from the long-term rate, or the difference between where you are today, where r_t is today from b, which is the long-term rate and the speed of mean reversion. So a times b minus r_t gives you the amount of the adjustment, and of course, you want to multiply that by the amount of time that has passed. So there's a small amount of time. In this minuscule amount of time or this infinite really small amount of time, this term represents the mean reversion. Then there's a second term here which is- so ignore the square root of r_t for a second, and that's basically something that you should have seen before, and that's actually the same as what we saw in the Brownian motion model, which is it is some measure of some random shop or a random number. So whenever you see dW_t, just think of it as a random number, a random normal, in fact. That random number is multiplied by a scaling factor, which is Sigma, which is the volatility. So in other words, if Sigma is high, then you'll get large changes at each point in time. If Sigma is small, then you'll get very small changes. At the point where when sigma is 0, this whole term goes away and you basically have no random effect. In other words, it's just a deterministic progression over time of the interest rate. So if what I said didn't make sense, that's fine. You're going to start to see it, but I always like to try and explain the intuition behind it, so that when we're coding it, it will hopefully be able to see what we're doing. So what this is doing is in a nutshell, it's a mean reversion model where it says that changes in the interest rate are a function of how far you are from the long-term mean plus some shock, which is a function of Sigma. Now, I told you, ignore the square root of r_t. In fact, if you just ignore the square root r_t, you would get what is the Vasicek model. The square root of r_t is just a way to prevent negative interest rates. So what that tends to happen, as you'll see, is as r_t tends to 0, this amount, the shock amount, will basically have no impact, and so the chances that you will just jump across to a negative number interest rates are very, very small. So that's why the square root of r_t is put in there as a multiplier. It basically says when r_t grows to 0, the shock becomes smaller and smaller, and so you basically will eliminate the effect of the shot, and so the chances that you'll end up with a negative interest rates also goes away, or at least goes down. So that's good. The one other little parenthetical remark I would like to make before we actually get started, is what exactly is this interest rate? So this interest rate is something that we call a short rate. So let me just make a detour here. Well, I guess it's not a detour because we haven't really even started coding this. But let me take a few minutes before we get started to just explain the difference between the short rate. This is what we all think of as the annualized rate. The annualized rate of return. So let me just spend a couple minutes sort of walking you through that process of understanding that difference, because I think it's helpful. Okay. So let's think about exactly what the short rate is. The short rate is the instantaneous rate that you get for a very small amount of time. If you borrow a sum of money and it's just for, let's say, a day, how would you compute that interest rate? So let me motivate that by just studying with something that I think is obvious, and then we'll just shrink the time frame smaller and smaller, and hopefully you'll be able to see what's going on. So let's assume that I give you a dollar, today, and I tell you that I'm going to charge you 100 percent as your interest rate. So your interest rate is 100 percent. At the end of the year, so I'm going to loan it to you for a year, and you have to give me back the principal, plus the interest. So I'm asking you now, what does one dollar grow to at the end of one year at an interest rate of 100 percent? Well, this is not a trick question. It's very simple. You give back the dollar, plus the interest is 100 percent of one. So if I do that, it's basically $2. Not a trick question, no problem. So the sharp, no pun intended. So here is that if interest rates are 100 percent a year and you borrow $1, then at the end of the year, you owe me $2. Actually, it's a little bit more complicated than that. What if I change things that says, look, I'm not going to change your interest rate. Your interest is still 100 percent. I want you, however, to pay me that interest, not just at the end of the year, but I want you to pay me at the end of six months. Twice a year. At the end of six months, and then at the end of the year. So why would it make a difference? The reason is because- let's compute the numbers. So first, you have to, of course, return the principal, and then what do you have to do? What do you have to do? At the end of six months, you've got to give me six months worth of interest. That is half the year's worth of interest. We know the interest for the whole year is $1. So at the end of six months, you give me the principal times 0.5. That's fine, no problem. Except that now, at the end of the year, you have to give me the interest for that second six months, still, it's 0.5 like before. But the principal now is 1.5 because that $1 plus the 0.5, plus this amount. So in other words, here, maybe it's easier to see it this way. I'll have to give you- plus, I have to pay you for the total amount of money that I have over that second six months. Over the second six months, I have what? I have 1 plus 0.5 times 1, which is that amount. So that is the total amount that I am due. In other words, now, I have two payments. The first payment is that and the second payment is that. So it's the same 0.5, but it's now only larger principal. So far so good? So you get 2.25. This was not super-complicated. So in other words, if I am paying twice a year, in other words, the interest rate is applied every six months, instead of just once a year, then it's not $2 at the end of the year. It's $2.25. Well, I could take this one step further. I can say let's do it. Four times a year. So let's apply the same idea. So it's one, that's the principal. Plus, at the end of the first quarter, I owe you one times. Now, it's only a quarters worth of interests. So I have to pay you 0.25. Now, at the second quarter, what do I have to pay? It's again, times 0.25. So the interest rate is one-fourth of the year because it's only one-fourth of the year. But I have to pay you interest on everything I've got at that point. Good. So I've got to do that. That's what I owe you at the end of the second pay period, that it's the second quarter. Now, I'm going to have to do that for the third quarter, and let me just mark this up. Then I'm going to have to pay you for the same thing times 0.25 for the fourth quarter. So let's do the third quarter. So what is the third quarter? Well, it is all that stuff. Because that is how much it has grown too by the time the third quarter rolls around. Got it? Okay. Now, by the time the fourth quarter is rolled around, I've got all this in there. Okay. So very ugly looking expression but you understand what's going on. It's compounding and the interest is accruing at every single point of time. Now, I could extend this further and say, "Well, this is once a quarter, I want to do it once every month." So of course, I could do 1 plus 1 times let's say 1 by 12, and so on and so forth. I could keep doing this. Obviously, you don't want to see me sit and watch me type all the stuff. The good news is, that there actually is an expression for the general form of this. So let me just copy that expression here. If you compound N times a year. So N for me here, here is the expression for that. Let me just copy that N. There we go. That's the expression. So in general, if you compound every 1/N of a year, that in other words, N times a year, this is the expression you get. So let's just try it, just to make sure that we did the right thing here. So let's say 1 plus the interest rate is 100 percent, and it's two times of year, and I want to raise this to the second power. That's 2.25. Great. Let's just make sure we get the same number again. Let's do it again, 1 plus 1 divided by 4. The fourth power, that is quarterly payments, 2.44 that's exactly what we got before. Let me just do this one more time. Let's say, I said, "Okay. Not four times a year, I want you to do it 100 times a year." In other words, I want you to accrue the interest every 100th of a year. What was the number you'd get or 2.0. Of course, you could keep going with this. Let me say, I want you to do it once every, 10,000 times a year. So 10,000. So that means, every 10,000th of a year, I want you to accrue the interest, and you get something like 2.718. That number should look familiar to you. That is basically e. So in other words, so this is the punchline here, as the amount of time between compounding's decreases, the number of times increases, it converges to a particular value. So as N gets very large, this is the expression that you get, and I'm just going to copy that N so that you can see what is going on here. You get this relationship here. So you get 1 plus what we think of as the annual rate, which is that 100 percent is e to the power, the short rate or the instantaneous rate. So you will see people call it the instantaneous rate. You will see people call it the short rate. It's the same thing. So you have this annual rate and the instantaneous rate, and this is the relationship between the two, 1 plus the annual rate is equal to e to the power r instant. By the way, again, if intuitively as saying, "Wow, we've been seeing this one plus r a lot," that's really what's going on. It's this compounding effect. If that intuition helps, that's great. So now, we've got an expression for this. So we can start working with this idea of the annual rate and an instantaneous rate. But before I do that, let's do our usual incantations. So we can start coding now, numpy as np, import let's say pandas as pd, and import edhec_risk_kit as erk, and let's do the load_ext stuff, load_ext autoreload and autoreload 2. All right. So now, let's define our function that we know that we need to convert. So I'm going to define inst_to_ann of some interest rate, and that's just converts short rate to an annualized rate. What do we do here? Well, we just follow this formula. This thing where our annual is some extra power of something minus 1 that happens so often that NumPy actually has a short form for it. So they'll be np, and it'll be exponential. That's what that is. So it's e to the power of that minus 1. So you could just do this, the power r minus 1. But this happens so often, there is an efficient way of doing it. You can say, expm1 of r. That's the same as what I had before. Then the other thing we want to do is, I'm going to def ann_to_inst, the exact opposite of this r. This one does the opposite. It's exactly just the formula that we had here, convert annualized to a short rate. All right. This one is also simple enough. So why don't we just type it right in, and that says return. What do we want to do here? It's the exact opposite. It's log of 1 plus r, and there again that happens often enough that you'll find that it's convenient to just use log1p. So that's the same. This is exactly the same as 1 plus r. So I'm going to do this. It's just more efficient. Good. So now we've got those two functions and now, let me just say that. Now, we can go back to our main order of business, which is that equation. So let me just copy that equation because it'll be nice to see it on the screen. That's what we're going to do. So let's write that code. This is going to seem very, very, very close to what we've already done for the Brownian motion. So I'm going to blast through this part. Let's see if this all makes sense to you. If not, go back to the Brownian motion part because I spent a little bit more time there talking about it. So let's define our function CIR, the CIR model. What are we going to give it? You're going to ask it the number of years, and let's start that out with that's a given a default value of 10. So the number of years for which you want to simulate interest rates. Let's say the number of scenarios, and let's say by default, it's just one. What do we need? Now, we need to give it the parameters to the model. So a, and let's give it a default value of let's say 0.05. Let's assume very little mean reversion. Let's look at the long-term interest rate. Well, that should be let's call it 0.3 percent. These are just default values, you can change it to whatever you want. Sigma is 0.05, let's call it. That's a bit high for interest rates, but why not? Steps per year. Let's do this, we do a lot of stuff with monthly data. So let's just use steps per year is equal to 12. It's a bit large. But for purposes of illustrating what we're trying to do, this is fine. Now, when I say the step is too large, it means that the number of steps per year is too small. Typically, for more accurate simulation of interest rates, you want more data points than 12. But to illustrate the thing, it really is fine because then you'll be able to work things as you'll be able to see how it works on screen, 12 is good enough for that. Then, there's this thing of, remember, this gives you the change in the interest rate. So you have to start somewhere. What I'm going to do is, I'm going to define a default of none for that, and you're going to see in a second why? I'm going to say something very basic here, and implements the CIR model. Okay. So let me tell you why I said if r_0 is none. What I want to do is, if you don't give it the starting rate, I'm just going to assume we're going to start at the long-term mean, right? So I'm going to say if r_0 is none and that's how you check for something being none. I'm just going to say r_0 is assigned b. So in other words, all it's saying is that, hey, you can give me a starting interest rate, but if you don't give me one it's no big deal. I'll just take it to be the same as b. Of course, the important thing here is the interest rate that we were talking about, you've got to convert it to an instantaneous rate. So let's do that. So r_0 is let's say ann_ to_inst. I'm just going to show you how to do this. It's actually turns out that in the grand scheme of things, it's not that important to do this conversion from any lies to internal. Here it looked like a big deal in this case, because you had an interest rate of a 100 percent, right? So obviously that made a big difference between one plus one which is two, and a compounded version which is 2.71828. In reality, for small values of r, the instantaneous rate and the annualized rate are really not that different, and so I'm not going to actually make much more of a fuss about that. It's a subtlety that I really like you to know and understand. But I'm not going to dwell on that, and the difference really if you use one instead of the other. As long as you're talking about relatively small interest rates three, four or five, six percent, it really doesn't make that much difference. Now we have to come up with dt. So dt is the delta time, that we've already said is one divided by steps per year, that's the easy part. Anything else we need to do? Well, we can actually start getting right into that equation. So in order to get that equation to work remember, we need to generate random numbers. So that dwt, requires us to generate random numbers, which is the shock. So let's just do that. So shock is, let's call it, if you remember how we do it. We said np.random.normal. That's the function that generates random numbers. What you do is, you give it a mean center, and I'm going to give that zero, and the scale, that is basically the Sigma of that, is remember we have to do np.the square root of dt. Then we have to give it how many random numbers do we want? Well, first we want, how many rows and how many columns? So actually for the number of rows, let's just compute a new variable. Let's call it num_steps. What is the number of steps that we're going to need? It's going to be, the number of years that we wanted to simulate times the number of steps per year. So let's do that. I'm just going to be a little paranoid, and I am going to say int of that, and the why? Because, I want to be able to call it with things like 1.3 years and 1.4 years and stuff like that, right? So that's the total number of steps, that I want to have. So let's do that. So that's the number of rows, and how many columns? I want one column for every scenario, so that's just the number of scenarios. Okay, easy. I think we are done in terms of generating the shock. What else can we do? Well, actually I'm going to do something here. I'm going to have the number of steps, as that plus one, and I'll show you in a second why. Because, I'm going to initialize that array of rates. So I'm going to define an array of rates, and the rates are going to contain the initial rate at row zero. So I need one more time step. In other words, if I want to simulate say three years I want to start off with r_0 is the current rate. So let's do that. So I'm going to say, rates is np.empty_like, I think we've seen this before, shock. Is just a shorthand way of saying, just give me an empty NumPy array, that has the same dimensions as this. We've already been through the hassle of computing the exact size, that we want it right there. So I'm just going to use a shortcut and say, give me another array that looks exactly at that, in terms of rows and columns, number of rows and number of columns. Now, I can initialize that with r_0. Good. Great, so we've done all the setup. Now, all we have to do is start, simulating the changes in the rates. So what that means is for step in let's say range one to num steps. Why from one to num steps. Well because we already filled out r_0. Rate zero is already done. So now all we have to do is weights one, two, three, four, and just click through time as we go. Click through these simulation steps. So we're performing the actual simulation now. What do we have to do? Well, what we have to do is, we've got to compute the change, so let's do d_r_t. The change in rate, is what? It's nothing more than a times b minus r_t. Well, let's fill out r_t. So let me go in here. Let me initiatives. So what is r_t at this point? R_t is nothing more than the rates that you're coming into this which. Which is the rates at the end of the previous step. So at step one, you're looking at the rates that you had in step zero, at step five, you're looking at this rate that you had at the end of step four. Yeah. So b minus r_t is just that part, times a is that part, here. So far, it's just a very simple translation of that formula. Multiplied by d_t. So we're actually done with that first part, that's a times b minus r_t times d_t, that's exactly what we have here, plus the shock part. So what is the shock we want? We want Sigma, times square root of r_t. So square root of r_t. That's what we just computed. By the way, if I cut this out, if I just didn't type this in, I would get the Vasicek model. Times whatever the shock is, I just went through all this trouble of generating the shock. So let's just use that shock or the random number or that's basically this dwt. So it's the shock for this step. We've generated shocks for every possible step here. This is what we did here. We generate a whole bunch of shocks at one shock. So far so good. So now we know the rate that we were coming in with, we know the change in rate, well, it's pretty easy to update the rates now. So the rates, did I call rates? Yeah. Rates for the step is nothing more than whatever I had r_t, plus d_r_t, and I'm done. So what do I do here? Now I just say, return and I want to give it back as a dataframe, because then everything just looks nice, even if it's the same. Even if it's just a single column. So I want to say, data equal inst. I want to convert it back to annualized rates, inst_to_ann rates. What is my index? My index is, I'm just going to give it the range going from zero to however num_steps we came. So I'm just going to click it through. I'm going to give it just as an index, I'm going to go just go zero, one, two, three, four, up to the number of steps. That's it. So you can see that this formula is translated into this code and it's almost a one is to one. I think that's pretty much it. I'm going to do one thing here just to be paranoid, and I know we talked about how this number is a positive number. But just to be doubly sure that just through round-off error, we don't have some big shock that's close to zero and we've got a negative number. So just to be paranoid, I'm just going do this, it's really not necessary, it does not happen very often. But since it might, I'm just being super cautious. So I think we've done a lot of work. Let's see if any of this stuff works. It's I generally don't like typing in that much code. But let's try plotting something. So matplotlib inline just, you may not need to do this but I like doing it just to be paranoid. So let's generate some scenarios. So I'm going to do cir. Let's generate, I don't know 10 scenarios to be safe. I'm going to plot it and what should we do? Let's give it a nice big size. We're going to need it, is equal to 12,5 seem big enough. So let's try that. Oops. Yes, of course, np.square root. This is the problem when you just try and type all the stuff without looking and you type this much. I hope you're not going to watch me fix a whole bunch of bugs now at this point. Let's try that. Okay, there you go. Let's get rid of that legend. We don't need a legend for this. Beautiful. Those look like interest rates to me, right? They're moving around. There's one case that's going up. It's what you can see that the interest rates have shot up there quite a bit. Let's do this. Let's try and see what happens. So let's just play around with this for a little while. Let's say Sigma equals 0.01. So I'm cutting Sigma down, and what should you expect to see? You shouldn't see quite as many going up to like 14 or 15 percent. So let's just try it and see if that's true, and yeah, you can see that it goes to up to about 3.8 percent, then it goes down to about this but not much more. So you see you've been able to contain the number of the growth of interest rates. It doesn't go as crazily. You could actually play around with this stuff using the interactive widgets if you want. I will skip that because I think you already have an intuition for this, if not, take a look at the code that I wrote for the Brownian Motion. In fact, let me do this. Let me pause this recording here. I'm going to just type it in here, I'm going to show it to you, and then you can play around with this, and I'll give you this notebook for you to play around with. So let me just load it up and then I'll give you the notebook for you to play with. So all I did was cut and paste that code in here and let me hit Enter, and you can now play around with this. So here's the code to do that, you've probably seen versions of this already. You can play around what happens if let's say the number of scenarios, so let me increase the number of scenarios, you'll see it's much more. Now it's much more number of lines. Let me cut Sigma and you'll see if I cut it all the way down to zero, it just grows. Interest rates are just constantly going up and it's heading towards a long term mean. If the speed of mean reversion increases. So let me increase the speed of mean reversion. You'll see it's just getting to that number faster. If the speed of mean reversion decreases, you'll see it's moving to that long-term mean, but it's just doing it more slowly. The long-term mean is that so let me move that up and you should see, there you go, you can see the x-axis, the y-axis changes, so on and so forth. So I would encourage you to play around with this stuff. This is really not the goal here to teach you how the CIR model works. You can play around with it. The goal is actually to use the CIR model in what we're going to be doing and that is we want to see the effect of changes of interest rate. So now we have a way to change interest rates. Does it makes sense? Okay, so what do we really want to do? What we wanna do is we want to figure out when interest rates change, what happens to the price of that zero-coupon bond that we were talking about that we were using as a hedge. So let's look at how we can do that. The good news is that the CIR model, the math of what I'm going to explain to you, show you, is a little bit onerous. So I'm actually going to skip the math. I'm just going to give you the result. And the good news is that the CIR model actually includes in it, embeds in it, a way for you to compute the prices of a zero-coupon bond and that is the answer. So the price of a bond that at any time t, so the price of a bond at time t that matures at some, time capital T, is given by this expression: a times e to the power minus b, and a and b are given this way, where h, it's just an expansion. So this looks a little onerous and looks a little intimidating perhaps, but it's just typing. All of it is typing. So what I'm going to do now is I'm going to spare you watching me clumsily type all the stuff. So I'm going to just copy that code in here, and I'm going to modify this simple code. So this one, I hope, I typed it in as I went because I wanted you to see me type it. This, that I'm going to type in now or more accurately pasting now, is just a teeny tiny bit more complicated only because we've added these things. So we're adding an H. We've added these formulas for A and B, and we're just updating this in exactly the way that we had before where we have prices. So what I'm doing now is I've just updated my CIR model to give you back not just, see, if you look at what I had before in the CIR model, what I had there was I was just returning the interest rates. Now, I'm actually returning the interest rates which is exactly that, exactly what we had before, and I'm also returning the prices. So I'm going to hit Shift Enter and let's just take a look at what that looks like. So again, let me do this again so that you can just take a look at this. Again, I'm just copying it in because it's just nothing more than a repeat of what you've already seen further up ahead, and I'm going to show these prices which is different from showing the rates. So let's take a look at this. You can see here that as, let me squish the up and down so you can, okay, there you go. So now take a look at what's going on here. Exactly what we had before. Let me increase the number of scenarios to, I don't know, 27 of them. So this is the evolution in price of a zero-coupon bond, as interest rates go up, the price of the bond comes down. Remember that. As interest rates go up, the price of the bond comes down. As interest rates falls, the price of the bond goes up to the point where when interest rate, let's cut interest rates all the way down to zero, and let's cut Sigma down to zero. Let's cut it really small. Let's just say, there's some case when it doesn't like it when Sigma is down to zero. You can see that the prices converge very, very closely from each other. There's almost no impact of interest rates. I'm sorry, no changes in interest rate and as a result, it just ends up at zero. But the point I'm trying to make is, no matter what happens, as I increase this, as I increase risks, as I increase this, as I increase this, it doesn't matter. The price of the bond will eventually converge to one. It doesn't matter what happens along the way. But along the way, there can be a lot of variation in prices. So if you follow one of these things, let me cut my scenario down to one. If you look at this, you can see bond prices, the price of a zero-coupon bond, can stay flat for a while, can go down, can go up as interest rates change. Eventually when the bond maturity is very, very short when you're very close to maturity, it will end up being equal to one which is the amount of cash that the zero-coupon bond pays off on maturity. Okay, good. So what have we done in all of these? We have been able to build a model that changes interest rates, we have been able to figure out based on that model how prices change, and because we know now how prices change, we can simulate what would happen if we use zero-coupon bond as our hedge compared to if you use cash as our hedge. So what I'm going to do now is I'm going to look at what happens. All right, so let's start typing it. So let's assume that I have some assets today and I'm going to assume that I have let's say $0.75 million, that's $0.75, that's my cash on hand. Okay, and now what I'm going to do is I'm going to simulate the next 10 years. So let me say rates and bond prices is what? CIR. Right. Let's say r_0 that is my starting interest rate is three percent. Yeah. Let me assume the long-term interest rate is also three percent. Let me simulate just 10 scenarios for now so that I can actually look at them. So now, I've been able to simulate the changes that are going to happen over the next 10 years. I think that's the default, but just to be safe let me just do this. By the way, I can just shift tab if you remember in any of the standard. So this was unnecessary but why not? We'll leave it like that. Next, what are we going to do? We have the rates, and let's assume for simplicity that I am going to model my liabilities as just the bond prices. So let's look at this. Let's look at what I'm going to do here. Liabilities is nothing more than bond prices. What that means here is my liabilities are going to change over time, right? Because the idea is, my payoff, my liability that I'm trying to match is one million dollars at the end of my 10 years. But because the bond prices that I am simulating or actually the bond that I'm simulating is actually a zero-coupon bond, then obviously what's going to happen is as the price of that bond changes, the liabilities change appropriately. In other words, as interest rates change, the cost of paying off a dollar is my liability, and the cost of paying off a dollar is also the bond price, and therefore my liabilities are nothing more than the bond prices. Make sense, right? That's the basic little trick that I'm using here, is for me to model my liabilities changing over time, I'm just using nothing more than the changes in the prices of the zero-coupon bond because I know that they're basically the same thing. So now, I can try and figure out what the price of a zero-coupon bond today is. So what is the price if I wanted to buy a zero-coupon bond today? Let's call it zc_0. If I want to buy a zero-coupon bond today, what would it be? Well, we already know what that is. It's nothing more than the present value which we've already done in the past. What exactly is the liability? It's the present value of paying off a dollar 10 years in the future. Well, we know how to do that. It's just this. I'm paying off one dollar. The index, that is the time. When do I need to be off this dollar? I need to pay off the dollar in 10 years and my interest rate is 0.03. So we know exactly what the price of a zero-coupon bond today is, 10 years from now. It is nothing more than the present value of that dollar 10 years from now. I'm assuming an interest rate is three percent. So now, I know how much money I have. I have three-quarters of a million. I have been able to simulate the changes in that liability over time. That's basically what that is. The bond prices are nothing more than the evolution, the changes in the present value of that liability. I know what a zero-coupon bond today costs. So now, what would happen if I took my 0.75 million, if I took my assets, and I put it in the zero-coupon bond? How many bonds could I buy? So let's do it in steps. So the number of bonds that I can buy is what? It is however many dollars I have divided by whatever the price of the bond is. Yeah, that's nothing special. So now, I've got exactly how many bonds I have and I also know how those bond prices are changing over time. So now I know how my asset value is going to do over time. What is my asset value over time? So in fact let me say asset value, assuming I put my money in a zero-coupon bond. So let's call this av, which is the acid value assuming that I put it in zero-coupon bond. Well, I know how many bonds I have, and I know the prices of those bonds. That's what we just did. That's it. Now, what if I didn't take that 0.75 million, and what if I put it in cash instead? So let's compute the growth of 0.75 in cash. So you might think, okay, well, that's super safe. I'm just going to put that in cash. Well, what would it be? It will be whatever I have today times, and let's just assume that whatever I have in my rates. So my interest rates. I know my interest rate, and let's say I can always get that rate. So because that's the short rate. So it's basically whatever I am getting as my rates. Now remember, those are the annualized rates, so I'm going to divide that by 12. What else do I need to do? So it's 1 plus r of that of course. One plus r, times cumprod, the cumprod of that which we've seen now several times. So let me just walk you through this just in case you haven't. So what is this saying? The growth of that 0.75 that I put in cash is the initial value times whatever this array of rates that I've got. So I've now got this matrix, this t by n, where t is the number of time steps and n is the number of scenarios that I've simulated. Those are the annual interest rates, right?? That's what we that's what we simulated here for example. No, not here. All the way back. Yeah, this year. These are the changes in rates. That's an annualized rate. So what we want to do is we wanted to 1 plus r of that, and then you want to just basically do the cumprod that is compounded. In other words, this is nothing more than the compounded value. So at that point, you now know exactly how much the value of the asset would be if you had invested in cash. Well, that's where, oh yes here. Further proof that I actually I'm typing the same as I go. Well, of course, it's not the present value of that, it's the present value of that and I'm sure you guys were all yelling at me saying, "No, no, no, that's not what it is." I want to generate a series and that is my liabilities and what I should've done. Let's make it super clear. This is the zc bond. In 10 years. So it's a zero-coupon bond. So my zc bond, in 10 years, is pd.Series data equal 10, I'm sorry, 1, and my index equal 10. Better? Well, this makes much more sense now. That's the zero-coupon bond in 10 years. The cost of that bond today is nothing more than the present value of that bond, of that dollar, okay? Let's try that again. There you go. Good. So far, so good. So now, we should be able to look at how that cash account works. Let's plot it. Should we give it a title? That's legend equal False, because we don't want all those extra things there, and figsize is 12, 6. There you go. So you can see here, what is going on? Let's look at this chart and try and figure out what's going on. What this is saying is, this is the evolution of my asset value in 10 different scenarios. As interest rates change, some of them do very well, as you can see this little purple line is going all the way up to whats that? 1.2, so $1.2 million. So here's the way you should interpret this. If you put your money in cash, your $0.75 million in cash, and you just kept it in cash. That is your hedge. Remember, the liability you're trying to match is a million dollars 10 years from now. A hundred and twenty months from now. So what this is saying is, but I only have 0.75, I don't have a million dollars. I only have 0.75, three-quarters of a million now. So I'm going to take my three quarters of a million and I'm going to put it in cash as my hedge. Okay. What this is saying is, well, you might make it. You might not make it. There are some scenarios where you end up above $1 million. These are the cases where you could say, "I have succeeded. I am able to meet those liabilities." But what happens if these things happen? Well, you're short. As you can see here, you could be short by a little more than 20 percent. So if you're a pension fund, this is a huge problem. You cannot just go to your members and tell them, "I'm really sorry. We were super safe. We put your money in cash, but I'm sorry we don't have enough for you." That's simply not an option. Even in a personal situation, if you've got, say, a kid going to college 10 years from now, it would be quite a setback for your kid to basically tell you, "Hey, I got into Stanford," and you say, "Look, I'm sorry. I put my money in cash. I thought it was super safe. I'm sorry. I don't have the money for you." Or at least, I don't have enough. All right. We're going to solve this basic problem, but I just want to illustrate, this is all a very, very long way of formalizing the idea and computing and building a computing infrastructure that demonstrates that putting your money all in cash is not a safe hedge, because there's some realistic chance that you're simply not going to be able to make that $1 million liability. Well, is there an alternative? Well, sure. Remember, I did all that trouble to compute also the asset value of the zero-coupon bonds. So let's do take this exact same thing. So I'm going to do this, and I'm going to copy it, and I'm going to print this, and I'm going to do a different chart this time. But instead of the cash, let's look at what if I had actually put my money in zero-coupon bonds? There you see that at this point, I have been able to get to pretty much something very close to one. In fact, I believe it's slightly, slightly higher than one. So you can see here that this is the beauty of zero-coupon bonds. This is the beauty of being able to buy something that doesn't get affected by interest rates. It, of course, gets affected by interest rates as you go. There's a lot of changes. So if you look at the value of your portfolio, this might look like a nice smooth ride up, and you might think, wow, that's really not risky. But that's the wrong way to look at it, because what really you should be looking at is your funding ratio. What this is saying is, this looks super risky. But if you look at it in terms of your funding ratio, it's actually not. So let's try and compute exactly that measure. Let's look at it in terms of funding ratio. So what I'm going to do now is I'm going to compute- basically, what Lionel did in class. I'm going to compute here. Let me just copy it, because it's a little easier to walk you through the expression then type it in. So I'm going to do is I am going to- here, this is what I'm doing. So I'm going to take the cash divided by liabilities. That is the funding ratio that I have at any given point in time. Cash values are changing, liabilities values are changing, and I'm going to compute the return on that funding ratio. So I'm looking at basically the changes, percentage changes just over time. How much is it fluctuating by? So let's just look at that. You'll see it's pretty wild, especially in the beginning. Then, of course, at some point, it stabilizes when you go towards the end. So the returns on the funding ratio or the changes in the funding ratio, when I'm using cash as my asset, is extremely volatile. That's quite different from the story that you might conclude from here, where it looks like cash is a nice, steady, non-risky asset. But you can see here, when you look at it in terms of the funding ratio, it's actually very, very risky. Now, let's do the exact same thing. So I'm just going to again copy, and I'm going to paste. But now, I'm not going to look at the asset value of the cash portfolio. I'm going to look at the zero-coupon bond portfolio. In other words, what happens to my asset values instead of just putting it in cash, if I had bought bonds, what would I have done? All right. Hopefully, you're not going to be surprised by that. It is just one simple flat line. In other words, your funding ratio is steady. This is what you want. You want something where you've got a nice, fixed, reliable funding ratio. So that is the beauty of using zero-coupon bonds as your funding vehicle to hedge your liabilities. Now, the challenge, of course, is even though the zero-coupon bonds, if they existed, would be fantastic hedge vehicles, they do exist, but they're actually not that easy for retail investors to buy. Even for institutional investors, it gets hard to find exactly the right liability matching set of zero-coupon bonds. But really, the main benefit of the zero-coupon bond is the fact that it doesn't fluctuate. It's less sensitive to interest rates. So that you still end up where you need to end up. Even though, in the meanwhile, it could fluctuate a lot. So let me end by just looking at- everything we've done. I'm trying to wrap it up in a slightly different way now. I'm going to look at the terminal funding ratio. This is the range of ways in which the story could end when you invest with different vehicles. So what I'm going to do is- let's start typing again. So I've got the same old thing that I've got before. My asset value today is 0.75. Let's do the same thing that we had before. Let's say rates and bond_prices are cir, and let's say n_scenarios is, let's say 10,000. I'm going to look at 10,000 different ways this could work out. So r_0 is 0.03. Let's say b is also 0.03. Now, what am I going to do? I'm going to look at the same thing I did before. Liabilities as bond prices. Then what I'm going to do is do the same thing, which is the cost of my zero-coupon bond is the same thing I did before. It's erk.pv the present value of p.Series, so I'm not going to make that mistake again. In fact, we already did that. Didn't we already have that? It's zc something, zcbond_10. I'm going to do, assuming 0.03, again. So let's just do that. So now, we've got the price today of a zero-coupon bond. So how many bonds can I buy? N bonds is equal to whatever my assets are divided by the price of the bond. The account value of the zero-coupon bonds is exactly what we had before, which is the number of bonds times bond prices. That's my asset value. If I have 10 bonds and the price of $4, then asset value is $40. Now, what if I did it in cash? This is just a repeat of everything we've done before. What is that? Well, that's nothing more than whatever I have today multiplied by, let's say, rates divided by 12 plus 1,.cumpro- Okay. I think that all looks good. I think that looks good. Let me run it. But this time I'm running 10,000 scenarios. So it's going to take a few seconds to run especially if you're on a computer that is old and as slow as this. By the way, that's what that little star there means. That star there says, which now has now gone, means it's computing. So now I'm done. So what do we want to do? We want to look at the terminal funding ratios. So in other words, I don't care about all the intermediate values, I want to know how am I going to do at the very end, at that last point in time. So let's call it terminal funding ratio TFR, if I use cash. So what is this terminal funding ratio? It's nothing more than the account value of my cash. But I want the last one. So it's i loc minus 1. Remember what I said, i loc is the integer index and minus one is a way to say I want the last one. So my funding ratio is that divided by the liabilities at that same point in time. Good. So that's my terminal funding ratio. It is the account value that I have at that point in time divided by my liabilities at that point in time. Okay. So now I should just be able to do the exact same thing for bonds. So let's just do tfr zc bonds. Is the account value of zero-coupon bonds which is what we just computed up there, but we want the last one, that i loc, of the last one divided by, again, the liabilities at the very end. That's the terminal funding ratio of that. Okay. Now we can just plot it. So let's plot both of these as histograms. So what I'm going to do is tfr cash.plot.hist. So let's plot a histogram and let's label this is cash right? So these are the funding ratios and my figsize is, let's say, 12 or let's say 15, 6. That's how a lot of bins because I did 10,000 of them so let's put a 100 bins for my histogram. Yeah. I definitely want a legend here because I'm going to do something here. Legends equal True. So I'm going to do that and then I'm going to plot the other one on exactly the same thing. So I'm going to do TFR I'm going to do the bonds and I'm going to plot that on exactly the same one plot.hist. ax equal ax, we've done this a few times before now. Then this one is what happens if I have zero-coupon bonds. Again, bins is 100, what else do I need? legend equal to True. Right. Okay. So let's look at what happened, look at this. You can see that the axis, well, let's just give them two axes. There's an easy way to fix that which is just secondary y. Let's put this on a secondary y axis. Which is interesting because what this is basically telling you is every single one of these 10,000 cases, you end up with a funding ratio which has like with a reliable funding ratio. We'll just see what, there you go. Okay. So this picture tells everything that we've done so far so let me try and walk you through this. What this is saying is, though if you invest in, remember, the liability here that we were talking about is a million dollars, you started with three quarters of a million dollars and you had 10 years to get there. What we did is, we said "Hey, what would happen if I put my money in cash?" Then the second question is, what if I put my money in a zero-coupon bond? What this is telling you is, what is the spread of funding ratios? What this is saying is, if you put your money and cash, yeah, there is a certain chance that you'll do really well but there's also a significant chance that you're not going to have the money when you get there. So in other words, your funding ratio could be as low as something, let's call it 0.75, 0.8. You could fall short by almost 20 percent. You might overshoot but you could fall short. If you put your money in zero-coupon bonds, your funding ratio here is a nice solid, rock solid, one point something. It's slightly higher than one as you can tell. If I remember right, it's 1.03 but that's not the point. The point is that you've got a rock solid funding ratio which is really what the game is all about. The game is all about making sure that you can make the payments that you said you are going to make, that you can keep that promise if you're a pension fund. If you're an individual, the equivalent version of this is that you will have the money at the time that you need it. Of course, I'm making a very convenient assumption here and that is that my a zero is 0.75 and I've got 10 years to do it and what this is telling you is if interest rates are three percent, you're going to be fine. You're going to make it as long as you invest using a zero-coupon bond and not cash. If you've got cash and you put your money in cash, well, I don't know if you're going to make it. You might. You might do very well indeed, but you might also not make it. So that of course is a very convenient assumption that I made that I have three-quarters of a million dollars, if I had 0.5 instead of three quarters of a million, this picture wouldn't look quite as good. So let me just make that point. Let me switch it. Let's say my assets are only half a million dollars and I have 10 years and I do exactly the same thing. Now again, I'm running through 10,000 scenarios all over again and it's going to plot a picture that is going to look almost identical except that line here is now at about 0.7. So what this is telling you is a different problem. It says you simply don't have enough cash to be able to get there with any degree of certainty. You might be able to get there, there's a small percentage chance that you can get there if you go to cash, but our whole game now is going to be, well, I want to be able to have a higher degree of certainty. So one way of thinking about this is, I want to have this spread, b, as skinny, as narrow as possible and I want it to be as close to a funding ratio of one as possible. That's what we're going to be doing in the remaining sessions. We're going to try and see what we can do to get there. Thank you very much. It's been a long session. I hope this all made sense to you. I will have a more extended version of this in the notebook, where I'll have a few more steps in the middle which might be helpful to you if you found that I went too fast here and I'd encourage you to go take a look at that notebook. In the meanwhile, what I would say is, all of this code that we've developed here, I've just typed it right in here so things like the CIR model in particular, add that to the toolkit because in the very next class, or in the next lab session, we're going to be using it. So I would say, copy and paste this code. If you've been following along, just copy and paste this code from this into the toolkit and then we'll use it from the toolkit the next time around. Thank you very much. I will see you there.