Inverse variance channel weighting

From wenmin-wu/ds-skills · View interactive version

Inverse variance channel weighting is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Weight multi-channel signals by inverse per-channel variance with percentile clipping, emphasizing low-noise channels in aggregation

How to use this skill

Skill file

Inverse-Variance Channel Weighting

Overview

When aggregating multi-channel (multi-band, multi-sensor) time series, weight each channel by the inverse of its temporal variance. Noisy channels get downweighted; stable channels dominate. Clip extreme weights at the 5th/95th percentile to prevent any single channel from dominating, then normalize so weights sum to the number of channels (preserving scale).

Quick Start

import numpy as np

def inverse_variance_weight(data, clip_pct=(5.0, 95.0)):
    """Weight channels by inverse variance.
    
    Args:
        data: (n_timesteps, n_channels) array
        clip_pct: percentile bounds for weight clipping
    Returns:
        weighted data (n_timesteps, n_channels)
    """
    var = np.nanvar(data, axis=0, ddof=1)
    median_var = np.nanmedian(var)
    
    # Replace invalid variances with median
    safe_var = np.where(~np.isfinite(var) | (var <= 0), median_var, var)
    weights = 1.0 / safe_var
    
    # Clip extremes
    lo, hi = np.nanpercentile(weights, clip_pct)
    if lo < hi:
        weights = np.clip(weights, lo, hi)
    
    # Normalize to preserve scale
    n_channels = data.shape[1]
    weights *= n_channels / np.nansum(weights)
    
    return data * weights[None, :]

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.