Learn R Programming

resLIK (version 0.1.2)

reslik: Residual Likelihood Sensor

Description

The Residual Likelihood (ResLik) sensor measures the conformity of a latent representation against a population reference distribution. It acts as a soft gate, suppressing "out-of-distribution" (OOD) signals while preserving "in-distribution" (ID) fidelity.

Usage

reslik(z, ref_mean = 0, ref_sd = 1, lambda = 1, tau = 0.05)

Value

A list containing:

gated

The gated representation (same shape as z).

diagnostics

A list of diagnostic metrics:

  • discrepancy: The raw discrepancy scores.

  • max_discrepancy: The maximum discrepancy in the batch.

  • mean_discrepancy: The mean discrepancy in the batch.

Arguments

z

Numeric vector or matrix. The latent representation to evaluate.

ref_mean

Numeric or vector. The reference mean of the population. Defaults to 0.

ref_sd

Numeric or vector. The reference standard deviation of the population. Defaults to 1.

lambda

Numeric. The sensitivity of the gate. Higher values suppress OOD samples more aggressively. Defaults to 1.0.

tau

Numeric. The dead-zone threshold. Discrepancies below this value are ignored. Defaults to 0.05.

Details

Population-Level Sensor (ResLik)

The sensor operates in four steps:

  1. Normalization: The input z is Z-score normalized using ref_mean and ref_sd.

  2. Discrepancy: The Mean Absolute Deviation (MAD) is computed for each sample.

  3. Gating: A gating factor is computed as exp(-lambda * max(0, discrepancy - tau)). This creates a "dead-zone" tau where minor deviations are ignored.

  4. Output: The original z is scaled by the gating factor.

This implementation is fully deterministic and stateless.

Examples

Run this code
# Example 1: In-Distribution Sample
z_id <- c(0.1, -0.2, 0.05)
out_id <- reslik(z_id)
print(out_id$gated) # Should be close to z_id

# Example 2: Out-of-Distribution Sample
z_ood <- c(5.0, 5.0, 5.0)
out_ood <- reslik(z_ood)
print(out_ood$gated) # Should be strongly suppressed

Run the code above in your browser using DataLab