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

Add Nadaraya-Watson kernel regression with Gaussian kernel #145

Merged
merged 62 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
52e96ea
edit installation instructions in readme
gianlucadetommaso May 15, 2023
5e0076d
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso May 15, 2023
4c7fd28
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso May 15, 2023
6cb6581
bump up version
gianlucadetommaso May 15, 2023
1b39780
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso May 16, 2023
cb2b49a
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso May 16, 2023
14e3ca4
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso May 25, 2023
580067d
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso May 27, 2023
048ef09
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jun 2, 2023
ad542a4
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jun 12, 2023
41417c1
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jun 12, 2023
64be374
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jun 14, 2023
a2d0f34
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jun 14, 2023
66bba06
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jun 15, 2023
911aa82
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jun 15, 2023
01f959b
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jun 15, 2023
79f8dca
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jun 15, 2023
4dea50f
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jun 21, 2023
1ced008
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jul 18, 2023
6992692
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jul 18, 2023
b2540c1
make small change in readme because of publish to pypi error
gianlucadetommaso Jul 18, 2023
2362998
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jul 18, 2023
6e030f2
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jul 25, 2023
9bd6f67
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jul 25, 2023
c5bc94f
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jul 25, 2023
d3ab46b
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jul 26, 2023
0e2aca5
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jul 26, 2023
9520273
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jul 30, 2023
e9c4108
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jul 30, 2023
bc64a01
bump up version
gianlucadetommaso Jul 30, 2023
25072da
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jul 30, 2023
e27b378
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Jul 30, 2023
a175e16
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Aug 1, 2023
6e202f1
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Aug 1, 2023
635e7c9
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Aug 9, 2023
8e23b32
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Aug 16, 2023
f5efef8
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Aug 24, 2023
958b245
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Aug 24, 2023
577d169
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Aug 28, 2023
69a454e
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Aug 30, 2023
6e880ba
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Aug 30, 2023
f606545
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Sep 11, 2023
63e09bb
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Sep 11, 2023
b2402b5
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Sep 12, 2023
591d842
refactor tabular analysis of benchmarks
gianlucadetommaso Sep 13, 2023
3dcf217
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Sep 13, 2023
d1b5b4a
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Sep 18, 2023
b4c161e
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Sep 21, 2023
744dff1
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Sep 21, 2023
a22f97f
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Sep 24, 2023
fffdd76
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Sep 26, 2023
c23d16d
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Sep 26, 2023
1cb2917
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Sep 27, 2023
9c1d07a
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Sep 29, 2023
4b83638
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Oct 10, 2023
610fc37
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Oct 10, 2023
e5b67ba
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Oct 10, 2023
1f03d4e
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Oct 10, 2023
d49ed29
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Oct 11, 2023
8200e42
Merge branch 'main' of https://github.com/awslabs/fortuna
gianlucadetommaso Oct 19, 2023
114cc9a
add nadaraya-watson kernel regression w/ gaussian kernel
gianlucadetommaso Oct 19, 2023
6e718d6
fix test
gianlucadetommaso Oct 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fortuna/kernel_regression/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from fortuna.kernel_regression.nadaraya_watson import NadarayaWatsonKernelRegressor
7 changes: 7 additions & 0 deletions fortuna/kernel_regression/kernels/gaussian.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import jax.numpy as jnp

from fortuna.typing import Array


def gaussian_kernel(x: Array) -> Array:
return jnp.exp(-0.5 * x**2) / jnp.sqrt(2 * jnp.pi)
92 changes: 92 additions & 0 deletions fortuna/kernel_regression/nadaraya_watson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from typing import Callable

from jax import vmap
import jax.numpy as jnp

from fortuna.kernel_regression.kernels.gaussian import gaussian_kernel
from fortuna.typing import Array


class NadarayaWatsonKernelRegressor:
def __init__(
self,
train_inputs: Array,
train_targets: Array,
kernel: Callable[[Array], Array] = gaussian_kernel,
):
"""
A Nadaraya-Watson kernel regressor.

Parameters
----------
kernel: Callable[[Array], Array]
A kernel.
train_inputs: Array
Training inputs.
train_targets: Array
Training targets.
"""
self.kernel = kernel

if (train_inputs.ndim > 1) or (train_targets.ndim > 1):
raise ValueError(
"Both `train_inputs` and `train_targets` must be one-dimensional arrays."
)
if len(train_inputs) != len(train_targets):
raise ValueError(
"`train_inputs` and `train_targets` must have the same length."
)
self.train_inputs = jnp.copy(train_inputs)
self._mean_train_targets = jnp.mean(train_targets)
self._std_train_targets = jnp.std(train_targets)
self.standardized_train_targets = (
jnp.copy(train_targets) - self._mean_train_targets
) / self._std_train_targets

def predict(self, inputs: Array, bandwidth: float = 0.1):
"""
Predict the target

Parameters
----------
inputs: Array
Inputs.
bandwidth: float
Kernel bandwidth.

Returns
-------
Predictions for the given inputs.
"""
""""""
if inputs.ndim > 1:
raise ValueError("`inputs` must be a one-dimensional array.")
inputs = jnp.copy(inputs)
kernels = vmap(
lambda x: self.evaluate_scaled_kernel(
x - self.train_inputs, bandwidth=bandwidth
)
)(inputs)
m = jnp.sum(kernels * self.standardized_train_targets[None], axis=1) / jnp.sum(
kernels, axis=1
)
m *= self._std_train_targets
m += self._mean_train_targets
return m

def evaluate_scaled_kernel(self, inputs: Array, bandwidth: float) -> Array:
"""
Given a kernel :math:`K(x)`, Evaluate the scaled kernel :math:`K_h(x) := \frac{1}{h}K\left(\frac{x}{h}\right)`.

Parameters
----------
inputs: Array
Inputs.
bandwidth: Array
Bandwidth.

Returns
-------
Evaluation of the scaled kernel.
"""
return self.kernel(inputs / bandwidth) / bandwidth
37 changes: 37 additions & 0 deletions tests/fortuna/test_kernel_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import unittest

from jax import random
import jax.numpy as jnp
import numpy as np

from fortuna.kernel_regression.nadaraya_watson import NadarayaWatsonKernelRegressor


class TestKernelRegression(unittest.TestCase):
def test_nadaraya_watson(self):
train_x = random.normal(random.PRNGKey(0), shape=(3,))
train_y = random.normal(random.PRNGKey(1), shape=(3,))
eval_x = random.normal(random.PRNGKey(2), shape=(4,))

kr = NadarayaWatsonKernelRegressor(train_inputs=train_x, train_targets=train_y)
preds = kr.predict(inputs=eval_x)
assert preds.shape == (4,)

kr = NadarayaWatsonKernelRegressor(
train_inputs=np.array(train_x), train_targets=np.array(train_y)
)
preds = kr.predict(inputs=np.array(eval_x))
assert preds.shape == (4,)

with self.assertRaises(ValueError):
NadarayaWatsonKernelRegressor(
train_inputs=train_x, train_targets=train_y[None]
)
with self.assertRaises(ValueError):
NadarayaWatsonKernelRegressor(
train_inputs=train_x[None], train_targets=train_y
)
with self.assertRaises(ValueError):
NadarayaWatsonKernelRegressor(
train_inputs=jnp.concatenate((train_x, train_y)), train_targets=train_y
)
Loading