Skip to content

poisson_glm module

Estimates the semilinear Choo and Siow homoskedastic (2006) model using Poisson GLM.

choo_siow_poisson_glm(muhat, phi_bases, no_singles=False, tol=1e-12, max_iter=10000, verbose=1)

Estimates the semilinear Choo and Siow homoskedastic (2006) model using Poisson GLM.

Parameters:

Name Type Description Default
muhat Matching

the observed Matching

required
phi_bases np.ndarray

an (X, Y, K) array of bases

required
no_singles bool

if True, we do not observe the singles

False
tol float | None

tolerance level for linear_model.PoissonRegressor.fit

1e-12
max_iter int | None

maximum number of iterations for linear_model.PoissonRegressor.fit

10000
verbose int | None

defines how much output we want (0 = least)

1

Returns:

Type Description
PoissonGLMResults

a PoissonGLMResults instance

Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
n_households = 1e6
X, Y, K = 4, 3, 6
# we setup a quadratic set of basis functions
phi_bases = np.zeros((X, Y, K))
phi_bases[:, :, 0] = 1
for x in range(X):
    phi_bases[x, :, 1] = x
    phi_bases[x, :, 3] = x * x
    for y in range(Y):
        phi_bases[x, y, 4] = x * y
for y in range(Y):
    phi_bases[:, y, 2] = y
    phi_bases[:, y, 5] = y * y

lambda_true = np.random.randn(K)
phi_bases = np.random.randn(X, Y, K)
Phi = phi_bases @ lambda_true

# we simulate a Choo and Siow sample from a population
#  with equal numbers of men and women of each type
n = np.ones(X)
m = np.ones(Y)
choo_siow_instance = ChooSiowPrimitives(Phi, n, m)
mus_sim = choo_siow_instance.simulate(n_households)
muxy_sim, mux0_sim, mu0y_sim, n_sim, m_sim = mus_sim.unpack()

results = choo_siow_poisson_glm(mus_sim, phi_bases)

# compare true and estimated parameters
results.print_results(
    lambda_true,
    u_true=-np.log(mux0_sim / n_sim),
    v_true=-np.log(mu0y_sim / m_sim)
)
Source code in cupid_matching/poisson_glm.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
def choo_siow_poisson_glm(
    muhat: Matching,
    phi_bases: np.ndarray,
    no_singles: bool = False,
    tol: float | None = 1e-12,
    max_iter: int | None = 10000,
    verbose: int | None = 1,
) -> PoissonGLMResults:
    """Estimates the semilinear Choo and Siow homoskedastic (2006) model
        using Poisson GLM.

    Args:
        muhat: the observed Matching
        phi_bases: an (X, Y, K) array of bases
        no_singles: if True, we do not observe the singles
        tol: tolerance level for `linear_model.PoissonRegressor.fit`
        max_iter: maximum number of iterations
            for `linear_model.PoissonRegressor.fit`
        verbose: defines how much output we want (0 = least)

    Returns:
        a `PoissonGLMResults` instance

    Example:
        ```py
        n_households = 1e6
        X, Y, K = 4, 3, 6
        # we setup a quadratic set of basis functions
        phi_bases = np.zeros((X, Y, K))
        phi_bases[:, :, 0] = 1
        for x in range(X):
            phi_bases[x, :, 1] = x
            phi_bases[x, :, 3] = x * x
            for y in range(Y):
                phi_bases[x, y, 4] = x * y
        for y in range(Y):
            phi_bases[:, y, 2] = y
            phi_bases[:, y, 5] = y * y

        lambda_true = np.random.randn(K)
        phi_bases = np.random.randn(X, Y, K)
        Phi = phi_bases @ lambda_true

        # we simulate a Choo and Siow sample from a population
        #  with equal numbers of men and women of each type
        n = np.ones(X)
        m = np.ones(Y)
        choo_siow_instance = ChooSiowPrimitives(Phi, n, m)
        mus_sim = choo_siow_instance.simulate(n_households)
        muxy_sim, mux0_sim, mu0y_sim, n_sim, m_sim = mus_sim.unpack()

        results = choo_siow_poisson_glm(mus_sim, phi_bases)

        # compare true and estimated parameters
        results.print_results(
            lambda_true,
            u_true=-np.log(mux0_sim / n_sim),
            v_true=-np.log(mu0y_sim / m_sim)
        )
        ```

    """
    X, Y, K = phi_bases.shape
    XY = X * Y

    # the vector of weights for the Poisson regression
    w = (
        2 * np.ones(XY)
        if no_singles
        else np.concatenate((2 * np.ones(XY), np.ones(X + Y)))
    )
    # reshape the bases
    phi_mat = make_XY_K_mat(phi_bases)

    id_X = np.eye(X)
    id_Y = np.eye(Y)
    ones_X = np.ones((X, 1))
    ones_Y = np.ones((Y, 1))
    if no_singles:
        Z_unweighted = np.hstack(
            [-np.kron(id_X, ones_Y), -np.kron(ones_X, id_Y), phi_mat]
        )
        # we need to normalize u_1 = 0, so we delete the first column
        Z_unweighted = Z_unweighted[:, 1:]
    else:
        zeros_XK = np.zeros((X, K))
        zeros_YK = np.zeros((Y, K))
        zeros_XY = np.zeros((X, Y))
        zeros_YX = np.zeros((Y, X))
        Z_unweighted = np.vstack(
            [
                np.hstack([-np.kron(id_X, ones_Y), -np.kron(ones_X, id_Y), phi_mat]),
                np.hstack([-id_X, zeros_XY, zeros_XK]),
                np.hstack([zeros_YX, -id_Y, zeros_YK]),
            ]
        )
    Z = Z_unweighted / w.reshape((-1, 1))

    var_muhat = variance_muhat(muhat)
    (
        muhat_norm,
        var_muhat_norm,
        n_households,
        n_individuals,
    ) = prepare_data(muhat, var_muhat, no_singles=no_singles)

    clf = linear_model.PoissonRegressor(
        fit_intercept=False,
        tol=tol,
        verbose=verbose,
        alpha=0,
        max_iter=max_iter,
    )
    if no_singles:
        muxyhat_norm = muhat_norm[:XY]
        clf.fit(Z, muxyhat_norm, sample_weight=w)
    else:
        clf.fit(Z, muhat_norm, sample_weight=w)
    gamma_est = clf.coef_

    # we compute_ the variance-covariance of the estimator
    var_allmus_norm = var_muhat_norm.var_allmus
    var_norm = var_allmus_norm[:XY, :XY] if no_singles else var_allmus_norm
    nr, nc = Z.shape
    exp_Zg = np.exp(Z @ gamma_est).reshape(nr)
    A_hat = np.zeros((nc, nc))
    B_hat = np.zeros((nc, nc))
    for i in range(nr):
        Zi = Z[i, :]
        wi = w[i]
        A_hat += wi * exp_Zg[i] * np.outer(Zi, Zi)
        for j in range(nr):
            Zj = Z[j, :]
            B_hat += wi * w[j] * var_norm[i, j] * np.outer(Zi, Zj)

    A_inv = spla.inv(A_hat)
    varcov_gamma = A_inv @ B_hat @ A_inv
    stderrs_gamma = np.sqrt(np.diag(varcov_gamma))

    beta_est = gamma_est[-K:]
    varcov_beta = varcov_gamma[-K:, -K:]
    beta_std = stderrs_gamma[-K:]
    Phi_est = phi_bases @ beta_est

    # we correct for the effect of the normalization
    _, _, _, n, m = muhat.unpack()
    n_norm = n / n_individuals
    m_norm = m / n_individuals
    if no_singles:
        u_est = gamma_est[: (X - 1)]
        v_est = gamma_est[(X - 1) : -K]
        # normalize u_1 = 0
        n_0 = n_norm[0]
        u_est = np.concatenate((np.zeros(1), u_est + np.log(n_norm[1:] / n_0)))
        v_est += np.log(m_norm * n_0)
    else:
        u_est = gamma_est[:X] + np.log(n_norm)
        v_est = gamma_est[X:-K] + np.log(m_norm)

    # since u and v are translated from gamma we need to adjust the estimated stderrs
    A_inv_Z = A_inv @ Z_unweighted.T
    if no_singles:
        u_std = _stderrs_u_no_singles(
            varcov_gamma, n_norm, var_muhat_norm, A_inv_Z, X, Y
        )
        v_std = _stderrs_v_no_singles(
            varcov_gamma, m_norm, n_norm, var_muhat_norm, A_inv_Z, X, Y
        )
    else:
        u_std = _stderrs_u(varcov_gamma, n_norm, var_muhat_norm, A_inv_Z, X, Y)
        v_std = _stderrs_v(varcov_gamma, m_norm, var_muhat_norm, A_inv_Z, X, Y)

    results = PoissonGLMResults(
        X=X,
        Y=Y,
        K=K,
        number_households=n_households,
        number_individuals=n_individuals,
        estimated_gamma=gamma_est,
        estimated_Phi=Phi_est,
        estimated_beta=beta_est,
        estimated_u=u_est,
        estimated_v=v_est,
        varcov_gamma=varcov_gamma,
        varcov_beta=varcov_beta,
        stderrs_gamma=stderrs_gamma,
        stderrs_beta=beta_std,
        stderrs_u=u_std,
        stderrs_v=v_std,
    )

    return results