Skip to content

Commit

Permalink
add Covariance methods (#4)
Browse files Browse the repository at this point in the history
* add Covariance methods

* add tests, use metricball instead of internal field

* Update src/covariance.jl

Co-authored-by: Júlio Hoffimann <[email protected]>

* update covariance methods

* Apply suggestions from code review

* Minor adjustments

---------

Co-authored-by: Mark Baum <>
Co-authored-by: Júlio Hoffimann <[email protected]>
  • Loading branch information
markmbaum and juliohm authored Mar 25, 2024
1 parent da6d967 commit 0ac7c03
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
60 changes: 55 additions & 5 deletions src/covariance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,67 @@ Parent type of all covariance functions (e.g. Gaussian covariance).
abstract type Covariance end

"""
cov(x₁, x₂)
isstationary(cov)
Evaluate the covariance at objects `x₁` and `x₁`.
Check if covariance `cov` possesses the 2nd-order stationary property.
"""
(cov::Covariance)(x₁, x₂) = sill(cov.γ) - cov.γ(x₁, x₂)
isstationary(cov::Covariance) = isstationary(cov.γ)

"""
isisotropic(cov)
Tells whether or not covariance `cov` is isotropic.
"""
isisotropic(cov::Covariance) = isisotropic(cov.γ)

"""
sill(cov)
Return the sill of the covariance `cov`.
"""
sill(cov::Covariance) = sill(cov.γ)

"""
nugget(cov)
Return the nugget of the covariance `cov`.
"""
nugget(cov::Covariance) = nugget(cov.γ)

"""
metricball(cov)
Return the metric ball of the covariance `cov`.
"""
metricball(cov::Covariance) = metricball(cov.γ)

"""
range(cov)
Return the maximum range of the covariance `cov`.
"""
Base.range(cov::Covariance) = range(cov.γ)

"""
scale(cov, s)
Scale metric ball of covariance `cov` with strictly
positive scaling factor `s`.
"""
scale(cov::Cov, s::Real) where {Cov <: Covariance} = Cov(scale(cov.γ, s))

"""
cov(g₁, g₂)
Evaluate the covariance at geometries `g₁` and `g₁`.
"""
(cov::Covariance)(g₁, g₂) = sill(cov.γ) - cov.γ(g₁, g₂)

"""
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₂`.
Expand Down
9 changes: 9 additions & 0 deletions test/covariance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
@test eltype(Γ_f) == Float32
@test issymmetric(Γ_f)

cov = GaussianCovariance(range=1.0, sill=1.0, nugget=0.0)
@test isstationary(cov)
@test isisotropic(cov)
@test sill(cov) == 1.0
@test nugget(cov) == 0.0
@test metricball(cov) == MetricBall(1.0)
@test range(cov) == 1.0
@test GeoStatsFunctions.scale(cov, 2) == GaussianCovariance(range=2.0, sill=1.0, nugget=0.0)

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

0 comments on commit 0ac7c03

Please sign in to comment.