Skip to content

Commit

Permalink
Autodot (#5)
Browse files Browse the repository at this point in the history
* rewrite _autodot and check_lags

* swap StatsBase dependency for Statistics

* update compat

* bump patch version

* remove Statistics compat for julia 1.0 compatibility
  • Loading branch information
mastrof authored Aug 6, 2023
1 parent 2ea9b56 commit 178719e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
5 changes: 2 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
name = "MeanSquaredDisplacement"
uuid = "13c93d70-909c-440c-af92-39d48ffa2ba2"
authors = ["Riccardo Foffi <riccardo.foffi@protonmail.com>"]
version = "0.1.0"
version = "0.1.1"

[deps]
AxisArrays = "39de3d68-74b9-583c-8d2d-e117c070f3a9"
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
AxisArrays = "0.4"
DSP = "0.7"
OffsetArrays = "1"
StatsBase = "0.33, 0.34"
julia = "1"

[extras]
Expand Down
11 changes: 6 additions & 5 deletions src/MeanSquaredDisplacement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using AxisArrays
using LinearAlgebra: dot
using DSP
using OffsetArrays
using StatsBase
using Statistics: mean

export emsd, imsd, unfold!

Expand Down Expand Up @@ -38,7 +38,7 @@ Return the ensemble average of the mean squared displacement of each column
of `x` at lag times `lags`.
If not specified `lags` defaults to `0:size(x,1)-1`.
"""
function emsd(x::AbstractMatrix, lags::AbstractVector{<:Integer}=0:size(x,1)-1)
function emsd(x::AbstractMatrix, lags=0:size(x,1)-1)
vec(mean(imsd(x, lags), dims=2))
end

Expand All @@ -49,16 +49,16 @@ end
Return the mean squared displacement of each column of `x` at lag times `lags`.
If not specified `lags` defaults to `0:size(x,1)-1`.
"""
function imsd(x::AbstractMatrix, lags::AbstractVector{<:Integer}=0:size(x,1)-1)
function imsd(x::AbstractMatrix, lags=0:size(x,1)-1)
mapslices(y -> imsd(y, lags), x, dims=1)
end

"""
imsd(x::AbstractVector, [lags])
Return the mean squared displacement of `x` at lag times `lags`.
If not specified `lags` defaults to `0:length(x)-1`.
If not specified `lags` defaults to `0:size(x,1)-1`.
"""
function imsd(x::AbstractVector, lags::AbstractVector{<:Integer}=0:size(x,1)-1)
function imsd(x::AbstractVector, lags=0:size(x,1)-1)
l = length(x)
S₂ = acf(x, lags)
D = OffsetArray([0.0; dot.(x,x); 0.0], -1:l)
Expand All @@ -75,6 +75,7 @@ end


include("acf.jl")
include("autodot.jl")
include("unfold.jl")

end # module MeanSquaredDisplacement
20 changes: 9 additions & 11 deletions src/acf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function fftacf!(r::AbstractVector, x::AbstractVector, lags)
lx = length(x)
m = length(lags)
@assert length(r) == m
StatsBase.check_lags(lx, lags)
check_lags(lx, lags)
A = conv(x, reverse(x))
for k in 1:m
δ = lags[k]
Expand All @@ -34,7 +34,7 @@ function fftacf!(r::AbstractVector, x::AbstractVector{T}, lags) where {T<:Union{
lx = length(x)
m = length(lags)
@assert length(r) == m
StatsBase.check_lags(lx, lags)
check_lags(lx, lags)
y = [getindex.(x,i) for i in eachindex(first(x))]
A = sum([conv(s, reverse(s)) for s in y])
for k in 1:m
Expand All @@ -44,6 +44,11 @@ function fftacf!(r::AbstractVector, x::AbstractVector{T}, lags) where {T<:Union{
return r
end

check_lags(lx::Int, lags::AbstractVector) = (
maximum(lags) < lx ||
error("lags must be less than the sample length.")
)

#== Autocovariance function with autodot ==#
# We follow the structure of `StatsBase.autocov` but
# 1. allow for non-scalar timeseries
Expand All @@ -63,17 +68,10 @@ function rawacf!(r::AbstractVector, x::AbstractVector, lags)
lx = length(x)
m = length(lags)
@assert length(r) == m
StatsBase.check_lags(lx, lags)
check_lags(lx, lags)
for k in 1:m
δ = lags[k]
r[k] = StatsBase._autodot(x, lx, δ) / (lx-δ)
r[k] = _autodot(x, lx, δ) / (lx-δ)
end
return r
end

# Extend to accept non-scalar timeseries
StatsBase._autodot(x::AbstractVector{T}, lx::Int, l::Int) where {T<:Union{AbstractVector,NTuple}} =
dot(view(x, 1:(lx-l)), view(x, (1+l):lx))



4 changes: 4 additions & 0 deletions src/autodot.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
_autodot(x::AbstractVector{<:Union{Float32, Float64}}, lx::Int, l::Int) = dot(x, 1:(lx-l), x, (1+l):lx)
_autodot(x::AbstractVector{<:Real}, lx::Int, l::Int) = dot(view(x, 1:(lx-l)), view(x, (1+l):lx))
_autodot(x::AbstractVector{<:AbstractVector}, lx::Int, l::Int) = dot(view(x, 1:(lx-l)), view(x, (1+l):lx))
_autodot(x::AbstractVector{<:NTuple{N,<:Real}}, lx::Int, l::Int) where N = dot(view(x, 1:(lx-l)), view(x, (1+l):lx))
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using Test
@test m m2

# emsd is the average of imsds
using StatsBase: mean
using Statistics: mean
@test emsd(x) vec(mean(m2, dims=2))

x = [collect(1:10), collect(1:10)]
Expand Down

2 comments on commit 178719e

@mastrof
Copy link
Owner Author

@mastrof mastrof commented on 178719e Aug 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/89148

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.1 -m "<description of version>" 178719e7203dcf1a4a6ebd7da03e5d5673c627a1
git push origin v0.1.1

Please sign in to comment.