-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathgeometric.jl
167 lines (123 loc) · 4.25 KB
/
geometric.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
Geometric(p)
A *Geometric distribution* characterizes the number of failures before the first success in a sequence of independent Bernoulli trials with success rate `p`.
```math
P(X = k) = p (1 - p)^k, \\quad \\text{for } k = 0, 1, 2, \\ldots.
```
```julia
Geometric() # Geometric distribution with success rate 0.5
Geometric(p) # Geometric distribution with success rate p
params(d) # Get the parameters, i.e. (p,)
succprob(d) # Get the success rate, i.e. p
failprob(d) # Get the failure rate, i.e. 1 - p
```
External links
* [Geometric distribution on Wikipedia](http://en.wikipedia.org/wiki/Geometric_distribution)
"""
struct Geometric{T<:Real} <: DiscreteUnivariateDistribution
p::T
function Geometric{T}(p::T) where {T <: Real}
new{T}(p)
end
end
function Geometric(p::Real; check_args::Bool=true)
@check_args Geometric (p, zero(p) < p < one(p))
return Geometric{typeof(p)}(p)
end
Geometric() = Geometric{Float64}(0.5)
@distr_support Geometric 0 Inf
### Conversions
convert(::Type{Geometric{T}}, p::Real) where {T<:Real} = Geometric(T(p))
Base.convert(::Type{Geometric{T}}, d::Geometric) where {T<:Real} = Geometric{T}(T(d.p))
Base.convert(::Type{Geometric{T}}, d::Geometric{T}) where {T<:Real} = d
### Parameters
succprob(d::Geometric) = d.p
failprob(d::Geometric) = 1 - d.p
params(d::Geometric) = (d.p,)
partype(::Geometric{T}) where {T<:Real} = T
### Statistics
mean(d::Geometric) = failprob(d) / succprob(d)
median(d::Geometric) = -fld(logtwo, log1p(-d.p)) - 1
mode(d::Geometric{T}) where {T<:Real} = zero(T)
var(d::Geometric) = (1 - d.p) / abs2(d.p)
skewness(d::Geometric) = (2 - d.p) / sqrt(1 - d.p)
kurtosis(d::Geometric) = 6 + abs2(d.p) / (1 - d.p)
entropy(d::Geometric) = (-xlogx(succprob(d)) - xlogx(failprob(d))) / d.p
function kldivergence(p::Geometric, q::Geometric)
x = succprob(p)
y = succprob(q)
if x == y
return zero(float(x / y))
elseif isone(x)
return -log(y / x)
else
return log(x) - log(y) + (inv(x) - one(x)) * (log1p(-x) - log1p(-y))
end
end
### Evaluations
function logpdf(d::Geometric, x::Real)
insupport(d, x) ? log(d.p) + log1p(-d.p) * x : log(zero(d.p))
end
function cdf(d::Geometric, x::Int)
p = succprob(d)
n = max(x + 1, 0)
p < 1/2 ? -expm1(log1p(-p)*n) : 1 - (1 - p)^n
end
ccdf(d::Geometric, x::Real) = ccdf_int(d, x)
function ccdf(d::Geometric, x::Int)
p = succprob(d)
n = max(x + 1, 0)
p < 1/2 ? exp(log1p(-p)*n) : (1 - p)^n
end
logcdf(d::Geometric, x::Real) = logcdf_int(d, x)
logcdf(d::Geometric, x::Int) = log1mexp(log1p(-d.p) * max(x + 1, 0))
logccdf(d::Geometric, x::Real) = logccdf_int(d, x)
logccdf(d::Geometric, x::Int) = log1p(-d.p) * max(x + 1, 0)
quantile(d::Geometric, p::Real) = invlogccdf(d, log1p(-p))
cquantile(d::Geometric, p::Real) = invlogccdf(d, log(p))
invlogcdf(d::Geometric, lp::Real) = invlogccdf(d, log1mexp(lp))
function invlogccdf(d::Geometric{T}, lp::Real) where T<:Real
if (lp > zero(d.p)) || isnan(lp)
return T(NaN)
elseif isinf(lp)
return T(Inf)
elseif lp == zero(d.p)
return zero(T)
end
max(ceil(lp/log1p(-d.p)) - 1, zero(T))
end
function laplace_transform(d::Geometric, t)
p = succprob(d)
p / (p - (1 - p) * expm1(-t))
end
mgf(d::Geometric, t::Real) = laplace_transform(d, -t)
function cgf(d::Geometric, t)
p = succprob(d)
# log(p / (1 - (1-p) * exp(t)))
log(p) - log1mexp(t + log1p(-p))
end
cf(d::Geometric, t::Real) = laplace_transform(d, -t*im)
### Sampling
rand(rng::AbstractRNG, d::Geometric) = floor(Int,-randexp(rng) / log1p(-d.p))
### Model Fitting
struct GeometricStats <: SufficientStats
sx::Float64
tw::Float64
GeometricStats(sx::Real, tw::Real) = new(sx, tw)
end
suffstats(::Type{<:Geometric}, x::AbstractArray{T}) where {T<:Integer} = GeometricStats(sum(x), length(x))
function suffstats(::Type{<:Geometric}, x::AbstractArray{T}, w::AbstractArray{Float64}) where T<:Integer
n = length(x)
if length(w) != n
throw(DimensionMismatch("Inconsistent argument dimensions."))
end
sx = 0.
tw = 0.
for i = 1:n
wi = w[i]
sx += wi * x[i]
tw += wi
end
GeometricStats(sx, tw)
end
fit_mle(::Type{<:Geometric}, ss::GeometricStats) = Geometric(1 / (ss.sx / ss.tw + 1))