Package: lppinv
A non-iterated general implementation of the LPLS estimator for cOLS, TM, and custom casesThe program implements theLPLS(linear programming through least squares) estimator with the help of the Moore-Penrose inverse (pseudoinverse), calculated usingsingular value decomposition (SVD), with emphasis on the estimation of OLS constrained in values (cOLS), Transaction Matrix (TM), andcustom(user-defined) cases. The pseudoinverse offers a
unique minimum-norm least-squares solution, which is the best linear unbiased estimator (BLUE); see Albert (1972, Chapter VI). (Over)determined problems are accompanied byregressionanalysis, which is feasible in their case. For such and especially all remaining cases, a Monte Carlo-basedttestof meanNRMSE(normalized by the standard deviation of the RHS) is performed, the sample being drawn from a uniform or user-provided distribution (via aPython function).OLS constrained in values (cOLS) is an estimation based on constraints in the model and/or data but not in parameters. Typically, such models are of size ≤kN, whereNis the number of observations, since the number of their constraints may vary in the LHS (e.g., level, derivatives, etc.).Example of a cOLS problem:Estimate the trend and the cyclical component of a country's GDP given the textbook or any other definition of its peaks, troughs, and saddles. For a pre-LPLS approach to this problem, see (Bolotov, 2014).Transaction Matrix (TM) of size (M x N) is a formal model of interaction (allocation, assignment, etc.) betweenMandNelements in any imaginable system, such as intercompany transactions (netting tables), industries within/between economies (input-output tables), cross-border trade/investment (trade/investment matrices), etc., whererowandcolumn sumsare known, butindividual elementsof the TM may not be:a netting table is a type ofTMwhereM = Nand the elements are subsidiaries of a MNC;an input-output table (IOT) is a type ofTMwhereM = Nand the elements are industries;a matrix of trade/investment is a type ofTMwhereM = Nand the elements are countries or (macro)regions, where diagonal elements may be equal to zero;a country-product matrix is a type ofTMwhereM ≠ Nand the elements are of different types;...Example of a TM problem:Estimate the matrix of trade/investment with/without zero diagonal elements, the country shares in which are unknown. For a pre-LPLS approach to this problem, see (Bolotov, 2015).Methods and formulas:The LP problem in theLPLSestimator is a matrix equationa @ x = b, loosely based on the structure of the Simplex tableau, whereaconsists of coefficients for CONSTRAINTS, LP-type CHARACTERISTIC and/or SPECIFIC, and for SLACK/SURPLUS VARIABLES (the upper part) as well as for the MODEL (the lower part), as illustrated in Figure 1. Each part ofacan be omitted to accommodate a particular case:cOLSproblems require SPECIFIC CONSTRAINTS, no LP-type CHARACTERISTIC CONSTRAINTS, and a MODEL;TMrequires LP-type CHARACTERISTIC CONSTRAINTS, no SPECIFIC CONSTRAINTS, and an optional MODEL;SLACK/SURPLUS VARIABLES are included only for inequality constraints and should be set to1or-1;...Figure 1: Matrix equationa @ x = babCONSTRAINTS: CHARACTERISTIC/SPECIFICSL/SU VARIABLESCONSTRAINTSMODELMODELSource: self-preparedThe solution to the equation,x = pinv(a) @ b, is estimated with the help of SVD and is aminimum-norm least-squares generalized solutionif the rank ofais not full. To check ifais within the computational limits, its (maximum) dimensions can be calculated using the formulas:(k * N) x (K + K*)cOLS- without slack/surplus variables;(k * N) x (K + K* + l)cOLS- with slack/surplus variables;(M + N) x (M * N)TM- without slack/surplus variables;(M + N) x (M * N + l)TM- with slack/surplus variables;M x Ncustom- without slack/surplus variables;M x (N + l)custom- with slack/surplus variables;where, incOLSproblems,Kis the number of independent variables in the model (including the constant),K*is the number of eventual extra variables in CONSTRAINTS, andNis the number of observations; inTM,MandNare the dimensions of the matrix; and incustomcases,MandNorM x (N + l)are the dimensions ofa.Parameters:Type of the LP problemcols=False:bool, OLS constrained in values (cOLS)tm=False:bool, Transaction Matrix (TM), optionscolsandtmare mutually exclusive (not specifying any of them equalscustom)rhs=np.empty((0,0)):array_like, right-hand side of the problem (rowsums first for TM problems)Constructing the LHSmodel=np.empty((0,0)):array_like, the MODEL part ofa, including an eventual user-provided constant as a matrix column incOLS(see Methods and formulas)constraints=np.empty((0,0)):array_like, the CONSTRAINTS part ofaslackvars=np.empty((0,0)):array_like, the SLACK/SURPLUS VARIABLES part ofazero_diagonal=False:bool, set all the diagonal elements ofato 0SVD-based estimationtolerance=None:float, scipy.linalg.lstsq's cutoff for `small' singular values; used to determine effective rank of a. Singular values smaller than cond * largest_singular_value are considered zerolevel=95:int, confidence level (by default:95)Monte-Carlo-based t-testseed=123456789:int, random-number seed, # is any number between 0 and 2^31-1 (or 2,147,483,647) (by default:123456789)iterate=300:int, number of iterations, # must be divisible by 50 (by default:300)distribution=runiform:fucntion, random-variable generating function, name of an earlier declared Python object returning anarray_like(r x c)with two arguments, real scalarsrandc(by default:lppinv.runiform, see Examples on how to pass np.random.uniform tolppinv)mc=True:bool, skip the Monte Carlo-based t-testtrace=True:bool, hide any output with the exception of dotsResults:class lppinv.LPpinvResult(OLSResults:statsmodels.regression.linear_model.OLSResultsorNone, statsmodels' results class for for an OLS modelTtestResult:(scipy.stats._result_classes.TtestResult, scipy.stats._result_classes.TtestResult, scipy.stats._result_classes.TtestResult)or(None, None, None), scipy's result of a t-testsolution:array_like, the solution obtained from theLPLSestimatora:array_like, theamatrixnrmse:float, root mean square error normalized by the standard deviation of`b'r2_c:floatornp.nan, R-squared for CONSTRAINTS in TM)Errors:LPpinvError:lppinv.LPpinvError, specific Python-exception-derived object raised by lppinv functionsLinAlgError:scipy.linalg.LinAlgError, generic Python-exception-derived object raised by linalg functionsExamples:import numpy  as np
import lppinv as lp

# cOLS problem
print('\n', lp.solve(
    cols=True,
    rhs=np.array([[0, 0, 0, 0, -1, 0.2, 0.9, 2.1]]).T,
    constraints=np.array([np.ones(4), [0, 1, 2, 3], [0, 5, 2, 8]]).T,
    model=np.array([np.ones(4), [0, 1, 2, 3], [0, 5, 2, 8]]).T,
    slackvars=np.array([[-1, 1, -1, 0]]).T,
    mc=False
))

# TM problem
def rnormal(r=None, c=None):
    return (np.random.normal(loc=0.0, scale=1.0, size=(r, c)))

print('\n', lp.solve(
    tm=True,
    rhs=np.array([[4, 5, 3, 4, 6], [1, 2, 0, np.nan, np.nan]]).T,
    zero_diagonal=True,
    distribution=rnormal
))

# custom problem
print('\n', lp.solve(
    rhs=np.array([[2, 3, 9], [5, 7, 9]]).T,
    model=np.vstack([[0, 1, 1], [1, 0, 1], [1, 1, 0]]),
    tolerance=10**-10,
    trace=False
))References:Albert, A., 1972.Regression And The Moore-Penrose Pseudoinverse.New York: Academic Press.Bolotov, I. 2014.Modeling of Time Series Cyclical Component on a Defined Set of Stationary Points and its Application on the US Business Cycle. [Paper presentation]. The 8th International Days of Statistics and Economics: Prague.https://msed.vse.cz/msed_2014/article/348-Bolotov-Ilya-paper.pdfBolotov, I. 2015.Modeling Bilateral Flows in Economics by Means of Exact Mathematical Methods.[Paper presentation]. The 9th International Days of Statistics and Economics: Prague.https://msed.vse.cz/msed_2015/article/111-Bolotov-Ilya-paper.pdfPSPlease also check the Web of Science (WoS) for new research on LPLS.