Repository navigation
[repo-assist] feat: add Imputation.kNearestWeightedImpute for distance-weighted KNN - #372
Conversation
Adds Imputation.kNearestWeightedImpute to FSharp.Stats.ML, addressing the weighted KNN imputation request in #318. The new function accepts a pluggable distance metric and a distanceToWeight converter, allowing both inverse-Euclidean and similarity-based (e.g. Pearson correlation) weighting strategies. Changes: - src/FSharp.Stats/ML/Imputation.fs: new kNearestWeightedImpute function - tests/FSharp.Stats.Tests/Imputation.fs: 6 new tests (1200/1200 pass) - tests/.../FSharp.Stats.Tests.fsproj: register new test file Closes #318 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
/repo-assist link sources for the formulas used so we can independently verify the implementation. |
|
✓ Repo Assist completed successfully, see workflow run. |
This comment has been minimized.
This comment has been minimized.
|
/repo-assist Use the information above and the pull request to do additional testing and provide proof that this implementation is correct/incorrect |
|
✗ Repo Assist encountered failed, see workflow run. Warning Firewall blocked 2 domainsThe following domains were blocked by the firewall during workflow execution:
To allow these domains, add them to the network:
allowed:
- defaults
- "api.nuget.org"
- "dc.services.visualstudio.com"See Network Configuration for more information.
|
|
🤖 This is an automated response from Repo Assist. I tested Correct
Bug in the documented usage example The Suggested changes
I haven't pushed any changes, since I was asked only for testing and proof. Warning Firewall blocked 2 domainsThe following domains were blocked by the firewall during workflow execution:
To allow these domains, add them to the network:
allowed:
- defaults
- "api.nuget.org"
- "dc.services.visualstudio.com"See Network Configuration for more information.
Add this agentic workflow to your repoTo install this agentic workflow, run |
|
I had already started working on this PR, so here are my thoughts before this is merged: 1: In kNearestImpute, euclidianNaNSquared is hardcoded as the distance metric. I don't know exactly why that is. It made me wonder whether both functions should be consistent in how they choose the distance metric. To me, it seems weird to have the weighted version be more liberal than the unweighted one in that regard, as the resulting interface reads like this: Personally, I don't think I would expect this behaviour as a user. The options I see are My personal preference would be a). In that case nothing would need to change in this PR and I would just open a seperate issue/pull request. I'd be interested in hearing other opinions on this. 2: The documentation says that distanceToWeight converts a raw distance value into a non-negative weight, but since the function is user-supplied, this is not actually enforced. I think the wording should be adjusted here to make clear that this is an expectation rather than a guarantee provided by the implementation. 3: On that note, as that function is user-inputted, nothing keeps it from supplying NaN or Infinity weights. In that case those values would be propagated through the calculation and may result in NaN imputation, even if all neighbours themselves were valid. 4: Again in the documentation it says that
I don't think this is correct. Since neighbours are selected by sorting ascending on the returned value, using Pearson correlation directly would select the most negative correlation first rather than the most positive. 5: Regarding the RA's suggestion about Double.Epsilon, I would like to defer to someone with more experience on numerical programming than me. The argument seems reasonable, though. |
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
Implements the distance-weighted KNN imputation variant requested in #318, adding
Imputation.kNearestWeightedImputetoFSharp.Stats.ML.Motivation
The existing
kNearestImputetreats all k neighbours equally (simple mean). Issue #318 asks for:euclideanNaNSquared)Changes
src/FSharp.Stats/ML/Imputation.fsNew function:
Parameters
distanceMetricfloat[] → float[] → floatdistance; useDistanceMetrics.Array.euclideanNaNSquaredto skip NaN positionsdistanceToWeightfun d → 1.0 / (d + epsilon); for a correlation similarity measure passidor its reciprocalkBehaviour
knearest complete rows bydistanceMetric.distanceToWeight(distance).totalWeight = 0(all weights zero), falls back to an unweighted mean (graceful degradation).nanif the complete-rows pool is empty.Typical usage
Notes & Trade-offs
kNearestImputeis unchanged.Impute→ already deprecated), missing-value encoding parameterisation, and documentation examples. These could be tackled in follow-up PRs or directly by the maintainer.euclideanNaNSquaredreturns 0. Callers using1/(d+epsilon)still get numerically stable results becauseepsilonprevents true division-by-zero and the equal-weight case degrades to the arithmetic mean.Closes #318