API Reference

Installation | Core concepts | Mechanisms | Patterns | Mathematical details | Benchmarking workflow | API reference

simulate_missingness

simulate_missingness(
    X,
    mechanism,
    missing_rate,
    seed=None,
    pattern="pointwise",
    **kwargs,
)

Simulate missingness in a 2D (time, features) or 3D (samples, time, features) NumPy array.

Returns:

X_missing, mask

where X_missing is a copy of X with NaNs inserted and mask is a boolean array with True for observed values and False for missing values.

Core parameters:

Parameter

Type

Description

X

np.ndarray

Real numeric data, shape (T, D) or (N, T, D). Integer input is promoted to float; Boolean, complex, object, and string arrays are rejected.

mechanism

str

"mcar", "mar", or "mnar"

missing_rate

float

Target fraction missing, clipped to [0, 1]

seed

int | None

Random seed

pattern

str

Missingness pattern

Unknown mechanism- or pattern-specific keyword arguments raise TypeError, so misspelled configuration names cannot be silently ignored. Numerical configuration values must be finite, and infinite input values are rejected.

Mechanism parameters:

Parameter

Mechanisms

Default

Description

target

all

"all"; MAR defaults to non-driver dimensions

Dimensions to mask

driver_dims

MAR

[0]

Driver dimensions

driver_weights

MAR

None

Per-driver weights

strength

MAR, MNAR

2.0

Dependency strength

base_rate

MAR

0.01

Minimum probability floor

direction

MAR

"positive"

Driver direction

allow_self_masking

MAR

False

Allow target/driver overlap for explicit hybrid behavior

mnar_mode

MNAR

"extreme"

MNAR scoring mode

Pattern parameters:

Parameter

Patterns

Default

Description

block_len

block

10

Block length in timesteps

block_frac

block

None

Relative block length as a fraction of the time axis, or (min_frac, max_frac) for variable-length blocks. Recommended for long time series.

block_density

block

1.0

Fraction of missingness in blocks. Set below 1.0 to retain some pointwise missingness.

decay_rate

decay

3.0

Decay ramp steepness

decay_center

decay

0.7

Normalized ramp center

persist

markov, gilbert_elliott

0.8

Missing/bad-state persistence. For gilbert_elliott, MCAR only.

bad_loss

gilbert_elliott

1.0

Loss probability in the bad state. MCAR only.

good_loss

gilbert_elliott

0.0

Loss probability in the good state. MCAR only. See the feasibility constraint below.

gilbert_elliott supports only mechanism="mcar". missing_rate=0 adds no artificial missingness and missing_rate=1 masks all eligible entries. Partial rates must satisfy good_loss <= missing_rate <= good_loss + (bad_loss - good_loss) / (2 - persist). Rates outside this persistence-aware feasible range raise ValueError.

For the Markov pattern, partial missing rates must satisfy missing_rate <= 1 / (2 - persist). Infeasible combinations raise ValueError; missing_rate=1.0 remains an explicit all-missing special case.

For pointwise, MAR and MNAR dependencies apply to the final individual masked positions. For temporal patterns such as block, decay, markov, and monotone, MAR/MNAR generate a mechanism-informed missingness budget that is then reshaped in time by the selected pattern.

The monotone pattern applies its allocated budget to the final eligible timestamps in each series. Pre-existing NaNs remain missing but do not consume the artificial missingness budget or define the artificial dropout point.

simulate_many_rates

from tsgap import simulate_many_rates

rates = [0.05, 0.15, 0.25]
results = simulate_many_rates(X, "mcar", rates, seed=42)

Returns a dictionary mapping each rate to (X_missing, mask). When a seed is provided, each rate gets a deterministic offset from the base seed.

MissingnessSimulator

from tsgap import MissingnessSimulator

sim = MissingnessSimulator(
    "mar", missing_rate=0.25, seed=42,
    driver_dims=[0], pattern="block", block_len=10
)

X_missing, mask = sim.generate(X)

This object-oriented wrapper is useful when the same missingness configuration is applied to multiple arrays.

Registries

from tsgap import MECHANISMS, PATTERNS

print(MECHANISMS.keys())
print(PATTERNS.keys())

The registries expose the currently available mechanism and pattern names, including aliases.