| 1521 | |
| 1522 | @derived_from(np) |
| 1523 | def cov( |
| 1524 | m, |
| 1525 | y=None, |
| 1526 | rowvar=True, |
| 1527 | bias=False, |
| 1528 | ddof=None, |
| 1529 | fweights=None, |
| 1530 | aweights=None, |
| 1531 | *, |
| 1532 | dtype=None, |
| 1533 | ): |
| 1534 | # This was copied almost verbatim from np.cov |
| 1535 | # See numpy license at https://github.com/numpy/numpy/blob/master/LICENSE.txt |
| 1536 | # or NUMPY_LICENSE.txt within this directory |
| 1537 | if ddof is not None and ddof != int(ddof): |
| 1538 | raise ValueError("ddof must be integer") |
| 1539 | |
| 1540 | # Handles complex arrays too |
| 1541 | m = asarray(m) |
| 1542 | |
| 1543 | if y is None: |
| 1544 | dtype = result_type(m, np.float64) |
| 1545 | else: |
| 1546 | y = asarray(y) |
| 1547 | dtype = result_type(m, y, np.float64) |
| 1548 | |
| 1549 | if m.ndim > 2: |
| 1550 | raise ValueError("m has more than 2 dimensions") |
| 1551 | if y is not None and y.ndim > 2: |
| 1552 | raise ValueError("y has more than 2 dimensions") |
| 1553 | |
| 1554 | X = array(m, ndmin=2, dtype=dtype) |
| 1555 | |
| 1556 | if ddof is None: |
| 1557 | ddof = 1 if bias == 0 else 0 |
| 1558 | |
| 1559 | if not rowvar and m.ndim != 1: |
| 1560 | X = X.T |
| 1561 | if X.shape[0] == 0: |
| 1562 | return array([]).reshape(0, 0) |
| 1563 | if y is not None: |
| 1564 | y = array(y, ndmin=2, dtype=dtype) |
| 1565 | if not rowvar and y.shape[0] != 1: |
| 1566 | y = y.T |
| 1567 | X = concatenate((X, y), axis=0) |
| 1568 | |
| 1569 | # Unlike NumPy, these checks don't include: |
| 1570 | # - if fweights are all integers |
| 1571 | # - if either fweights or aweights are all non-negative |
| 1572 | # These checks potentially expensive for distributed arrays. |
| 1573 | w = None |
| 1574 | if fweights is not None: |
| 1575 | fweights = asarray(fweights, dtype=float) |
| 1576 | if fweights.ndim > 1: |
| 1577 | raise RuntimeError("cannot handle multidimensional fweights") |
| 1578 | if fweights.shape[0] != X.shape[1]: |
| 1579 | raise RuntimeError("incompatible numbers of samples and fweights") |
| 1580 | w = fweights |