Source code for queens.models.likelihood_models.likelihood_model
#
# SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (c) 2024-2025, QUEENS contributors.
#
# This file is part of QUEENS.
#
# QUEENS 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. QUEENS 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 QUEENS. If not,
# see <https://www.gnu.org/licenses/>.
#
"""Module to define likelihood functions."""
import abc
import numpy as np
from queens.models.model import Model
[docs]
class LikelihoodModel(Model):
"""Base class for likelihood models.
Attributes:
forward_model (obj): Forward model on which the likelihood model is based
y_obs (np.array): Observation data
"""
def __init__(self, forward_model, y_obs):
"""Initialize the likelihood model.
Args:
forward_model (obj): Forward model that is evaluated during the likelihood evaluation
y_obs (array_like): Observation data
"""
super().__init__()
self.forward_model = forward_model
self.y_obs = np.array(y_obs)
[docs]
@abc.abstractmethod
def evaluate(self, samples):
"""Evaluate model with current set of input samples.
Args:
samples (np.ndarray): Input samples
"""
[docs]
@abc.abstractmethod
def grad(self, samples, upstream_gradient):
r"""Evaluate gradient of model w.r.t. current set of input samples.
Consider current model f(x) with input samples x, and upstream function g(f). The provided
upstream gradient is :math:`\frac{\partial g}{\partial f}` and the method returns
:math:`\frac{\partial g}{\partial f} \frac{df}{dx}`.
Args:
samples (np.array): Input samples
upstream_gradient (np.array): Upstream gradient function evaluated at input samples
:math:`\frac{\partial g}{\partial f}`
Returns:
gradient (np.array): Gradient w.r.t. current set of input samples
:math:`\frac{\partial g}{\partial f} \frac{df}{dx}`
"""