Utilities for MDE - Cupid Matching

Skip to content

# `min_distance_utils` module

Utility programs used in `min_distance.py`.

## `MDEResults` `dataclass`

The results from minimum-distance estimation and testing.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`X` | `int` | int | required
`Y` | `int` | int | required
`K` | `int` | int | required
`number_households` | `int` | int | required
`estimated_coefficients` | `np.ndarray` | np.ndarray | required
`varcov_coefficients` | `np.ndarray` | np.ndarray | required
`stderrs_coefficients` | `np.ndarray` | np.ndarray | required
`estimated_Phi` | `np.ndarray` | np.ndarray | required
`test_statistic` | `float` | float | required
`test_pvalue` | `float` | float | required
`ndf` | `int` | int | required
`parameterized_entropy` | `Optional[bool]` | Optional[bool] = False | `False`

Source code in `cupid_matching/min_distance_utils.py`

     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    @dataclass
    class MDEResults:
        """
        The results from minimum-distance estimation and testing.

        Args:
            X: int
            Y: int
            K: int
            number_households: int
            estimated_coefficients: np.ndarray
            varcov_coefficients: np.ndarray
            stderrs_coefficients: np.ndarray
            estimated_Phi: np.ndarray
            test_statistic: float
            test_pvalue: float
            ndf: int
            parameterized_entropy: Optional[bool] = False
        """

        X: int
        Y: int
        K: int
        number_households: int
        estimated_coefficients: np.ndarray
        varcov_coefficients: np.ndarray
        stderrs_coefficients: np.ndarray
        estimated_Phi: np.ndarray
        test_statistic: float
        test_pvalue: float
        ndf: int
        parameterized_entropy: Optional[bool] = False

        def __str__(self):
            line_stars = "*" * 80 + "\n"
            if self.parameterized_entropy:
                n_alpha = self.estimated_coefficients.size - self.K
                entropy_str = f"     The entropy has {n_alpha} parameters."
            else:
                entropy_str = "     The entropy is parameter-free."
                n_alpha = 0
            model_str = f"The data has {self.number_households} households\n\n"
            model_str += f"The model has {self.X}x{self.Y} margins\n {entropy_str} \n"
            model_str += f"We use {self.K} basis functions.\n\n"
            repr_str = line_stars + model_str
            repr_str += "The estimated coefficients (and their standard errors) are\n\n"
            if self.parameterized_entropy:
                for i, coeff in enumerate(self.estimated_coefficients[:n_alpha]):
                    repr_str += (
                        f"   alpha({i + 1}): {coeff: > 10.3f}  "
                        + f"({self.stderrs_coefficients[i]: .3f})\n"
                    )
                repr_str += "\n"
            for i, coeff in enumerate(self.estimated_coefficients[n_alpha:]):
                repr_str += (
                    f"   base {i + 1}: {coeff: > 10.3f} "
                    + f"({self.stderrs_coefficients[n_alpha + i]: .3f})\n"
                )
            repr_str += "\nSpecification test:\n"
            repr_str += (
                f"   the value of the test statistic is {self.test_statistic: > 10.3f}\n"
            )
            repr_str += (
                f"     for a chi2({self.ndf}), the p-value is {self.test_pvalue: > 10.3f}\n"
            )
            return repr_str + line_stars

        def print_results(
            self, true_coeffs: Optional[np.ndarray] = None, n_alpha: int = 0
        ) -> None | float:
            estimates = self.estimated_coefficients
            stderrs = self.stderrs_coefficients

            if true_coeffs is not None:
                repr_str = (
                    "The true and estimated coefficients "
                    + "(and their standard errors) are\n\n"
                )
                for i, coeff in enumerate(estimates[:n_alpha]):
                    repr_str += f"   alpha({i + 1}): {true_coeffs[i]: > 10.3f}"
                    repr_str += f"{coeff: > 10.3f}  ({stderrs[i]: > 10.3f})\n"
                    repr_str += "\n"
                for i, coeff in enumerate(estimates[n_alpha:]):
                    j = n_alpha + i
                    repr_str += (
                        f"   base {i + 1}: {true_coeffs[j]: > 10.3f}  "
                        + f"{coeff: > 10.3f}  ({stderrs[j]: > 10.3f})\n"
                    )
                print_stars(repr_str)
                discrepancy = npmaxabs(true_coeffs - estimates)
                print_stars(f"The true-estimated discrepancy is {discrepancy}")
            else:
                repr_str = (
                    "The estimated coefficients " + "(and their standard errors) are\n\n"
                )
                for i, coeff in enumerate(estimates[:n_alpha]):
                    repr_str + f"{coeff: > 10.3f}  ({stderrs[i]: > 10.3f})\n"
                    repr_str += "\n"
                for i, coeff in enumerate(estimates[n_alpha:]):
                    j = n_alpha + i
                    repr_str += f"{coeff: > 10.3f}  ({stderrs[j]: > 10.3f})\n"

            repr_str += "\nSpecification test:\n"
            repr_str += (
                "   the value of the test statistic is "
                + f"{self.test_statistic: > 10.3f}\n"
            )
            repr_str += (
                f"     for a chi2({self.ndf}), "
                + f"the p-value is {self.test_pvalue: > 10.3f}\n"
            )
            print_stars(repr_str)
            if true_coeffs is not None:
                return discrepancy
            return None