Return the least-squares solution to a linear matrix equation using QR decomposition. Solves the equation `a x = b` by computing a vector `x` that minimizes the Euclidean 2-norm `|| b - a x ||^2`. The equation may be under-, well-, or over- determined (i.e., the number of
(a, b)
| 1404 | |
| 1405 | |
| 1406 | def lstsq(a, b): |
| 1407 | """ |
| 1408 | Return the least-squares solution to a linear matrix equation using |
| 1409 | QR decomposition. |
| 1410 | |
| 1411 | Solves the equation `a x = b` by computing a vector `x` that |
| 1412 | minimizes the Euclidean 2-norm `|| b - a x ||^2`. The equation may |
| 1413 | be under-, well-, or over- determined (i.e., the number of |
| 1414 | linearly independent rows of `a` can be less than, equal to, or |
| 1415 | greater than its number of linearly independent columns). If `a` |
| 1416 | is square and of full rank, then `x` (but for round-off error) is |
| 1417 | the "exact" solution of the equation. |
| 1418 | |
| 1419 | Parameters |
| 1420 | ---------- |
| 1421 | a : (M, N) array_like |
| 1422 | "Coefficient" matrix. |
| 1423 | b : {(M,), (M, K)} array_like |
| 1424 | Ordinate or "dependent variable" values. If `b` is two-dimensional, |
| 1425 | the least-squares solution is calculated for each of the `K` columns |
| 1426 | of `b`. |
| 1427 | |
| 1428 | Returns |
| 1429 | ------- |
| 1430 | x : {(N,), (N, K)} Array |
| 1431 | Least-squares solution. If `b` is two-dimensional, |
| 1432 | the solutions are in the `K` columns of `x`. |
| 1433 | residuals : {(1,), (K,)} Array |
| 1434 | Sums of residuals; squared Euclidean 2-norm for each column in |
| 1435 | ``b - a*x``. |
| 1436 | If `b` is 1-dimensional, this is a (1,) shape array. |
| 1437 | Otherwise the shape is (K,). |
| 1438 | rank : Array |
| 1439 | Rank of matrix `a`. |
| 1440 | s : (min(M, N),) Array |
| 1441 | Singular values of `a`. |
| 1442 | """ |
| 1443 | q, r = qr(a) |
| 1444 | x = solve_triangular(r, q.T.conj().dot(b)) |
| 1445 | residuals = b - a.dot(x) |
| 1446 | residuals = abs(residuals**2).sum(axis=0, keepdims=b.ndim == 1) |
| 1447 | |
| 1448 | token = tokenize(a, b) |
| 1449 | |
| 1450 | # r must be a triangular with single block |
| 1451 | |
| 1452 | # rank |
| 1453 | rname = f"lstsq-rank-{token}" |
| 1454 | rdsk = {(rname,): (np.linalg.matrix_rank, (r.name, 0, 0))} |
| 1455 | graph = HighLevelGraph.from_collections(rname, rdsk, dependencies=[r]) |
| 1456 | # rank must be an integer |
| 1457 | rank = Array(graph, rname, shape=(), chunks=(), dtype=int) |
| 1458 | |
| 1459 | # singular |
| 1460 | sname = f"lstsq-singular-{token}" |
| 1461 | rt = r.T.conj() |
| 1462 | sdsk = { |
| 1463 | (sname, 0): ( |
nothing calls this directly
no test coverage detected
searching dependent graphs…