Constraints module

Classes for generating quadratic programming constraints for BLM.

All classes are for solving a quadratic programming model of the following form:

min_x(1/2 x.T @ P @ x + q.T @ x) s.t. Gx <= h

Ax = b

class pytwoway.constraints.constraints.BoundedAbove(ub=0, nt=2)

Bases: object

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

Parameters
  • ub (float) – upper bound

  • nt (int) – number of time periods

class pytwoway.constraints.constraints.BoundedBelow(lb=0, nt=2)

Bases: object

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

Parameters
  • lb (float) – lower bound

  • nt (int) – number of time periods

class pytwoway.constraints.constraints.Linear(nnt=None, nt=2)

Bases: object

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

Parameters
  • nnt (int or list of ints or None) – time periods to constrain. This should be set to 0 if Linear() is being used in conjunction with Stationary(). None is equivalent to range(nt).

  • nt (int) – number of time periods

class pytwoway.constraints.constraints.LinearAdditive(nnt=None, nt=2)

Bases: object

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

Parameters
  • nnt (int or list of ints or None) – time periods to constrain. This should be set to 1 if LinearAdditive() is being used in conjunction with Stationary(). None is equivalent to range(nt).

  • nt (int) – number of time periods

class pytwoway.constraints.constraints.MinFirmType(min_firm_type, md=0, is_min=True, cross_period_mean=False, nnt=None, nt=2)

Bases: object

Generate BLM constraints so that the mean of a given firm type is less than or equal to the mean of every other firm type.

Parameters
  • min_firm_type (int) – minimum firm type

  • md (float) – minimum difference between the lowest firm type and other types

  • is_min (bool) – if True, constraint firm type to be the lowest firm type; if False, constraint it to be the highest firm type

  • cross_period_mean (bool) – if True, rather than checking means are monotonic for each period separately, consider the mean worker effects over all periods jointly

  • nnt (int or list of ints or None) – time periods to constrain; None is equivalent to range(nt)

  • nt (int) – number of time periods

class pytwoway.constraints.constraints.Monotonic(md=0, increasing=True, nt=2)

Bases: object

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

Parameters
  • md (float) – minimum difference between consecutive types

  • increasing (bool) – if True, monotonic increasing; if False, monotonic decreasing

  • nt (int) – number of time periods

class pytwoway.constraints.constraints.MonotonicMean(md=0, increasing=True, cross_period_mean=False, nnt=None, nt=2)

Bases: object

Generate BLM constraints so that the mean of worker types effects over all firm types must increase (or decrease) monotonically.

Parameters
  • md (float) – minimum difference between consecutive types

  • increasing (bool) – if True, monotonic increasing; if False, monotonic decreasing

  • cross_period_mean (bool) – if True, rather than checking means are monotonic for each period separately, consider the mean worker effects over all periods jointly

  • nnt (int or list of ints or None) – time periods to constrain; None is equivalent to range(nt)

  • nt (int) – number of time periods

class pytwoway.constraints.constraints.NoWorkerTypeInteraction(nnt=None, nt=2)

Bases: object

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

Parameters
  • nnt (int or list of ints or None) – time periods to constrain; None is equivalent to range(nt)

  • nt (int) – number of time periods

class pytwoway.constraints.constraints.NormalizeAll(min_firm_type, cross_period_normalize=False, nnt=None, nt=2)

Bases: object

Generate BLM constraints so that all worker-firm type pairs that include the lowest firm type have effect 0.

Parameters
  • min_firm_type (int) – lowest firm type

  • cross_period_normalize (bool) – if True, rather than normalizing for each period separately, normalize the mean of all worker-firm type pair effects over all periods jointly

  • nnt (int or list of ints or None) – time periods to constrain; None is equivalent to range(nt)

  • nt (int) – number of time periods

class pytwoway.constraints.constraints.NormalizeLowest(min_firm_type, cross_period_normalize=False, nnt=None, nt=2)

Bases: object

Generate BLM constraints so that the lowest worker-firm type pair has effect 0.

Parameters
  • min_firm_type (int) – lowest firm type

  • cross_period_normalize (bool) – if True, rather than normalizing for each period separately, normalize the mean of the lowest worker-firm type pair effect over all periods jointly

  • nnt (int or list of ints or None) – time periods to constrain; None is equivalent to range(nt)

  • nt (int) – number of time periods

class pytwoway.constraints.constraints.QPConstrained(nl, nk)

Bases: object

Solve a quadratic programming model of the following form:

min_x(1/2 x.T @ P @ x + q.T @ x) s.t. Gx <= h

Ax = b

Parameters
  • nl (int) – number of worker types

  • nk (int) – number of firm types

add_constraints(constraints)

Add a built-in constraint.

Parameters

constraints (object or list of objects) – constraint objects with class method ._get_constraints() that defines constraints to add

check_feasible()

Check that constraints are feasible.

Returns

True if constraints feasible, False otherwise

Return type

(bool)

clear_constraints(inequality=True, equality=True)

Clear constraints.

Parameters
  • inequality (bool) – if True, clear inequality constraints

  • equality (bool) – if True, clear equality constraints

pad(l=0, r=0)

Add padding to the left and/or right of C matrix.

Parameters
  • l (int) – how many columns to add on left

  • r (int) – how many columns to add on right

solve(P, q, solver='quadprog', verbose=False, **kwargs)
Solve a quadratic programming model of the following form:

min_x(1/2 x.T @ P @ x + q.T @ x) s.t. Gx <= h

Ax = b

Parameters
  • P (NumPy Array) – P in quadratic programming problem

  • q (NumPy Array) – q in quadratic programming problem

  • solver (str) – solver to use

  • verbose (bool) – if True, print extra output

  • **kwargs – parameters for solver

Returns

x that solves quadratic programming problem

Return type

(NumPy Array)

class pytwoway.constraints.constraints.Stationary(nwt=-1, nt=2)

Bases: object

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

Parameters
  • nwt (int) – number of worker types to constrain. This is used in conjunction with Linear(), as only two worker types are required to be constrained in this case; or in conjunction with NoWorkerTypeInteraction(), as only one worker type is required to be constrained in this case. Setting ns=-1 constrains all worker types.

  • nt (int) – number of time periods

class pytwoway.constraints.constraints.StationaryFirmTypeVariation(nnt=None, nt=2)

Bases: object

Generate BLM constraints so that the firm type induced variation of worker-firm pair effects is the same in all periods. In particular, this is equivalent to setting A2 = (np.mean(A2, axis=1) + A1.T - np.mean(A1, axis=1)).T.

Parameters
  • nnt (int or list of ints or None) – time periods to constrain; None is equivalent to range(1, nt)

  • nt (int) – number of time periods