Stats Bincount#
bincount
#
| CLASS | DESCRIPTION |
|---|---|
RunningBincount |
Running histogram for integer-valued data with optional sample weights. |
Classes#
RunningBincount(state: dict | None = None)
#
Bases: Stats
Running histogram for integer-valued data with optional sample weights.
Maintains a count (or weighted sum) of occurrences for each unique integer value, incrementally updated across batches. Useful for computing histograms of categorical or discrete data without storing all samples. Supports weighted samples for robust statistics and importance sampling.
| PARAMETER | DESCRIPTION |
|---|---|
state
|
Previous state to restore from.
TYPE:
|
Properties
count : int Total number of elements processed.
bin_count : torch.Tensor Histogram counts (or weighted sums) for each integer value (length = max_value + 1).
Notes
Input tensors must contain non-negative integers. The bin_count tensor automatically expands to accommodate larger values as they are encountered.
When weights are provided, each bin accumulates the sum of weights for samples with that value instead of just counting occurrences.
Examples:
Basic usage with categorical data:
>>> from spectre.stats import RunningBincount
>>> import torch
>>>
>>> stat = RunningBincount()
>>> for _ in range(10):
... batch = torch.randint(0, 5, (100,)) # Categories 0-4
... stat.add(batch)
>>>
>>> histogram = stat.bin_count()
>>> histogram.shape
torch.Size([5])
Weighted histogram:
>>> stat = RunningBincount()
>>> labels = torch.tensor([0, 1, 1, 2, 2, 2, 3])
>>> weights = torch.tensor([1.0, 2.0, 3.0, 0.5, 1.5, 2.0, 1.0])
>>> stat.add(labels, weights=weights)
>>> weighted_counts = stat.bin_count()
>>> # weighted_counts[0] = 1.0, weighted_counts[1] = 5.0, etc.
Counting class labels:
>>> stat = RunningBincount()
>>> labels = torch.tensor([0, 1, 1, 2, 2, 2, 3])
>>> stat.add(labels)
>>> counts = stat.bin_count()
>>> counts # [1, 2, 3, 1] - counts for classes 0, 1, 2, 3
| METHOD | DESCRIPTION |
|---|---|
add |
Add batch of integer values to histogram with optional sample weights. |
to |
Move internal tensors to specified device. |
state_dict |
Save state to dictionary. |
load_state_dict |
Load state from dictionary. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
count |
Get total number of elements processed.
TYPE:
|
bin_count |
Get current histogram counts.
TYPE:
|
Source code in spectre/stats/bincount.py
Attributes#
count: int
property
#
Get total number of elements processed.
bin_count: torch.Tensor
property
#
Get current histogram counts.
Functions#
add(X: torch.Tensor, weights: torch.Tensor | None = None, count: int | None = None) -> None
#
Add batch of integer values to histogram with optional sample weights.
Automatically expands bin_count tensor to accommodate new larger values. When weights are provided, each bin accumulates the sum of weights for samples with that value instead of just counting occurrences.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Integer tensor with non-negative values. Automatically flattened.
TYPE:
|
weights
|
Sample weights with same shape as X. If None, all samples have weight 1.0 (equivalent to standard counting). By default None.
TYPE:
|
count
|
Explicit size to add to count (overrides X.numel()).
TYPE:
|