LibKriging
kriging library for performance and wide language support
Install / Use
/learn @libKriging/LibKrigingREADME
Table of contents
- Installation from pre-built packages
- Compilation
If you want to contribute read Contribution guide.
Installation from pre-built packages
For the most common target {Python, R, Octave, Matlab, Julia} x {Linux, macOS, Windows} x { x86-64, ARM }, you can use released binaries, or R CRAN or Python PyPI.
pylibkriging for Python
pip3 install pylibkriging
or for pre-release packages (according to your OS and Python version, see https://github.com/libKriging/libKriging/releases)
pip3 install https://github.com/libKriging/libKriging/releases/download/v0.9.0/pylibkriging-0.9.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Usage example here
<details> <summary>👆The sample code below should give you a taste. Please refer to the reference file linked above for a CI certified example.</summary>import numpy as np
X = [0.0, 0.25, 0.5, 0.75, 1.0]
f = lambda x: (1 - 1 / 2 * (np.sin(12 * x) / (1 + x) + 2 * np.cos(7 * x) * x ** 5 + 0.7))
y = [f(xi) for xi in X]
import pylibkriging as lk
k_py = lk.Kriging(y, X, "gauss")
print(k_py.summary())
# you can also check logLikelhood using:
# def ll(t): return k_py.logLikelihoodFun(t,False,False)[0]
# t = np.arange(0,1,1/99); pyplot.figure(1); pyplot.plot(t, [ll(ti) for ti in t]); pyplot.show()
x = np.arange(0, 1, 1 / 99)
p = k_py.predict(x, True, False)
p = {"mean": p[0], "stdev": p[1], "cov": p[2]} # This should be done by predict
import matplotlib.pyplot as pyplot
pyplot.figure(1)
pyplot.plot(x, [f(xi) for xi in x])
pyplot.scatter(X, [f(xi) for xi in X])
pyplot.plot(x, p['mean'], color='blue')
pyplot.fill(np.concatenate((x, np.flip(x))),
np.concatenate((p['mean'] - 2 * p['stdev'], np.flip(p['mean'] + 2 * p['stdev']))), color='blue',
alpha=0.2)
pyplot.show()
s = k_py.simulate(10, 123, x)
pyplot.figure(2)
pyplot.plot(x, [f(xi) for xi in x])
pyplot.scatter(X, [f(xi) for xi in X])
for i in range(10):
pyplot.plot(x, s[:, i], color='blue', alpha=0.2)
pyplot.show()
</details>
<details>
<summary>Note for older versions (< 0.5)</summary>
NB: On Windows, it should require [extra DLL](https://github.com/libKriging/libKriging/releases/download/v0.4.2/extra_dlls_for_python_on_windows.zip) not (yet) embedded in the python package.
To load them into Python's search PATH, use:
```python
import os
os.environ['PATH'] = 'c:\\Users\\User\\Path\\to\\dlls' + os.pathsep + os.environ['PATH']
import pylibkriging as lk
```
</details>
rlibkriging for R
From R:
install.packages('rlibkriging')
Or using the archive from libKriging releases
# in R
install.packages("https://github.com/libKriging/rlibkriging/releases/download/0.9-0/rlibkriging_0.9-0_R_x86_64-pc-linux-gnu.tar.gz", repos=NULL)
Usage example here
<details> <summary>👆The sample code below should give you a taste. Please refer to the reference file linked above for a CI certified example.</summary>X <- as.matrix(c(0.0, 0.25, 0.5, 0.75, 1.0))
f <- function(x) 1 - 1 / 2 * (sin(12 * x) / (1 + x) + 2 * cos(7 * x) * x^5 + 0.7)
y <- f(X)
library(rlibkriging)
k_R <- Kriging(y, X, "gauss")
print(k_R)
# you can also check logLikelhood using:
# ll = function(t) logLikelihoodFun(k_R,t)$logLikelihood; plot(ll)
x <- as.matrix(seq(0, 1, , 100))
p <- predict(k_R, x, TRUE, FALSE)
plot(f)
points(X, y)
lines(x, p$mean, col = 'blue')
polygon(c(x, rev(x)), c(p$mean - 2 * p$stdev, rev(p$mean + 2 * p$stdev)), border = NA, col = rgb(0, 0, 1, 0.2))
s <- simulate(k_R,nsim = 10, seed = 123, x=x)
plot(f)
points(X,y)
matplot(x,s,col=rgb(0,0,1,0.2),type='l',lty=1,add=T)
</details>
mlibkriging for Octave and MATLAB
⚠️ Matlab/Windows binary packages are done on request (GitHub Action does not support all required Operating Systems)
Download and uncompress the Octave archive from libKriging releases
# example
curl -LO https://github.com/libKriging/libKriging/releases/download/v0.9.0/mLibKriging_0.9.0_Linux-x86_64.tgz
Then
octave --path /path/to/mLibKriging/installation
or inside Octave or Matlab
addpath("path/to/mLibKriging")
Usage example here
<details> <summary>👆The sample code below should give you a taste. Please refer to the reference file linked above for a CI certified example.</summary>X = [0.0;0.25;0.5;0.75;1.0];
f = @(x) 1-1/2.*(sin(12*x)./(1+x)+2*cos(7.*x).*x.^5+0.7)
y = f(X);
k_m = Kriging(y, X, "gauss");
disp(k_m.summary());
% you can also check logLikelhood using:
% function llt = ll (tt) global k_m; llt=k_m.logLikelihoodFun(tt); endfunction; t=0:(1/99):1; plot(t,arrayfun(@ll,t))
x = reshape(0:(1/99):1,100,1);
[p_mean, p_stdev] = k_m.predict(x, true, false);
h = figure(1)
hold on;
plot(x,f(x));
scatter(X,f(X));
plot(x,p_mean,'b')
poly = fill([x; flip(x)], [(p_mean-2*p_stdev); flip(p_mean+2*p_stdev)],'b');
set( poly, 'facealpha', 0.2);
hold off;
s = k_m.simulate(int32(10),int32(123), x);
h = figure(2)
hold on;
plot(x,f(x));
scatter(X,f(X));
for i=1:10
plot(x,s(:,i),'b');
end
hold off;
</details>
jlibkriging for Julia
The Julia binding requires building libKriging from source with -DENABLE_JULIA_BINDING=ON:
git clone --recurse-submodules https://github.com/libKriging/libKriging.git
cd libKriging
cmake -B build -DCMAKE_BUILD_TYPE=Release -DENABLE_JULIA_BINDING=ON .
cmake --build build
Then install the Julia package (the library is auto-detected from the build/ directory):
julia -e 'using Pkg; Pkg.develop(path="bindings/Julia/jlibkriging")'
using jlibkriging
X = reshape([0.0, 0.25, 0.5, 0.75, 1.0], :, 1)
f(x) = 1 - 1/2 * (sin(12*x) / (1+x) + 2*cos(7*x) * x^5 + 0.7)
y = f.(X[:, 1])
k = Kriging(y, X, "gauss")
println(jlibkriging.summary(k))
x = reshape(collect(0:0.01:1), :, 1)
p = predict(k, x; stdev=true, cov=false)
println("Predicted mean: ", p.mean[1:5])
println("Predicted stdev: ", p.stdev[1:5])
s = simulate(k, 10, 123, x)
println("Simulation size: ", size(s))
Usage example here
Expected demo results
Using the previous linked examples (in Python, R, Octave, Matlab or Julia), you should obtain the following results
predict plot | simulate plot
:-------------------------------------------:|:---------------------------------------------:
| 
Tested installation
with libKriging 0.9
<!-- ✔ ⌛️ ✘ -->| | Linux Ubuntu:22 | macOS 14 (x86-64 & ARM) | Windows 10 | |:-------|:--------------------------------------------|:--------------------------------------------|:--------------------------------------------| | Python | <span style="color:green">✔</span> 3.7-3.12 | <span style="color:green">✔</span> 3.7-3.12 | <span style="color:green">✔</span> 3.7-3.12 | | R | <span style="color:green">✔</span> 4.0-4.4 | <span style="color:green">✔</span> 4.0-4.4 | <span style="color:green">✔</span> 4.0-4.4 | | Octave | <span style="color:green">✔</span> 7.2 | <span style="color:green">✔</span> 7.2 | <span style="color:green">✔</span> 8.3 | | Matlab | <span style="color:green">️✔</span> R2022a | <span style="color:green">✔</span> R2022* | <span style="color:green">✔</span> R2022* | | Julia | <span style="color:orange"><b>?</b></span> 1.10+ | <span style="color:orange"><b>?</b></span> 1.10+ | <span style="color:orange"><b>?</b></span> 1.10+ |
-
* : no pre-built package or CI
-
<span
