0
# Core Gaussian Process Operations
1
2
Fundamental functionality for constructing, evaluating, and conditioning Gaussian processes. This includes creating GPs with custom kernels and means, evaluating them at specific input points to create finite-dimensional distributions, and performing Bayesian conditioning on observations to obtain posterior distributions.
3
4
## Capabilities
5
6
### Gaussian Process Construction
7
8
Create Gaussian processes with specified mean functions and kernels. The GP class supports flexible initialization with automatic measure management and optional naming.
9
10
```python { .api }
11
class GP(RandomProcess):
12
def __init__(self, mean=None, kernel=None, *, measure=None, name=None):
13
"""
14
Initialize a Gaussian process.
15
16
Parameters:
17
- mean: Mean function (mlkernels.Mean), function, or numeric. Defaults to zero.
18
- kernel: Kernel function (mlkernels.Kernel). Required.
19
- measure: Measure to attach to. Optional.
20
- name: Name for the GP. Optional.
21
"""
22
23
def __init__(self, kernel, *, measure=None, name=None):
24
"""Initialize GP with zero mean and specified kernel."""
25
26
def __init__(self):
27
"""Initialize empty GP (for advanced use cases)."""
28
```
29
30
### GP Properties
31
32
Access key properties of Gaussian processes including their associated measure, kernel, mean function, and name.
33
34
```python { .api }
35
class GP:
36
@property
37
def measure(self) -> Measure:
38
"""Measure that the GP was constructed with."""
39
40
@property
41
def kernel(self):
42
"""Kernel of the GP."""
43
44
@property
45
def mean(self):
46
"""Mean function of the GP."""
47
48
@property
49
def name(self) -> str:
50
"""Name of the GP."""
51
52
@name.setter
53
def name(self, name: str):
54
"""Set the name of the GP."""
55
56
@property
57
def stationary(self) -> bool:
58
"""Whether the GP is stationary."""
59
```
60
61
### Finite-Dimensional Distributions
62
63
Evaluate Gaussian processes at specific input points to create finite-dimensional distributions. These represent the GP's beliefs at the specified locations and can include additive noise.
64
65
```python { .api }
66
class GP:
67
def __call__(self, x, noise=None) -> FDD:
68
"""
69
Construct a finite-dimensional distribution at specified locations.
70
71
Parameters:
72
- x: Input points to evaluate at
73
- noise: Optional additive noise (scalar, vector, or matrix)
74
75
Returns:
76
- FDD: Finite-dimensional distribution
77
"""
78
```
79
80
```python { .api }
81
class FDD(Normal):
82
def __init__(self, p, x, noise=None):
83
"""
84
Initialize finite-dimensional distribution.
85
86
Parameters:
87
- p: Gaussian process or process ID
88
- x: Input points
89
- noise: Optional additive noise
90
"""
91
92
def __init__(self, p, x):
93
"""Initialize FDD without noise."""
94
95
p: Union[GP, int] # Process of FDD
96
x: Any # Inputs that process is evaluated at
97
noise: Optional[Any] # Additive noise matrix
98
```
99
100
### Conditioning and Posterior Inference
101
102
Condition Gaussian processes on observations to obtain posterior distributions. Supports both direct conditioning and shorthand operator syntax.
103
104
```python { .api }
105
class GP:
106
def condition(self, *args) -> GP:
107
"""
108
Condition on observations and obtain posterior GP.
109
110
Parameters can be:
111
- observations: Observations object
112
- fdd, y: FDD and corresponding values
113
- Multiple (fdd, y) pairs
114
115
Returns:
116
- GP: Posterior Gaussian process
117
"""
118
119
def __or__(self, *args) -> GP:
120
"""Shorthand for condition() using | operator."""
121
```
122
123
### GP Display and Inspection
124
125
Display Gaussian processes with optional custom formatting for debugging and inspection purposes.
126
127
```python { .api }
128
class GP:
129
def display(self, formatter=None):
130
"""
131
Display GP with optional formatting.
132
133
Parameters:
134
- formatter: Optional custom formatter function
135
"""
136
```
137
138
### Utility Functions
139
140
Helper functions for working with multiple Gaussian processes, including validation and intersection operations.
141
142
```python { .api }
143
def assert_same_measure(*ps):
144
"""
145
Assert that processes are associated to the same measure.
146
147
Parameters:
148
- *ps: Gaussian processes to check
149
150
Raises:
151
- AssertionError: If processes have different measures
152
"""
153
154
def intersection_measure_group(*ps):
155
"""
156
Get the intersection of measures associated to processes.
157
158
Parameters:
159
- *ps: Gaussian processes
160
161
Returns:
162
- set: Intersection of measure groups
163
"""
164
```
165
166
## Usage Examples
167
168
### Basic GP Construction and Evaluation
169
170
```python
171
import stheno
172
import numpy as np
173
174
# Create GP with exponential quadratic kernel
175
gp = stheno.GP(kernel=stheno.EQ())
176
177
# Evaluate at specific points
178
x = np.linspace(0, 1, 10)
179
fdd = gp(x)
180
181
# Access properties
182
print(f"Mean: {fdd.mean}")
183
print(f"Variance diagonal: {fdd.var_diag}")
184
```
185
186
### Conditioning on Observations
187
188
```python
189
# Generate synthetic observations
190
y = np.sin(x) + 0.1 * np.random.randn(len(x))
191
192
# Condition GP on observations using | operator
193
posterior = gp | (fdd, y)
194
# or using explicit condition method:
195
posterior = gp.condition(fdd, y)
196
197
# Make predictions
198
x_new = np.linspace(0, 1, 50)
199
pred = posterior(x_new)
200
mean, lower, upper = pred.marginal_credible_bounds()
201
```
202
203
### Working with Named GPs
204
205
```python
206
# Create named GP
207
gp = stheno.GP(kernel=stheno.EQ(), name="signal")
208
print(f"GP name: {gp.name}")
209
210
# Change name
211
gp.name = "updated_signal"
212
```
213
214
### Adding Noise
215
216
```python
217
# Add scalar noise
218
noisy_fdd = gp(x, noise=0.1)
219
220
# Add heteroscedastic noise
221
noise_var = np.random.uniform(0.05, 0.2, len(x))
222
noisy_fdd = gp(x, noise=noise_var)
223
224
# Add correlated noise (full covariance)
225
noise_cov = 0.1 * np.eye(len(x))
226
noisy_fdd = gp(x, noise=noise_cov)
227
```