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

Using weights for individual data points #245

Open
esheldon opened this issue Oct 17, 2024 · 3 comments
Open

Using weights for individual data points #245

esheldon opened this issue Oct 17, 2024 · 3 comments

Comments

@esheldon
Copy link

I'm finding this code very useful, thank you for writing such a nice package.

I have some data I'd like to fit but each point can have quite variable uncertainty. I find that in this case of different errors the code performs sub optimally.

Is there a way to weight each point? I thought this might be related to the the HeteroscedasticNoise, but my attempts with that failed as it wants a particular shape that I could not figure out.

Thanks in advance.

@esheldon
Copy link
Author

I think I figured it out.

import numpy as np
from MuyGPyS.gp import MuyGPS
from MuyGPyS.neighbors import NN_Wrapper
from MuyGPyS.gp.noise import HeteroscedasticNoise

from MuyGPyS.gp.kernels import Matern
from MuyGPyS.gp.hyperparameter import Parameter
from MuyGPyS.gp.deformation import Isotropy, l2

# features and responses for training set
train_features = "some array here"
train_variances = "some array here"
train_responses = "some array here"

# features to predict
features = "some array here"

length_scale = 2000.0
nn_count = 30

kernel = Matern(
    smoothness=Parameter(1.5),
    deformation=Isotropy(
        l2,
        length_scale=Parameter(length_scale),
    ),
)

nnwrap = NN_Wrapper(
    train_features,
    nn_count,
    nn_method="exact",
    algorithm="ball_tree"
)
nn_indices, _ = nnwrap.get_nns(features)

matchvars = train_variances[nn_indices]
noise_model = HeteroscedasticNoise(matchvars)

muygps = MuyGPS(kernel=kernel, noise=noise_model)

res = muygps.make_predict_tensors(
    batch_indices=np.arange(features.shape[0]),
    batch_nn_indices=nn_indices,
    test_features=features,
    train_features=train_features,
    train_targets=train_responses,
)
crosswise_dists, pairwise_dists, nn_responses = res

Kcross = muygps.kernel(crosswise_dists)
Kin = muygps.kernel(pairwise_dists)

predicted = muygps.posterior_mean(
    Kin, Kcross, nn_responses
)

@esheldon
Copy link
Author

Maybe something like this could be added as an example discussed in #114

@bwpriest
Copy link
Member

Thanks for the comments! The HeteroscedasticNoise class is the right tool for your use case. The current implementation does not play nicely with the MPI backend, so it is still considered an experimental feature. You are right it would be good to have an example for users. We will try to add one when there is time.

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

No branches or pull requests

2 participants