Skip to content

timweiland/GaussianMarkovRandomFields.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GaussianMarkovRandomFields.jl

Logo for the GaussianMarkovRandomFields.jl package.
⚡ Fast, flexible and user-centered Julia package for Bayesian inference with sparse Gaussians

Build Status Coverage Aqua

Gaussian Markov Random Fields (GMRFs) are Gaussian distributions with sparse precision (inverse covariance) matrices. GaussianMarkovRandomFields.jl provides utilities for working with GMRFs in Julia. The goal is to enable flexible and efficient Bayesian inference from GMRFs, powered by sparse linear algebra.

In particular, we support the creation of GMRFs through finite element method discretizations of stochastic partial differential equations (SPDEs). This unlocks efficient GMRF-based approximations to commonly used Gaussian process priors. Furthermore, the expressive power of SPDEs allows for flexible, problem-tailored priors.

Contents

Installation

GaussianMarkovRandomFields.jl is not yet a registered Julia package. Until it is, you can install it from this GitHub repository. To do so:

  1. Download Julia (>= version 1.10).

  2. Launch the Julia REPL and type ] add https://github.com/timweiland/GaussianMarkovRandomFields.jl.

Your first GMRF

Let's construct a GMRF approximation to a Matern process on a square grid:

using GaussianMarkovRandomFields

# Define mesh via Ferrite.jl
using Ferrite
grid = generate_grid(Triangle, (50, 50))
interpolation_fn = Lagrange{RefTriangle, 1}()
quad_rule = QuadratureRule{RefTriangle}(2)
disc = FEMDiscretization(grid, interpolation_fn, quad_rule)

# Define SPDE and discretize to get a GMRF
spde = MaternSPDE{2}(range = 0.3, smoothness = 1)
x = discretize(spde, disc)

x is a Gaussian distribution, and we can compute all the things Gaussians are known for.

# Get interesting quantities
using Distributions
μ = mean(x)
σ_marginal = std(x)
samp = rand(x) # Sample
Q_linmap = precision_map(x) # Linear map
Q = to_matrix(Q_linmap) # Sparse matrix

# Form posterior under point observations
A = evaluation_matrix(disc, [Tensors.Vec(0.1, 0.0), Tensors.Vec(-0.3, 0.55)])
noise_precision = 1e2 # Inverse of the noise variance
y = [0.83, 0.12]
x_cond = condition_on_observations(x, A, noise_precision, y) # Again a GMRF!

Make sure to check the documentation for further examples!

Contributing

Check our contribution guidelines.