Coverage for gemlib/mcmc/test_util.py: 100%
19 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-18 09:01 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-18 09:01 +0000
1"""Simple counting kernel for testing"""
3from typing import NamedTuple
5from .sampling_algorithm import (
6 ChainAndKernelState,
7 ChainState,
8 SamplingAlgorithm,
9)
11__all__ = ["CountingKernelInfo", "CountingKernelState", "counting_kernel"]
14class CountingKernelState(NamedTuple):
15 invocation: int
18class CountingKernelInfo(NamedTuple):
19 is_accepted: bool
22def counting_kernel():
23 def init_fn(target_log_prob_fn, position):
24 chain_state = ChainState(
25 position=position,
26 log_density=target_log_prob_fn(**position._asdict()),
27 log_density_grad=(),
28 )
29 kernel_state = CountingKernelState(0)
31 return ChainAndKernelState(chain_state, kernel_state)
33 def step_fn(target_log_prob_fn, chain_and_kernel_state, seed): # noqa: ARG001
34 chain_state, kernel_state = chain_and_kernel_state
36 new_position = chain_state.position.__class__(
37 **{k: v + 1.0 for k, v in chain_state.position._asdict().items()}
38 )
40 new_chain_state = ChainState(
41 position=new_position,
42 log_density=target_log_prob_fn(**new_position._asdict()),
43 log_density_grad=(),
44 )
45 new_kernel_state = CountingKernelState(kernel_state.invocation + 1)
47 return ChainAndKernelState(
48 new_chain_state, new_kernel_state
49 ), CountingKernelInfo(True)
51 return SamplingAlgorithm(init_fn, step_fn)