Skip to content

Basic Usage

Prior Sampling

Sample f and g Together

The simplest use case: sample a function and its integral on the same grid.

import numpy as np
from gp4c import sample_prior, SamplingSpec

x = np.linspace(0, 5, 100)
spec = SamplingSpec(x_f=x, x_g=x)
result = sample_prior(
    spec,
    sigma2=1.0,    # Kernel variance
    ell=0.5,       # Length scale
    n_samples=5,
    seed=42
)
print(result.f.shape)  # (5, 100) - function samples
print(result.g.shape)  # (5, 100) - integral samples

Parameters:

  • spec: SamplingSpec defining which functions to sample and where
  • sigma2: RBF kernel variance parameter
  • ell: RBF kernel length scale
  • n_samples: Number of joint samples
  • seed: Random seed for reproducibility

Sample on Different Grids

You can sample f and g at different points:

x_f = np.linspace(0, 5, 100)   # Dense grid for function
x_g = np.linspace(0, 5, 20)    # Coarse grid for integral

spec = SamplingSpec(x_f=x_f, x_g=x_g)
result = sample_prior(spec, ell=0.5, n_samples=3)
print(result.f.shape)  # (3, 100)
print(result.g.shape)  # (3, 20)

Flexible API

Sample Any Combination

Use sample_prior with SamplingSpec to sample any subset of (f, g, h):

from gp4c import sample_prior, SamplingSpec

# Only f
spec = SamplingSpec(x_f=x)
result = sample_prior(spec, ell=0.5, n_samples=5)
print(result.f.shape)  # (5, 100)
print(result.g)        # None
print(result.h)        # None

# Only derivative h
spec = SamplingSpec(x_h=x)
result = sample_prior(spec, ell=0.5, n_samples=5)
print(result.h.shape)  # (5, 100)

# f and h together
spec = SamplingSpec(x_f=x, x_h=x)
result = sample_prior(spec, ell=0.5, n_samples=5)
print(result.f.shape)  # (5, 100)
print(result.h.shape)  # (5, 100)

All Seven Combinations

The API supports all possible combinations:

  1. f only
  2. g only
  3. h only
  4. f + g
  5. f + h
  6. g + h
  7. f + g + h
# Sample all three on different grids
spec = SamplingSpec(
    x_f=np.linspace(0, 5, 100),
    x_g=np.linspace(0, 5, 50),
    x_h=np.linspace(0.1, 4.9, 80)
)
result = sample_prior(spec, ell=0.5, n_samples=10)

Tip

The flexible API lets you sample any combination of f, g, and h on different grids, giving you complete control over what to sample and where.

Kernel Parameters

Length Scale Effects

The length scale ell controls correlation distance:

# Smooth, slowly varying
result_smooth = sample_prior(spec, ell=2.0, n_samples=5)

# Rough, rapidly varying
result_rough = sample_prior(spec, ell=0.2, n_samples=5)

Variance Parameter

The variance sigma2 controls amplitude:

# Small amplitude
result_small = sample_prior(spec, sigma2=0.1, ell=0.5, n_samples=5)

# Large amplitude
result_large = sample_prior(spec, sigma2=10.0, ell=0.5, n_samples=5)

Next Steps