@@ -1486,7 +1486,7 @@ def roll_weighted_var(const float64_t[:] values, const float64_t[:] weights,
1486
1486
1487
1487
def ewma (const float64_t[:] vals , const int64_t[:] start , const int64_t[:] end ,
1488
1488
int minp , float64_t com , bint adjust , bint ignore_na ,
1489
- const float64_t[:] deltas ) -> np.ndarray:
1489
+ const float64_t[:] deltas = None ) -> np.ndarray:
1490
1490
"""
1491
1491
Compute exponentially-weighted moving average using center-of-mass.
1492
1492
@@ -1499,7 +1499,8 @@ def ewma(const float64_t[:] vals, const int64_t[:] start, const int64_t[:] end,
1499
1499
com : float64
1500
1500
adjust : bool
1501
1501
ignore_na : bool
1502
- deltas : ndarray (float64 type )
1502
+ deltas : ndarray (float64 type ), optional. If None , implicitly assumes equally
1503
+ spaced points (used when `times` is not passed )
1503
1504
1504
1505
Returns
1505
1506
-------
@@ -1508,14 +1509,17 @@ def ewma(const float64_t[:] vals, const int64_t[:] start, const int64_t[:] end,
1508
1509
1509
1510
cdef:
1510
1511
Py_ssize_t i , j , s , e , nobs , win_size , N = len (vals), M = len (start)
1511
- const float64_t[:] sub_deltas , sub_vals
1512
+ const float64_t[:] sub_vals
1513
+ const float64_t[:] sub_deltas = None
1512
1514
ndarray[float64_t] sub_output , output = np.empty(N, dtype = np.float64)
1513
1515
float64_t alpha , old_wt_factor , new_wt , weighted_avg , old_wt , cur
1514
- bint is_observation
1516
+ bint is_observation , use_deltas
1515
1517
1516
1518
if N == 0:
1517
1519
return output
1518
1520
1521
+ use_deltas = deltas is not None
1522
+
1519
1523
alpha = 1. / (1. + com)
1520
1524
old_wt_factor = 1. - alpha
1521
1525
new_wt = 1. if adjust else alpha
@@ -1526,7 +1530,8 @@ def ewma(const float64_t[:] vals, const int64_t[:] start, const int64_t[:] end,
1526
1530
sub_vals = vals[s:e]
1527
1531
# note that len(deltas) = len(vals) - 1 and deltas[i] is to be used in
1528
1532
# conjunction with vals[i+1]
1529
- sub_deltas = deltas[s:e - 1 ]
1533
+ if use_deltas:
1534
+ sub_deltas = deltas[s:e - 1 ]
1530
1535
win_size = len (sub_vals)
1531
1536
sub_output = np.empty(win_size, dtype = np.float64)
1532
1537
@@ -1544,7 +1549,10 @@ def ewma(const float64_t[:] vals, const int64_t[:] start, const int64_t[:] end,
1544
1549
if weighted_avg == weighted_avg:
1545
1550
1546
1551
if is_observation or not ignore_na:
1547
- old_wt *= old_wt_factor ** sub_deltas[i - 1 ]
1552
+ if use_deltas:
1553
+ old_wt *= old_wt_factor ** sub_deltas[i - 1 ]
1554
+ else :
1555
+ old_wt *= old_wt_factor
1548
1556
if is_observation:
1549
1557
1550
1558
# avoid numerical errors on constant series
0 commit comments