So this should be a short video.
I want to show you how to simulate
continuous time dynamics the same way we did
very quickly for discrete event.
So in an earlier video, we saw how to accept or reject
discrete event with probability.
Let's call it P, right?
I said all we need to do,
let me erase this.
All we need to do is to be able
to draw random numbers between zero and one.
If we can draw uniformly,
meaning that any interval of the same length,
no matter where it's at here,
has the same probability of being chosen
by our random number generator.
Then we can simulate
discrete event with probability P.
Why?
Because any event here is as likely to be drawn.
Every number here is as as likely to be drawn
on this interval.
So if I ask what's the probability
or what's the fraction of draws
that are gonna fall below some value,
let's say 0.4,
or it's gonna be 40%.
It's gonna be 0.4 of my draws are gonna fall below.
So if I want to say, well do this event
which occurs with probability,
beta times delta T equals to 0.4,
does it occur over this time step?
I draw a number between zero and one.
If it's smaller than 0.4, yes.
If not, no.
That's all we had to do.
And in fact, I claim that being able to draw uniformly
between zero and one, which is easy to do.
It's ran in math lab.
It's ran over max end or rend end and in C++.
All languages have an easy way to draw
pseudo random numbers between zero and one.
That's the only thing that we need to know how to do.
And that's what we're gonna use now
for continuous time event.
Well, now it's not just a matter
of what's the probability of that event.
In continuous time,
I have a distribution PFT
for the time until the next event, right?
If it's a plus all process,
this distribution here would just be
lambda times exponential to the minus lambda,
by definition of the plus all process.
We solved for that in an earlier video,
but it could also be something crazier.
It could be something that goes back to zero
and goes back up, something with multiple peaks.
All that I know is that the area under the curve,
the integral of this is one,
because it's a probability distribution.
So it's PFT times DT is a probability density
and the event has to occur.
So this has to integrate to one.
That's all I know.
Well, to be able to leverage our random number generator
between zero and one and draw
from this distribution of time to the next event,
we're gonna fall back on our CDF trick.
If you remember, CDF is a cumulative distribution function,
which is the integral of the probability density
to the left.
And what that means is that
in some cases, we'll be able to do the integral by hand.
So we saw the CDF for the plus all distribution
was one minus E to the minus lambda T.
I'll use capital T here
'cause it's integrated.
But in most cases,
maybe you have a crazy distribution on the left
because it's an input from some data.
It's an input of your model.
So you might want to do your integral numerically.
So basically you take very small steps in your interval T
and you sum the values that you get.
So here my integral is growing faster and faster.
As I get bigger and bigger values,
eventually it stops growing.
So I get an inflection point, but it keeps growing.
This is the sum.
So it's never gonna decrease, keeps growing,
eventually doesn't grow at all
'cause I go back to zero here,
and then start growing again
and then stops growing forever.
And as I said, we know that it sums to one.
So this CDF is between zero and one by definition.
Well that's cool.
So now what I'm gonna say is that if we draw,
here we were drawing on the X axis visually and asking,
what's the probability that it falls below a line?
Here we're gonna draw on the Y axis.
So you draw a random number, again, between zero and one.
So it falls like somewhere here.
And I'm gonna say that the places
where it crosses this line
or the chances that it crosses at any point
is proportional to this PFT.
And you can think of it this way.
Where is it gonna cross?
Well, if the derivative, like here,
is really, really small,
in fact here it's almost zero,
then it's almost impossible for a point to cross here
'cause it's flat.
So you can't like cross a flat line during this interval T.
So while my probability distribution here was zero
on the left,
I end up here with a region where I can't really cross.
It's very unlikely.
And basically what that means
is that as I ask what's the probability
of crossing at any given point,
what matters is this cross section
and this cross section by construction is PFT times DT,
where DT is the time step that we use
to integrate the function.
So that's it all over.
You just do that all over the space
and then you keep your CDF in memory.
So that would be,
if my time interval that I care about
is in days, from zero to a thousand days
and I have little DT of half a day,
and then I have 2,000 values that I store.
So those values go from zero to one
It's my CDF over all my increments DT.
And then whenever I draw a random number
between zero and one, I can do a smart, binary search
and find where does my CDF cross that value?
And then that interval becomes my time T
at which that event happens.
It's useful to draw a few example
and play with it to convince yourself
that what matters is indeed
sort of a cross section of that CDF,
which is by definition its slope
and therefore the the probability density.
But that's a simple trick.
And a lot of models to be computationally
easy to run will either keep a file of the CDF,
so you might have a lot of different CDFs
for different type of event.
You might want to keep them on file, just load them once
to not have to construct or do this numerical integration
time and time again.
But otherwise, it's pretty easy.
You just draw between zero and one
and now you run a binary search
to ask what is the time T
at which my CDF crosses that value.
And then you're done.
You have your time T at which the event happened.
So just to make things a little clearer,
if on the left this was a funny distribution
of time to recovery, right?
There's a lot of people that have an easy time
and recover on average in a week
and some people are unlucky
and recover on average in two weeks,
which is the integral of this second bump here.
Well, basically I just do this step
and now I draw between zero and one
and for one person,
the intersection between the number that I drew in my CDF
gives me the time to recovery for that person.
So you have to do this process of finding the intersection
any time you want to draw from the distribution of the left,
but it's still very easy to do
and doesn't take too much computational power.
So we'll go over that in class eventually
as we start doing more advanced numerical model.
We'll go over all of these things again
and show sort of a complete numerical recipe for modeling.
But this will be a key tool
in our numerical toolbox or computational toolbox.
So I'll see you in class for some discussion
about continuous time models.
Bye.