| 1523 | |
| 1524 | @derived_from(np) |
| 1525 | def cov(m, y=None, rowvar=1, bias=0, ddof=None): |
| 1526 | # This was copied almost verbatim from np.cov |
| 1527 | # See numpy license at https://github.com/numpy/numpy/blob/master/LICENSE.txt |
| 1528 | # or NUMPY_LICENSE.txt within this directory |
| 1529 | if ddof is not None and ddof != int(ddof): |
| 1530 | raise ValueError("ddof must be integer") |
| 1531 | |
| 1532 | # Handles complex arrays too |
| 1533 | m = asarray(m) |
| 1534 | if y is None: |
| 1535 | dtype = np.result_type(m, np.float64) |
| 1536 | else: |
| 1537 | y = asarray(y) |
| 1538 | dtype = np.result_type(m, y, np.float64) |
| 1539 | X = array(m, ndmin=2, dtype=dtype) |
| 1540 | |
| 1541 | if X.shape[0] == 1: |
| 1542 | rowvar = 1 |
| 1543 | if rowvar: |
| 1544 | N = X.shape[1] |
| 1545 | axis = 0 |
| 1546 | else: |
| 1547 | N = X.shape[0] |
| 1548 | axis = 1 |
| 1549 | |
| 1550 | # check ddof |
| 1551 | if ddof is None: |
| 1552 | if bias == 0: |
| 1553 | ddof = 1 |
| 1554 | else: |
| 1555 | ddof = 0 |
| 1556 | fact = float(N - ddof) |
| 1557 | if fact <= 0: |
| 1558 | warnings.warn("Degrees of freedom <= 0 for slice", RuntimeWarning) |
| 1559 | fact = 0.0 |
| 1560 | |
| 1561 | if y is not None: |
| 1562 | y = array(y, ndmin=2, dtype=dtype) |
| 1563 | X = concatenate((X, y), axis) |
| 1564 | |
| 1565 | X = X - X.mean(axis=1 - axis, keepdims=True) |
| 1566 | if not rowvar: |
| 1567 | return (dot(X.T, X.conj()) / fact).squeeze() |
| 1568 | else: |
| 1569 | return (dot(X, X.T.conj()) / fact).squeeze() |
| 1570 | |
| 1571 | |
| 1572 | @derived_from(np) |