Skip to content

fix(core): include reference-only distribution categories - #2630

Open
Rana Singh (ranadeepsingh) wants to merge 1 commit into
microsoft:masterfrom
ranadeepsingh:copilot/issue-2010-reference-categories
Open

fix(core): include reference-only distribution categories#2630
Rana Singh (ranadeepsingh) wants to merge 1 commit into
microsoft:masterfrom
ranadeepsingh:copilot/issue-2010-reference-categories

Conversation

@ranadeepsingh

@ranadeepsingh Rana Singh (ranadeepsingh) commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2010

Root cause

DistributionBalanceMeasure derived its category rows only from observed data, then looked up custom reference probabilities on those rows. A category that existed only in the reference map therefore never reached any metric aggregation, truncating the reference distribution.

Change

  • Materialize the positive-probability reference support as a DataFrame with the sensitive column's integral or string type.
  • Union it with distributed observed statistics and aggregate by category, assigning zero observed probability/count to reference-only categories and zero reference probability/count to observed-only categories.
  • Validate custom probabilities, sums, numeric key conversion, and duplicate typed keys.
  • Preserve empty-map uniform behavior, output schema, public setters, and legacy results.
  • Preserve custom-reference parameter persistence and add a focused round-trip regression.
  • Clarify custom-reference support semantics in the data-balance documentation.

Tests

All SBT commands ran through the JDK 11 synapseml-sbt.sh wrapper while holding the named mutex SynapseMLSbtValidation for the full command.

  • core/testOnly com.microsoft.azure.synapse.ml.exploratory.DistributionBalanceMeasureSuite — 19/19 passed.
  • core/testOnly com.microsoft.azure.synapse.ml.exploratory.AggregateBalanceMeasureSuite com.microsoft.azure.synapse.ml.exploratory.DistributionBalanceMeasureSuite com.microsoft.azure.synapse.ml.exploratory.FeatureBalanceMeasureSuite — 36/36 passed.
  • core/compile — passed.
  • scalastyle and test:scalastyle — passed with zero findings.
  • core/codegen — passed; Python and R wrappers generated.
  • Filtered UDFTransformerSuite local smoke test — 1/1 passed.
  • black --check --extend-exclude 'docs/' . — passed (189 files unchanged).

Regression coverage includes reference-only and observed-only categories, mixed map ordering and integral/string types, explicit zero probabilities, invalid sums/probabilities/keys, persistence, and unchanged issue-example results.

Performance notes

The observed support remains fully distributed. The implementation does not collect observed categories or use RDD APIs; only the already driver-resident reference parameter is converted to a small DataFrame. unionByName plus groupBy performs support alignment on Spark and retains the existing per-feature support-cardinality action.

Review

The project code-review checklist was applied independently to numerical correctness, DataFrame scalability, API compatibility, serialization, and edge cases. No high- or medium-severity findings remained. A later automated review note about NaN test assertions was fixed, and the release-branch patch was adjusted to apply cleanly to spark4.1.

Copilot AI lite review requested due to automatic review settings August 14, 2026 20:55
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@ranadeepsingh

Copy link
Copy Markdown
Collaborator Author

/azp run

@github-actions

Copy link
Copy Markdown

Hey Rana Singh (@ranadeepsingh) 👋!
Thank you so much for contributing to our repository 🙌.
Someone from SynapseML Team will be reviewing this pull request soon.

We use semantic commit messages to streamline the release process.
Before your pull request can be merged, you should make sure your first commit and PR title start with a semantic prefix.
This helps us to create release messages and credit you for your hard work!

Examples of commit messages with semantic prefixes:

  • fix: Fix LightGBM crashes with empty partitions
  • feat: Make HTTP on Spark back-offs configurable
  • docs: Update Spark Serving usage
  • build: Add codecov support
  • perf: improve LightGBM memory usage
  • refactor: make python code generation rely on classes
  • style: Remove nulls from CNTKModel
  • test: Add test coverage for CNTKModel

To test your commit locally, please follow our guild on building from source.
Check out the developer guide for additional guidance on testing your change.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes DistributionBalanceMeasure in the core exploratory module so that custom reference distributions correctly contribute categories that appear only in the reference map (not observed in the dataset), ensuring distance metrics (e.g., JSD) are computed over the full intended support. It also updates documentation to describe the new/clarified support semantics and adds regression tests covering reference-only and observed-only categories, key typing, validation, and persistence.

Changes:

  • Materialize reference-only (positive-probability) categories into a small DataFrame, union with observed stats, and aggregate so metrics are computed on the union support.
  • Add validation for reference probabilities (range + sum-to-1 tolerance) and key conversion/uniqueness for integral sensitive columns; improve numeric decoding robustness for persisted params.
  • Expand Scala test coverage and clarify documentation for custom reference distribution behavior.
Show a summary per file
File Description
docs/Explore Algorithms/Responsible AI/Data Balance Analysis.md Clarifies custom reference distribution semantics, including union support behavior for observed-only and reference-only categories.
core/src/test/scala/com/microsoft/azure/synapse/ml/exploratory/DistributionBalanceMeasureSuite.scala Adds regression tests for reference-only/observed-only categories, ordering/typing alignment, validation failures, and persistence.
core/src/main/scala/com/microsoft/azure/synapse/ml/exploratory/DistributionBalanceMeasure.scala Implements union-support alignment for custom references, validates distributions/keys, and ensures persisted numeric probabilities decode safely.

Review details

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Lite

## Summary
Fix DistributionBalanceMeasure so custom comparisons cover the union of observed and positive-probability reference categories, with zero observed values for reference-only categories. Add validation, persistence coverage, focused regressions, and a semantics clarification.

## Prompting Intent
Own GitHub issue microsoft#2010 end-to-end with a focused, scalable DataFrame fix. Preserve legacy outputs; cover reference-only and observed-only categories, ordering and supported types, zeros, invalid inputs, persistence, and generated APIs; validate only through the JDK 11 wrapper and the shared SBT mutex.

## Linked Sources
- GitHub issue: microsoft#2010
- Data balance documentation: docs/Explore Algorithms/Responsible AI/Data Balance Analysis.md

## Rationale
Materialize only the already-driver-resident reference parameter as a typed DataFrame, then union and aggregate it with distributed observed statistics. This aligns categories without collecting observed support or using RDD APIs. Positive-probability keys extend support, explicit zero entries behave like omitted keys, and typed-key validation prevents ambiguous numeric categories while preserving the public API and output schema.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@ranadeepsingh
Rana Singh (ranadeepsingh) force-pushed the copilot/issue-2010-reference-categories branch from 273536a to 1642707 Compare August 14, 2026 21:42
Copilot AI review requested due to automatic review settings August 14, 2026 21:42
@ranadeepsingh

Copy link
Copy Markdown
Collaborator Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Rana Singh (ranadeepsingh) pushed a commit to ranadeepsingh/SynapseML that referenced this pull request Aug 16, 2026
## Summary
Strengthen Jensen-Shannon distance verification with exact-value, symmetry, normalization, cancellation, zero-support, bounds, schema, and end-to-end assertions. Clarify the mathematical domain and smoothing semantics in the docs, and remove a stale chart whose embedded values used the old natural-log scale.

## Prompting Intent
Independently make GitHub PR microsoft#2631 merge-ready by auditing its full mathematical and runtime behavior, API compatibility, edge cases, related PR microsoft#2630 interactions, review feedback, checks, generated bindings, Spark compatibility, and downstream documentation. Fix every valid issue without bundling the separate reference-support implementation.

## Linked Sources
- GitHub issue microsoft#2006: microsoft#2006
- Pull request microsoft#2631: microsoft#2631
- Related reference-support pull request microsoft#2630: microsoft#2630
- Copilot review feedback: microsoft#2631 (review)
- Jensen-Shannon definition: https://en.wikipedia.org/wiki/Jensen%E2%80%93Shannon_divergence

## Rationale
Keep the production correction narrowly scoped to dividing Jensen-Shannon divergence by ln(2), preserving natural-log KL divergence and all public APIs. Use independently calculated constants and tolerant assertions to validate semantics across Spark/JVM versions. Document that the unit bound assumes valid probability distributions and that midpoint mixing, not additive smoothing, handles zero support. Leave reference-only category materialization and custom-distribution validation to microsoft#2630 to avoid duplicating unrelated work.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@ranadeepsingh

Copy link
Copy Markdown
Collaborator Author

Coordination note for #2631 (5d91a6143f6f9623bcb4ac64f5d2605fcf7663a0): it changes JS distance from natural-log units to base-2 normalized units without bundling this PR's reference-support work. When #2630 rebases after #2631, its explicit JS constants should become:

  • reference-only example: 0.33841498603440373 (was 0.28174895710781067)
  • all-reference-categories-observed example: 0.1975751820353933 (was 0.1644921288538882)

The shared DistributionMetricsCalculator expectation will update automatically when the #2631 test-base change is present. All non-JS metric expectations remain unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] The setReferenceDistribution method of DistributionBalanceMeasures does not correctly handle categories not present in source dataset

2 participants