Skip to content

Testing

Test suite and validation.

Running Tests

Run all tests:

pytest tests/ -v

Run specific test file:

pytest tests/test_gp4c.py -v

Run with coverage:

pytest tests/ --cov=gp4c --cov-report=html

Test Coverage

The test suite includes:

Sampling Combinations

All 7 combinations of (f, g, h):

  • f only
  • g only
  • h only
  • f + g
  • f + h
  • g + h
  • f + g + h
def test_all_combinations():
    """Test all valid sampling combinations."""
    # Tests each combination with assertions

Mathematical Relationships

Integral Relationship

Verify g matches numerical integration of f:

def test_integral_relationship():
    """g should match cumulative integral of f."""
    # Compare analytical g with numerical integration

Derivative Relationship

Verify h correlates with numerical gradient of f:

def test_derivative_relationship():
    """h should correlate with numerical gradient of f."""
    # Compare analytical h with finite differences

Cross-Validation

Derivative of integral recovers f:

def test_cross_validation():
    """Derivative of g should recover f."""
    # d/dx[g(x)] ≈ f(x)

Posterior Interpolation

Mean passes through training points:

def test_posterior_interpolation():
    """Posterior mean should interpolate training data."""
    # Check f_mean at x_train matches y_train

Numerical Stability

Different Grid Sizes

def test_grid_sizes():
    """Test various grid sizes from 10 to 1000."""

Length Scale Range

def test_length_scales():
    """Test stability across length scales 0.1 to 10.0."""

Derivative Stability

def test_derivative_stability():
    """Derivative kernel with small ell can be unstable."""

Reproducibility

Same seed produces same results:

def test_reproducibility():
    """Same seed should give identical samples."""

Input Validation

Invalid Specifications

def test_invalid_spec():
    """Should raise error for empty SamplingSpec."""

Mismatched Observations

def test_mismatched_observations():
    """x and y arrays must have same length."""

Invalid Parameters

def test_invalid_parameters():
    """Negative ell or sigma2 should raise error."""

Test Organization

tests/
├── test_gp4c.py               # Core sampling tests
├── test_flexible_sampler.py   # Flexible API tests
└── test_posterior.py          # Posterior sampling tests

Custom Test

Write your own validation:

import numpy as np
from gp4c import sample_prior, SamplingSpec

def test_custom():
    """Custom test case."""
    x = np.linspace(0, 5, 100)
    spec = SamplingSpec(x_f=x, x_h=x)
    result = sample_prior(spec, ell=0.5, n_samples=10)

    # Verify shapes
    assert result.f.shape == (10, 100)
    assert result.h.shape == (10, 100)
    assert result.g is None

    # Verify finite values
    assert np.all(np.isfinite(result.f))
    assert np.all(np.isfinite(result.h))

Continuous Integration

Tests run automatically on:

  • Pull requests
  • Commits to main branch
  • Scheduled daily builds

Next Steps