Private function for the computation of covariance and correlation coefficients.
(x, y=None, rowvar=True, allow_masked=True)
| 1399 | |
| 1400 | |
| 1401 | def _covhelper(x, y=None, rowvar=True, allow_masked=True): |
| 1402 | """ |
| 1403 | Private function for the computation of covariance and correlation |
| 1404 | coefficients. |
| 1405 | |
| 1406 | """ |
| 1407 | x = ma.array(x, ndmin=2, copy=True, dtype=float) |
| 1408 | xmask = ma.getmaskarray(x) |
| 1409 | # Quick exit if we can't process masked data |
| 1410 | if not allow_masked and xmask.any(): |
| 1411 | raise ValueError("Cannot process masked data.") |
| 1412 | # |
| 1413 | if x.shape[0] == 1: |
| 1414 | rowvar = True |
| 1415 | # Make sure that rowvar is either 0 or 1 |
| 1416 | rowvar = int(bool(rowvar)) |
| 1417 | axis = 1 - rowvar |
| 1418 | if rowvar: |
| 1419 | tup = (slice(None), None) |
| 1420 | else: |
| 1421 | tup = (None, slice(None)) |
| 1422 | # |
| 1423 | if y is None: |
| 1424 | xnotmask = np.logical_not(xmask).astype(int) |
| 1425 | else: |
| 1426 | y = array(y, copy=False, ndmin=2, dtype=float) |
| 1427 | ymask = ma.getmaskarray(y) |
| 1428 | if not allow_masked and ymask.any(): |
| 1429 | raise ValueError("Cannot process masked data.") |
| 1430 | if xmask.any() or ymask.any(): |
| 1431 | if y.shape == x.shape: |
| 1432 | # Define some common mask |
| 1433 | common_mask = np.logical_or(xmask, ymask) |
| 1434 | if common_mask is not nomask: |
| 1435 | xmask = x._mask = y._mask = ymask = common_mask |
| 1436 | x._sharedmask = False |
| 1437 | y._sharedmask = False |
| 1438 | x = ma.concatenate((x, y), axis) |
| 1439 | xnotmask = np.logical_not(np.concatenate((xmask, ymask), axis)).astype(int) |
| 1440 | x -= x.mean(axis=rowvar)[tup] |
| 1441 | return (x, xnotmask, rowvar) |
| 1442 | |
| 1443 | |
| 1444 | def cov(x, y=None, rowvar=True, bias=False, allow_masked=True, ddof=None): |