Python API

Overview

The main PyTwoWay API is split into multiple classes for estimating models and two for simulating data according to the BLM and Borovickova-Shimer dgps. It also has four modules: one for constructing the variance-covariance matrix for AKM and its bias corrections; one for generating constraints for BLM; one for generating attrition plots using increasing (building up from a fixed set of firms) or decreasing (with varying sets of firms) fractions of movers; and one for generating diagnostics for bipartite labor data. PyTwoWay is canonically imported using

import pytwoway as tw

Classes

  • pytwoway.FEEstimator: Class for estimating AKM and its bias corrections without controls

  • pytwoway.FEControlEstimator: Class for estimating AKM and its bias corrections with controls

  • pytwoway.CREEstimator: Class for estimating the CRE model

  • pytwoway.BLMModel: Class for estimating the BLM model with one set of starting values

  • pytwoway.BLMEstimator: Class for estimating the BLM model with multiple sets of starting values

  • pytwoway.BLMBootstrap: Class for estimating the BLM model with bootstrapped confidence intervals

  • pytwoway.BLMVarianceDecomposition: Class for estimating the variance decomposition of the BLM model with bootstrapped confidence intervals

  • pytwoway.InteractedBLMModel: Class for estimating the interacted BLM model

  • pytwoway.SorkinEstimator: Class for estimating the fixed-point revealed preference model from Sorkin

  • pytwoway.BSEstimator: Class for estimating the non-parametric sorting model from Borovickova and Shimer

  • pytwoway.Attrition: Class for generating attrition plots

  • pytwoway.MonteCarlo: Class for running Monte Carlo estimations

  • pytwoway.SimBLM: Class for simulating BLM data

  • pytwoway.SimBS: Class for simulating Borovickova-Shimer data

Modules

  • pytwoway.Q: Module for constructing the variance-covariance matrix for AKM and its bias corrections

  • pytwoway.constraints: Module for generating constraints for BLM

  • pytwoway.attrition_utils: Module for estimating attrition using increasing (building up from a fixed set of firms) or decreasing (with varying sets of firms) fractions of movers

  • pytwoway.diagnostics: Module for generating diagnostics for bipartite labor data

Classes and Methods

pytwoway.FEEstimator

FEEstimator(adata[, params])

Uses multigrid and partialing out to solve two way fixed effect models.

fit([rng])

Estimate FE model.

pytwoway.FEControlEstimator

FEControlEstimator(data[, params])

Solve two way fixed effect models with control variables.

fit([rng])

Estimate FE model.

pytwoway.CREEstimator

CREEstimator(data[, params])

Uses multigrid and partialing out to solve two way Fixed Effect model.

fit([rng])

Run CRE solver.

pytwoway.BLMModel

BLMModel(params[, rng])

Class for estimating BLM using a single set of starting values.

compute_connectedness_measure([all])

Computes graph connectedness measure among the movers within each type and updates self.connectedness to be the smallest value.

fit_movers(jdata[, compute_NNm])

EM algorithm for movers.

fit_movers_cstr_uncstr(jdata[, compute_NNm])

Run fit_movers(), first constrained, then using results as starting values, run unconstrained.

fit_stayers(sdata[, compute_NNs])

EM algorithm for stayers.

plot_log_earnings([period, grid, dpi])

Plot log-earnings by worker-firm type pairs.

plot_type_proportions([period, subset, dpi])

Plot proportions of worker types at each firm class.

pytwoway.BLMEstimator

BLMEstimator(params)

Class for estimating BLM using multiple sets of starting values.

fit(jdata, sdata[, n_init, n_best, ncore, rng])

Estimate BLM using multiple sets of starting values.

plot_liks_connectedness([jitter, dpi])

Plot likelihoods vs connectedness for the estimations run.

plot_log_earnings([period, grid, dpi])

Plot log-earnings by worker-firm type pairs.

plot_type_proportions([period, subset, dpi])

Plot proportions of worker types at each firm class.

pytwoway.BLMBootstrap

BLMBootstrap(params)

Class for estimating BLM using bootstrapping.

fit(jdata, sdata[, blm_model, n_samples, ...])

Estimate bootstrap.

plot_log_earnings([period, grid, dpi])

Plot log-earnings by worker-firm type pairs.

plot_type_proportions([period, subset, dpi])

Plot proportions of worker types at each firm class.

pytwoway.BLMVarianceDecomposition

BLMVarianceDecomposition(params)

Class for estimating BLM variance decomposition using bootstrapping.

fit(jdata, sdata[, blm_model, n_samples, ...])

Estimate variance decomposition.

pytwoway.InteractedBLMModel

pytwoway.SorkinEstimator

SorkinEstimator()

Class for estimating the fixed-point revealed preference model from Sorkin (2018).

fit(adata[, max_iters, threshold])

Estimate the fixed-point revealed preference model from Sorkin (2018).

pytwoway.BSEstimator

BSEstimator()

Class for estimating the non-parametric sorting model from Borovickova and Shimer.

fit(adata[, alternative_estimator, weighted])

Estimate the non-parametric sorting model from Borovickova and Shimer.

pytwoway.Attrition

Attrition([min_movers_threshold, ...])

Class of Attrition, which generates attrition plots using bipartite labor data.

attrition(bdf[, N, ncore, copy, rng])

Run Monte Carlo on attrition estimations of TwoWay to estimate variance of parameter estimates given fraction of movers remaining.

boxplots([fe, ho, he, cre, bs1, bs2, ...])

Generate attrition result boxplots.

plots([fe, ho, he, cre, bs1, bs2, ...])

Generate attrition result plots.

pytwoway.MonteCarlo

MonteCarlo([sim_params, fe_params, ...])

Class of MonteCarlo, where MonteCarlo runs a Monte Carlo estimation by simulating bipartite networks of firms and workers.

hist([fe, ho, he, cre, bs1, bs2, density])

Plot histogram of how Monte Carlo simulation results differ from truth.

monte_carlo([N, ncore, rng])

Run Monte Carlo simulations of two way fixed effect models to see the distribution of the true vs.

pytwoway.SimBLM

SimBLM([sim_params])

Class of SimBLM, where SimBLM simulates a bipartite BLM network of firms and workers.

simulate([return_parameters, rng])

Simulate data (movers and stayers).

pytwoway.SimBS

SimBS([sim_params])

Class of SimBS, where SimBS simulates a bipartite Borovickova-Shimer network of firms and workers.

simulate([rng])

Simulate data.

Modules and Methods

pytwoway.Q

CovCovariate(cov_names_1, cov_names_2)

Generate Q to estimate cov(covariate 1, covariate 2).

CovPsiAlpha()

Generate Q to estimate cov(psi, alpha).

CovPsiPrevPsiNext()

Generate Q to estimate cov(psi_t, psi_{t+1}).

VarAlpha()

Generate Q to estimate var(alpha).

VarCovariate(cov_names)

Generate Q to estimate var(covariate) or var(sum(covariates)) if multiple covariates are listed.

VarPsi()

Generate Q to estimate var(psi).

VarPsiPlusAlpha()

Generate Q to estimate var(psi + alpha).

pytwoway.constraints

BoundedAbove([ub, nt])

Generate BLM constraints so that worker-firm pair effects are bounded above.

BoundedBelow([lb, nt])

Generate BLM constraints so that worker-firm pair effects are bounded below.

Linear([nnt, nt])

Generate BLM constraints so that for a fixed firm type, worker types effects must change linearly.

Monotonic([md, increasing, nt])

Generate BLM constraints so that for a fixed firm type, worker types effects must increase (or decrease) monotonically.

NoWorkerTypeInteraction([nnt, nt])

Generate BLM constraints so that for a fixed firm type, worker types effects must all be the same.

QPConstrained(nl, nk)

Solve a quadratic programming model of the following form:

Stationary([nwt, nt])

Generate BLM constraints so that worker-firm pair effects are the same in all periods.

StationaryFirmTypeVariation([nnt, nt])

Generate BLM constraints so that the firm type induced variation of worker-firm pair effects is the same in all periods.

pytwoway.attrition_utils

AttritionIncreasing([subset_fractions, ...])

Generate increasing subsets of a dataset to estimate the effects of attrition.

AttritionDecreasing([subset_fractions])

Generate decreasing subsets of a dataset to estimate the effects of attrition.

pytwoway.diagnostics

plot_extendedeventstudy(adata[, ...])

Generate event study plots.