Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • c75bac1
  • /
  • gpflow
  • /
  • logdensities.py
Raw File Download
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:252a8a420b1190f807b962e77c122b8e373e1064
directory badge Iframe embedding
swh:1:dir:44ed0910f9c7860f73f6503dca97dc9d390a9a7f
Citations

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
logdensities.py
# Copyright 2016-2020 The GPflow Contributors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
import tensorflow as tf

from .base import TensorType
from .experimental.check_shapes import check_shapes
from .utilities import to_default_float


@check_shapes(
    "x: [broadcast shape...]",
    "mu: [broadcast shape...]",
    "var: [broadcast shape...]",
    "return: [shape...]",
)
def gaussian(x: TensorType, mu: TensorType, var: TensorType) -> tf.Tensor:
    return -0.5 * (np.log(2 * np.pi) + tf.math.log(var) + tf.square(mu - x) / var)


@check_shapes(
    "x: [broadcast shape...]",
    "mu: [broadcast shape...]",
    "var: [broadcast shape...]",
    "return: [shape...]",
)
def lognormal(x: TensorType, mu: TensorType, var: TensorType) -> tf.Tensor:
    lnx = tf.math.log(x)
    return gaussian(lnx, mu, var) - lnx


@check_shapes(
    "x: [broadcast shape...]",
    "p: [broadcast shape...]",
    "return: [shape...]",
)
def bernoulli(x: TensorType, p: TensorType) -> tf.Tensor:
    return tf.math.log(tf.where(tf.equal(x, 1), p, 1 - p))


@check_shapes(
    "x: [broadcast shape...]",
    "lam: [broadcast shape...]",
    "return: [shape...]",
)
def poisson(x: TensorType, lam: TensorType) -> tf.Tensor:
    return x * tf.math.log(lam) - lam - tf.math.lgamma(x + 1.0)


@check_shapes(
    "x: [broadcast shape...]",
    "scale: [broadcast shape...]",
    "return: [shape...]",
)
def exponential(x: TensorType, scale: TensorType) -> tf.Tensor:
    return -x / scale - tf.math.log(scale)


@check_shapes(
    "x: [broadcast shape...]",
    "shape: [broadcast shape...]",
    "scale: [broadcast shape...]",
    "return: [shape...]",
)
def gamma(x: TensorType, shape: TensorType, scale: TensorType) -> tf.Tensor:
    return (
        -shape * tf.math.log(scale)
        - tf.math.lgamma(shape)
        + (shape - 1.0) * tf.math.log(x)
        - x / scale
    )


@check_shapes(
    "x: [broadcast shape...]",
    "mean: [broadcast shape...]",
    "scale: [broadcast shape...]",
    "df: [broadcast shape...]",
    "return: [shape...]",
)
def student_t(x: TensorType, mean: TensorType, scale: TensorType, df: TensorType) -> tf.Tensor:
    df = to_default_float(df)
    const = (
        tf.math.lgamma((df + 1.0) * 0.5)
        - tf.math.lgamma(df * 0.5)
        - 0.5 * (tf.math.log(tf.square(scale)) + tf.math.log(df) + np.log(np.pi))
    )
    return const - 0.5 * (df + 1.0) * tf.math.log(
        1.0 + (1.0 / df) * (tf.square((x - mean) / scale))
    )


@check_shapes(
    "x: [broadcast shape...]",
    "alpha: [broadcast shape...]",
    "beta: [broadcast shape...]",
    "return: [shape...]",
)
def beta(x: TensorType, alpha: TensorType, beta: TensorType) -> tf.Tensor:
    # need to clip x, since log of 0 is nan...
    x = tf.clip_by_value(x, 1e-6, 1 - 1e-6)
    return (
        (alpha - 1.0) * tf.math.log(x)
        + (beta - 1.0) * tf.math.log(1.0 - x)
        + tf.math.lgamma(alpha + beta)
        - tf.math.lgamma(alpha)
        - tf.math.lgamma(beta)
    )


@check_shapes(
    "x: [broadcast shape...]",
    "mu: [broadcast shape...]",
    "sigma: [broadcast shape...]",
    "return: [shape...]",
)
def laplace(x: TensorType, mu: TensorType, sigma: TensorType) -> tf.Tensor:
    return -tf.abs(mu - x) / sigma - tf.math.log(2.0 * sigma)


@check_shapes(
    "x: [D, broadcast N]",
    "mu: [D, broadcast N]",
    "L: [D, D]",
    "return: [N]",
)
def multivariate_normal(x: TensorType, mu: TensorType, L: TensorType) -> tf.Tensor:
    """
    Computes the log-density of a multivariate normal.

    :param x: sample(s) for which we want the density
    :param mu: mean(s) of the normal distribution
    :param L: Cholesky decomposition of the covariance matrix
    :return: log densities
    """

    d = x - mu
    alpha = tf.linalg.triangular_solve(L, d, lower=True)
    num_dims = tf.cast(tf.shape(d)[0], L.dtype)
    p = -0.5 * tf.reduce_sum(tf.square(alpha), 0)
    p -= 0.5 * num_dims * np.log(2 * np.pi)
    p -= tf.reduce_sum(tf.math.log(tf.linalg.diag_part(L)))

    return p

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Contact— JavaScript license information— Web API

back to top