Exemplo n.º 1
0
def log_normcdf(x):
    """Elementwise log of the cumulative distribution function of a standard normal random variable.

    Implementation is a quadratic approximation with modest accuracy over [-4, 4].
    """
    A = scipy.sparse.diags(
        np.sqrt(
            [
                0.02301291,
                0.08070214,
                0.16411522,
                0.09003495,
                0.08200854,
                0.01371543,
                0.04641081,
            ]
        )
    )
    b = np.array([[3.0, 2.0, 1.0, 0.0, -1.0, -2.5, -3.5]]).reshape(-1, 1)

    x = Expression.cast_to_const(x)
    flat_x = reshape(x, (1, x.size))

    y = A @ (b @ np.ones(flat_x.shape) - np.ones(b.shape) @ flat_x)
    out = -sum_(maximum(y, 0) ** 2, axis=0)

    return reshape(out, x.shape)
Exemplo n.º 2
0
def log_normcdf(x):  # noqa: E501
    """Elementwise log of the cumulative distribution function of a standard normal random variable.

    The implementation is a quadratic approximation with modest accuracy over [-4, 4].
    For details on the nature of the approximation, refer to
    `CVXPY GitHub PR #1224 <https://github.com/cvxpy/cvxpy/pull/1224#issue-793221374>`_.

    .. note::

        SciPy's analog of ``log_normcdf`` is called `log_ndtr <https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.log_ndtr.html>`_.
        We opted not to use that name because its meaning would not be obvious to the casual user.
    """
    A = scipy.sparse.diags(
        np.sqrt(
            [
                0.02301291,
                0.08070214,
                0.16411522,
                0.09003495,
                0.08200854,
                0.01371543,
                0.04641081,
            ]
        )
    )
    b = np.array([[3.0, 2.0, 1.0, 0.0, -1.0, -2.5, -3.5]]).reshape(-1, 1)

    x = Expression.cast_to_const(x)
    flat_x = reshape(x, (1, x.size))

    y = A @ (b @ np.ones(flat_x.shape) - np.ones(b.shape) @ flat_x)
    out = -sum_(maximum(y, 0) ** 2, axis=0)

    return reshape(out, x.shape)
Exemplo n.º 3
0
def loggamma(x):
    """Elementwise log of the gamma function.

    Implementation has modest accuracy over the full range, approaching perfect
    accuracy as x goes to infinity.
    """

    return maximum(
        2.18382 - 3.62887 * x,
        1.79241 - 2.4902 * x,
        1.21628 - 1.37035 * x,
        0.261474 - 0.28904 * x,
        0.577216 - 0.577216 * x,
        -0.175517 + 0.03649 * x,
        -1.27572 + 0.621514 * x,
        -0.845568 + 0.422784 * x,
        -0.577216 * x - log(x),
        0.918939 - x - entr(x) - 0.5 * log(x),
    )
Exemplo n.º 4
0
def loggamma(x):
    """Elementwise log of the gamma function.

    Implementation has modest accuracy over the full range, approaching perfect
    accuracy as x goes to infinity. For details on the nature of the approximation,
    refer to `CVXPY GitHub Issue #228 <https://github.com/cvxpy/cvxpy/issues/228#issuecomment-544281906>`_.
    """

    return maximum(
        2.18382 - 3.62887*x,
        1.79241 - 2.4902*x,
        1.21628 - 1.37035*x,
        0.261474 - 0.28904*x,
        0.577216 - 0.577216*x,
        -0.175517 + 0.03649*x,
        -1.27572 + 0.621514*x,
        -0.845568 + 0.422784*x,
        -0.577216*x - log(x),
        0.918939 - x - entr(x) - 0.5*log(x),
    )
Exemplo n.º 5
0
def minimum(*args):
    return -maximum(*[-Elementwise.cast_to_const(arg) for arg in args])
Exemplo n.º 6
0
def pos(x):
    """ Alias for maximum{x,0}.

    """
    return maximum(x, 0)
Exemplo n.º 7
0
def minimum_canon(expr, args):
    del expr
    tmp = maximum(*[-arg for arg in args])
    canon, constr = maximum_canon(tmp, tmp.args)
    return -canon, constr
Exemplo n.º 8
0
def minimum(*args):
    """Elementwise minimum of a sequence of Expressions."""
    return -maximum(*[-Elementwise.cast_to_const(arg) for arg in args])