pytmpinv 1.0.2__tar.gz → 1.0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pytmpinv might be problematic. Click here for more details.
- {pytmpinv-1.0.2/pytmpinv.egg-info → pytmpinv-1.0.4}/PKG-INFO +5 -1
- {pytmpinv-1.0.2 → pytmpinv-1.0.4}/README.md +4 -0
- {pytmpinv-1.0.2 → pytmpinv-1.0.4}/pyproject.toml +1 -1
- {pytmpinv-1.0.2 → pytmpinv-1.0.4/pytmpinv.egg-info}/PKG-INFO +5 -1
- {pytmpinv-1.0.2 → pytmpinv-1.0.4}/tmpinv/__init__.py +1 -1
- {pytmpinv-1.0.2 → pytmpinv-1.0.4}/tmpinv/tmpinv.py +54 -6
- {pytmpinv-1.0.2 → pytmpinv-1.0.4}/LICENSE +0 -0
- {pytmpinv-1.0.2 → pytmpinv-1.0.4}/pytmpinv.egg-info/SOURCES.txt +0 -0
- {pytmpinv-1.0.2 → pytmpinv-1.0.4}/pytmpinv.egg-info/dependency_links.txt +0 -0
- {pytmpinv-1.0.2 → pytmpinv-1.0.4}/pytmpinv.egg-info/requires.txt +0 -0
- {pytmpinv-1.0.2 → pytmpinv-1.0.4}/pytmpinv.egg-info/top_level.txt +0 -0
- {pytmpinv-1.0.2 → pytmpinv-1.0.4}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytmpinv
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.4
|
|
4
4
|
Summary: Tabular Matrix Problems via Pseudoinverse Estimation
|
|
5
5
|
Author-email: The Economist <29724411+econcz@users.noreply.github.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -114,6 +114,10 @@ If *True*, enforces the zero diagonal.
|
|
|
114
114
|
`reduced` : *tuple* of *(int, int)*, optional
|
|
115
115
|
Dimensions of the reduced problem. If specified, the problem is estimated as a set of reduced problems constructed from contiguous submatrices of the original table. For example, `reduced` = *(6, 6)* implies *5×5* data blocks with *1* slack row and *1* slack column each (edge blocks may be smaller).
|
|
116
116
|
|
|
117
|
+
`symmetric` : *bool*, default = *False*
|
|
118
|
+
If True, enforces symmetry of the estimated solution matrix as: x = 0.5 * (x + x.T)
|
|
119
|
+
Symmetrization can slightly alter row/column totals. For exact symmetry under all constraints, add explicit symmetry constraints to M in a full-model solve instead of using this flag.
|
|
120
|
+
|
|
117
121
|
`bounds` : *sequence* of *(low, high)*, optional
|
|
118
122
|
Bounds on cell values. If a single tuple *(low, high)* is given, it is applied to all `m` * `p` cells. Example: *(0, None)*.
|
|
119
123
|
|
|
@@ -91,6 +91,10 @@ If *True*, enforces the zero diagonal.
|
|
|
91
91
|
`reduced` : *tuple* of *(int, int)*, optional
|
|
92
92
|
Dimensions of the reduced problem. If specified, the problem is estimated as a set of reduced problems constructed from contiguous submatrices of the original table. For example, `reduced` = *(6, 6)* implies *5×5* data blocks with *1* slack row and *1* slack column each (edge blocks may be smaller).
|
|
93
93
|
|
|
94
|
+
`symmetric` : *bool*, default = *False*
|
|
95
|
+
If True, enforces symmetry of the estimated solution matrix as: x = 0.5 * (x + x.T)
|
|
96
|
+
Symmetrization can slightly alter row/column totals. For exact symmetry under all constraints, add explicit symmetry constraints to M in a full-model solve instead of using this flag.
|
|
97
|
+
|
|
94
98
|
`bounds` : *sequence* of *(low, high)*, optional
|
|
95
99
|
Bounds on cell values. If a single tuple *(low, high)* is given, it is applied to all `m` * `p` cells. Example: *(0, None)*.
|
|
96
100
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytmpinv
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.4
|
|
4
4
|
Summary: Tabular Matrix Problems via Pseudoinverse Estimation
|
|
5
5
|
Author-email: The Economist <29724411+econcz@users.noreply.github.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -114,6 +114,10 @@ If *True*, enforces the zero diagonal.
|
|
|
114
114
|
`reduced` : *tuple* of *(int, int)*, optional
|
|
115
115
|
Dimensions of the reduced problem. If specified, the problem is estimated as a set of reduced problems constructed from contiguous submatrices of the original table. For example, `reduced` = *(6, 6)* implies *5×5* data blocks with *1* slack row and *1* slack column each (edge blocks may be smaller).
|
|
116
116
|
|
|
117
|
+
`symmetric` : *bool*, default = *False*
|
|
118
|
+
If True, enforces symmetry of the estimated solution matrix as: x = 0.5 * (x + x.T)
|
|
119
|
+
Symmetrization can slightly alter row/column totals. For exact symmetry under all constraints, add explicit symmetry constraints to M in a full-model solve instead of using this flag.
|
|
120
|
+
|
|
117
121
|
`bounds` : *sequence* of *(low, high)*, optional
|
|
118
122
|
Bounds on cell values. If a single tuple *(low, high)* is given, it is applied to all `m` * `p` cells. Example: *(0, None)*.
|
|
119
123
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import warnings
|
|
1
2
|
import numpy as np
|
|
2
3
|
from math import ceil
|
|
3
4
|
from typing import Sequence
|
|
@@ -81,6 +82,7 @@ def TMPinvSolve(
|
|
|
81
82
|
j: int = 1,
|
|
82
83
|
zero_diagonal: bool = False,
|
|
83
84
|
reduced: tuple[int, int] | None = None,
|
|
85
|
+
symmetric: bool = False,
|
|
84
86
|
*args, **kwargs
|
|
85
87
|
) -> TMPinvResult:
|
|
86
88
|
"""
|
|
@@ -119,6 +121,12 @@ def TMPinvSolve(
|
|
|
119
121
|
constructed from contiguous submatrices of the original table.
|
|
120
122
|
For example, reduced = (6, 6) implies 5×5 data blocks with 1 slack
|
|
121
123
|
row and 1 slack column each (edge blocks may be smaller).
|
|
124
|
+
symmetric : bool, default = False
|
|
125
|
+
If True, enforces symmetry of the estimated solution matrix as:
|
|
126
|
+
x = 0.5 * (x + x.T)
|
|
127
|
+
Symmetrization can slightly alter row/column totals. For exact symmetry
|
|
128
|
+
under all constraints, add explicit symmetry constraints to M in a
|
|
129
|
+
full-model solve instead of using this flag.
|
|
122
130
|
|
|
123
131
|
Returns
|
|
124
132
|
-------
|
|
@@ -146,17 +154,23 @@ def TMPinvSolve(
|
|
|
146
154
|
p = b_col.shape[0]
|
|
147
155
|
if S is not None:
|
|
148
156
|
S = np.asarray(S, dtype=np.float64)
|
|
157
|
+
if S.shape[0] != m + p:
|
|
158
|
+
raise TMPinvInputError(f"S must have exactly {m + p} rows.")
|
|
149
159
|
if (not np.all((S == -1) | (S == 0) | (S == 1)) or
|
|
150
160
|
np.abs(S).sum(axis=0).max() > 1 or
|
|
151
161
|
np.abs(S).sum(axis=1).max() > 1):
|
|
152
162
|
raise TMPinvInputError("S must be a zero-padded subset of ±I.")
|
|
153
163
|
if M is not None:
|
|
164
|
+
if b_val is None:
|
|
165
|
+
raise TMPinvInputError("Both M and b_val must be defined.")
|
|
154
166
|
M = np.asarray(M, dtype=np.float64)
|
|
155
167
|
if M.ndim == 1:
|
|
156
168
|
M = M.reshape(1, -1)
|
|
157
169
|
if M.shape[1] != m * p:
|
|
158
170
|
raise TMPinvInputError(f"M must have exactly {m * p} columns.")
|
|
159
171
|
if b_val is not None:
|
|
172
|
+
if M is None:
|
|
173
|
+
raise TMPinvInputError("Both M and b_val must be defined.")
|
|
160
174
|
if not np.isfinite(b_val).all():
|
|
161
175
|
raise TMPinvInputError("b_val must not contain inf or NaN.")
|
|
162
176
|
b_val = np.asarray(b_val, dtype=np.float64).reshape(-1, 1)
|
|
@@ -165,6 +179,11 @@ def TMPinvSolve(
|
|
|
165
179
|
raise TMPinvInputError(f"M and b_val must have the same number "
|
|
166
180
|
f"of rows: "
|
|
167
181
|
f"{M.shape[0]} vs {b_val.shape[0]}")
|
|
182
|
+
kw = {k: v for k, v in kwargs.items() if k not in {"b", "S", "M",
|
|
183
|
+
"m", "p",
|
|
184
|
+
"i", "j",
|
|
185
|
+
"zero_diagonal",
|
|
186
|
+
"symmetric"}}
|
|
168
187
|
|
|
169
188
|
# perform full estimation and return the result
|
|
170
189
|
if reduced is None:
|
|
@@ -173,13 +192,14 @@ def TMPinvSolve(
|
|
|
173
192
|
if b_val is not None:
|
|
174
193
|
b_blocks.append(b_val)
|
|
175
194
|
b = np.vstack(b_blocks)
|
|
176
|
-
result.model = CLSP().solve(
|
|
195
|
+
result.model = CLSP().solve(*args,
|
|
196
|
+
problem='ap', b=b, S=S, M=M,
|
|
177
197
|
m=m,
|
|
178
198
|
p=p,
|
|
179
199
|
i=i,
|
|
180
200
|
j=j,
|
|
181
201
|
zero_diagonal=zero_diagonal,
|
|
182
|
-
|
|
202
|
+
**kw)
|
|
183
203
|
result.x = result.model.x
|
|
184
204
|
|
|
185
205
|
# perform reduced estimation and return the result
|
|
@@ -191,12 +211,27 @@ def TMPinvSolve(
|
|
|
191
211
|
result = TMPinvResult(False, [], np.empty((m, p), dtype=float))
|
|
192
212
|
m_subset = reduced[0] - 1
|
|
193
213
|
p_subset = reduced[1] - 1
|
|
214
|
+
if zero_diagonal: # not processed by CLSP
|
|
215
|
+
M_diag = np.zeros((min(m, p), m * p))
|
|
216
|
+
b_diag = np.zeros((min(m, p), 1))
|
|
217
|
+
for k in range(min(m, p)):
|
|
218
|
+
M_diag[k, k * p + k] = 1
|
|
219
|
+
M, idx = np.unique(M_diag.copy() if M is None or M.size == 0
|
|
220
|
+
else np.vstack([M, M_diag]),
|
|
221
|
+
axis=0, return_index=True)
|
|
222
|
+
b_val = (b_diag.copy() if b_val is None or b_val.size == 0
|
|
223
|
+
else np.vstack([b_val, b_diag])
|
|
224
|
+
)[idx.reshape(-1)]
|
|
225
|
+
del M_diag, b_diag, idx
|
|
194
226
|
if M is not None:
|
|
195
227
|
if not all(np.count_nonzero(row) == 1 and
|
|
196
228
|
np.all((row == 0) | (row == 1)) for row in M):
|
|
197
229
|
raise TMPinvInputError("M must be a row subset of the identity "
|
|
198
230
|
"matrix in the reduced model.")
|
|
199
231
|
M = (np.abs(np.sign(M)) * b_val).sum(axis=0).reshape(m, p)
|
|
232
|
+
if S is not None:
|
|
233
|
+
warnings.warn("User-provided S is ignored in the reduced model.",
|
|
234
|
+
UserWarning)
|
|
200
235
|
for row_block in range(ceil(m / m_subset)):
|
|
201
236
|
for col_block in range(ceil(p / p_subset)):
|
|
202
237
|
m_start = row_block * m_subset
|
|
@@ -220,16 +255,24 @@ def TMPinvSolve(
|
|
|
220
255
|
else:
|
|
221
256
|
M_subset, b_val_subset = None, None
|
|
222
257
|
b = np.vstack(b_blocks)
|
|
223
|
-
tmp = CLSP().solve(
|
|
258
|
+
tmp = CLSP().solve(*args,
|
|
259
|
+
problem='ap', b=b, S=S, M=M_subset,
|
|
224
260
|
m=m_end - m_start,
|
|
225
261
|
p=p_end - p_start,
|
|
226
262
|
i=i,
|
|
227
263
|
j=j,
|
|
228
264
|
zero_diagonal=False,
|
|
229
|
-
|
|
265
|
+
**kw)
|
|
230
266
|
result.model.append(tmp)
|
|
231
267
|
result.x[m_start:m_end, p_start:p_end] = tmp.x
|
|
232
268
|
|
|
269
|
+
# enforce symmetry
|
|
270
|
+
if symmetric:
|
|
271
|
+
if result.x.shape[0] != result.x.shape[1]:
|
|
272
|
+
raise TMPinvInputError("symmetric=True requires a square matrix "
|
|
273
|
+
"(m == p).")
|
|
274
|
+
result.x = float(0.5) * (result.x + result.x.T)
|
|
275
|
+
|
|
233
276
|
return result
|
|
234
277
|
|
|
235
278
|
def tmpinv(
|
|
@@ -285,9 +328,9 @@ def tmpinv(
|
|
|
285
328
|
(h is not None and h < 0) for l, h in bounds):
|
|
286
329
|
raise TMPinvInputError("Negative lower or upper bounds are not "
|
|
287
330
|
"allowed in tabular matrix programs.")
|
|
331
|
+
kw = {k: v for k, v in kwargs.items() if k not in {"M", "b_val"}}
|
|
288
332
|
|
|
289
333
|
# (result) Perform bound-constrained iterative refinement
|
|
290
|
-
kw = {k: v for k, v in kwargs.items() if k not in {"M", "b_val"}}
|
|
291
334
|
for _ in range(iteration_limit):
|
|
292
335
|
M_idx, b_val = [], []
|
|
293
336
|
x = result.x.reshape(-1, 1)
|
|
@@ -300,7 +343,7 @@ def tmpinv(
|
|
|
300
343
|
b_val.append(v)
|
|
301
344
|
if len(M_idx) < n_cells:
|
|
302
345
|
M = np.eye(n_cells, dtype=np.float64)[M_idx]
|
|
303
|
-
result = TMPinvSolve(M=M, b_val=b_val,
|
|
346
|
+
result = TMPinvSolve(*args, M=M, b_val=b_val, **kw)
|
|
304
347
|
else:
|
|
305
348
|
break
|
|
306
349
|
|
|
@@ -313,4 +356,9 @@ def tmpinv(
|
|
|
313
356
|
x[(x < x_lb - tolerance) | (x > x_ub + tolerance)] = replace_value
|
|
314
357
|
result.x = x.reshape(result.x.shape)
|
|
315
358
|
|
|
359
|
+
# enforce (final) symmetry
|
|
360
|
+
if kwargs.get("symmetric", False):
|
|
361
|
+
if result.x.shape[0] == result.x.shape[1]:
|
|
362
|
+
result.x = float(0.5) * (result.x + result.x.T)
|
|
363
|
+
|
|
316
364
|
return result
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|