Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nearest Neighbor Model #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

akleeman
Copy link
Collaborator

@akleeman akleeman commented Sep 10, 2019

Adds a model which (given a distance metric) will produce predictions for the nearest neighbor.

https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation

Copy link
Contributor

@pgrgich pgrgich left a comment

Choose a reason for hiding this comment

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

Lgtm, but would be great if we could unify it with the Oracle model, in this PR if feasible.

for (const auto &pair : indexer) {
assert(preds.at(pair.first).size() == pair.second.size());
set_subset(preds.at(pair.first).mean, pair.second, &mean);
set_subset(preds.at(pair.first).covariance.diagonal(), pair.second,
&variance);
if (preds.at(pair.first).has_covariance()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to do this or just add 1e6 as variances where we currently have none?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In this method we're taking a pair of MarginalDistribution and concatenating them, so if the both don't have a defined covariance then we want to preserve that in the concatenation. Somewhere on my list of want to dos is to remove the optional behavior for convariances in favor of a third distribution type, something like:

using MeanOnlyDistribution = Distribution<Empty>;
using MarginalDistribution = Distribution<DiagonalMatrixXd>;
using JointDistribution = Distribution<Eigen::MatrixXd>;

or something along those lines, but that's out of scope here.

const JointDistribution &prediction) const {
const NearestNeighborModel<DistanceMetric> m(*this);
MarginalDistribution marginal_pred(
prediction.mean, prediction.covariance.diagonal().asDiagonal());
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't prediction.covariance work here? Or are you looking to zero the non-diagonal elements?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah exactly, I need to zero the non-diagonal elements since the NearestNeighbor model can never actually predict off diagonals.

std::size_t min_index = 0;
double min_distance = distance_metric(ref, features[0]);

for (std::size_t i = 1; i < features.size(); ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we turn min_distance into an optional so the loop can start at 0? The only difference in the loop would be !min_distance && going at the start of the if.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I like that pattern better too ... but so far albatross doesn't used any optionals! So we'd have to add a third party lib for it which I've been avoiding (though perhaps the time has come).

Copy link
Contributor

Choose a reason for hiding this comment

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

An alternative is to initialize the min_distance to DBL_MAX or some such, so it will always be replaced by the first distance.

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.

None yet

2 participants