@@ -116,9 +116,10 @@ cdef inline void remove_sum(float64_t val, int64_t *nobs, float64_t *sum_x,
116
116
def roll_sum (const float64_t[:] values , ndarray[int64_t] start ,
117
117
ndarray[int64_t] end , int64_t minp ):
118
118
cdef:
119
+ Py_ssize_t i, j
119
120
float64_t sum_x = 0 , compensation_add = 0 , compensation_remove = 0
120
121
int64_t s, e
121
- int64_t nobs = 0 , i, j, N = len (values)
122
+ int64_t nobs = 0 , N = len (values)
122
123
ndarray[float64_t] output
123
124
bint is_monotonic_increasing_bounds
124
125
@@ -493,12 +494,13 @@ cdef inline void remove_skew(float64_t val, int64_t *nobs,
493
494
def roll_skew (ndarray[float64_t] values , ndarray[int64_t] start ,
494
495
ndarray[int64_t] end , int64_t minp ):
495
496
cdef:
497
+ Py_ssize_t i, j
496
498
float64_t val, prev, min_val, mean_val, sum_val = 0
497
499
float64_t compensation_xxx_add = 0 , compensation_xxx_remove = 0
498
500
float64_t compensation_xx_add = 0 , compensation_xx_remove = 0
499
501
float64_t compensation_x_add = 0 , compensation_x_remove = 0
500
502
float64_t x = 0 , xx = 0 , xxx = 0
501
- int64_t nobs = 0 , i, j, N = len (values), nobs_mean = 0
503
+ int64_t nobs = 0 , N = len (values), nobs_mean = 0
502
504
int64_t s, e
503
505
ndarray[float64_t] output, mean_array, values_copy
504
506
bint is_monotonic_increasing_bounds
@@ -674,13 +676,14 @@ cdef inline void remove_kurt(float64_t val, int64_t *nobs,
674
676
def roll_kurt (ndarray[float64_t] values , ndarray[int64_t] start ,
675
677
ndarray[int64_t] end , int64_t minp ):
676
678
cdef:
679
+ Py_ssize_t i, j
677
680
float64_t val, prev, mean_val, min_val, sum_val = 0
678
681
float64_t compensation_xxxx_add = 0 , compensation_xxxx_remove = 0
679
682
float64_t compensation_xxx_remove = 0 , compensation_xxx_add = 0
680
683
float64_t compensation_xx_remove = 0 , compensation_xx_add = 0
681
684
float64_t compensation_x_remove = 0 , compensation_x_add = 0
682
685
float64_t x = 0 , xx = 0 , xxx = 0 , xxxx = 0
683
- int64_t nobs = 0 , i, j, s, e, N = len (values), nobs_mean = 0
686
+ int64_t nobs = 0 , s, e, N = len (values), nobs_mean = 0
684
687
ndarray[float64_t] output, values_copy
685
688
bint is_monotonic_increasing_bounds
686
689
@@ -754,15 +757,13 @@ def roll_kurt(ndarray[float64_t] values, ndarray[int64_t] start,
754
757
def roll_median_c (const float64_t[:] values , ndarray[int64_t] start ,
755
758
ndarray[int64_t] end , int64_t minp ):
756
759
cdef:
757
- float64_t val, res, prev
758
- bint err = False
759
- int ret = 0
760
- skiplist_t * sl
761
760
Py_ssize_t i, j
761
+ bint err = False , is_monotonic_increasing_bounds
762
+ int midpoint, ret = 0
762
763
int64_t nobs = 0 , N = len (values), s, e, win
763
- int midpoint
764
+ float64_t val, res, prev
765
+ skiplist_t * sl
764
766
ndarray[float64_t] output
765
- bint is_monotonic_increasing_bounds
766
767
767
768
is_monotonic_increasing_bounds = is_monotonic_increasing_start_end_bounds(
768
769
start, end
@@ -933,8 +934,8 @@ cdef _roll_min_max(ndarray[numeric] values,
933
934
bint is_max):
934
935
cdef:
935
936
numeric ai
936
- int64_t i, k, curr_win_size, start
937
- Py_ssize_t nobs = 0 , N = len (values)
937
+ int64_t curr_win_size, start
938
+ Py_ssize_t i, k, nobs = 0 , N = len (values)
938
939
deque Q[int64_t] # min/max always the front
939
940
deque W[int64_t] # track the whole window for nobs compute
940
941
ndarray[float64_t, ndim= 1 ] output
@@ -1017,14 +1018,14 @@ def roll_quantile(const float64_t[:] values, ndarray[int64_t] start,
1017
1018
O(N log(window)) implementation using skip list
1018
1019
"""
1019
1020
cdef:
1021
+ Py_ssize_t i, j, s, e, N = len (values), idx
1022
+ int ret = 0
1023
+ int64_t nobs = 0 , win
1020
1024
float64_t val, prev, midpoint, idx_with_fraction
1021
- skiplist_t * skiplist
1022
- int64_t nobs = 0 , i, j, s, e, N = len (values), win
1023
- Py_ssize_t idx
1024
- ndarray[float64_t] output
1025
1025
float64_t vlow, vhigh
1026
+ skiplist_t * skiplist
1026
1027
InterpolationType interpolation_type
1027
- int ret = 0
1028
+ ndarray[float64_t] output
1028
1029
1029
1030
if quantile <= 0.0 or quantile >= 1.0 :
1030
1031
raise ValueError (f" quantile value {quantile} not in [0, 1]" )
@@ -1041,10 +1042,10 @@ def roll_quantile(const float64_t[:] values, ndarray[int64_t] start,
1041
1042
# actual skiplist ops outweigh any window computation costs
1042
1043
output = np.empty(N, dtype = float )
1043
1044
1044
- if (end - start).max() == 0 :
1045
+ win = (end - start).max()
1046
+ if win == 0 :
1045
1047
output[:] = NaN
1046
1048
return output
1047
- win = (end - start).max()
1048
1049
skiplist = skiplist_init(< int > win)
1049
1050
if skiplist == NULL :
1050
1051
raise MemoryError (" skiplist_init failed" )
@@ -1473,9 +1474,9 @@ def roll_weighted_var(const float64_t[:] values, const float64_t[:] weights,
1473
1474
# ----------------------------------------------------------------------
1474
1475
# Exponentially weighted moving average
1475
1476
1476
- def ewma (float64_t[:] vals , int64_t[:] start , int64_t[:] end , int minp ,
1477
- float64_t com , bint adjust , bint ignore_na , float64_t[:] times ,
1478
- float64_t halflife ):
1477
+ def ewma (const float64_t[:] vals , const int64_t[:] start , const int64_t[:] end ,
1478
+ int minp , float64_t com , bint adjust , bint ignore_na ,
1479
+ const float64_t[:] times , float64_t halflife ):
1479
1480
"""
1480
1481
Compute exponentially-weighted moving average using center-of-mass.
1481
1482
@@ -1486,8 +1487,10 @@ def ewma(float64_t[:] vals, int64_t[:] start, int64_t[:] end, int minp,
1486
1487
end: ndarray (int64 type)
1487
1488
minp : int
1488
1489
com : float64
1489
- adjust : int
1490
+ adjust : bool
1490
1491
ignore_na : bool
1492
+ times : ndarray (float64 type)
1493
+ halflife : float64
1491
1494
1492
1495
Returns
1493
1496
-------
@@ -1496,7 +1499,7 @@ def ewma(float64_t[:] vals, int64_t[:] start, int64_t[:] end, int minp,
1496
1499
1497
1500
cdef:
1498
1501
Py_ssize_t i, j, s, e, nobs, win_size, N = len (vals), M = len (start)
1499
- float64_t[:] sub_vals
1502
+ const float64_t[:] sub_vals
1500
1503
ndarray[float64_t] sub_output, output = np.empty(N, dtype = float )
1501
1504
float64_t alpha, old_wt_factor, new_wt, weighted_avg, old_wt, cur, delta
1502
1505
bint is_observation
@@ -1555,8 +1558,9 @@ def ewma(float64_t[:] vals, int64_t[:] start, int64_t[:] end, int minp,
1555
1558
# Exponentially weighted moving covariance
1556
1559
1557
1560
1558
- def ewmcov (float64_t[:] input_x , int64_t[:] start , int64_t[:] end , int minp ,
1559
- float64_t[:] input_y , float64_t com , bint adjust , bint ignore_na , bint bias ):
1561
+ def ewmcov (const float64_t[:] input_x , const int64_t[:] start , const int64_t[:] end ,
1562
+ int minp , const float64_t[:] input_y , float64_t com , bint adjust ,
1563
+ bint ignore_na , bint bias ):
1560
1564
"""
1561
1565
Compute exponentially-weighted moving variance using center-of-mass.
1562
1566
@@ -1568,9 +1572,9 @@ def ewmcov(float64_t[:] input_x, int64_t[:] start, int64_t[:] end, int minp,
1568
1572
minp : int
1569
1573
input_y : ndarray (float64 type)
1570
1574
com : float64
1571
- adjust : int
1575
+ adjust : bool
1572
1576
ignore_na : bool
1573
- bias : int
1577
+ bias : bool
1574
1578
1575
1579
Returns
1576
1580
-------
@@ -1583,7 +1587,7 @@ def ewmcov(float64_t[:] input_x, int64_t[:] start, int64_t[:] end, int minp,
1583
1587
float64_t alpha, old_wt_factor, new_wt, mean_x, mean_y, cov
1584
1588
float64_t sum_wt, sum_wt2, old_wt, cur_x, cur_y, old_mean_x, old_mean_y
1585
1589
float64_t numerator, denominator
1586
- float64_t[:] sub_x_vals, sub_y_vals
1590
+ const float64_t[:] sub_x_vals, sub_y_vals
1587
1591
ndarray[float64_t] sub_out, output = np.empty(N, dtype = float )
1588
1592
bint is_observation
1589
1593
@@ -1594,6 +1598,8 @@ def ewmcov(float64_t[:] input_x, int64_t[:] start, int64_t[:] end, int minp,
1594
1598
return output
1595
1599
1596
1600
alpha = 1. / (1. + com)
1601
+ old_wt_factor = 1. - alpha
1602
+ new_wt = 1. if adjust else alpha
1597
1603
1598
1604
for j in range (L):
1599
1605
s = start[j]
@@ -1603,9 +1609,6 @@ def ewmcov(float64_t[:] input_x, int64_t[:] start, int64_t[:] end, int minp,
1603
1609
win_size = len (sub_x_vals)
1604
1610
sub_out = np.empty(win_size, dtype = float )
1605
1611
1606
- old_wt_factor = 1. - alpha
1607
- new_wt = 1. if adjust else alpha
1608
-
1609
1612
mean_x = sub_x_vals[0 ]
1610
1613
mean_y = sub_y_vals[0 ]
1611
1614
is_observation = (mean_x == mean_x) and (mean_y == mean_y)
0 commit comments