src/estimagic/optimization/trust_region_sampling.py
def compute_optimality_criterion(x, criterion, target):
    """Compute optimality criterion of data matrix along axis.

    Implements criteria that measure the dispersion of sample points in a space.
    Optimization with respect to the criteria leads to certain properties of, for
    example, the estimated parametes in a least squares regression. See
    https://en.wikipedia.org/wiki/Optimal_design for more information on the specific
    criteria.

    Brief explaination:
    - a: minimizes the average variance of the least squares estimator
    - d: maximizes the determinant information matrix of the least squares estimator
    - e: maximizes the minimum eigenvalue of the information matrix
    - g: minimizes the maximum variance of the predicted value in the least squares case
    - maximin: maximizes the minimum (l-infinity) distance between any two points

    Args:
        x (np.ndarray): Data matrix. If 3d, computes the criterion value along the first
            dimension.
        criterion (str): Criterion type, must be in {'a-optimal', 'd-optimal',
            'e-optimal', 'g-optimal', 'maximin'}.
        target (str): One of "linear", "quadratic" or "polynomial". Determines in which
            space the criterion is evaluated. For "quadratic" the Latin-Hypercube sample
            X is mapped onto Y(X) = [X, X ** 2] and the criterion value for X is
            computed using Y(X). For "polynomial" Y(X) will also include all cross
            terms. This can be useful if one chooses an optimality criterion that
            minimizes e.g. the variance of the least-squares estimator, while using a
            quadratic or polynomial model.

    Returns:
        crit_vals (np.ndarray): Criterion values.

    """
    implemented_criteria = {
        "a-optimal",
        "d-optimal",
        "e-optimal",
        "g-optimal",
        "maximin",
    }
    if criterion not in implemented_criteria:
        raise ValueError(f"Invalid criterion. Must be in {implemented_criteria}.")

    implemented_targets = {"linear", "quadratic"}
    if target not in implemented_targets:
        raise ValueError(f"Invalid target. Must be in {implemented_targets}.")

    # pre-processing
    if x.ndim == 2:
        x = np.expand_dims(x, axis=0)

    if target == "quadratic":
        x = np.concatenate((x, x**2), axis=2)

    # common computations
    prod = np.matmul(x.transpose(0, 2, 1), x)

    if criterion in {"a-optimal", "g-optimal"}:
        if x.shape[1] < x.shape[2]:
            inv = np.linalg.inv(prod + 0.01 * np.eye(x.shape[2]))
        else:
            is_invertible = np.linalg.cond(prod) < 1 / np.finfo(float).eps
            inv = np.linalg.inv(prod[is_invertible])
            crit_vals = np.tile(np.inf, x.shape[0])

    # compute criteria
    if criterion == "a-optimal":
        crit_vals[is_invertible] = inv.trace(axis1=1, axis2=2)
    elif criterion == "g-optimal":
        hat_mat = np.matmul(
            np.matmul(x[is_invertible], inv), x[is_invertible].transpose(0, 2, 1)
        )
        crit_vals[is_invertible] = np.max(np.diagonal(hat_mat.T), axis=1)
    elif criterion == "d-optimal":
        crit_vals = -np.linalg.det(prod)  # minus because we maximize
    elif criterion == "e-optimal":
        eig_vals = np.linalg.eig(prod)[0]
        crit_vals = -np.min(eig_vals, axis=1)  # minus because we maximize
    elif criterion == "maximin":
        distances = []
        for row1, row2 in list(combinations(range(x.shape[1]), 2)):
            dist = np.linalg.norm(x[:, row1, :] - x[:, row2, :], axis=1, ord=np.inf)
            distances.append(dist)
        crit_vals = -np.vstack(distances).min(axis=0)  # minus because we maximize

    return crit_vals
Compute optimality criterion of data matrix along axis.

    Implements criteria that measure the dispersion of sample points in a space.
    Optimization with respect to the criteria leads to certain properties of, for
    example, the estimated parametes in a least squares regression. See
    https://en.wikipedia.org/wiki/Optimal_design for more information on the specific
    criteria.

    Brief explaination:
    - a: minimizes the average variance of the least squares estimator
    - d: maximizes the determinant information matrix of the least squares estimator
    - e: maximizes the minimum eigenvalue of the information matrix
    - g: minimizes the maximum variance of the predicted value in the least squares case
    - maximin: maximizes the minimum (l-infinity) distance between any two points

    Args:
        x (np.ndarray): Data matrix. If 3d, computes the criterion value along the first
            dimension.
        criterion (str): Criterion type, must be in {'a-optimal', 'd-optimal',
            'e-optimal', 'g-optimal', 'maximin'}.
        target (str): One of "linear", "quadratic" or "polynomial". Determines in which
            space the criterion is evaluated. For "quadratic" the Latin-Hypercube sample
            X is mapped onto Y(X) = [X, X ** 2] and the criterion value for X is
            computed using Y(X). For "polynomial" Y(X) will also include all cross
            terms. This can be useful if one chooses an optimality criterion that
            minimizes e.g. the variance of the least-squares estimator, while using a
            quadratic or polynomial model.

    Returns:
        crit_vals (np.ndarray): Criterion values.

    
Compute optimality criterion of data matrix along axis.
Implements criteria that measure the dispersion of sample points in a space.
Optimization with respect to the criteria leads to certain properties of, for
example, the estimated parametes in a least squares regression.


Compute optimality criterion of data matrix along axis . Implements criteria that measure the dispersion of sample points in a space . Optimization with respect to the criteria leads to certain properties of for example the estimated parametes in a least squares regression .
def compute_optimality_criterion(x, criterion, target):
    implemented_criteria = {
        "a-optimal",
        "d-optimal",
        "e-optimal",
        "g-optimal",
        "maximin",
    }
    if criterion not in implemented_criteria:
        raise ValueError(f"Invalid criterion. Must be in {implemented_criteria}.")
    implemented_targets = {"linear", "quadratic"}
    if target not in implemented_targets:
        raise ValueError(f"Invalid target. Must be in {implemented_targets}.")
    if x.ndim == 2:
        x = np.expand_dims(x, axis=0)
    if target == "quadratic":
        x = np.concatenate((x, x**2), axis=2)
    prod = np.matmul(x.transpose(0, 2, 1), x)
    if criterion in {"a-optimal", "g-optimal"}:
        if x.shape[1] < x.shape[2]:
            inv = np.linalg.inv(prod + 0.01 * np.eye(x.shape[2]))
        else:
            is_invertible = np.linalg.cond(prod) < 1 / np.finfo(float).eps
            inv = np.linalg.inv(prod[is_invertible])
            crit_vals = np.tile(np.inf, x.shape[0])
    if criterion == "a-optimal":
        crit_vals[is_invertible] = inv.trace(axis1=1, axis2=2)
    elif criterion == "g-optimal":
        hat_mat = np.matmul(
            np.matmul(x[is_invertible], inv), x[is_invertible].transpose(0, 2, 1)
        )
        crit_vals[is_invertible] = np.max(np.diagonal(hat_mat.T), axis=1)
    elif criterion == "d-optimal":
        crit_vals = -np.linalg.det(prod)  
    elif criterion == "e-optimal":
        eig_vals = np.linalg.eig(prod)[0]
        crit_vals = -np.min(eig_vals, axis=1)  
    elif criterion == "maximin":
        distances = []
        for row1, row2 in list(combinations(range(x.shape[1]), 2)):
            dist = np.linalg.norm(x[:, row1, :] - x[:, row2, :], axis=1, ord=np.inf)
            distances.append(dist)
        crit_vals = -np.vstack(distances).min(axis=0)  
    return crit_vals
def compute_optimality_criterion ( x , criterion , target ) : implemented_criteria = { "a-optimal" , "d-optimal" , "e-optimal" , "g-optimal" , "maximin" , } if criterion not in implemented_criteria : raise ValueError ( f"Invalid criterion. Must be in {implemented_criteria}." ) implemented_targets = { "linear" , "quadratic" } if target not in implemented_targets : raise ValueError ( f"Invalid target. Must be in {implemented_targets}." ) if x . ndim == 2 : x = np . expand_dims ( x , axis = 0 ) if target == "quadratic" : x = np . concatenate ( ( x , x ** 2 ) , axis = 2 ) prod = np . matmul ( x . transpose ( 0 , 2 , 1 ) , x ) if criterion in { "a-optimal" , "g-optimal" } : if x . shape [ 1 ] < x . shape [ 2 ] : inv = np . linalg . inv ( prod + 0.01 * np . eye ( x . shape [ 2 ] ) ) else : is_invertible = np . linalg . cond ( prod ) < 1 / np . finfo ( float ) . eps inv = np . linalg . inv ( prod [ is_invertible ] ) crit_vals = np . tile ( np . inf , x . shape [ 0 ] ) if criterion == "a-optimal" : crit_vals [ is_invertible ] = inv . trace ( axis1 = 1 , axis2 = 2 ) elif criterion == "g-optimal" : hat_mat = np . matmul ( np . matmul ( x [ is_invertible ] , inv ) , x [ is_invertible ] . transpose ( 0 , 2 , 1 ) ) crit_vals [ is_invertible ] = np . max ( np . diagonal ( hat_mat . T ) , axis = 1 ) elif criterion == "d-optimal" : crit_vals = - np . linalg . det ( prod ) elif criterion == "e-optimal" : eig_vals = np . linalg . eig ( prod ) [ 0 ] crit_vals = - np . min ( eig_vals , axis = 1 ) elif criterion == "maximin" : distances = [ ] for row1 , row2 in list ( combinations ( range ( x . shape [ 1 ] ) , 2 ) ) : dist = np . linalg . norm ( x [ : , row1 , : ] - x [ : , row2 , : ] , axis = 1 , ord = np . inf ) distances . append ( dist ) crit_vals = - np . vstack ( distances ) . min ( axis = 0 ) return crit_vals
Compute optimality criterion of data matrix along axis.
Compute optimality criterion of data matrix along axis .
"""Compute optimality criterion of data matrix along axis.

    Implements criteria that measure the dispersion of sample points in a space.
    Optimization with respect to the criteria leads to certain properties of, for
    example, the estimated parametes in a least squares regression. See
    https://en.wikipedia.org/wiki/Optimal_design for more information on the specific
    criteria.

    Brief explaination:
    - a: minimizes the average variance of the least squares estimator
    - d: maximizes the determinant information matrix of the least squares estimator
    - e: maximizes the minimum eigenvalue of the information matrix
    - g: minimizes the maximum variance of the predicted value in the least squares case
    - maximin: maximizes the minimum (l-infinity) distance between any two points

    Args:
        x (np.ndarray): Data matrix. If 3d, computes the criterion value along the first
            dimension.
        criterion (str): Criterion type, must be in {'a-optimal', 'd-optimal',
            'e-optimal', 'g-optimal', 'maximin'}.
        target (str): One of "linear", "quadratic" or "polynomial". Determines in which
            space the criterion is evaluated. For "quadratic" the Latin-Hypercube sample
            X is mapped onto Y(X) = [X, X ** 2] and the criterion value for X is
            computed using Y(X). For "polynomial" Y(X) will also include all cross
            terms. This can be useful if one chooses an optimality criterion that
            minimizes e.g. the variance of the least-squares estimator, while using a
            quadratic or polynomial model.

    Returns:
        crit_vals (np.ndarray): Criterion values.

    """ # pre-processing # common computations # compute criteria # minus because we maximize # minus because we maximize # minus because we maximize
crit_vals (np.ndarray): Criterion values.
crit_vals ( np . ndarray ) : Criterion values .
x
Data matrix. If 3d, computes the criterion value along the first
dimension.
Data matrix . If 3d computes the criterion value along the first dimension .
criterion
Criterion type, must be in {'a-optimal', 'd-optimal',
'e-optimal', 'g-optimal', 'maximin'}.
Criterion type must be in { ' a - optimal ' ' d - optimal ' ' e - optimal ' ' g - optimal ' ' maximin ' } .
target
One of "linear", "quadratic" or "polynomial". Determines in which
space the criterion is evaluated. For "quadratic" the Latin-Hypercube sample
X is mapped onto Y(X) = [X, X ** 2] and the criterion value for X is
computed using Y(X). For "polynomial" Y(X) will also include all cross
terms. This can be useful if one chooses an optimality criterion that
minimizes e.g. the variance of the least-squares estimator, while using a
quadratic or polynomial model.
One of " linear " " quadratic " or " polynomial " . Determines in which space the criterion is evaluated . For " quadratic " the Latin - Hypercube sample X is mapped onto Y ( X ) = [ X X ** 2 ] and the criterion value for X is computed using Y ( X ) . For " polynomial " Y ( X ) will also include all cross terms . This can be useful if one chooses an optimality criterion that minimizes e . g . the variance of the least - squares estimator while using a quadratic or polynomial model .