@@ -252,7 +252,7 @@ def ewma(ndarray[double_t] input, double_t com):
252
252
253
253
@ cython.boundscheck (False )
254
254
@ cython.wraparound (False )
255
- def nancorr (ndarray[float64_t , ndim = 2 ] mat, na_ok = False ):
255
+ def nancorr (ndarray[float64_t , ndim = 2 ] mat):
256
256
cdef:
257
257
Py_ssize_t i, j, xi, yi, N, K
258
258
ndarray[float64_t, ndim= 2 ] result
@@ -276,27 +276,30 @@ def nancorr(ndarray[float64_t, ndim=2] mat, na_ok=False):
276
276
sumx += vx
277
277
sumy += vy
278
278
279
- meanx = sumx / nobs
280
- meany = sumy / nobs
279
+ if nobs == 0 :
280
+ result[xi, yi] = result[yi, xi] = np.NaN
281
+ else :
282
+ meanx = sumx / nobs
283
+ meany = sumy / nobs
281
284
282
- # now the cov numerator
283
- sumx = 0
285
+ # now the cov numerator
286
+ sumx = 0
284
287
285
- for i in range (N):
286
- if mask[i, xi] and mask[i, yi]:
287
- vx = mat[i, xi] - meanx
288
- vy = mat[i, yi] - meany
288
+ for i in range (N):
289
+ if mask[i, xi] and mask[i, yi]:
290
+ vx = mat[i, xi] - meanx
291
+ vy = mat[i, yi] - meany
289
292
290
- sumx += vx * vy
291
- sumxx += vx * vx
292
- sumyy += vy * vy
293
+ sumx += vx * vy
294
+ sumxx += vx * vx
295
+ sumyy += vy * vy
293
296
294
- divisor = sqrt(sumxx * sumyy)
297
+ divisor = sqrt(sumxx * sumyy)
295
298
296
- if na_ok = = 0 :
297
- result[xi, yi] = result[yi, xi] = sumx / divisor if divisor ! = 0 else np.NaN
298
- else :
299
- result[xi, yi] = result[yi, xi] = sumx / divisor
299
+ if divisor ! = 0 :
300
+ result[xi, yi] = result[yi, xi] = sumx / divisor
301
+ else :
302
+ result[xi, yi] = result[yi, xi] = np.NaN
300
303
301
304
return result
302
305
0 commit comments