Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pairwise! for Covariance types #5

Merged
merged 5 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/covariance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,31 @@ Evaluate the covariance at objects `x₁` and `x₁`.

"""
pairwise(cov, domain)

Evaluate covariance `cov` between all elements in the `domain`.

pairwise(cov, domain₁, domain₂)

Evaluate covariance `cov` between all elements of `domain₁` and `domain₂`.
"""
pairwise(cov::Covariance, args...) = sill(cov.γ) .- pairwise(cov.γ, args...)

"""
pairwise!(Γ, cov, domain)
Copy link
Member

Choose a reason for hiding this comment

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

We denote the covariance matrix by C or \Sigma. \Gamma is reserved for the variogram matrix.


Evaluates covariance `cov` between all elements in the `domain` in-place, filling the matrix `Γ`.

pairwise!(Γ, cov, domain₁, domain₂)

Evaluates covariance `cov` between all elements of `domain₁` and `domain₂` in-place, filling the matrix `Γ`."""
function pairwise!(Γ, cov::Covariance, args...)
pairwise!(Γ, cov.γ, args...)
for i ∈ eachindex(Γ)
Γ[i] = sill(cov.γ) - Γ[i]
end
Γ
end

# -----------
# IO METHODS
# -----------
Expand Down
10 changes: 10 additions & 0 deletions src/variogram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ function pairwise(γ::Variogram, domain)
pairwise!(Γ, γ, domain)
end

"""
pairwise!(Γ, γ, domain)

Evaluates covariance `γ` between all elements in the `domain` in-place, filling the matrix `Γ`.
"""
function pairwise!(Γ, γ::Variogram, domain)
n = length(domain)
@inbounds for j in 1:n
Expand Down Expand Up @@ -185,6 +190,11 @@ function pairwise(γ::Variogram, domain₁, domain₂)
pairwise!(Γ, γ, domain₁, domain₂)
end

"""
pairwise!(Γ, γ, domain)

Evaluates covariance `γ` between all elements of `domain₁` and `domain₂` in-place, filling the matrix `Γ`.
juliohm marked this conversation as resolved.
Show resolved Hide resolved
"""
function pairwise!(Γ, γ::Variogram, domain₁, domain₂)
m = length(domain₁)
n = length(domain₂)
Expand Down
5 changes: 5 additions & 0 deletions test/covariance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
@test eltype(Γ_f) == Float32
@test issymmetric(Γ_f)

𝒟 = PointSet(Matrix(1.0I, 3, 3))
Γ = Matrix{Float64}(undef, 3, 3)
GeoStatsFunctions.pairwise!(Γ, GaussianCovariance(range=1.0, sill=1.0, nugget=1.0), 𝒟)
@test issymmetric(Γ)

# shows
cov = CircularCovariance()
@test sprint(show, cov) == "CircularCovariance(sill: 1.0, nugget: 0.0, range: 1.0, distance: Euclidean)"
Expand Down