In [1]:
using Plots
In [2]:
λ = 2.0

f(t) = λ * exp(-λ * t)
Out[2]:
f (generic function with 1 method)
In [5]:
plot(f, 0, 5)
Out[5]:
In [8]:
plot(0:0.1:5, f, m=:o)
Out[8]:
In [11]:
r = 0:0.1:5

plot(r, f.(r), lw=3)
Out[11]:

Rate: Probability per unit time

Probability to decay in time $\delta t$ is $\lambda \, \delta t$

In [31]:
g = t -> exp(-t)  # anonymous function
# "the function which maps t to exp(-t)
# maths:  t ↦ exp(-t)

plot(0:0.1:5, g)
plot!(2:0.1:3, g, fill=true, alpha=0.2)
Out[31]:

How generate numerically exponential random variate

In [22]:
rand()  # uniform distribution in [0, 1]
Out[22]:
0.3929231826863011
In [32]:
function uniform_pdf(x)
    if 0  x  1
        return 1.0
    else
        return 0.0
    end
end
Out[32]:
uniform_pdf (generic function with 1 method)
In [34]:
plot(-2:0.02:2, uniform_pdf, ratio=1)
Out[34]:
In [35]:
function uniform_pdf(x, a, b)
    if a  x  b
        return 1 / (b - a)
    else
        return 0.0
    end
end
Out[35]:
uniform_pdf (generic function with 2 methods)
In [36]:
a = -0.5
b = 1.5

plot(-2:0.02:2, x -> uniform_pdf(x, a, b), ratio=1)
Out[36]:
In [37]:
rand()
Out[37]:
0.8021562956807884
In [48]:
2 * rand()
Out[48]:
1.8620315965058123
In [50]:
scatter(2 .* rand(1000) .- 1)
Out[50]:
In [51]:
"Generate a random variate which has a unif distn on [a, b]"
uniform(a, b) = a + rand() * (b - a)
Out[51]:
uniform
In [52]:
scatter([uniform(-0.5, 1.5) for i in 1:1000])
Out[52]:
In [56]:
data = [uniform(-0.5, 1.5) for i in 1:100];
In [63]:
histogram(data, nbins=20)  # function in Plots.jl
scatter!(data, zero.(data), ms=2, alpha=0.3)
Out[63]:
In [64]:
data = [uniform(-0.5, 1.5) for i in 1:1000] 
histogram(data, nbins=20)  # function in Plots.jl
scatter!(data, zero.(data), ms=2, alpha=0.3)
Out[64]:
In [66]:
data = [uniform(-0.5, 1.5) for i in 1:100000] 
histogram(data, nbins=20)  # function in Plots.jl
# scatter!(data, zero.(data), ms=2, alpha=0.3)
Out[66]:
In [67]:
data = [uniform(-0.5, 1.5) for i in 1:100] 
histogram(data, nbins=20)  # function in Plots.jl
scatter!(data, zero.(data), ms=2, alpha=0.3)
Out[67]:
In [78]:
N = 100
data = [uniform(-0.5, 1.5) for i in 1:N] 
plot(sort(data), 1/N:1/N:1)  # empirical cumulative distribution function

plot!([-0.5, 1.5], [0, 1])
Out[78]:

Kolmogorov--Smirnov test

In [81]:
N = 100000
data = [uniform(-0.5, 1.5) for i in 1:N] 
plot(sort(data), 1/N:1/N:1)  # empirical cumulative distribution function

plot!([-0.5, 1.5], [0, 1])
Out[81]: