Mean Functions¶
GPs with non-zero mean functions.
Zero Mean (Default)¶
By default, GPs have zero mean:
from gp4c import sample_prior, SamplingSpec
import numpy as np
x = np.linspace(0, 5, 100)
spec = SamplingSpec(x_f=x, x_g=x)
result = sample_prior(spec, ell=0.5, n_samples=5)
# f samples fluctuate around 0
Constant Mean¶
Specify a constant mean function:
from gp4c import sample_prior, SamplingSpec, MeanSpec
mean = MeanSpec(type='constant', value=2.5)
spec = SamplingSpec(x_f=x, x_g=x)
result = sample_prior(
spec,
ell=0.5,
n_samples=5
)
# f samples fluctuate around 2.5
Mean Function Propagation¶
For constant mean \(c\):
- \(m_f(x) = c\) — function mean
- \(m_g(x) = c \cdot x\) — integral mean (integral of constant)
- \(m_h(x) = 0\) — derivative mean (derivative of constant)
mean = MeanSpec(type='constant', value=3.0)
spec = SamplingSpec(x_f=x, x_g=x, x_h=x)
result = sample_prior(spec, mean=mean, ell=0.5, n_samples=5)
# f samples: fluctuate around 3.0
# g samples: fluctuate around 3.0 * x
# h samples: fluctuate around 0
Mean in Posterior Sampling¶
Mean functions work with posterior sampling:
from gp4c import sample_posterior, Observations, SamplingSpec, MeanSpec
# Observations with non-zero trend
x_train = np.array([0.0, 1.0, 2.0, 3.0, 4.0])
y_train = 2.0 + np.sin(x_train) # Constant + fluctuation
mean = MeanSpec(type='constant', value=2.0)
obs = Observations(x_f=x_train, y_f=y_train, noise_f=0.01)
x_test = np.linspace(0, 5, 100)
spec = SamplingSpec(x_f=x_test, mean=mean)
result = sample_posterior(obs, spec, ell=1.0, n_samples=10)
# Posterior captures trend + deviation
Specifying Mean in SamplingSpec¶
Mean can be set in SamplingSpec:
spec = SamplingSpec(
x_f=x,
x_g=x,
mean=MeanSpec(type='constant', value=1.5)
)
result = sample_prior(spec, ell=0.5, n_samples=5)
Or passed directly to sampling functions:
The mean parameter in the function call takes precedence over the one in SamplingSpec.
Zero Mean Explicitly¶
These are equivalent:
# Implicit zero mean
result = sample_prior(spec, ell=0.5, n_samples=5)
# Explicit zero mean
mean = MeanSpec(type='zero')
result = sample_prior(spec, mean=mean, ell=0.5, n_samples=5)
# Or just use None
result = sample_prior(spec, mean=None, ell=0.5, n_samples=5)
Next Steps¶
- Posterior Sampling - Conditioning with mean functions
- API Reference - MeanSpec details