fallbacks.jl 7.01 KB
 John Myles White committed May 27, 2013 1 2 3 4 5 6 ``````############################################################################## # # Fallback methods, usually overridden for specific distributions # ############################################################################## `````` Micky Latowicki committed Apr 20, 2014 7 8 9 10 11 12 13 ``````# array-like characteristics # size(d::Distribution) = size(rand(d)) size(d::UnivariateDistribution) = () size(d::MultivariateDistribution) = (dim(d),) size(d::MatrixDistribution) = (dim(d), dim(d)) # override if the matrix isn't square `````` Dahua Lin committed Jun 18, 2014 14 15 ``````length(d::UnivariateDistribution) = 1 length(d::MultivariateDistribution) = dim(d) `````` Micky Latowicki committed Apr 20, 2014 16 17 ``````length(d::Distribution) = prod(size(d)) `````` 18 19 20 21 22 23 ``````# support handling isbounded(d::UnivariateDistribution) = isupperbounded(d) && islowerbounded(d) hasfinitesupport(d::DiscreteUnivariateDistribution) = isbounded(d) hasfinitesupport(d::ContinuousUnivariateDistribution) = false `````` Dahua Lin committed Sep 21, 2013 24 25 ``````insupport(d::Distribution, x) = false `````` Simon Byrne committed May 12, 2014 26 27 28 29 30 31 32 33 ``````function insupport!{D<:UnivariateDistribution}(r::AbstractArray, d::Union(D,Type{D}), X::AbstractArray) if length(r) != length(X) throw(ArgumentError("Inconsistent array dimensions.")) end for i in 1 : length(X) r[i] = insupport(d, X[i]) end r `````` Dahua Lin committed Sep 21, 2013 34 35 ``````end `````` Simon Byrne committed May 12, 2014 36 ``````insupport{D<:UnivariateDistribution}(d::Union(D,Type{D}), X::AbstractArray) = insupport!(BitArray(size(X)), d, X) `````` Dahua Lin committed Sep 21, 2013 37 `````` `````` Simon Byrne committed May 12, 2014 38 39 40 41 42 43 44 ``````function insupport!{D<:MultivariateDistribution}(r::AbstractArray, d::Union(D,Type{D}), X::AbstractArray) n = div(length(X),size(X,1)) if length(r) != n throw(ArgumentError("Inconsistent array dimensions.")) end for i in 1 : n r[i] = insupport(d, X[:, i]) `````` Dahua Lin committed Sep 21, 2013 45 `````` end `````` Simon Byrne committed May 12, 2014 46 `````` r `````` Dahua Lin committed Sep 21, 2013 47 ``````end `````` Simon Byrne committed May 12, 2014 48 ``````insupport{D<:MultivariateDistribution}(d::Union(D,Type{D}), X::AbstractArray) = insupport!(BitArray(size(X)[2:end]), d, X) `````` Dahua Lin committed Sep 21, 2013 49 `````` `````` Simon Byrne committed May 12, 2014 50 51 52 53 54 55 56 ``````function insupport!{D<:MatrixDistribution}(r::AbstractArray, d::Union(D,Type{D}), X::AbstractArray) n = div(length(X),size(X,1)*size(X,2)) if length(r) != n throw(ArgumentError("Inconsistent array dimensions.")) end for i in 1 : n r[i] = insupport(d, X[:, :, i]) `````` Dahua Lin committed Sep 21, 2013 57 `````` end `````` Simon Byrne committed May 12, 2014 58 `````` r `````` Dahua Lin committed Sep 21, 2013 59 ``````end `````` Simon Byrne committed May 12, 2014 60 ``````insupport{D<:MatrixDistribution}(d::Union(D,Type{D}), X::AbstractArray) = insupport!(BitArray(size(X)[3:end]), d, X) `````` Dahua Lin committed Sep 21, 2013 61 `````` `````` Dahua Lin committed Jul 19, 2013 62 63 64 65 66 ``````# generic function to get number of samples nsamples{D<:UnivariateDistribution}(dt::Type{D}, x::Array) = length(x) nsamples{D<:MultivariateDistribution}(dt::Type{D}, x::Matrix) = size(x, 2) nsamples{D<:MatrixDistribution,T}(dt::Type{D}, x::Array{Matrix{T}}) = length(x) `````` Dahua Lin committed Jul 13, 2013 67 68 `````` #### Statistics #### `````` Simon Byrne committed Jul 20, 2013 69 ``````mean(d::Distribution) = throw(MethodError(mean,(d,))) `````` Dahua Lin committed Jul 13, 2013 70 71 72 73 74 75 76 77 78 79 ``````std(d::Distribution) = sqrt(var(d)) # What's the purpose for this function? function var{M <: Real}(d::UnivariateDistribution, mu::AbstractArray{M}) V = similar(mu, Float64) for i in 1:length(mu) V[i] = var(d, mu[i]) end return V end `````` John Myles White committed May 27, 2013 80 `````` `````` John Myles White committed May 27, 2013 81 ``````function cor(d::MultivariateDistribution) `````` Dahua Lin committed Jul 17, 2013 82 `````` R = cov(d) `````` John Myles White committed May 27, 2013 83 84 85 86 87 88 89 90 91 `````` m, n = size(R) for j in 1:n for i in 1:n R[i, j] = d.cov[i, j] / sqrt(d.cov[i, i] * d.cov[j, j]) end end return R end `````` Dahua Lin committed Jul 13, 2013 92 ``````binaryentropy(d::Distribution) = entropy(d) / log(2) `````` John Myles White committed May 27, 2013 93 `````` `````` Dahua Lin committed Jul 13, 2013 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 ``````# kurtosis returns excess kurtosis by default # proper kurtosis can be returned with correction = false function kurtosis(d::Distribution, correction::Bool) if correction return kurtosis(d) else return kurtosis(d) + 3.0 end end excess(d::Distribution) = kurtosis(d) excess_kurtosis(d::Distribution) = kurtosis(d) proper_kurtosis(d::Distribution) = kurtosis(d, false) isplatykurtic(d::Distribution) = kurtosis(d) > 0.0 isleptokurtic(d::Distribution) = kurtosis(d) < 0.0 ismesokurtic(d::Distribution) = kurtosis(d) == 0.0 `````` Dahua Lin committed Jul 14, 2013 112 ``````median(d::UnivariateDistribution) = quantile(d, 0.5) `````` John Myles White committed May 27, 2013 113 `````` `````` 114 115 ``````modes(d::Distribution) = [mode(d)] `````` Dahua Lin committed Jul 13, 2013 116 117 118 119 120 121 ``````#### pdf, cdf, and quantile #### logpdf(d::UnivariateDistribution, x::Real) = log(pdf(d, x)) pdf(d::MultivariateDistribution, x::Vector) = exp(logpdf(d, x)) ccdf(d::UnivariateDistribution, q::Real) = 1.0 - cdf(d, q) `````` John Myles White committed May 27, 2013 122 123 ``````cquantile(d::UnivariateDistribution, p::Real) = quantile(d, 1.0 - p) `````` Dahua Lin committed Jul 13, 2013 124 125 126 127 128 129 130 ``````logcdf(d::Distribution, q::Real) = log(cdf(d,q)) logccdf(d::Distribution, q::Real) = log(ccdf(d,q)) invlogccdf(d::Distribution, lp::Real) = quantile(d, -expm1(lp)) invlogcdf(d::Distribution, lp::Real) = quantile(d, exp(lp)) #### log likelihood #### `````` John Myles White committed May 27, 2013 131 `````` `````` John Myles White committed May 31, 2013 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 ``````function loglikelihood(d::UnivariateDistribution, X::Array) ll = 0.0 for i in 1:length(X) ll += logpdf(d, X[i]) end return ll end function loglikelihood(d::MultivariateDistribution, X::Matrix) ll = 0.0 for i in 1:size(X, 2) ll += logpdf(d, X[:, i]) end return ll end `````` John Myles White committed May 27, 2013 148 `````` `````` Dahua Lin committed Jul 13, 2013 149 ``````#### Vectorized functions for univariate distributions #### `````` John Myles White committed May 27, 2013 150 `````` `````` Dahua Lin committed Jul 13, 2013 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 ``````for fun in [:pdf, :logpdf, :cdf, :logcdf, :ccdf, :logccdf, :invlogcdf, :invlogccdf, :quantile, :cquantile] fun! = symbol(string(fun, '!')) @eval begin function (\$fun!)(r::AbstractArray, d::UnivariateDistribution, X::AbstractArray) if length(r) != length(X) throw(ArgumentError("Inconsistent array dimensions.")) end for i in 1 : length(X) r[i] = (\$fun)(d, X[i]) end r end function (\$fun)(d::UnivariateDistribution, X::AbstractArray) (\$fun!)(Array(Float64, size(X)), d, X) end `````` John Myles White committed May 27, 2013 168 169 170 `````` end end `````` Dahua Lin committed Jul 13, 2013 171 ``````#### Vectorized functions for multivariate distributions #### `````` John Myles White committed May 27, 2013 172 `````` `````` John Myles White committed May 27, 2013 173 174 175 176 177 ``````function logpdf!(r::AbstractArray, d::MultivariateDistribution, x::AbstractMatrix) n::Int = size(x, 2) if length(r) != n throw(ArgumentError("Inconsistent array dimensions.")) end `````` Dahua Lin committed Jul 13, 2013 178 `````` for i in 1 : n `````` John Myles White committed May 27, 2013 179 180 `````` r[i] = logpdf(d, x[:, i]) end `````` Dahua Lin committed Jul 13, 2013 181 `````` r `````` John Myles White committed May 27, 2013 182 183 ``````end `````` Dahua Lin committed Jul 13, 2013 184 185 ``````function logpdf(d::MultivariateDistribution, X::AbstractMatrix) logpdf!(Array(Float64, size(X, 2)), d, X) `````` John Myles White committed May 27, 2013 186 187 ``````end `````` Dahua Lin committed Jul 13, 2013 188 189 190 191 192 193 194 ``````function pdf!(r::AbstractArray, d::MultivariateDistribution, X::AbstractMatrix) logpdf!(r, d, X) # size checking is done by logpdf! for i in 1 : size(X, 2) r[i] = exp(r[i]) end r end `````` John Myles White committed May 27, 2013 195 `````` `````` John Myles White committed May 27, 2013 196 ``````function pdf(d::MultivariateDistribution, X::AbstractMatrix) `````` Dahua Lin committed Jul 13, 2013 197 `````` pdf!(Array(Float64, size(X, 2)), d, X) `````` John Myles White committed May 27, 2013 198 199 200 ``````end `````` Dahua Lin committed Jul 13, 2013 201 202 203 204 205 206 207 208 ``````#### logpmf & pmf for discrete distributions #### logpmf(d::DiscreteDistribution, args::Any...) = logpdf(d, args...) logpmf!(r::AbstractArray, d::DiscreteDistribution, args::Any...) = logpdf!(r, d, args...) pmf(d::DiscreteDistribution, args::Any...) = pdf(d, args...) #### Sampling: rand & rand! #### `````` John Myles White committed Jun 05, 2013 209 `````` `````` Simon Byrne committed Apr 04, 2014 210 211 212 ``````# default: inverse transform sampling rand(d::UnivariateDistribution) = quantile(d, rand()) `````` John Myles White committed May 27, 2013 213 214 215 216 ``````function sprand(m::Integer, n::Integer, density::Real, d::Distribution) return sprand(m, n, density, n -> rand(d, n)) end `````` Dahua Lin committed Jul 20, 2013 217 `````` `````` Dahua Lin committed Jul 12, 2013 218 219 ``````# Fitting `````` Dahua Lin committed Jul 21, 2013 220 221 222 223 224 ``````function suffstats{D<:Distribution}(dt::Type{D}, xs...) argtypes = tuple(D, map(typeof, xs)...) error("suffstats is not implemented for \$argtypes.") end `````` Dahua Lin committed Jul 20, 2013 225 226 227 228 229 230 ``````fit_mle{D<:UnivariateDistribution}(dt::Type{D}, x::Array) = fit_mle(D, suffstats(D, x)) fit_mle{D<:UnivariateDistribution}(dt::Type{D}, x::Array, w::Array) = fit_mle(D, suffstats(D, x, w)) fit_mle{D<:MultivariateDistribution}(dt::Type{D}, x::Matrix) = fit_mle(D, suffstats(D, x)) fit_mle{D<:MultivariateDistribution}(dt::Type{D}, x::Matrix, w::Array) = fit_mle(D, suffstats(D, x, w)) `````` Dahua Lin committed Sep 14, 2013 231 232 ``````fit{D<:Distribution}(dt::Type{D}, x) = fit_mle(D, x) fit{D<:Distribution}(dt::Type{D}, args...) = fit_mle(D, args...) `````` Dahua Lin committed Jul 12, 2013 233