Page MenuHomec4science

kernel.py
No OneTemporary

File Metadata

Created
Thu, Nov 28, 09:50

kernel.py

# Copyright (C) 2018-2020 by the RROMPy authors
#
# This file is part of RROMPy.
#
# RROMPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RROMPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RROMPy. If not, see <http://www.gnu.org/licenses/>.
#
import numpy as np
from sklearn.gaussian_process.kernels import (WhiteKernel, RBF, Matern,
RationalQuadratic,
ExpSineSquared)
from rrompy.utilities.base.types import Np1D, Tuple
from rrompy.utilities.exception_manager import RROMPyException
__all__ = ['kernels']
def setThetaAuto(val, bounds, name : str = "theta", log : bool = True):
if val is None:
if bounds is None:
raise RROMPyException(("Must specify at least one of {0} and "
"{0}_bounds.".format(name)))
val = 10. ** np.mean(np.log10(bounds)) if log else np.mean(bounds)
if bounds is None: bounds = "fixed"
return (val, bounds)
def addNoise(kernel, noise_level : float = None,
noise_level_bounds : Tuple[float] = None):
try:
noise_level, noise_level_bounds = setThetaAuto(noise_level,
noise_level_bounds,
"noise_level")
except RROMPyException: return 1.0 * kernel
return 1.0 * kernel + WhiteKernel(noise_level = noise_level,
noise_level_bounds = noise_level_bounds)
def radialGaussianKernel(length_scale : Np1D = None,
length_scale_bounds : Tuple[float] = None,
noise_level : float = None,
noise_level_bounds : Tuple[float] = None):
length_scale, length_scale_bounds = setThetaAuto(length_scale,
length_scale_bounds,
"length_scale")
return addNoise(RBF(length_scale = length_scale,
length_scale_bounds = length_scale_bounds),
noise_level, noise_level_bounds)
def maternKernel(length_scale : Np1D = None,
length_scale_bounds : Tuple[float] = None, nu : float = 1.5,
noise_level : float = None,
noise_level_bounds : Tuple[float] = None):
length_scale, length_scale_bounds = setThetaAuto(length_scale,
length_scale_bounds,
"length_scale")
return addNoise(Matern(length_scale = length_scale,
length_scale_bounds = length_scale_bounds, nu = nu),
noise_level, noise_level_bounds)
def rationalQuadraticKernel(length_scale : Np1D = None, alpha : float = None,
length_scale_bounds : Tuple[float] = None,
alpha_bounds : Tuple[float] = None,
noise_level : float = None,
noise_level_bounds : Tuple[float] = None):
length_scale, length_scale_bounds = setThetaAuto(length_scale,
length_scale_bounds,
"length_scale")
alpha, alpha_bounds = setThetaAuto(alpha, alpha_bounds, "alpha", False)
return addNoise(RationalQuadratic(length_scale = length_scale,
alpha = alpha,
length_scale_bounds = length_scale_bounds,
alpha_bounds = alpha_bounds),
noise_level, noise_level_bounds)
def expSineSquaredKernel(length_scale : Np1D = None,
periodicity : float = None,
length_scale_bounds : Tuple[float] = None,
periodicity_bounds : Tuple[float] = None,
noise_level : float = None,
noise_level_bounds : Tuple[float] = None):
length_scale, length_scale_bounds = setThetaAuto(length_scale,
length_scale_bounds,
"length_scale")
periodicity, periodicity_bounds = setThetaAuto(periodicity,
periodicity_bounds,
"periodicity", False)
return addNoise(ExpSineSquared(length_scale = length_scale,
periodicity = periodicity,
length_scale_bounds = length_scale_bounds,
periodicity_bounds = periodicity_bounds),
noise_level, noise_level_bounds)
kernels = {"GAUSSIAN" : radialGaussianKernel, "MATERN" : maternKernel,
"RATIONALQUADRATIC" : rationalQuadraticKernel,
"EXPSINESQUARED": expSineSquaredKernel}

Event Timeline