src/estimagic/inference/ml_covs.py
def _stratification(jac, design_info):
    """Variance estimatio for each strata stratum.

    The function takes the sum of the jacobian observations for each cluster
    within strata. The result is the meat of the sandwich estimator.

    Args:
        design_options (pd.DataFrame): dataframe containing psu, stratum,
            population/design weight and/or a finite population corrector (fpc)
        jac (np.array): "jacobian" - an n x k + 1-dimensional array of first
            derivatives of the pseudo-log-likelihood function w.r.t. the parameters

    Returns:
        strata_meat (np.array): 2d square array of length k + 1. Variance of
        the likelihood equation

    """
    n_params = len(jac[0, :])
    stratum_col = design_info["strata"]
    # Stratification does not require clusters
    if "psu" not in design_info:
        design_info["psu"] = design_info.index
    else:
        pass
    psu_col = design_info["psu"]
    strata_meat = np.zeros([n_params, n_params])
    # Variance estimation per stratum
    for stratum in stratum_col.unique():
        psu_in_strata = psu_col[stratum_col == stratum].unique()
        psu_jac = np.zeros([n_params])
        if "fpc" in design_info:
            fpc = design_info["fpc"][stratum_col == stratum].unique()
        else:
            fpc = 1
        # psu_jac stacks the sum of the observations for each cluster.
        for psu in psu_in_strata:
            psu_jac = np.vstack([psu_jac, np.sum(jac[psu_col == psu], axis=0)])
        psu_jac_mean = np.sum(psu_jac, axis=0) / len(psu_in_strata)
        if len(psu_in_strata) > 1:
            mid_step = np.dot(
                (psu_jac[1:] - psu_jac_mean).T, (psu_jac[1:] - psu_jac_mean)
            )
            strata_meat += (
                fpc * (len(psu_in_strata) / (len(psu_in_strata) - 1)) * mid_step
            )
        # Apply "grand-mean" method for single unit stratum
        elif len(psu_in_strata) == 1:
            strata_meat += fpc * np.dot(psu_jac[1:].T, psu_jac[1:])

    return strata_meat
Variance estimatio for each strata stratum.

    The function takes the sum of the jacobian observations for each cluster
    within strata. The result is the meat of the sandwich estimator.

    Args:
        design_options (pd.DataFrame): dataframe containing psu, stratum,
            population/design weight and/or a finite population corrector (fpc)
        jac (np.array): "jacobian" - an n x k + 1-dimensional array of first
            derivatives of the pseudo-log-likelihood function w.r.t. the parameters

    Returns:
        strata_meat (np.array): 2d square array of length k + 1. Variance of
        the likelihood equation

    
Variance estimatio for each strata stratum.
The function takes the sum of the jacobian observations for each cluster
within strata. The result is the meat of the sandwich estimator.
Variance estimatio for each strata stratum . The function takes the sum of the jacobian observations for each cluster within strata . The result is the meat of the sandwich estimator .
def _stratification(jac, design_info):
    n_params = len(jac[0, :])
    stratum_col = design_info["strata"]
    if "psu" not in design_info:
        design_info["psu"] = design_info.index
    else:
        pass
    psu_col = design_info["psu"]
    strata_meat = np.zeros([n_params, n_params])
    for stratum in stratum_col.unique():
        psu_in_strata = psu_col[stratum_col == stratum].unique()
        psu_jac = np.zeros([n_params])
        if "fpc" in design_info:
            fpc = design_info["fpc"][stratum_col == stratum].unique()
        else:
            fpc = 1
        for psu in psu_in_strata:
            psu_jac = np.vstack([psu_jac, np.sum(jac[psu_col == psu], axis=0)])
        psu_jac_mean = np.sum(psu_jac, axis=0) / len(psu_in_strata)
        if len(psu_in_strata) > 1:
            mid_step = np.dot(
                (psu_jac[1:] - psu_jac_mean).T, (psu_jac[1:] - psu_jac_mean)
            )
            strata_meat += (
                fpc * (len(psu_in_strata) / (len(psu_in_strata) - 1)) * mid_step
            )
        elif len(psu_in_strata) == 1:
            strata_meat += fpc * np.dot(psu_jac[1:].T, psu_jac[1:])
    return strata_meat
def _stratification ( jac , design_info ) : n_params = len ( jac [ 0 , : ] ) stratum_col = design_info [ "strata" ] if "psu" not in design_info : design_info [ "psu" ] = design_info . index else : pass psu_col = design_info [ "psu" ] strata_meat = np . zeros ( [ n_params , n_params ] ) for stratum in stratum_col . unique ( ) : psu_in_strata = psu_col [ stratum_col == stratum ] . unique ( ) psu_jac = np . zeros ( [ n_params ] ) if "fpc" in design_info : fpc = design_info [ "fpc" ] [ stratum_col == stratum ] . unique ( ) else : fpc = 1 for psu in psu_in_strata : psu_jac = np . vstack ( [ psu_jac , np . sum ( jac [ psu_col == psu ] , axis = 0 ) ] ) psu_jac_mean = np . sum ( psu_jac , axis = 0 ) / len ( psu_in_strata ) if len ( psu_in_strata ) > 1 : mid_step = np . dot ( ( psu_jac [ 1 : ] - psu_jac_mean ) . T , ( psu_jac [ 1 : ] - psu_jac_mean ) ) strata_meat += ( fpc * ( len ( psu_in_strata ) / ( len ( psu_in_strata ) - 1 ) ) * mid_step ) elif len ( psu_in_strata ) == 1 : strata_meat += fpc * np . dot ( psu_jac [ 1 : ] . T , psu_jac [ 1 : ] ) return strata_meat
Variance estimatio for each strata stratum.
Variance estimatio for each strata stratum .
"""Variance estimatio for each strata stratum.

    The function takes the sum of the jacobian observations for each cluster
    within strata. The result is the meat of the sandwich estimator.

    Args:
        design_options (pd.DataFrame): dataframe containing psu, stratum,
            population/design weight and/or a finite population corrector (fpc)
        jac (np.array): "jacobian" - an n x k + 1-dimensional array of first
            derivatives of the pseudo-log-likelihood function w.r.t. the parameters

    Returns:
        strata_meat (np.array): 2d square array of length k + 1. Variance of
        the likelihood equation

    """ # Stratification does not require clusters # Variance estimation per stratum # psu_jac stacks the sum of the observations for each cluster. # Apply "grand-mean" method for single unit stratum
strata_meat (np.array): 2d square array of length k + 1. Variance of
the likelihood equation
strata_meat ( np . array ) : 2d square array of length k + 1 . Variance of the likelihood equation
jac
"jacobian" - an n x k + 1-dimensional array of first
derivatives of the pseudo-log-likelihood function w.r.t. the parameters
" jacobian " - an n x k + 1 - dimensional array of first derivatives of the pseudo - log - likelihood function w . r . t . the parameters
design_info
design_options
dataframe containing psu, stratum,
population/design weight and/or a finite population corrector (fpc)
dataframe containing psu stratum population / design weight and / or a finite population corrector ( fpc )