-
Notifications
You must be signed in to change notification settings - Fork 63
RKHS inner product #550
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
base: develop
Are you sure you want to change the base?
RKHS inner product #550
Changes from 3 commits
738b653
56626ef
a8da0a0
b6d22f2
2a205f6
b09cac5
4242d77
90bdc80
0b97d49
5045dca
d371477
5c30710
5655385
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,210 @@ | ||||||
| r""" | ||||||
| Reproducing Kernel Hilbert Space Inner Product for the Brownian Bridge | ||||||
| ======================================================================= | ||||||
| This example shows how to compute the inner product of two functions in the | ||||||
| reproducing kernel Hilbert space (RKHS) of the Brownian Bridge. | ||||||
| """ | ||||||
|
|
||||||
| # Author: Martín Sánchez Signorini | ||||||
| # License: MIT | ||||||
|
|
||||||
| import numpy as np | ||||||
| import pandas as pd | ||||||
|
|
||||||
| from skfda.misc.rkhs_product import rkhs_inner_product | ||||||
| from skfda.representation import FDataGrid | ||||||
| from skfda.typing._numpy import NDArrayFloat | ||||||
|
|
||||||
| ############################################################################### | ||||||
| # The kernel corresponding to a Brownian Bridge process | ||||||
| # :cite:p:`gutierrez++_1992_numerical` in the interval :math:`[0, 1]` is | ||||||
m5signorini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| # | ||||||
| # .. math:: | ||||||
| # k(s, t) = \min(s, t) - st | ||||||
| # | ||||||
| # The RKHS inner product method | ||||||
| # :func:`~skfda.misc.rkhs_product.rkhs_inner_product` requires the kernel to | ||||||
| # be defined as a function of two vector arguments that returns the matrix of | ||||||
| # values of the kernel in the corresponding grid. | ||||||
| # The following function defines the kernel as such a function. | ||||||
|
|
||||||
|
|
||||||
| def brownian_bridge_covariance( | ||||||
| t: NDArrayFloat, | ||||||
| s: NDArrayFloat, | ||||||
| ) -> NDArrayFloat: | ||||||
| """Covariance function of the Brownian Bridge process. | ||||||
| This function must receive two vectors of points while returning the | ||||||
| matrix of values of the covariance function in the corresponding grid. | ||||||
| """ | ||||||
| # Compute the matrix of minimum values between the points of the vectors | ||||||
| min_values = np.amin( | ||||||
| np.transpose( | ||||||
| np.meshgrid(t, s), | ||||||
| (2, 1, 0), | ||||||
| ), | ||||||
| axis=2, | ||||||
| ) | ||||||
| return ( # type: ignore[no-any-return] | ||||||
| min_values - np.outer(t, s) | ||||||
| ) | ||||||
m5signorini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
|
|
||||||
| ############################################################################### | ||||||
| # The RKHS of this kernel :cite:p:`berlinet+thomas-agnan_2011_reproducing` | ||||||
| # is the set of functions | ||||||
| # | ||||||
| # .. math:: | ||||||
| # f: [0, 1] \to \mathbb{R} \quad \text{ such that } | ||||||
| # f \text{ is absolutely continuous, } | ||||||
| # f(0) = f(1) = 0 \text{ and } | ||||||
| # f' \in L^2([0, 1]). | ||||||
| # | ||||||
| # For this example we will be using the following functions in this RKHS: | ||||||
| # | ||||||
| # .. math:: | ||||||
| # \begin{align} | ||||||
| # f(t) &= 1 - (2t - 1)^2 \\ | ||||||
| # g(t) &= \sin(\pi t) | ||||||
| # \end{align} | ||||||
| # | ||||||
| # The following code defines a method to compute the inner product of these | ||||||
| # two functions in the RKHS of the Brownian Bridge, using a variable number of | ||||||
| # points of discretization of the functions. | ||||||
|
|
||||||
| def brownian_bridge_rkhs_inner_product( | ||||||
| num_points: int, | ||||||
| ) -> float: | ||||||
| """Inner product of two functions in the RKHS of the Brownian Bridge.""" | ||||||
| # Define the functions | ||||||
| # Remove first and last points to avoid a singular covariance matrix | ||||||
| grid_points = np.linspace(0, 1, num_points + 2)[1:-1] | ||||||
| f = FDataGrid( | ||||||
| [1 - (2 * grid_points - 1)**2], | ||||||
| grid_points, | ||||||
| ) | ||||||
| g = FDataGrid( | ||||||
| [np.sin(np.pi * grid_points)], | ||||||
| grid_points, | ||||||
| ) | ||||||
|
|
||||||
| # Compute the inner product | ||||||
| return rkhs_inner_product( # type: ignore[no-any-return] | ||||||
| fdata1=f, | ||||||
| fdata2=g, | ||||||
| cov_function=brownian_bridge_covariance, | ||||||
| )[0] | ||||||
|
|
||||||
|
|
||||||
| # Plot the functions f and g in the same figure | ||||||
| FDataGrid( | ||||||
| np.concatenate( | ||||||
| [ | ||||||
| 1 - (2 * np.linspace(0, 1, 100) - 1)**2, | ||||||
| np.sin(np.pi * np.linspace(0, 1, 100)), | ||||||
| ], | ||||||
| axis=0, | ||||||
| ).reshape(2, 100), | ||||||
| np.linspace(0, 1, 100), | ||||||
| ).plot() | ||||||
|
|
||||||
|
||||||
| plt.show() |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add a new row in a Pandas dataframe just setting its value, without doing this.
Uh oh!
There was an error while loading. Please reload this page.