Back, I want to talk a little more about this idea
that I introduced in the last video,
which is the fact that whenever we have mechanisms
or events governed by fixed rate
that occur regardless of what happened before,
we call that a Poisson process in both discrete time
or in the limit of very small discrete steps,
continuous time.
And really I wanted to talk a little more
about the mathematics of these processes
because we encountered two problem in trying to code up
a very simple random or stochastic simulation
of a simple model.
So one of those is that what happens is if those rates
are very high?
How do we make it so that the probability
in any one step of something occurring is smaller than one?
The other one is if those probabilities are very high,
you can have multiple events happening at the same time,
and you don't want that.
I don't wanna both infect and recover,
infect someone with a disease and recover from the disease
at the same time.
Really it's, the model, the system that we're thinking of
doesn't have those simultaneous event.
One has to happen before the other.
So we don't want our random simulation to do that.
So we want to think about what happens,
how do we deal with competing Poisson processes?
Okay, so here's how to think about it, right?
Let's say we're only dealing with people leaving,
so our departure term from the cloning factory, right?
Well, what is the probability that they leave
in the first step?
So someone in the clone factory.
Whoops.
In the clone factory
leaving in first step.
Well that's pretty simple, right?
So if I say that I have a departure rate or a leaving rate,
which we call l, and an increment of time
that we call t, delta t,
we said we simply test l times delta t
if is my random number smaller than that.
So the probability that I leave in the first step
should be l times delta t.
The probability that I leave in second step
would simply be me not leaving in the first step,
one minus l delta t,
then leaving in the second one.
So the first time, first day, first step in the process,
you go over me, you ask me, do you choose to leave?
Roll the dice, no.
And then in the second step.
Do you choose to leave roll the dice? Yes.
I run a random number smaller than l times delta t,
I choose to leave, okay.
Well here, what I want to talk about,
the reason I'm going through this is I want to talk about
what happens if we renormalize rates, right?
So if l times delta t is too big, let's say it's 1.2,
everyone's gonna leave and it doesn't matter if it's 1.2
or 1.5, people are not leaving faster,
they're limited by this delta t.
So I could renormalize, right?
By that I just mean, you know,
sort of divide this delta t by something.
So let's say that we now have delta t tilda,
which I'm just gonna call delta t over two.
So instead of days we're thinking of half days,
or instead of hours, we're thinking of half hours.
That's sort of the process.
Okay, well, what I wanna see is if I renormalize like that,
how much of a difference does it really make
between this renormalize stochastic simulation
and the previous stochastic simulation with delta t?
Well, now the probability of leaving in the first delta t,
so leaving in first delta t
is, well, I either leave in the very first delta t over two,
which is what I'm testing over now.
So if I do that, that's good.
Or I choose not to leave in the first delta t over two,
and I leave in the second
delta t over two.
By the way, I said I anthropomorphize a lot.
Whenever I say I choose to leave or I choose not to leave,
that's just wrong, right?
It's just a pure roll of the dice.
Does the gut of the dice
choose to make that person leave or not,
the random number generator being the gut here?
Okay, so this is what I have.
Well, I'm gonna have this l times delta two over two here,
another l times t delta two over here,
and then some other term.
So really what I have is l times delta t
minus l delta t over two squared.
Okay, so I'm pretty close to what I had originally now,
just with a correction factor here.
Interesting.
With the probability of leaving in second step,
in the second delta t, right?
Well, that one's a little more complicated.
So regardless, I need to not leave
in the first two delta t over two,
that's one minus l delta t over two.
So I don't leave in the first two half day or half hours.
And then I could leave in the third one.
Or I don't leave in the first three,
and I leave in the fourth one, right?
If you're getting confused here, just think of it this way.
The first calculation we did way over here at the beginning
was looking at whether people were leaving in the first
or second step where step is in steps of delta t.
So either left initially or in the second one.
What we're doing now after the renormalization
is just testing more.
So you can leave in the first delta two over two
or the second one, which both mean leaving
in the first delta t, or you can leave in the third delta t
over two or the fourth one,
which both mean leaving in the second delta t, okay?
I think I erased something important here. Oops.
Over two, okay.
Well, if you do the math of like expending all of these,
essentially what you're gonna get here
is something pretty simple.
So you're gonna get, trying to do it quickly.
You're gonna get, now then the t over two from this,
you're gonna get two of those.
So you're gonna get your l delta t,
which is gonna be the same as this term here.
You're gonna get your,
you're gonna get two l delta t squared
over two where the two is in squared.
And that's gonna be again your term here.
And then you're gonna get in the same thing.
You're gonna get some some third and fourth order term,
and by that I mean proportional to delta t over three.
Okay, so it's sort of the same thing.
We're still getting the same probability
to first approximation of getting out
in the first or second step,
and then we have some order-order comparison.
What this means is that if our delta t are pretty big,
if our probabilities l times delta t,
maybe I should keep an l here,
are pretty big, whenever we renormalize our rates,
we're not gonna get exactly the same dynamics, right?
We're gonna get some correction terms all over.
But if our l times delta t gets really, really small,
imagine that l times delta t is one in 1,000.
Well, this l times delta t cubed, you know,
goes from 10 to the minus three to 10 to the minus nine.
And that's tiny, right?
So the smaller our rates are,
eventually those error terms don't matter anymore,
and renormalizing your rates don't matter.
So one easy way to like solve this problem
is just to use small discrete increment,
and then you're free.
You know, if you make sure that all your rates
are at most of the order of one in 1,000,
all your your rates time delta t,
then you're free to renormalize sort of as you please,
and you know that the errors that you make
are gonna be tiny, right?
So that's one way to get around the problem
of what if my rates are really big, just renormalize.
You were thinking in terms of discrete steps that were days,
think in terms of discrete step that are minutes,
and now it's gonna be pretty smooth
and you're not gonna have any problem.
So that's one easy thing.
The more interesting thing, and here I'm gonna go
a little deeper in the math, is this idea of
what if I can do two things, right?
So you might have like my face over this.
So what if I have competing processes?
Which is what we had in the cloning building,
in the clone factory because people could both leave
or get cloned, and the question is
which one do I do first, right?
So here what we want to talk about is,
let's focus again on just one person
and let that person be me.
Okay, so I entered a clone factory, then the question is,
at every delta t I'm gonna be asked, do you choose to leave?
And I can only get cloned while I'm in the building.
So really what I want to know beforehand is
how long am I gonna stay in this building, right?
So what is the period of time,
let's call it l, so the duration l for length,
length of time that someone,
and that's me, stays in the building.
Okay.
Well, the easiest way to derive this for Poisson processes,
or the way that's most intuitive to me
is to go through the cumulative,
I'll spell it out for this one,
cumulative distribution function.
I wasn't even sure saying the word.
It's usually just called CDF, right?
So this is a function as a function of time,
let's say l that starts at zero.
So there's a probability zero that you don't stay
in the building at all because I'm sort of saying
I'm in the building.
And this is gonna go up in some way,
but can't go beyond one
'cause it's just a sum of probabilities.
So essentially here what I'm calculating
is what's the probability that I've left.
And I shouldn't use the same l here.
Let's just call it t.
So it's the probability that I've left before time t,
so it's zero at t equals zero, and it initially goes up,
and eventually it's gonna be one.
What's the probability that someone
has left before one million years?
It's one, right?
Nobody's staying in the building for that long.
So what does that look like? So let's call it...
I'm just gonna use l for all like things.
So hopefully it doesn't get too confusing.
So here, to be able to do math,
we're gonna have to take the limit
of very small time step delta t.
Like I said when we were talking about renormalization,
as delta t goes to zero,
then the scale of our discrete process doesn't matter.
It doesn't matter because now we have essentially
a continuous time dynamics,
and it makes it a lot easier to do math, right?
And I'm gonna explain what I'm writing in a second, okay?
So the probability that I've left in a time smaller than t,
than l, sorry, the probability that I've left
in a time smaller than l, which is what this mean,
well, it's everything but people that are still there.
So whenever you say everything but in probability,
that means one minus,
everything but the people that are still there.
And the people that are still there
through all the time steps,
and how many time steps have we taken?
It's l divided by delta t, right?
So this here is number of steps that we've done.
So if l is a million year and delta t is a year
to make it easy, then we have, you know,
a million a year divided by a year,
we have one million times steps, right?
And then for all of those times steps
this one minus l times delta t
is the probability of not leaving.
So all that's left is everything
but the people that have not left in any
of these l divided by delta t times steps.
Cool, cool, cool. Well, what is this?
This is equal to one minus the exponential of minus lL.
And if this feels like it's out of nowhere right now,
I'll invite you to watch a bonus video that I'll be making.
Let me just highlight this.
So there's gonna be a bonus video
on Taylor series and approximation
to explain this.
But really is that when we have a very small probability,
and we know it's small because delta t
is going to zero here, we can rewrite that as...
Well, I don't wanna go through the video right now.
I wanna keep it short.
I've already been talking for a while,
so sorry about that.
It basically involves the fact that delta t
is gonna be really small so that we know this is gonna be
like an exponential distribution, okay,
an exponential function.
So we have this one minus,
one minus exponential of minus small l,
the rate times arbitration, okay?
So that's our cumulative distribution function.
This cumulative distribution function,
it's called cumulative because it's the sum
of the probability of, I'm gonna try and highlight here,
of leaving at zero, leaving it at the first step,
second step, third step.
And you sum them up to ask who has left
at any time before this.
So you're just summing all of the steps, right?
So in continuous really,
this c of l is the integral
of the probability of leaving at a time exactly l,
well this is getting confusing with all the l's.
It's at a time exactly t overall possible time t.
So the probability that I leave,
and that's what I'm calling P at a time exactly l
is the derivative of cL
over dL, right?
So if the cumulative is the sum of all the possibilities
of leaving at all events, it's the integral,
well to reverse that relationship,
the probability distribution function,
P of L, is simply gonna be the derivative.
Well then the derivative of this one minus exponential
of minus l times l is is really simple.
It's simply l e the minus lL.
Well you might have seen those distribution before.
Usually there is simply t or x,
or whatever variable you want.
And this lambda is the rate of these Poisson process.
This is a Poisson distribution, right?
So it all sort of ties together.
We know that the average is one over lambda.
So the average time that it takes to leave
is one over l in this case, right?
So if your rate at which you leave is 0.1 per week,
we know that it's gonna take you on average 10 weeks
to leave the cloning factory, right?
Very mysterious building.
Okay, well, so now we're curious about competing processes.
So what is the probability
of cloning before leaving?
Well, what we're gonna wanna do is integrate
over the probability of leaving at time exactly l.
And then if I know that I'm leaving at time l,
I wanna ask what's the probability that I've cloned myself
in a time less than l.
Well this probability of cloning myself is gonna be
the same cumulative distribution here,
it's gonna be the same as this
but with a rate c instead of l, right?
So it's gonna be one minus e
to the minus cL is the probability that I've cloned myself
before l, so it's this cumulative distribution.
And then I'm just integrating those
over all possible times to l.
All right, I'm running out space here, let me switch.
So very quickly, my probability I was integrating
from zero to infinity, the probability that I leave
at time l which is a Poisson distribution,
I can write that from memory.
And then the probability that I've cloned myself
before this time l you said is one minus e cL.
Okay.
So if you draw on your own calculus days,
you just like split up this integral into two terms.
And now we have this fun sum of rates here,
c plus l when I do the product of my two exponential terms.
Okay?
Well this one is an easy integral
if you remember all your rules.
So that that's simply, you know, this l e minus lL
is really the derivative of the exponential
minus lL and it's minus that, right?
And I'm evaluating it at infinity and at zero.
So from this I'm just gonna get,
it's minus zero minus minus one, so it's one.
Okay, this one is gonna give me l over c plus l.
I'm just taking the integral of the exponential.
And I'm, again, evaluating, this is my notation by the way,
for like evaluating at l equal infinity.
Those are my boundaries on my definite integral.
So this one is just gonna give me,
just gonna give me l over c plus l.
So I have one minus l over c plus l.
I'm just gonna put that on the same denominator,
so I get c plus l minus l over c plus l.
And then I get c over c plus l.
Okay, well you might have a little bit,
you might either be bored, or if you followed it all,
you might have a little bit of a eureka moment here.
Let me, like, erase some of this.
What is this really?
Well, when we took the product here
of two of our Poisson processes,
well, we just learned something here with this one,
is that the rate
of a sum
of Poisson processes
is the sum of the rates.
Well really a sum of Poisson processes is a,
is simply, I mean, is a Poisson process
with the sum of the rates.
Okay, so that's interesting. So now what is this saying?
Is saying that one way to do our previous code
is not to go over everyone and ask,
"Do you leave, do you clone yourself?"
We can combine those two Poisson process
and ask every person, "Do you do something?"
And doing something happens at a rate c plus l.
Okay, and then when they do something,
we can ask, "Well, what's the probability
that this something was a cloning event?"
Well, the probability
of Poisson process,
and I'm kind of gonna call it lambda one.
So a Poisson process which rate lambda one
occurring, and there's probably gonna be a typo here.
I apologize,
before a Poisson process
with a rate lambda two
is simply gonna be lambda one over the sum
of lambda one plus lambda two.
That's what we calculated really.
And this is an incredibly useful result.
I use it, you know, all the time in actual paper.
One way to think about this is, you know,
imagine you're a grad student, you're in Burlington,
your parents are far away, and they all call you.
Let's say that your mom calls you on average once a week
and your dad calls you on average twice a week.
Well that means you get three phone calls a week, right?
So this is this conclusion in blue,
the rate of a sum of Poisson processes
is the sum of the rates.
So if you have one phone call from your mom a week,
two from your dad a week, you get three phone calls a week.
Easy. Now, let's say that right now your phone rings.
What's the probability that it's your dad, right?
Well he calls, like, two out of three calls,
he calls twice a week,
and you get three phone calls a week.
So two out of three calls are from your dad.
So it's this two over two plus one.
It's just this 2/3.
It's not a crazy result, but it's super useful,
because if we go back, let me see,
do I have our old code here?
So this is from the previous video.
Really we could replace this entire block,
we could draw less random numbers here.
Now order doesn't matter 'cause we're fixing that.
We've renormalize our rates,
so we don't have to worry about all of this.
So we have our clone factory and our different rates.
Now, when we go over people, we're just gonna ask,
"Is this person doing something," right?
So I'm drawing one random number.
This person is doing something
at a rate c if it clones itself,
l if it leaves, or c plus l delta t.
That's my sum of Poisson process.
Okay, I have this now.
Now let's say they choose to do something,
well, with probability,
so with probability c over c plus l,
it's a cloning event.
So now, oh, this is a different sheet.
So we say we add N of t as our variable.
I got confused here. Oops.
And ft.
We go over people equal one.
And this doesn't matter that much, but, okay, N of t.
So it's a cloning event, so N of t goes to N of t plus one.
And else, it's a leaving event, right?
So N of t goes to N of t minus one.
And now there's no more problem of which,
are you cloning yourself at the same time as you're leaving?
There's none of this now.
We've sort of fixed this by using this property
that a sum of Poisson processes is a Poisson process,
and then dealing with it with our probabilities
for each event.
So that's it for this video.
There's a lot more tricks that are gonna,
or other problems that are gonna pop up.
But these like are the key things
that you have to think about, order of operation,
of different mechanism and the scale of different rates.
And the key mathematical tricks are
this convergence of Poisson processes
when you discretize with very small steps,
and the fact that you can deal with competing processes
as a sum of Poisson processes.
So that really goes to the like mathematical core
of these compartmental models,
these simple flows of density and all that.
So I really like this.
If you want to talk more about how to implement them
or how to do math around this,
we can use some of our discussion time to go over it.
But otherwise, I'll see you in the next one.