Density to count roundtrip

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

Density to count roundtrip is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Convert a density metric back to integer counts using known population, round to nearest integer, then recompute density to exploit the discrete nature of the target

How to use this skill

Skill file

Density-to-Count Roundtrip

Overview

When the target variable is a ratio (count / population), the true underlying quantity is an integer count. Predictions that ignore this produce values on a continuous scale that can never exactly match the true density. Converting predicted density to integer count via the known population denominator, rounding, and converting back snaps predictions to the set of possible true values — often improving SMAPE by 0.001-0.01.

Quick Start

import numpy as np

def roundtrip(predicted_density, population):
    count = predicted_density * population / 100  # density is per 100 people
    count_rounded = np.round(count).astype(int)
    count_rounded = np.maximum(count_rounded, 0)
    return count_rounded / population * 100

df["pred_snapped"] = roundtrip(df["pred_density"], df["population"])

Workflow

  1. Obtain the population denominator for each entity (e.g., from Census ACS data by FIPS code)
  2. Convert predicted density to raw count: count = density * population / scale_factor
  3. Round count to the nearest non-negative integer
  4. Convert back: snapped_density = rounded_count / population * scale_factor
  5. Use snapped density as the final submission

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.