## Cumulative distribution of some geometric random variables¶

Probability that we have recovered at or before time $N$

In :
"""
plot_cumulative!(p, N, δ=1)

- p is prob to decay at each time step
- N is total number of time steps
- δ is time between steps

Prob mass function is f_X(n) = p (1 - p)^{n-1}
"""
function plot_cumulative!(p, N, δ=1; label="")
ps = [p * (1 - p)^(n-1) for n in 1:N]
cumulative = cumsum(ps)

ys = [0; reduce(vcat, [ [cumulative[n], cumulative[n]] for n in 1:N ])]

pop!(ys)
pushfirst!(ys, 0)

xs = [0; reduce(vcat, [ [n*δ, n*δ] for n in 1:N ])];

plot!(xs, ys, label=label)
scatter!([n*δ for n in 1:N], cumulative, label="")
end

Out:
plot_cumulative!
In :
?plot_cumulative!

search: plot_cumulative!


Out:
plot_cumulative!(p, N, δ=1)
• p is prob to decay at each time step
• N is total number of time steps
• δ is time between steps

Prob mass function is $f_X(n) = p (1 - p)^{n-1}$

In :
using Plots

In :
plot_cumulative!(0.1, 20)

xlabel!("n")
ylabel!("cum. prob. <= n")

Out:
In :
p = 0.1
N = 50
δ = 0.4

plot()
plot_cumulative!(p, N, 1.0, label="delta = 1")
plot_cumulative!(p, N, δ, label="delta = $(δ)") #$a inserts value of variable a

Out:
In :
using Interact


In :
p = 0.1
N = 100
δ = 0.5

@manipulate for p2 in 0.01:0.01:1.0
plot()
plot_cumulative!(p, 40, 1.0, label="delta = 1")
plot_cumulative!(p2, N, δ, label="delta = $(δ)") #$a inserts value of variable a

xlims!(0, 40)
ylims!(0, 1)
end

Out:

Cumulative distribution function for geometric random variable $X \sim \text{Geom}(p)$

$$F_X(n) = 1 - (1 - p)^n$$

$F_{Y_\delta}(n) = 1 - [1 - p(\delta)]^n$

Fix time $t = n$. Find $p(\delta)$ such that probabilities are the same at time $t$

$t = n \delta$

So $n = t / \delta$

$$1 - [1 - p(\delta)]^{t/\delta} = 1 - (1 - p)^t$$

Exercise: Solve for $p(\delta)$

## Continuous limit¶

Limit $\delta \to 0$

Need $p(\delta) \to 0$

If $x$ is small, $1 - x \simeq \exp(-x)$

$\exp(x) = 1 + x + \frac{1}{2} x^2 + \frac{1}{3!} x^3 + \cdots + \frac{1}{n!} x^n + \cdots$

$\exp(x) = \sum_{n=0}^\infty \frac{1}{n!} x^n$

Define $\exp{}$ as the function which is equal to its derivative and suppose that I can write as a power series in $x$

In :
x = 0.01

Out:
0.01
In :
1 - x

Out:
0.99
In :
exp(-x)

Out:
0.9900498337491681
In :
x^2 / 2

Out:
5.0e-5
$$1 −[1−𝑝(𝛿)]^{𝑡/𝛿} \simeq 1 - \{ \exp[-p(\delta)] \}^{t/\delta}$$
$$1 − \exp \left[ −𝑝(𝛿) \frac{𝑡}{𝛿} \right]$$

Need $p(\delta) / \delta \to \lambda$ when $\delta \to 0$

End result: $F(t) = 1 - \exp[-\lambda t]$

$\lambda$ is rate -- probability per unit time

$1 - (1 - p)^t$

Choose $\lambda$ such that $1 - p = \exp(- \lambda)$

In :
plot()

p = 0.1
λ = -log(1 - p)
plot_cumulative!(p, 50)

plot!(0:0.01:20, t -> 1 - exp(-λ*t), lw=3)

Out:

Exponential random variable $Z$

$F_Z(t) = \mathbb{P}(Z \le t)$

In [ ]: