boofun.core.spaces

Mathematical spaces for Boolean function analysis.

Provides conversions between different representations: - BOOLEAN_CUBE: {0, 1}^n - PLUS_MINUS_CUBE: {-1, +1}^n - REAL: ℝ^n - LOG: Log-probability space - GAUSSIAN: Standard normal space

WARNING: Some conversions are lossy (continuous → discrete). These will emit warnings unless explicitly silenced.

Classes

Measure([p])

A probability measure on the Boolean hypercube.

Space(value)

class boofun.core.spaces.Space(value)[source]
BOOLEAN_CUBE = 1
PLUS_MINUS_CUBE = 2
REAL = 3
LOG = 4
GAUSSIAN = 5
static translate(input: int | float | ndarray, source_space: Space, target_space: Space) int | float | ndarray[source]

Translate a scalar or array from one space to another.

Parameters:
  • input – Input value(s) to translate

  • source_space – Source mathematical space

  • target_space – Target mathematical space

Returns:

Translated value(s) in target space

Examples

>>> Space.translate([0, 1], Space.BOOLEAN_CUBE, Space.PLUS_MINUS_CUBE)
array([-1,  1])
>>> Space.translate([-1, 1], Space.PLUS_MINUS_CUBE, Space.BOOLEAN_CUBE)
array([0, 1])
static get_canonical_space() Space[source]

Get the canonical space for internal computations.

static is_discrete(space: Space) bool[source]

Check if space uses discrete values.

static is_continuous(space: Space) bool[source]

Check if space uses continuous values.

static get_default_threshold(space: Space) float[source]

Get default threshold for converting continuous to discrete.

class boofun.core.spaces.Measure(p: float = 0.5)[source]

A probability measure on the Boolean hypercube.

Supports uniform (p=0.5) and p-biased measures where each bit is 1 independently with probability p.

This unifies p-biased analysis: instead of calling separate functions from analysis/p_biased.py, pass a Measure to standard methods.

Examples

>>> uniform = Measure.uniform()
>>> biased = Measure.p_biased(0.3)
>>> biased.p       # 0.3
>>> biased.sigma    # sqrt(0.3 * 0.7)
__init__(p: float = 0.5)[source]
classmethod uniform() Measure[source]

The uniform measure (p = 1/2).

classmethod p_biased(p: float) Measure[source]

P-biased measure: each bit is 1 with probability p.

property p: float

Bias parameter.

property sigma: float

sqrt(p(1-p)).

Type:

Standard deviation of a single bit

property is_uniform: bool

True if this is the uniform measure (p = 0.5).

sample(n: int, rng=None) ndarray[source]

Sample a random input from this measure.

Parameters:
  • n – Number of variables

  • rng – NumPy random generator (optional)

Returns:

Binary array of length n

sample_batch(n: int, n_samples: int, rng=None) ndarray[source]

Sample multiple inputs from this measure.

Parameters:
  • n – Number of variables

  • n_samples – Number of samples

  • rng – NumPy random generator (optional)

Returns:

Array of shape (n_samples, n) with binary entries

weight(x) float[source]

Probability of a specific input under this measure.

Parameters:

x – Input as integer index or binary array

Returns:

mu_p(x) = p^|x| * (1-p)^(n-|x|)