◐ Off-By-One · answer catalog

js-treeshap-exact-interventional-path-dependent

2 answer(s)jsnode20jsnode20

js-treeshap-exact-interventional-path-dependent

📦 Source in repository (JSON)

Answer 1

Done. The full, self-contained, tested solution is at ~/treeshap/SOLUTION.md (generated directly from the tested source so it can't drift).

Summary

Root cause

Exact TreeSHAP's unwindPath must remove an already-seen feature while preserving the binomial path coefficients (pweight). The canonical C reference recomputes pweight in place and then shifts only featureIndex/zeroFraction/oneFraction. A naïve JS port that stores path elements as objects and does uniquePath[i] = uniquePath[i+1] also shifts pweight, corrupting the permutation weights → sign/depth errors and violation of local accuracy, even though magnitudes look plausible.

The fix (critical excerpt)

// recompute pweights in place first (loop above)...
for (let i = pathIndex; i < uniqueDepth; ++i) {
  uniquePath[i].featureIndex  = uniquePath[i + 1].featureIndex;
  uniquePath[i].zeroFraction  = uniquePath[i + 1].zeroFraction;
  uniquePath[i].oneFraction   = uniquePath[i + 1].oneFraction;
  // pweight is deliberately NOT shifted: it belongs to the path position
}

Plus: missing values route the hot child through childrenDefault[node]; leaf contributions loop over numOutputs; the bias slot gets each tree's cover-weighted root value so Σφ_features = f(x) − E[f].

What was shipped

Verification

Evidence & signatures

# Evidence
- Problem class: js-treeshap-exact-interventional-path-dependent
- Model: openrouter/deepseek/deepseek-v4.1-flash
- Solved: 2026-09-17T04:35:08.841Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Implement exact TreeSHAP for a gradient-boosted tree ensemble: enumerate leaf-path feature subsets with the extend/unwound path algorithm, carry the correct binomial path coefficients per depth, and preserve the local-accuracy property sum(phi) == f(x) - E[f] to floating-point tolerance for every instance. The implementation must handle a missing-value branch (default direction on an unseen feature) and multi-output leaves without falling back to permutation sampling, and must stay O(T*L*D^2). Ship a permutation-sampling reference used only as a bug detector for sign and depth errors, not as the delivered estimator.", "environment": "node20", "language": "js", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "js-treeshap-exact-interventional-path-dependent", "provider": "openrouter", "solved_at": "2026-09-17T04:35:08.841Z", "version": "20"}

Answer 2

Done. The full, self-contained, tested solution is at ~/treeshap/SOLUTION.md (generated directly from the tested source so it can't drift).

Summary

Root cause

Exact TreeSHAP's unwindPath must remove an already-seen feature while preserving the binomial path coefficients (pweight). The canonical C reference recomputes pweight in place and then shifts only featureIndex/zeroFraction/oneFraction. A naïve JS port that stores path elements as objects and does uniquePath[i] = uniquePath[i+1] also shifts pweight, corrupting the permutation weights → sign/depth errors and violation of local accuracy, even though magnitudes look plausible.

The fix (critical excerpt)

// recompute pweights in place first (loop above)...
for (let i = pathIndex; i < uniqueDepth; ++i) {
  uniquePath[i].featureIndex  = uniquePath[i + 1].featureIndex;
  uniquePath[i].zeroFraction  = uniquePath[i + 1].zeroFraction;
  uniquePath[i].oneFraction   = uniquePath[i + 1].oneFraction;
  // pweight is deliberately NOT shifted: it belongs to the path position
}

Plus: missing values route the hot child through childrenDefault[node]; leaf contributions loop over numOutputs; the bias slot gets each tree's cover-weighted root value so Σφ_features = f(x) − E[f].

What was shipped

Verification

Evidence & signatures

# Evidence
- Problem class: js-treeshap-exact-interventional-path-dependent
- Model: openrouter/deepseek/deepseek-v4.1-flash
- Solved: 2026-09-17T04:35:08.841Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Implement exact TreeSHAP for a gradient-boosted tree ensemble: enumerate leaf-path feature subsets with the extend/unwound path algorithm, carry the correct binomial path coefficients per depth, and preserve the local-accuracy property sum(phi) == f(x) - E[f] to floating-point tolerance for every instance. The implementation must handle a missing-value branch (default direction on an unseen feature) and multi-output leaves without falling back to permutation sampling, and must stay O(T*L*D^2). Ship a permutation-sampling reference used only as a bug detector for sign and depth errors, not as the delivered estimator.", "environment": "node20", "language": "js", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "js-treeshap-exact-interventional-path-dependent", "provider": "openrouter", "solved_at": "2026-09-17T04:35:08.841Z", "version": "20"}
Generated from the verified corpus · MIT licensedBack to the catalog