forked from JuliaStats/Distributions.jl
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniform.jl
186 lines (138 loc) · 4.84 KB
/
uniform.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
Uniform(a,b)
The *continuous uniform distribution* over an interval ``[a, b]`` has probability density function
```math
f(x; a, b) = \\frac{1}{b - a}, \\quad a \\le x \\le b
```
```julia
Uniform() # Uniform distribution over [0, 1] - Float64
Uniform(a, b) # Uniform distribution over [a, b] - Float64
Uniform{T}(a, b) # Uniform distribution over [a, b] - with T type
Uniform(T, a, b) # Uniform distribution over [a, b] - with T type
params(d) # Get the parameters, i.e. (a, b)
minimum(d) # Get the lower bound, i.e. a
maximum(d) # Get the upper bound, i.e. b
location(d) # Get the location parameter, i.e. a
scale(d) # Get the scale parameter, i.e. b - a
```
External links
* [Uniform distribution (continuous) on Wikipedia](http://en.wikipedia.org/wiki/Uniform_distribution_(continuous))
"""
struct Uniform{T} <: ContinuousUnivariateDistribution
a::T
b::T
# Uniform{T}(a::T, b::T) constructor
function Uniform{T}(a::T, b::T; check_args=true) where {T <: Real}
check_args && @check_args(Uniform, a < b)
return new{T}(a, b)
end
# Abstract a,b - Uniform{T}(a, b) constructor
function Uniform{T}(a::Real, b::Real; check_args=true) where {T <: Real}
check_args && @check_args(Uniform, a < b)
return new{T}(T(a), T(b))
end
# Uniform{T}(a::T, b::T) constructor
function Uniform{T}(a::T, b::T) where {T <:Complex}
new{T}(a, b)
end
# Abstract a,b - Uniform{T}(a, b) constructor
function Uniform{T}(a, b) where {T <: Complex}
return new{T}(T(a), T(b))
end
end
# Real
# zeros() like constructor (Uniform(T, a::T, b::T))
function Uniform(::Type{T}, a::T, b::T; check_args=true) where {T <: Real}
return Uniform{T}(a, b, check_args = check_args)
end
# Abstract a,b - zeros() like constructor (Uniform(T, a, b))
function Uniform(::Type{T}, a, b; check_args=true) where {T <: Real}
return Uniform{T}(T(a), T(b), check_args = check_args)
end
# No type specified constructor:
function Uniform(a::Float64, b::Float64; check_args=true)
return Uniform{Float64}(a, b, check_args = check_args)
end
# Abstract a,b - no type specified constructor:
function Uniform(a, b; check_args=true)
return Uniform{Float64}(Float64(a), Float64(b), check_args = check_args)
end
# Complex
# zeros() like constructor (Uniform(T, a::T, b::T))
function Uniform(::Type{T}, a::T, b::T) where {T <: Complex}
return Uniform{T}(a, b)
end
# Abstract a,b - zeros() like constructor (Uniform(T, a, b))
function Uniform(::Type{T}, a, b) where {T <: Complex}
return Uniform{T}(T(a), T(b))
end
# No type specified constructor:
function Uniform(a::ComplexF64, b::ComplexF64)
return Uniform{ComplexF64}(a, b)
end
Uniform() = Uniform(0.0, 1.0, check_args=false)
@distr_support Uniform d.a d.b
#### Conversions
convert(::Type{Uniform{T}}, a::Real, b::Real) where {T<:Real} = Uniform(T, a, b)
convert(::Type{Uniform{T}}, d::Uniform{S}) where {T<:Real, S<:Real} = Uniform(T, d.a, d.b, check_args=false)
#### Parameters
params(d::Uniform) = (d.a, d.b)
partype(::Uniform{T}) where {T<:Real} = T
location(d::Uniform) = d.a
scale(d::Uniform) = d.b - d.a
#### Statistics
mean(d::Uniform) = middle(d.a, d.b)
median(d::Uniform) = mean(d)
mode(d::Uniform) = mean(d)
modes(d::Uniform) = Float64[]
var(d::Uniform) = (w = d.b - d.a; w^2 / 12)
skewness(d::Uniform{T}) where {T<:Real} = zero(T)
kurtosis(d::Uniform{T}) where {T<:Real} = -6/5*one(T)
entropy(d::Uniform) = log(d.b - d.a)
#### Evaluation
pdf(d::Uniform{T}, x::Real) where {T<:Real} = insupport(d, x) ? 1 / (d.b - d.a) : zero(T)
logpdf(d::Uniform{T}, x::Real) where {T<:Real} = insupport(d, x) ? -log(d.b - d.a) : -T(Inf)
function cdf(d::Uniform{T}, x::Real) where T<:Real
(a, b) = params(d)
x <= a ? zero(T) :
x >= d.b ? one(T) : (x - a) / (b - a)
end
function ccdf(d::Uniform{T}, x::Real) where T<:Real
(a, b) = params(d)
x <= a ? one(T) :
x >= d.b ? zero(T) : (b - x) / (b - a)
end
quantile(d::Uniform, p::Real) = d.a + p * (d.b - d.a)
cquantile(d::Uniform, p::Real) = d.b + p * (d.a - d.b)
function mgf(d::Uniform, t::Real)
(a, b) = params(d)
u = (b - a) * t / 2
u == zero(u) && return one(u)
v = (a + b) * t / 2
exp(v) * (sinh(u) / u)
end
function cf(d::Uniform, t::Real)
(a, b) = params(d)
u = (b - a) * t / 2
u == zero(u) && return complex(one(u))
v = (a + b) * t / 2
cis(v) * (sin(u) / u)
end
#### Sampling
rand(rng::AbstractRNG, d::Uniform{T}) where {T} = d.a + (d.b - d.a) * convert(T,rand(rng))
#### Fitting
function fit_mle(::Type{<:Uniform}, x::AbstractArray{T}) where T<:Real
if isempty(x)
throw(ArgumentError("x cannot be empty."))
end
xmin = xmax = x[1]
for i = 2:length(x)
xi = x[i]
if xi < xmin
xmin = xi
elseif xi > xmax
xmax = xi
end
end
Uniform(xmin, xmax)
end