Skip to content

Commit

Permalink
Updates for 0.7 (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Dec 19, 2017
1 parent 77dd18b commit 558d8cf
Show file tree
Hide file tree
Showing 28 changed files with 109 additions and 71 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
julia 0.6
DataStructures 0.5.0
SpecialFunctions 0.1.0
Compat 0.32
Compat 0.39.0
4 changes: 4 additions & 0 deletions src/StatsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ module StatsBase

using Compat, SortingAlgorithms

if VERSION >= v"0.7.0-DEV.3052"
using Printf
end

## tackle compatibility issues

export
Expand Down
8 changes: 7 additions & 1 deletion src/counts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

const IntUnitRange{T<:Integer} = UnitRange{T}

if isdefined(Base, :ht_keyindex2)
const ht_keyindex2! = Base.ht_keyindex2
else
using Base: ht_keyindex2!
end

#### functions for counting a single list of integers (1D)
"""
addcounts!(r, x, levels::UnitRange{<:Int}, [wv::AbstractWeights])
Expand Down Expand Up @@ -267,7 +273,7 @@ end
"""Dict-based addcounts method"""
function addcounts_dict!(cm::Dict{T}, x::AbstractArray{T}) where T
for v in x
index = Base.ht_keyindex2(cm, v)
index = ht_keyindex2!(cm, v)
if index > 0
@inbounds cm.vals[index] += 1
else
Expand Down
2 changes: 1 addition & 1 deletion src/deprecates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ end
For each element in `b`, find its first index in `a`. If the value does
not occur in `a`, the corresponding index is 0.
"""
findat(a::AbstractArray, b::AbstractArray) = findat!(Array{Int}(size(b)), a, b)
findat(a::AbstractArray, b::AbstractArray) = findat!(Array{Int}(uninitialized, size(b)), a, b)

@deprecate df(obj::StatisticalModel) dof(obj)
@deprecate df_residual(obj::StatisticalModel) dof_residual(obj)
Expand Down
2 changes: 1 addition & 1 deletion src/empirical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function ecdf(X::RealVector{T}) where T<:Real
function ef(v::RealVector)
ord = sortperm(v)
m = length(v)
r = Vector{T}(m)
r = Vector{T}(uninitialized, m)
r0 = 0

i = 1
Expand Down
4 changes: 2 additions & 2 deletions src/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function inverse_rle(vals::AbstractVector{T}, lens::IntegerVector) where T
m = length(vals)
length(lens) == m || raise_dimerror()

r = Vector{T}(sum(lens))
r = Vector{T}(uninitialized, sum(lens))
p = 0
@inbounds for i = 1 : m
j = lens[i]
Expand Down Expand Up @@ -192,7 +192,7 @@ function _indicatormat_sparse(x::AbstractArray{T}, c::AbstractArray{T}) where T
m = length(c)
n = length(x)

rinds = Vector{Int}(n)
rinds = Vector{Int}(uninitialized, n)
@inbounds for i = 1 : n
rinds[i] = d[x[i]]
end
Expand Down
8 changes: 4 additions & 4 deletions src/ranking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Return the [ordinal ranking](https://en.wikipedia.org/wiki/Ranking#Ordinal_ranki
All items in `x` are given distinct, successive ranks based on their
position in `sort(x)`.
"""
ordinalrank(x::RealArray) = ordinalrank!(Array{Int}(size(x)), x, sortperm(x))
ordinalrank(x::RealArray) = ordinalrank!(Array{Int}(uninitialized, size(x)), x, sortperm(x))


# Competition ranking ("1224" ranking) -- resolve tied ranks using min
Expand Down Expand Up @@ -77,7 +77,7 @@ Return the [standard competition ranking](http://en.wikipedia.org/wiki/Ranking#S
array. Items that compare equal are given the same rank, then a gap is left
in the rankings the size of the number of tied items - 1.
"""
competerank(x::RealArray) = competerank!(Array{Int}(size(x)), x, sortperm(x))
competerank(x::RealArray) = competerank!(Array{Int}(uninitialized, size(x)), x, sortperm(x))


# Dense ranking ("1223" ranking) -- resolve tied ranks using min
Expand Down Expand Up @@ -115,7 +115,7 @@ Return the [dense ranking](http://en.wikipedia.org/wiki/Ranking#Dense_ranking_.2
compare equal receive the same ranking, and the next subsequent rank is
assigned with no gap.
"""
denserank(x::RealArray) = denserank!(Array{Int}(size(x)), x, sortperm(x))
denserank(x::RealArray) = denserank!(Array{Int}(uninitialized, size(x)), x, sortperm(x))


# Tied ranking ("1 2.5 2.5 4" ranking) -- resolve tied ranks using average
Expand Down Expand Up @@ -161,5 +161,5 @@ also called fractional or "1 2.5 2.5 4" ranking,
of a real-valued array. Items that compare equal receive the mean of the
rankings they would have been assigned under ordinal ranking.
"""
tiedrank(x::RealArray) = tiedrank!(Array{Float64}(size(x)), x, sortperm(x))
tiedrank(x::RealArray) = tiedrank!(Array{Float64}(uninitialized, size(x)), x, sortperm(x))

24 changes: 12 additions & 12 deletions src/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function fisher_yates_sample!(rng::AbstractRNG, a::AbstractArray, x::AbstractArr
k = length(x)
k <= n || error("length(x) should not exceed length(a)")

inds = Vector{Int}(n)
inds = Vector{Int}(uninitialized, n)
for i = 1:n
@inbounds inds[i] = i
end
Expand Down Expand Up @@ -370,7 +370,7 @@ Optionally specify a random number generator `rng` as the first argument
"""
function sample(rng::AbstractRNG, a::AbstractArray{T}, n::Integer;
replace::Bool=true, ordered::Bool=false) where T
sample!(rng, a, Vector{T}(n); replace=replace, ordered=ordered)
sample!(rng, a, Vector{T}(uninitialized, n); replace=replace, ordered=ordered)
end
sample(a::AbstractArray, n::Integer; replace::Bool=true, ordered::Bool=false) =
sample(Base.GLOBAL_RNG, a, n; replace=replace, ordered=ordered)
Expand All @@ -390,7 +390,7 @@ Optionally specify a random number generator `rng` as the first argument
"""
function sample(rng::AbstractRNG, a::AbstractArray{T}, dims::Dims;
replace::Bool=true, ordered::Bool=false) where T
sample!(rng, a, Array{T}(dims), rng; replace=replace, ordered=ordered)
sample!(rng, a, Array{T}(uninitialized, dims), rng; replace=replace, ordered=ordered)
end
sample(a::AbstractArray, dims::Dims; replace::Bool=true, ordered::Bool=false) =
sample(Base.GLOBAL_RNG, a, dims; replace=replace, ordered=ordered)
Expand Down Expand Up @@ -477,8 +477,8 @@ function make_alias_table!(w::AbstractVector{Float64}, wsum::Float64,
@inbounds a[i] = w[i] * ac
end

larges = Vector{Int}(n)
smalls = Vector{Int}(n)
larges = Vector{Int}(uninitialized, n)
smalls = Vector{Int}(uninitialized, n)
kl = 0 # actual number of larges
ks = 0 # actual number of smalls

Expand Down Expand Up @@ -528,8 +528,8 @@ function alias_sample!(rng::AbstractRNG, a::AbstractArray, wv::AbstractWeights,
length(wv) == n || throw(DimensionMismatch("Inconsistent lengths."))

# create alias table
ap = Vector{Float64}(n)
alias = Vector{Int}(n)
ap = Vector{Float64}(uninitialized, n)
alias = Vector{Int}(uninitialized, n)
make_alias_table!(values(wv), sum(wv), ap, alias)

# sampling
Expand Down Expand Up @@ -560,7 +560,7 @@ function naive_wsample_norep!(rng::AbstractRNG, a::AbstractArray,
length(wv) == n || throw(DimensionMismatch("Inconsistent lengths."))
k = length(x)

w = Vector{Float64}(n)
w = Vector{Float64}(uninitialized, n)
copy!(w, values(wv))
wsum = sum(wv)

Expand Down Expand Up @@ -784,14 +784,14 @@ sample!(a::AbstractArray, wv::AbstractWeights, x::AbstractArray) =

sample(rng::AbstractRNG, a::AbstractArray{T}, wv::AbstractWeights, n::Integer;
replace::Bool=true, ordered::Bool=false) where {T} =
sample!(rng, a, wv, Vector{T}(n); replace=replace, ordered=ordered)
sample!(rng, a, wv, Vector{T}(uninitialized, n); replace=replace, ordered=ordered)
sample(a::AbstractArray, wv::AbstractWeights, n::Integer;
replace::Bool=true, ordered::Bool=false) =
sample(Base.GLOBAL_RNG, a, wv, n; replace=replace, ordered=ordered)

sample(rng::AbstractRNG, a::AbstractArray{T}, wv::AbstractWeights, dims::Dims;
replace::Bool=true, ordered::Bool=false) where {T} =
sample!(rng, a, wv, Array{T}(dims); replace=replace, ordered=ordered)
sample!(rng, a, wv, Array{T}(uninitialized, dims); replace=replace, ordered=ordered)
sample(a::AbstractArray, wv::AbstractWeights, dims::Dims;
replace::Bool=true, ordered::Bool=false) =
sample(Base.GLOBAL_RNG, a, wv, dims; replace=replace, ordered=ordered)
Expand Down Expand Up @@ -845,7 +845,7 @@ Optionally specify a random number generator `rng` as the first argument
"""
wsample(rng::AbstractRNG, a::AbstractArray{T}, w::RealVector, n::Integer;
replace::Bool=true, ordered::Bool=false) where {T} =
wsample!(rng, a, w, Vector{T}(n); replace=replace, ordered=ordered)
wsample!(rng, a, w, Vector{T}(uninitialized, n); replace=replace, ordered=ordered)
wsample(a::AbstractArray, w::RealVector, n::Integer;
replace::Bool=true, ordered::Bool=false) =
wsample(Base.GLOBAL_RNG, a, w, n; replace=replace, ordered=ordered)
Expand All @@ -862,7 +862,7 @@ Optionally specify a random number generator `rng` as the first argument
"""
wsample(rng::AbstractRNG, a::AbstractArray{T}, w::RealVector, dims::Dims;
replace::Bool=true, ordered::Bool=false) where {T} =
wsample!(rng, a, w, Array{T}(dims); replace=replace, ordered=ordered)
wsample!(rng, a, w, Array{T}(uninitialized, dims); replace=replace, ordered=ordered)
wsample(a::AbstractArray, w::RealVector, dims::Dims;
replace::Bool=true, ordered::Bool=false) =
wsample(Base.GLOBAL_RNG, a, w, dims; replace=replace, ordered=ordered)
4 changes: 2 additions & 2 deletions src/scalarstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,13 @@ In particular, when `μ` and `σ` are arrays, they should have the same size, an
"""
function zscore(X::AbstractArray{T}, μ::Real, σ::Real) where T<:Real
ZT = typeof((zero(T) - zero(μ)) / one(σ))
_zscore!(Array{ZT}(size(X)), X, μ, σ)
_zscore!(Array{ZT}(uninitialized, size(X)), X, μ, σ)
end

function zscore(X::AbstractArray{T}, μ::AbstractArray{U}, σ::AbstractArray{S}) where {T<:Real,U<:Real,S<:Real}
_zscore_chksize(X, μ, σ)
ZT = typeof((zero(T) - zero(U)) / one(S))
_zscore!(Array{ZT}(size(X)), X, μ, σ)
_zscore!(Array{ZT}(uninitialized, size(X)), X, μ, σ)
end

zscore(X::AbstractArray{<:Real}) = ((μ, σ) = mean_and_std(X); zscore(X, μ, σ))
Expand Down
50 changes: 25 additions & 25 deletions src/signalcorr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function autocov!(r::RealMatrix, x::AbstractMatrix{T}, lags::IntegerVector; deme
size(r) == (m, ns) || throw(DimensionMismatch())
check_lags(lx, lags)

z = Vector{T}(lx)
z = Vector{T}(uninitialized, lx)
for j = 1 : ns
demean_col!(z, x, j, demean)
for k = 1 : m
Expand Down Expand Up @@ -107,11 +107,11 @@ When left unspecified, the lags used are the integers from 0 to
The output is not normalized. See [`autocor`](@ref) for a function with normalization.
"""
function autocov(x::AbstractVector{T}, lags::IntegerVector; demean::Bool=true) where T<:Real
autocov!(Vector{fptype(T)}(length(lags)), float(x), lags; demean=demean)
autocov!(Vector{fptype(T)}(uninitialized, length(lags)), float(x), lags; demean=demean)
end

function autocov(x::AbstractMatrix{T}, lags::IntegerVector; demean::Bool=true) where T<:Real
autocov!(Matrix{fptype(T)}(length(lags), size(x,2)), float(x), lags; demean=demean)
autocov!(Matrix{fptype(T)}(uninitialized, length(lags), size(x,2)), float(x), lags; demean=demean)
end

autocov(x::AbstractVecOrMat{<:Real}; demean::Bool=true) = autocov(x, default_autolags(size(x,1)); demean=demean)
Expand Down Expand Up @@ -153,7 +153,7 @@ function autocor!(r::RealMatrix, x::AbstractMatrix{T}, lags::IntegerVector; deme
size(r) == (m, ns) || throw(DimensionMismatch())
check_lags(lx, lags)

z = Vector{T}(lx)
z = Vector{T}(uninitialized, lx)
for j = 1 : ns
demean_col!(z, x, j, demean)
zz = dot(z, z)
Expand Down Expand Up @@ -183,11 +183,11 @@ The output is normalized by the variance of `x`, i.e. so that the lag 0
autocorrelation is 1. See [`autocov`](@ref) for the unnormalized form.
"""
function autocor(x::AbstractVector{T}, lags::IntegerVector; demean::Bool=true) where T<:Real
autocor!(Vector{fptype(T)}(length(lags)), float(x), lags; demean=demean)
autocor!(Vector{fptype(T)}(uninitialized, length(lags)), float(x), lags; demean=demean)
end

function autocor(x::AbstractMatrix{T}, lags::IntegerVector; demean::Bool=true) where T<:Real
autocor!(Matrix{fptype(T)}(length(lags), size(x,2)), float(x), lags; demean=demean)
autocor!(Matrix{fptype(T)}(uninitialized, length(lags), size(x,2)), float(x), lags; demean=demean)
end

autocor(x::AbstractVecOrMat{<:Real}; demean::Bool=true) = autocor(x, default_autolags(size(x,1)); demean=demean)
Expand Down Expand Up @@ -243,7 +243,7 @@ function crosscov!(r::RealMatrix, x::AbstractMatrix{T}, y::AbstractVector{T}, la
(length(y) == lx && size(r) == (m, ns)) || throw(DimensionMismatch())
check_lags(lx, lags)

zx = Vector{T}(lx)
zx = Vector{T}(uninitialized, lx)
zy::Vector{T} = demean ? y .- mean(y) : y
for j = 1 : ns
demean_col!(zx, x, j, demean)
Expand All @@ -262,7 +262,7 @@ function crosscov!(r::RealMatrix, x::AbstractVector{T}, y::AbstractMatrix{T}, la
check_lags(lx, lags)

zx::Vector{T} = demean ? x .- mean(x) : x
zy = Vector{T}(lx)
zy = Vector{T}(uninitialized, lx)
for j = 1 : ns
demean_col!(zy, y, j, demean)
for k = 1 : m
Expand Down Expand Up @@ -294,8 +294,8 @@ function crosscov!(r::AbstractArray{T,3}, x::AbstractMatrix{T}, y::AbstractMatri
push!(zxs, xj)
end

zx = Vector{T}(lx)
zy = Vector{T}(lx)
zx = Vector{T}(uninitialized, lx)
zy = Vector{T}(uninitialized, lx)
for j = 1 : ny
demean_col!(zy, y, j, demean)
for i = 1 : nx
Expand Down Expand Up @@ -326,19 +326,19 @@ When left unspecified, the lags used are the integers from
The output is not normalized. See [`crosscor`](@ref) for a function with normalization.
"""
function crosscov(x::AbstractVector{T}, y::AbstractVector{T}, lags::IntegerVector; demean::Bool=true) where T<:Real
crosscov!(Vector{fptype(T)}(length(lags)), float(x), float(y), lags; demean=demean)
crosscov!(Vector{fptype(T)}(uninitialized, length(lags)), float(x), float(y), lags; demean=demean)
end

function crosscov(x::AbstractMatrix{T}, y::AbstractVector{T}, lags::IntegerVector; demean::Bool=true) where T<:Real
crosscov!(Matrix{fptype(T)}(length(lags), size(x,2)), float(x), float(y), lags; demean=demean)
crosscov!(Matrix{fptype(T)}(uninitialized, length(lags), size(x,2)), float(x), float(y), lags; demean=demean)
end

function crosscov(x::AbstractVector{T}, y::AbstractMatrix{T}, lags::IntegerVector; demean::Bool=true) where T<:Real
crosscov!(Matrix{fptype(T)}(length(lags), size(y,2)), float(x), float(y), lags; demean=demean)
crosscov!(Matrix{fptype(T)}(uninitialized, length(lags), size(y,2)), float(x), float(y), lags; demean=demean)
end

function crosscov(x::AbstractMatrix{T}, y::AbstractMatrix{T}, lags::IntegerVector; demean::Bool=true) where T<:Real
crosscov!(Array{fptype(T),3}(length(lags), size(x,2), size(y,2)), float(x), float(y), lags; demean=demean)
crosscov!(Array{fptype(T),3}(uninitialized, length(lags), size(x,2), size(y,2)), float(x), float(y), lags; demean=demean)
end

crosscov(x::AbstractVecOrMat{T}, y::AbstractVecOrMat{T}; demean::Bool=true) where {T<:Real} = crosscov(x, y, default_crosslags(size(x,1)); demean=demean)
Expand Down Expand Up @@ -383,7 +383,7 @@ function crosscor!(r::RealMatrix, x::AbstractMatrix{T}, y::AbstractVector{T}, la
(length(y) == lx && size(r) == (m, ns)) || throw(DimensionMismatch())
check_lags(lx, lags)

zx = Vector{T}(lx)
zx = Vector{T}(uninitialized, lx)
zy::Vector{T} = demean ? y .- mean(y) : y
yy = dot(zy, zy)
for j = 1 : ns
Expand All @@ -404,7 +404,7 @@ function crosscor!(r::RealMatrix, x::AbstractVector{T}, y::AbstractMatrix{T}, la
check_lags(lx, lags)

zx::Vector{T} = demean ? x .- mean(x) : x
zy = Vector{T}(lx)
zy = Vector{T}(uninitialized, lx)
xx = dot(zx, zx)
for j = 1 : ns
demean_col!(zy, y, j, demean)
Expand All @@ -427,7 +427,7 @@ function crosscor!(r::AbstractArray{T,3}, x::AbstractMatrix{T}, y::AbstractMatri
# cached (centered) columns of x
zxs = Vector{Vector{T}}(0)
sizehint!(zxs, nx)
xxs = Vector{T}(nx)
xxs = Vector{T}(uninitialized, nx)

for j = 1 : nx
xj = x[:,j]
Expand All @@ -441,8 +441,8 @@ function crosscor!(r::AbstractArray{T,3}, x::AbstractMatrix{T}, y::AbstractMatri
xxs[j] = dot(xj, xj)
end

zx = Vector{T}(lx)
zy = Vector{T}(lx)
zx = Vector{T}(uninitialized, lx)
zy = Vector{T}(uninitialized, lx)
for j = 1 : ny
demean_col!(zy, y, j, demean)
yy = dot(zy, zy)
Expand Down Expand Up @@ -475,19 +475,19 @@ The output is normalized by `sqrt(var(x)*var(y))`. See [`crosscov`](@ref) for th
unnormalized form.
"""
function crosscor(x::AbstractVector{T}, y::AbstractVector{T}, lags::IntegerVector; demean::Bool=true) where T<:Real
crosscor!(Vector{fptype(T)}(length(lags)), float(x), float(y), lags; demean=demean)
crosscor!(Vector{fptype(T)}(uninitialized, length(lags)), float(x), float(y), lags; demean=demean)
end

function crosscor(x::AbstractMatrix{T}, y::AbstractVector{T}, lags::IntegerVector; demean::Bool=true) where T<:Real
crosscor!(Matrix{fptype(T)}(length(lags), size(x,2)), float(x), float(y), lags; demean=demean)
crosscor!(Matrix{fptype(T)}(uninitialized, length(lags), size(x,2)), float(x), float(y), lags; demean=demean)
end

function crosscor(x::AbstractVector{T}, y::AbstractMatrix{T}, lags::IntegerVector; demean::Bool=true) where T<:Real
crosscor!(Matrix{fptype(T)}(length(lags), size(y,2)), float(x), float(y), lags; demean=demean)
crosscor!(Matrix{fptype(T)}(uninitialized, length(lags), size(y,2)), float(x), float(y), lags; demean=demean)
end

function crosscor(x::AbstractMatrix{T}, y::AbstractMatrix{T}, lags::IntegerVector; demean::Bool=true) where T<:Real
crosscor!(Array{fptype(T),3}(length(lags), size(x,2), size(y,2)), float(x), float(y), lags; demean=demean)
crosscor!(Array{fptype(T),3}(uninitialized, length(lags), size(x,2), size(y,2)), float(x), float(y), lags; demean=demean)
end

crosscor(x::AbstractVecOrMat{T}, y::AbstractVecOrMat{T}; demean::Bool=true) where {T<:Real} = crosscor(x, y, default_crosslags(size(x,1)); demean=demean)
Expand Down Expand Up @@ -520,7 +520,7 @@ function pacf_regress!(r::RealMatrix, X::AbstractMatrix{T}, lags::IntegerVector,
end

function pacf_yulewalker!(r::RealMatrix, X::AbstractMatrix{T}, lags::IntegerVector, mk::Integer) where T<:RealFP
tmp = Vector{T}(mk)
tmp = Vector{T}(uninitialized, mk)
for j = 1 : size(X,2)
acfs = autocor(X[:,j], 1:mk)
for i = 1 : length(lags)
Expand Down Expand Up @@ -574,7 +574,7 @@ If `x` is a matrix, return a matrix of size `(length(lags), size(x, 2))`,
where each column in the result corresponds to a column in `x`.
"""
function pacf(X::AbstractMatrix{T}, lags::IntegerVector; method::Symbol=:regression) where T<:Real
pacf!(Matrix{fptype(T)}(length(lags), size(X,2)), float(X), lags; method=method)
pacf!(Matrix{fptype(T)}(uninitialized, length(lags), size(X,2)), float(X), lags; method=method)
end

function pacf(x::AbstractVector{T}, lags::IntegerVector; method::Symbol=:regression) where T<:Real
Expand Down

0 comments on commit 558d8cf

Please sign in to comment.