Пример #1
0
def _parse_shocks(optim_paras, params):
    """Parse the shock parameters and create the Cholesky factor."""
    if sum(f"shocks_{i}" in params.index for i in ["sdcorr", "cov", "chol"]) >= 2:
        raise ValueError("It is not allowed to define multiple shock matrices.")
    elif "shocks_sdcorr" in params.index:
        sorted_shocks = _sort_shocks_sdcorr(optim_paras, params.loc["shocks_sdcorr"])
        cov = sdcorr_params_to_matrix(sorted_shocks)
        optim_paras["shocks_cholesky"] = robust_cholesky(cov)
    elif "shocks_cov" in params.index:
        sorted_shocks = _sort_shocks_cov_chol(
            optim_paras, params.loc["shocks_cov"], "cov"
        )
        cov = cov_params_to_matrix(sorted_shocks)
        optim_paras["shocks_cholesky"] = robust_cholesky(cov)
    elif "shocks_chol" in params.index:
        sorted_shocks = _sort_shocks_cov_chol(
            optim_paras, params.loc["shocks_chol"], "chol"
        )
        optim_paras["shocks_cholesky"] = chol_params_to_lower_triangular_matrix(
            sorted_shocks
        )
    else:
        raise NotImplementedError

    return optim_paras
Пример #2
0
def is_valid_covariance_matrix(sd_corr):
    out = sdcorr_params_to_matrix(sd_corr)
    try:
        np.linalg.cholesky(out)
        return True
    except np.linalg.linalg.LinAlgError:
        return False
Пример #3
0
def _process_sdcorr_constraint(constraint, params, fixed):
    """Process sdcorr constraints.

    Args:
        constraint (dict)
        params (pd.DataFrame): see :ref:`params`.


    Returns:
        new_constr (dict): copy of *constraint* with a new entry called 'case',
            which can take the values 'all_fixed', 'uncorrelated' and 'free'.

    """
    new_constr = constraint.copy()
    params_subset = params.loc[constraint["index"]]
    value_mat = sdcorr_params_to_matrix(params_subset["value"].to_numpy())
    dim = len(value_mat)
    fixed_vec = fixed.loc[constraint["index"], "_fixed"].to_numpy().astype(int)
    fixed_diag = np.diag(fixed_vec[:dim])
    fixed_lower = np.zeros((dim, dim), dtype=int)
    fixed_lower[np.tril_indices(dim, k=-1)] = fixed_vec[dim:]
    fixed_mat = (fixed_lower + fixed_diag + fixed_lower.T).astype(bool)
    new_constr["case"] = _determine_cov_case(value_mat, fixed_mat,
                                             params_subset)
    new_constr["bounds_distance"] = constraint.pop("bounds_distance", 0.0)
    return new_constr
Пример #4
0
def test_sdcorr_params_to_matrix():
    sds = np.sqrt([1, 2, 3])
    corrs = [0.07071068, 0.11547005, 0.08981462]
    params = np.hstack([sds, corrs])
    expected = np.array([[1, 0.1, 0.2], [0.1, 2, 0.22], [0.2, 0.22, 3]])
    calculated = sdcorr_params_to_matrix(params)
    aaae(calculated, expected)
Пример #5
0
def sdcorr_to_internal_jacobian(external_values, constr):
    r"""Derivative of ``sdcorr_to_internal``.

    For reference see docstring of ``jacobian_sdcorr_from_internal``. In
    comparison to that function, however, here we want to differentiate the
    reverse graph

     external --> mod. corr-mat --> corr-mat --> cov --> cholesky --> internal

    Again use the vectors :math:`p` and :math:`x` to denote the external and
    internal values, respectively. To solve for the jacobian we make use of the
    identity

    .. math::
        \frac{\mathrm{d}x}{\mathrm{d}p} = (\frac{\mathrm{d}p}{\mathrm{d}x})^{-1}

    Args:
        external_values (np.ndarray): Row-wise half-vectorized modified correlation
            matrix.

    Returns:
        deriv: The Jacobian matrix.

    """
    cov = sdcorr_params_to_matrix(external_values)
    chol = robust_cholesky(cov)

    internal = chol[np.tril_indices(len(chol))]

    deriv = sdcorr_from_internal_jacobian(internal, constr=None)
    deriv = np.linalg.pinv(deriv)
    return deriv
Пример #6
0
def _parse_shocks(optim_paras, params):
    """Parse the shock parameters and create the Cholesky factor."""
    if sum(f"shocks_{i}" in params.index for i in ["sdcorr", "cov", "chol"]) >= 2:
        raise ValueError("It is not allowed to define multiple shock matrices.")
    elif "shocks_sdcorr" in params.index:
        cov = sdcorr_params_to_matrix(params.loc["shocks_sdcorr"])
        optim_paras["shocks_cholesky"] = robust_cholesky(cov)
    elif "shocks_cov" in params.index:
        cov = cov_params_to_matrix(params.loc["shocks_cov"])
        optim_paras["shocks_cholesky"] = robust_cholesky(cov)
    elif "shocks_chol" in params.index:
        optim_paras["shocks_cholesky"] = chol_params_to_lower_triangular_matrix(
            params.loc["shocks_chol"]
        )
    else:
        raise KeyError("No shock matrix is specified.")

    return optim_paras
Пример #7
0
def check_constraints_are_satisfied(pc, params):
    """Check that params satisfies all constraints.

    This should be called before the more specialized constraints are rewritten to
    linear constraints in order to get better error messages!

    We let the checks pass if all "values" are np.nan. This way `process_constraints`
    can be used on empty params DataFrames which is useful to construct templates for
    start parameters that can be filled out by the user.

    Args:
        pc (list): List of constraints with processed selectors.
        params (pd.DataFrame): See :ref:`params`

    Raises:
        ValueError if constraints are not satisfied.

    """

    if params["value"].notnull().any():
        for constr in pc:
            typ = constr["type"]
            subset = params.iloc[constr["index"]]["value"]
            msg = f"{{}}:\n{subset.to_frame()}"
            if typ == "covariance":
                cov = cov_params_to_matrix(subset)
                e, v = np.linalg.eigh(cov)
                if not np.all(e > -1e-8):
                    raise ValueError(msg.format("Invalid covariance parameters."))
            elif typ == "sdcorr":
                cov = sdcorr_params_to_matrix(subset)
                dim = len(cov)
                if (subset.iloc[:dim] < 0).any():
                    raise ValueError(msg.format("Invalid standard deviations."))
                if ((subset.iloc[dim:] < -1) | (subset.iloc[dim:] > 1)).any():
                    raise ValueError(msg.format("Invalid correlations."))
                e, v = np.linalg.eigh(cov)
                if not np.all(e > -1e-8):
                    raise ValueError(msg.format("Invalid sdcorr parameters."))
            elif typ == "probability":
                if not np.isclose(subset.sum(), 1, rtol=0.01):
                    raise ValueError(msg.format("Probabilities do not sum to 1"))
                if np.any(subset < 0):
                    raise ValueError(msg.format("Negative Probability."))
                if np.any(subset > 1):
                    raise ValueError(msg.format("Probability larger than 1."))
            elif typ == "increasing":
                if np.any(np.diff(subset) < 0):
                    raise ValueError(msg.format("Increasing constraint violated."))
            elif typ == "decreasing":
                if np.any(np.diff(subset) > 0):
                    raise ValueError(msg.format("Decreasing constraint violated"))
            elif typ == "linear":
                # using sr.dot is important in case weights are a series in wrong order
                wsum = subset.dot(constr["weights"])
                if "lower_bound" in constr and wsum < constr["lower_bound"]:
                    raise ValueError(
                        msg.format("Lower bound of linear constraint is violated")
                    )
                elif "upper_bound" in constr and wsum > constr["upper_bound"]:
                    raise ValueError(
                        msg.format("Upper bound of linear constraint violated")
                    )
                elif "value" in constr and not np.isclose(wsum, constr["value"]):
                    raise ValueError(
                        msg.format("Equality condition of linear constraint violated")
                    )
            elif typ == "equality":
                if len(subset.unique()) != 1:
                    raise ValueError(msg.format("Equality constraint violated."))
Пример #8
0
    described by the start values to be positive definite as opposed to positive
    semi-definite.

    Args:
        params_subset (DataFrame): relevant subset of non-internal params.
        case (str): can take the values 'free', 'uncorrelated' or 'all_fixed'.

    Returns:
        res (DataFrame): copy of params_subset with adjusted 'value' and 'lower' columns

    """
    res = params_subset.copy()
    if type_ == "covariance":
        cov = cov_params_to_matrix(params_subset["value"].to_numpy())
    elif type_ == "sdcorr":
        cov = sdcorr_params_to_matrix(params_subset["value"].to_numpy())
    else:
        raise ValueError("Invalid type_: {}".format(type_))

    dim = len(cov)

    e, v = np.linalg.eigh(cov)
    assert np.all(e > -1e-8), "Invalid covariance matrix."

    if case == "uncorrelated":

        res["lower"] = np.maximum(res["lower"], np.zeros(len(res)))
        assert (res["upper"] >=
                res["lower"]).all(), "Invalid upper bound for variance."
    elif case == "free":
        chol = robust_cholesky(cov, threshold=1e-8)
Пример #9
0
def sdcorr_to_internal(external_values, constr):
    """Convert sdcorr to cov and do a cholesky reparametrization."""
    cov = sdcorr_params_to_matrix(external_values)
    chol = robust_cholesky(cov)
    return chol[np.tril_indices(len(cov))]