1 00:00:00,080 --> 00:00:01,680 So this should be a short video. 2 00:00:01,680 --> 00:00:03,510 I want to show you how to simulate 3 00:00:03,510 --> 00:00:06,210 continuous time dynamics the same way we did 4 00:00:06,210 --> 00:00:08,733 very quickly for discrete event. 5 00:00:11,880 --> 00:00:16,880 So in an earlier video, we saw how to accept or reject 6 00:00:17,220 --> 00:00:22,220 discrete event with probability. 7 00:00:27,570 --> 00:00:31,350 Let's call it P, right? 8 00:00:31,350 --> 00:00:33,333 I said all we need to do, 9 00:00:34,440 --> 00:00:35,370 let me erase this. 10 00:00:35,370 --> 00:00:37,080 All we need to do is to be able 11 00:00:37,080 --> 00:00:39,783 to draw random numbers between zero and one. 12 00:00:40,890 --> 00:00:42,870 If we can draw uniformly, 13 00:00:42,870 --> 00:00:45,660 meaning that any interval of the same length, 14 00:00:45,660 --> 00:00:46,950 no matter where it's at here, 15 00:00:46,950 --> 00:00:49,800 has the same probability of being chosen 16 00:00:49,800 --> 00:00:52,590 by our random number generator. 17 00:00:52,590 --> 00:00:55,830 Then we can simulate 18 00:00:55,830 --> 00:00:58,080 discrete event with probability P. 19 00:00:58,080 --> 00:00:59,010 Why? 20 00:00:59,010 --> 00:01:02,100 Because any event here is as likely to be drawn. 21 00:01:02,100 --> 00:01:05,550 Every number here is as as likely to be drawn 22 00:01:05,550 --> 00:01:07,350 on this interval. 23 00:01:07,350 --> 00:01:09,390 So if I ask what's the probability 24 00:01:09,390 --> 00:01:12,330 or what's the fraction of draws 25 00:01:12,330 --> 00:01:14,340 that are gonna fall below some value, 26 00:01:14,340 --> 00:01:15,633 let's say 0.4, 27 00:01:18,150 --> 00:01:19,290 or it's gonna be 40%. 28 00:01:19,290 --> 00:01:22,770 It's gonna be 0.4 of my draws are gonna fall below. 29 00:01:22,770 --> 00:01:25,320 So if I want to say, well do this event 30 00:01:25,320 --> 00:01:27,090 which occurs with probability, 31 00:01:27,090 --> 00:01:29,551 beta times delta T equals to 0.4, 32 00:01:29,551 --> 00:01:31,200 does it occur over this time step? 33 00:01:31,200 --> 00:01:32,850 I draw a number between zero and one. 34 00:01:32,850 --> 00:01:34,860 If it's smaller than 0.4, yes. 35 00:01:34,860 --> 00:01:36,330 If not, no. 36 00:01:36,330 --> 00:01:39,510 That's all we had to do. 37 00:01:39,510 --> 00:01:42,450 And in fact, I claim that being able to draw uniformly 38 00:01:42,450 --> 00:01:44,460 between zero and one, which is easy to do. 39 00:01:44,460 --> 00:01:48,120 It's ran in math lab. 40 00:01:48,120 --> 00:01:53,120 It's ran over max end or rend end and in C++. 41 00:01:54,120 --> 00:01:56,910 All languages have an easy way to draw 42 00:01:56,910 --> 00:02:01,860 pseudo random numbers between zero and one. 43 00:02:01,860 --> 00:02:04,510 That's the only thing that we need to know how to do. 44 00:02:06,420 --> 00:02:08,350 And that's what we're gonna use now 45 00:02:09,870 --> 00:02:11,703 for continuous time event. 46 00:02:16,680 --> 00:02:17,790 Well, now it's not just a matter 47 00:02:17,790 --> 00:02:19,740 of what's the probability of that event. 48 00:02:19,740 --> 00:02:21,423 In continuous time, 49 00:02:22,770 --> 00:02:25,410 I have a distribution PFT 50 00:02:25,410 --> 00:02:27,600 for the time until the next event, right? 51 00:02:27,600 --> 00:02:29,160 If it's a plus all process, 52 00:02:29,160 --> 00:02:31,860 this distribution here would just be 53 00:02:31,860 --> 00:02:35,910 lambda times exponential to the minus lambda, 54 00:02:35,910 --> 00:02:37,500 by definition of the plus all process. 55 00:02:37,500 --> 00:02:39,840 We solved for that in an earlier video, 56 00:02:39,840 --> 00:02:41,430 but it could also be something crazier. 57 00:02:41,430 --> 00:02:43,530 It could be something that goes back to zero 58 00:02:43,530 --> 00:02:46,413 and goes back up, something with multiple peaks. 59 00:02:48,750 --> 00:02:52,230 All that I know is that the area under the curve, 60 00:02:52,230 --> 00:02:53,760 the integral of this is one, 61 00:02:53,760 --> 00:02:57,390 because it's a probability distribution. 62 00:02:57,390 --> 00:03:01,770 So it's PFT times DT is a probability density 63 00:03:01,770 --> 00:03:03,240 and the event has to occur. 64 00:03:03,240 --> 00:03:05,493 So this has to integrate to one. 65 00:03:06,750 --> 00:03:07,650 That's all I know. 66 00:03:08,940 --> 00:03:12,150 Well, to be able to leverage our random number generator 67 00:03:12,150 --> 00:03:13,950 between zero and one and draw 68 00:03:13,950 --> 00:03:17,400 from this distribution of time to the next event, 69 00:03:17,400 --> 00:03:19,530 we're gonna fall back on our CDF trick. 70 00:03:19,530 --> 00:03:23,430 If you remember, CDF is a cumulative distribution function, 71 00:03:23,430 --> 00:03:27,720 which is the integral of the probability density 72 00:03:27,720 --> 00:03:28,553 to the left. 73 00:03:31,050 --> 00:03:33,090 And what that means is that 74 00:03:33,090 --> 00:03:36,930 in some cases, we'll be able to do the integral by hand. 75 00:03:36,930 --> 00:03:40,080 So we saw the CDF for the plus all distribution 76 00:03:40,080 --> 00:03:44,193 was one minus E to the minus lambda T. 77 00:03:46,260 --> 00:03:48,070 I'll use capital T here 78 00:03:49,230 --> 00:03:50,553 'cause it's integrated. 79 00:03:51,900 --> 00:03:53,250 But in most cases, 80 00:03:53,250 --> 00:03:55,830 maybe you have a crazy distribution on the left 81 00:03:55,830 --> 00:03:58,350 because it's an input from some data. 82 00:03:58,350 --> 00:03:59,850 It's an input of your model. 83 00:03:59,850 --> 00:04:02,583 So you might want to do your integral numerically. 84 00:04:03,420 --> 00:04:06,630 So basically you take very small steps in your interval T 85 00:04:06,630 --> 00:04:11,630 and you sum the values that you get. 86 00:04:12,150 --> 00:04:14,730 So here my integral is growing faster and faster. 87 00:04:14,730 --> 00:04:16,833 As I get bigger and bigger values, 88 00:04:18,330 --> 00:04:19,890 eventually it stops growing. 89 00:04:19,890 --> 00:04:22,380 So I get an inflection point, but it keeps growing. 90 00:04:22,380 --> 00:04:23,213 This is the sum. 91 00:04:23,213 --> 00:04:25,560 So it's never gonna decrease, keeps growing, 92 00:04:25,560 --> 00:04:28,770 eventually doesn't grow at all 93 00:04:28,770 --> 00:04:30,573 'cause I go back to zero here, 94 00:04:31,590 --> 00:04:33,750 and then start growing again 95 00:04:33,750 --> 00:04:35,790 and then stops growing forever. 96 00:04:35,790 --> 00:04:40,790 And as I said, we know that it sums to one. 97 00:04:41,370 --> 00:04:45,063 So this CDF is between zero and one by definition. 98 00:04:46,440 --> 00:04:47,290 Well that's cool. 99 00:04:48,570 --> 00:04:52,290 So now what I'm gonna say is that if we draw, 100 00:04:52,290 --> 00:04:57,290 here we were drawing on the X axis visually and asking, 101 00:04:58,320 --> 00:05:00,840 what's the probability that it falls below a line? 102 00:05:00,840 --> 00:05:04,650 Here we're gonna draw on the Y axis. 103 00:05:04,650 --> 00:05:07,920 So you draw a random number, again, between zero and one. 104 00:05:07,920 --> 00:05:09,603 So it falls like somewhere here. 105 00:05:11,580 --> 00:05:15,630 And I'm gonna say that the places 106 00:05:15,630 --> 00:05:17,920 where it crosses this line 107 00:05:21,300 --> 00:05:23,790 or the chances that it crosses at any point 108 00:05:23,790 --> 00:05:26,193 is proportional to this PFT. 109 00:05:27,480 --> 00:05:28,740 And you can think of it this way. 110 00:05:28,740 --> 00:05:30,120 Where is it gonna cross? 111 00:05:30,120 --> 00:05:34,170 Well, if the derivative, like here, 112 00:05:34,170 --> 00:05:36,690 is really, really small, 113 00:05:36,690 --> 00:05:38,910 in fact here it's almost zero, 114 00:05:38,910 --> 00:05:43,910 then it's almost impossible for a point to cross here 115 00:05:44,340 --> 00:05:45,840 'cause it's flat. 116 00:05:45,840 --> 00:05:50,550 So you can't like cross a flat line during this interval T. 117 00:05:50,550 --> 00:05:55,550 So while my probability distribution here was zero 118 00:05:55,980 --> 00:05:56,883 on the left, 119 00:05:58,470 --> 00:06:01,440 I end up here with a region where I can't really cross. 120 00:06:01,440 --> 00:06:02,823 It's very unlikely. 121 00:06:05,340 --> 00:06:06,780 And basically what that means 122 00:06:06,780 --> 00:06:11,070 is that as I ask what's the probability 123 00:06:11,070 --> 00:06:13,560 of crossing at any given point, 124 00:06:13,560 --> 00:06:15,610 what matters is this cross section 125 00:06:18,450 --> 00:06:23,450 and this cross section by construction is PFT times DT, 126 00:06:24,450 --> 00:06:27,330 where DT is the time step that we use 127 00:06:27,330 --> 00:06:28,630 to integrate the function. 128 00:06:31,140 --> 00:06:33,210 So that's it all over. 129 00:06:33,210 --> 00:06:34,950 You just do that all over the space 130 00:06:34,950 --> 00:06:37,020 and then you keep your CDF in memory. 131 00:06:37,020 --> 00:06:38,040 So that would be, 132 00:06:38,040 --> 00:06:42,000 if my time interval that I care about 133 00:06:42,000 --> 00:06:45,810 is in days, from zero to a thousand days 134 00:06:45,810 --> 00:06:48,390 and I have little DT of half a day, 135 00:06:48,390 --> 00:06:51,510 and then I have 2,000 values that I store. 136 00:06:51,510 --> 00:06:53,280 So those values go from zero to one 137 00:06:53,280 --> 00:06:56,970 It's my CDF over all my increments DT. 138 00:06:56,970 --> 00:07:00,150 And then whenever I draw a random number 139 00:07:00,150 --> 00:07:03,210 between zero and one, I can do a smart, binary search 140 00:07:03,210 --> 00:07:08,010 and find where does my CDF cross that value? 141 00:07:08,010 --> 00:07:12,450 And then that interval becomes my time T 142 00:07:12,450 --> 00:07:14,433 at which that event happens. 143 00:07:17,460 --> 00:07:19,920 It's useful to draw a few example 144 00:07:19,920 --> 00:07:21,780 and play with it to convince yourself 145 00:07:21,780 --> 00:07:23,310 that what matters is indeed 146 00:07:23,310 --> 00:07:25,800 sort of a cross section of that CDF, 147 00:07:25,800 --> 00:07:28,950 which is by definition its slope 148 00:07:28,950 --> 00:07:31,770 and therefore the the probability density. 149 00:07:31,770 --> 00:07:33,090 But that's a simple trick. 150 00:07:33,090 --> 00:07:35,980 And a lot of models to be computationally 151 00:07:39,600 --> 00:07:43,290 easy to run will either keep a file of the CDF, 152 00:07:43,290 --> 00:07:45,000 so you might have a lot of different CDFs 153 00:07:45,000 --> 00:07:46,980 for different type of event. 154 00:07:46,980 --> 00:07:49,560 You might want to keep them on file, just load them once 155 00:07:49,560 --> 00:07:52,290 to not have to construct or do this numerical integration 156 00:07:52,290 --> 00:07:53,640 time and time again. 157 00:07:53,640 --> 00:07:55,200 But otherwise, it's pretty easy. 158 00:07:55,200 --> 00:07:56,880 You just draw between zero and one 159 00:07:56,880 --> 00:07:58,620 and now you run a binary search 160 00:07:58,620 --> 00:08:01,650 to ask what is the time T 161 00:08:01,650 --> 00:08:04,080 at which my CDF crosses that value. 162 00:08:04,080 --> 00:08:05,010 And then you're done. 163 00:08:05,010 --> 00:08:07,980 You have your time T at which the event happened. 164 00:08:07,980 --> 00:08:09,840 So just to make things a little clearer, 165 00:08:09,840 --> 00:08:12,030 if on the left this was a funny distribution 166 00:08:12,030 --> 00:08:13,770 of time to recovery, right? 167 00:08:13,770 --> 00:08:16,230 There's a lot of people that have an easy time 168 00:08:16,230 --> 00:08:18,190 and recover on average in a week 169 00:08:19,080 --> 00:08:20,280 and some people are unlucky 170 00:08:20,280 --> 00:08:22,410 and recover on average in two weeks, 171 00:08:22,410 --> 00:08:25,533 which is the integral of this second bump here. 172 00:08:28,980 --> 00:08:32,370 Well, basically I just do this step 173 00:08:32,370 --> 00:08:34,710 and now I draw between zero and one 174 00:08:34,710 --> 00:08:35,970 and for one person, 175 00:08:35,970 --> 00:08:39,900 the intersection between the number that I drew in my CDF 176 00:08:39,900 --> 00:08:43,920 gives me the time to recovery for that person. 177 00:08:43,920 --> 00:08:47,100 So you have to do this process of finding the intersection 178 00:08:47,100 --> 00:08:50,280 any time you want to draw from the distribution of the left, 179 00:08:50,280 --> 00:08:52,950 but it's still very easy to do 180 00:08:52,950 --> 00:08:55,410 and doesn't take too much computational power. 181 00:08:55,410 --> 00:08:57,450 So we'll go over that in class eventually 182 00:08:57,450 --> 00:09:00,780 as we start doing more advanced numerical model. 183 00:09:00,780 --> 00:09:03,360 We'll go over all of these things again 184 00:09:03,360 --> 00:09:07,290 and show sort of a complete numerical recipe for modeling. 185 00:09:07,290 --> 00:09:08,910 But this will be a key tool 186 00:09:08,910 --> 00:09:11,850 in our numerical toolbox or computational toolbox. 187 00:09:11,850 --> 00:09:13,770 So I'll see you in class for some discussion 188 00:09:13,770 --> 00:09:15,900 about continuous time models. 189 00:09:15,900 --> 00:09:16,733 Bye.