Skip to main content

fewlab: fewest items to label for most efficient unbiased OLS on shares

Python application Documentation PyPI version Downloads License: MIT Python

Problem: You have usage data (users × items) and want to understand how user traits relate to item preferences. But you can't afford to label every item. This tool tells you which items to label first to get the most accurate analysis.

When You Need This

You have:

  • A usage matrix: rows are users, columns are items (websites, products, apps)
  • User features you want to analyze (demographics, behavior patterns)
  • Limited budget to label items (safe/unsafe, brand affiliation, category)

You want to run a regression to understand relationships between user features and item traits, but labeling is expensive. Random sampling wastes budget on items that don't affect your analysis.

How It Works

The tool identifies items that most influence your regression coefficients. It prioritizes items that:

  1. Are used by many people
  2. Show different usage patterns across your user segments
  3. Would most change your conclusions if mislabeled

Think of it as "statistical leverage"—some items matter more for understanding user-trait relationships.

Quick Start

from fewlab import Design
import pandas as pd

# Your data: user features and item usage
user_features = pd.DataFrame(...)  # User characteristics
item_usage = pd.DataFrame(...)     # Usage counts per user-item

# Create design (caches expensive computations)
design = Design(item_usage, user_features)

# Get top 100 items to label
priority_items = design.select(budget=100)

# Send priority_items to your labeling team
print(f"Label these items first: {priority_items}")

Advanced Usage

from fewlab import Design
import pandas as pd

# Create design with automatic ridge detection
design = Design(item_usage, user_features, ridge="auto")

# Multiple selection strategies
deterministic_items = design.select(budget=100, method="deterministic")
greedy_items = design.select(budget=100, method="greedy")

# Probabilistic sampling methods
balanced_sample = design.sample(budget=100, method="balanced", seed=42)
hybrid_sample = design.sample(budget=100, method="core_plus_tail", tail_frac=0.3)
adaptive_sample = design.sample(budget=100, method="adaptive")

# Get inclusion probabilities
pi_aopt = design.inclusion_probabilities(budget=100, method="aopt")
pi_rowse = design.inclusion_probabilities(budget=100, method="row_se", eps2=0.01)

# Complete workflow: select, weight, estimate
selected = design.select(budget=50)
labels = pd.Series([1, 0, 1, ...], index=selected)  # Your labels
weights = design.calibrate_weights(selected)
estimates = design.estimate(selected, labels)

# Access diagnostics
print(f"Condition number: {design.diagnostics['condition_number']:.2e}")
print(f"Influence weights: {design.influence_weights.head()}")

What You Get

Primary Interface:

  • Design: Object-oriented API that caches expensive computations and provides unified access to all methods

Selection Methods:

  • .select(method="deterministic"): Batch A-optimal top-budget items (fastest)
  • .select(method="greedy"): Sequential greedy A-optimal selection
  • .sample(method="balanced"): Balanced probabilistic sampling
  • .sample(method="core_plus_tail"): Hybrid deterministic + probabilistic
  • .sample(method="adaptive"): Data-driven hybrid with automatic parameters

Probability Methods:

  • .inclusion_probabilities(method="aopt"): A-optimal square-root rule
  • .inclusion_probabilities(method="row_se"): Row-wise standard error minimization

Complete Workflow:

  • .calibrate_weights(): GREG-style weight calibration
  • .estimate(): Calibrated Horvitz-Thompson estimation
  • .diagnostics: Comprehensive design diagnostics

All methods leverage cached influence computations for efficiency and provide consistent, structured results.

Practical Considerations

Choosing budget: Start with 10-20% of items. You can always label more if needed.

Validation: Compare regression stability with different budget values. When coefficients stop changing significantly, you have enough labels.

Performance: The Design class caches expensive influence computations, making multiple method calls efficient.

Limitations:

  • Works best when usage patterns correlate with user features
  • Assumes item labels are binary (has trait / doesn't have trait)
  • Most effective for sparse usage matrices

Advanced: Ensuring Unbiased Estimates

The basic approach gives you optimal items to label but technically requires some randomization for completely unbiased statistical estimates. If you need formal statistical guarantees, add a small random sample on top of the priority list. See the statistical details for more.

Installation

pip install fewlab

Requirements: Python 3.12-3.14, numpy ≥1.23, pandas ≥1.5

Development:

pip install -e ".[dev]"  # Includes testing, linting, pre-commit hooks
pip install -e ".[docs]" # Includes documentation building

What's New in v1.0.0

  • 🎯 Object-Oriented API: New Design class caches expensive computations and provides unified interface
  • 🚀 Performance: Eliminate redundant influence computations across multiple method calls
  • 📊 Structured Results: Typed result classes replace loose tuples for better API consistency
  • 🔧 Standardized Parameters: All functions use budget parameter (was K), no backward compatibility
  • 📈 Comprehensive Diagnostics: Automatic condition number monitoring and ridge selection
  • 🧪 Enhanced Testing: Full test coverage for new Design class and edge cases
  • 🐍 Modern Python: Requires Python 3.12-3.14, uses latest type annotations
  • 🛡️ Robust Validation: Enhanced input validation with helpful error messages

Development

To contribute to this project, install dependencies and set up pre-commit hooks:

uv sync --all-groups
uv run pre-commit install

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fewlab-2.0.0.tar.gz (33.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fewlab-2.0.0-py3-none-any.whl (40.5 kB view details)

Uploaded Python 3

File details

Details for the file fewlab-2.0.0.tar.gz.

File metadata

  • Download URL: fewlab-2.0.0.tar.gz
  • Upload date:
  • Size: 33.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for fewlab-2.0.0.tar.gz
Algorithm Hash digest
SHA256 e6b8524f19555ee2e3c9f749f22f5aa35f5387eea0adf39f5b0fa9f4317d52a7
MD5 1be98921e5448eec2f0d4cbbd2e57cb2
BLAKE2b-256 e2f80909401d9c7c9912fb246dd69d6842babc7a83b60699bb1a32e2604adf80

See more details on using hashes here.

Provenance

The following attestation bundles were made for fewlab-2.0.0.tar.gz:

Publisher: python-publish.yml on finite-sample/fewlab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fewlab-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: fewlab-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 40.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for fewlab-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e90a9b02e3fd126d6b7de466419af8259599e0930cc77f4ecb3d57ca789eca9c
MD5 ee08d5d2ef0a56586ca599c0e294bbdf
BLAKE2b-256 f041c3a01329a62a5643acf8f5867e72062511404dc1d656c47c9fb91273dd69

See more details on using hashes here.

Provenance

The following attestation bundles were made for fewlab-2.0.0-py3-none-any.whl:

Publisher: python-publish.yml on finite-sample/fewlab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 files

1.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page