From ab23126d5e141acbfe4a38e98b0ef0d4a69bcfe3 Mon Sep 17 00:00:00 2001 From: Andreas Noack Date: Wed, 11 May 2016 16:29:22 -0400 Subject: [PATCH] Prepare for 0.5. No functors. Deprecated symbols. New getfield syntax. Fix some ambiguities. --- REQUIRE | 1 + src/Distributions.jl | 1 + src/common.jl | 4 ++-- src/multivariate/mvnormal.jl | 1 - src/multivariate/mvnormalcanon.jl | 8 ++++---- src/multivariate/mvtdist.jl | 2 +- src/multivariate/vonmisesfisher.jl | 5 ++++- src/show.jl | 2 +- src/univariates.jl | 24 ++++++++++++------------ test/categorical.jl | 2 +- test/mixture.jl | 2 +- test/mvnormal.jl | 2 +- test/truncate.jl | 10 +++++++--- test/univariates.jl | 4 ++-- 14 files changed, 38 insertions(+), 30 deletions(-) diff --git a/REQUIRE b/REQUIRE index 302ebd58d..63e618ab9 100644 --- a/REQUIRE +++ b/REQUIRE @@ -2,3 +2,4 @@ julia 0.4 PDMats 0.4.1 0.5 StatsFuns 0.1.1 StatsBase 0.7.0 +Compat 0.2.17 diff --git a/src/Distributions.jl b/src/Distributions.jl index 229d91c61..bd990e877 100644 --- a/src/Distributions.jl +++ b/src/Distributions.jl @@ -5,6 +5,7 @@ module Distributions using PDMats using StatsFuns using StatsBase +using Compat import Base.Random import Base: size, eltype, length, full, convert, show, getindex, scale, scale!, rand, rand! diff --git a/src/common.jl b/src/common.jl index 9792b7e2d..dfcfe5d7b 100644 --- a/src/common.jl +++ b/src/common.jl @@ -54,10 +54,10 @@ typealias DiscreteMatrixDistribution Distribution{Matrixvariate, Discret typealias ContinuousMatrixDistribution Distribution{Matrixvariate, Continuous} variate_form{VF<:VariateForm,VS<:ValueSupport}(::Type{Distribution{VF,VS}}) = VF -variate_form{T<:Distribution}(::Type{T}) = variate_form(super(T)) +variate_form{T<:Distribution}(::Type{T}) = variate_form(supertype(T)) value_support{VF<:VariateForm,VS<:ValueSupport}(::Type{Distribution{VF,VS}}) = VS -value_support{T<:Distribution}(::Type{T}) = value_support(super(T)) +value_support{T<:Distribution}(::Type{T}) = value_support(supertype(T)) ## TODO: the following types need to be improved abstract SufficientStats diff --git a/src/multivariate/mvnormal.jl b/src/multivariate/mvnormal.jl index 806abd7cf..d800fffac 100644 --- a/src/multivariate/mvnormal.jl +++ b/src/multivariate/mvnormal.jl @@ -148,7 +148,6 @@ immutable MvNormalKnownCov{Cov<:AbstractPDMat} Σ::Cov end -MvNormalKnownCov{Cov<:AbstractPDMat}(C::Cov) = MvNormalKnownCov{Cov}(C) MvNormalKnownCov(d::Int, σ::Real) = MvNormalKnownCov(ScalMat(d, abs2(Float64(σ)))) MvNormalKnownCov(σ::Vector{Float64}) = MvNormalKnownCov(PDiagMat(abs2(σ))) MvNormalKnownCov(Σ::Matrix{Float64}) = MvNormalKnownCov(PDMat(Σ)) diff --git a/src/multivariate/mvnormalcanon.jl b/src/multivariate/mvnormalcanon.jl index 69ca3b927..4cdfce430 100644 --- a/src/multivariate/mvnormalcanon.jl +++ b/src/multivariate/mvnormalcanon.jl @@ -34,9 +34,9 @@ function MvNormalCanon{P<:AbstractPDMat, T<:Real}(h::Vector{T}, J::P) MvNormalCanon{P,Vector{T}}(J \ h, h, J) end -MvNormalCanon(h::Vector, J::Matrix) = MvNormalCanon(h, PDMat(J)) -MvNormalCanon(h::Vector, prec::Vector) = MvNormalCanon(h, PDiagMat(prec)) -MvNormalCanon(h::Vector, prec) = MvNormalCanon(h, ScalMat(length(h), prec)) +MvNormalCanon{T<:Real}(h::Vector{T}, J::Matrix) = MvNormalCanon(h, PDMat(J)) +MvNormalCanon{T<:Real}(h::Vector{T}, prec::Vector) = MvNormalCanon(h, PDiagMat(prec)) +MvNormalCanon{T<:Real}(h::Vector{T}, prec) = MvNormalCanon(h, ScalMat(length(h), prec)) MvNormalCanon(J::Matrix) = MvNormalCanon(PDMat(J)) MvNormalCanon(prec::Vector) = MvNormalCanon(PDiagMat(prec)) @@ -85,4 +85,4 @@ unwhiten_winv!(J::AbstractPDMat, x::AbstractVecOrMat) = unwhiten!(inv(J), x) unwhiten_winv!(J::PDiagMat, x::AbstractVecOrMat) = whiten!(J, x) unwhiten_winv!(J::ScalMat, x::AbstractVecOrMat) = whiten!(J, x) -_rand!(d::MvNormalCanon, x::AbstractVecOrMat) = add!(unwhiten_winv!(d.J, randn!(x)), d.μ) +_rand!(d::MvNormalCanon, x::AbstractMatrix) = add!(unwhiten_winv!(d.J, randn!(x)), d.μ) diff --git a/src/multivariate/mvtdist.jl b/src/multivariate/mvtdist.jl index 1731af8bf..7b1e03f2f 100644 --- a/src/multivariate/mvtdist.jl +++ b/src/multivariate/mvtdist.jl @@ -133,7 +133,7 @@ function _logpdf!{T<:Real}(r::AbstractArray, d::AbstractMvTDist, x::AbstractMatr return r end -_pdf!{T<:Real}(r::AbstractArray, d::AbstractMvNormal, x::AbstractMatrix{T}) = exp!(_logpdf!(r, d, x)) +_pdf!{T<:Real}(r::AbstractArray, d::AbstractMvTDist, x::AbstractMatrix{T}) = exp!(_logpdf!(r, d, x)) function gradlogpdf{T<:Real}(d::GenericMvTDist, x::AbstractVector{T}) z::Vector{T} = d.zeromean ? x : x - d.μ diff --git a/src/multivariate/vonmisesfisher.jl b/src/multivariate/vonmisesfisher.jl index e375ddb86..e9374faad 100644 --- a/src/multivariate/vonmisesfisher.jl +++ b/src/multivariate/vonmisesfisher.jl @@ -28,7 +28,10 @@ end VonMisesFisher{T<:Real}(μ::Vector{T}, κ::Real) = VonMisesFisher(Float64(μ), Float64(κ)) -VonMisesFisher(θ::Vector{Float64}) = (κ = vecnorm(θ); VonMisesFisher(scale(θ, 1.0 / κ), κ)) +function VonMisesFisher(θ::Vector{Float64}) + κ = vecnorm(θ) + return VonMisesFisher(θ * (1 / κ), κ) +end VonMisesFisher{T<:Real}(θ::Vector{T}) = VonMisesFisher(Float64(θ)) show(io::IO, d::VonMisesFisher) = show(io, d, (:μ, :κ)) diff --git a/src/show.jl b/src/show.jl index bec50a4f5..8f94319c3 100644 --- a/src/show.jl +++ b/src/show.jl @@ -32,7 +32,7 @@ function _use_multline_show(d::Distribution, pnames) multline = false tlen = 0 for (i, p) in enumerate(pnames) - pv = d.(p) + pv = getfield(d, p) if !(isa(pv, Number) || isa(pv, NTuple) || isa(pv, AbstractVector)) multline = true else diff --git a/src/univariates.jl b/src/univariates.jl index c6c5a1190..1c86a5ace 100644 --- a/src/univariates.jl +++ b/src/univariates.jl @@ -185,8 +185,8 @@ for fun in [:pdf, :logpdf, :invlogcdf, :invlogccdf, :quantile, :cquantile] - _fun! = symbol(string('_', fun, '!')) - fun! = symbol(string(fun, '!')) + _fun! = Symbol('_', fun, '!') + fun! = Symbol(fun, '!') @eval begin function ($_fun!)(r::AbstractArray, d::UnivariateDistribution, X::AbstractArray) @@ -302,16 +302,16 @@ macro _delegate_statsfuns(D, fpre, psyms...) T = dt <: DiscreteUnivariateDistribution ? :Int : :Real # function names from StatsFuns - fpdf = symbol(string(fpre, "pdf")) - flogpdf = symbol(string(fpre, "logpdf")) - fcdf = symbol(string(fpre, "cdf")) - fccdf = symbol(string(fpre, "ccdf")) - flogcdf = symbol(string(fpre, "logcdf")) - flogccdf = symbol(string(fpre, "logccdf")) - finvcdf = symbol(string(fpre, "invcdf")) - finvccdf = symbol(string(fpre, "invccdf")) - finvlogcdf = symbol(string(fpre, "invlogcdf")) - finvlogccdf = symbol(string(fpre, "invlogccdf")) + fpdf = Symbol(fpre, "pdf") + flogpdf = Symbol(fpre, "logpdf") + fcdf = Symbol(fpre, "cdf") + fccdf = Symbol(fpre, "ccdf") + flogcdf = Symbol(fpre, "logcdf") + flogccdf = Symbol(fpre, "logccdf") + finvcdf = Symbol(fpre, "invcdf") + finvccdf = Symbol(fpre, "invccdf") + finvlogcdf = Symbol(fpre, "invlogcdf") + finvlogccdf = Symbol(fpre, "invlogccdf") # parameter fields pargs = [Expr(:(.), :d, Expr(:quote, s)) for s in psyms] diff --git a/test/categorical.jl b/test/categorical.jl index 9b068bc40..a451c63ab 100644 --- a/test/categorical.jl +++ b/test/categorical.jl @@ -45,5 +45,5 @@ d = Categorical(4) @test maximum(d) == 4 @test probs(d) == [0.25, 0.25, 0.25, 0.25] -p = scale(ones(10^6), 1.0e-6) +p = ones(10^6) * 1.0e-6 @test Distributions.isprobvec(p) diff --git a/test/mixture.jl b/test/mixture.jl index 389463045..8fe54c93b 100644 --- a/test/mixture.jl +++ b/test/mixture.jl @@ -118,7 +118,7 @@ function test_mixture(g::MultivariateMixture, n::Int, ns::Int) @test isa(Xs, Matrix{Float64}) @test size(Xs) == (length(g), ns) @test_approx_eq_eps vec(mean(Xs, 2)) mean(g) 0.01 - @test_approx_eq_eps cov(Xs, vardim=2) cov(g) 0.01 + @test_approx_eq_eps cov(Xs, 2) cov(g) 0.01 end function test_params(g::AbstractMixtureModel) diff --git a/test/mvnormal.jl b/test/mvnormal.jl index 46415db94..78729f02b 100644 --- a/test/mvnormal.jl +++ b/test/mvnormal.jl @@ -116,7 +116,7 @@ function _gauss_mle(x::Matrix{Float64}, w::Vector{Float64}) sw = sum(w) mu = (x * w) * (1/sw) z = x .- mu - C = (z * scale(w, z')) * (1/sw) + C = (z * (Diagonal(w) * z')) * (1/sw) Base.LinAlg.copytri!(C, 'U') return mu, C end diff --git a/test/truncate.jl b/test/truncate.jl index a0b33911f..75d998ed4 100644 --- a/test/truncate.jl +++ b/test/truncate.jl @@ -1,5 +1,7 @@ # Testing discrete univariate distributions +module TestTruncate + using Distributions import JSON using Base.Test @@ -9,10 +11,10 @@ using Compat function verify_and_test_drive(jsonfile, selected, n_tsamples::Int,lower::Int,upper::Int) R = JSON.parsefile(jsonfile) for (ex, dct) in R - dsym = symbol(dct["dtype"]) + dsym = Symbol(dct["dtype"]) dname = string(dsym) - dsymt = symbol("Truncated($(dct["dtype"]),$lower,$upper") + dsymt = Symbol("Truncated($(dct["dtype"]),$lower,$upper") dnamet = string(dsym) # test whether it is included in the selected list @@ -52,7 +54,7 @@ function verify_and_test(d::UnivariateDistribution, dct::Dict, n_tsamples::Int) # verify parameters pdct = dct["params"] for (fname, val) in pdct - f = eval(symbol(fname)) + f = eval(Symbol(fname)) @assert isa(f, Function) Base.Test.test_approx_eq(f(d.untruncated), val, "$fname(d.untruncated)", "val") end @@ -107,3 +109,5 @@ for c in ["discrete", verify_and_test_drive(jsonfile, ARGS, 10^6,3,5) println() end + +end \ No newline at end of file diff --git a/test/univariates.jl b/test/univariates.jl index f56fe4509..5592cb490 100644 --- a/test/univariates.jl +++ b/test/univariates.jl @@ -9,7 +9,7 @@ using Compat function verify_and_test_drive(jsonfile, selected, n_tsamples::Int) R = JSON.parsefile(jsonfile) for (ex, dct) in R - dsym = symbol(dct["dtype"]) + dsym = Symbol(dct["dtype"]) dname = string(dsym) # test whether it is included in the selected list @@ -51,7 +51,7 @@ function verify_and_test(d::UnivariateDistribution, dct::Dict, n_tsamples::Int) # verify parameters pdct = dct["params"] for (fname, val) in pdct - f = eval(symbol(fname)) + f = eval(Symbol(fname)) @assert isa(f, Function) Base.Test.test_approx_eq(f(d), val, "$fname(d)", "val") end