@@ -1604,13 +1604,13 @@ def roll_weighted_var(const float64_t[:] values, const float64_t[:] weights,
1604
1604
1605
1605
1606
1606
# ----------------------------------------------------------------------
1607
- # Exponentially weighted moving average
1607
+ # Exponentially weighted moving
1608
1608
1609
- def ewma (const float64_t[:] vals , const int64_t[:] start , const int64_t[:] end ,
1610
- int minp , float64_t com , bint adjust , bint ignore_na ,
1611
- const float64_t[:] deltas = None ) -> np.ndarray:
1609
+ def ewm (const float64_t[:] vals , const int64_t[:] start , const int64_t[:] end ,
1610
+ int minp , float64_t com , bint adjust , bint ignore_na ,
1611
+ const float64_t[:] deltas = None , bint normalize = True ) -> np.ndarray:
1612
1612
"""
1613
- Compute exponentially-weighted moving average using center-of-mass.
1613
+ Compute exponentially-weighted moving average or sum using center-of-mass.
1614
1614
1615
1615
Parameters
1616
1616
----------
@@ -1623,6 +1623,8 @@ def ewma(const float64_t[:] vals, const int64_t[:] start, const int64_t[:] end,
1623
1623
ignore_na : bool
1624
1624
deltas : ndarray (float64 type ), optional. If None , implicitly assumes equally
1625
1625
spaced points (used when `times` is not passed )
1626
+ normalize : bool , optional.
1627
+ If True , calculate the mean. If False , calculate the sum.
1626
1628
1627
1629
Returns
1628
1630
-------
@@ -1634,7 +1636,7 @@ def ewma(const float64_t[:] vals, const int64_t[:] start, const int64_t[:] end,
1634
1636
const float64_t[:] sub_vals
1635
1637
const float64_t[:] sub_deltas = None
1636
1638
ndarray[float64_t] sub_output , output = np.empty(N, dtype = np.float64)
1637
- float64_t alpha , old_wt_factor , new_wt , weighted_avg , old_wt , cur
1639
+ float64_t alpha , old_wt_factor , new_wt , weighted , old_wt , cur
1638
1640
bint is_observation , use_deltas
1639
1641
1640
1642
if N == 0:
@@ -1657,48 +1659,49 @@ def ewma(const float64_t[:] vals, const int64_t[:] start, const int64_t[:] end,
1657
1659
win_size = len (sub_vals)
1658
1660
sub_output = np.empty(win_size, dtype = np.float64)
1659
1661
1660
- weighted_avg = sub_vals[0 ]
1661
- is_observation = weighted_avg == weighted_avg
1662
+ weighted = sub_vals[0 ]
1663
+ is_observation = weighted == weighted
1662
1664
nobs = int (is_observation)
1663
- sub_output[0 ] = weighted_avg if nobs >= minp else NaN
1665
+ sub_output[0 ] = weighted if nobs >= minp else NaN
1664
1666
old_wt = 1.
1665
1667
1666
1668
with nogil:
1667
1669
for i in range (1 , win_size):
1668
1670
cur = sub_vals[i]
1669
1671
is_observation = cur == cur
1670
1672
nobs += is_observation
1671
- if weighted_avg == weighted_avg :
1673
+ if weighted == weighted :
1672
1674
1673
1675
if is_observation or not ignore_na:
1674
- if use_deltas:
1675
- old_wt *= old_wt_factor ** sub_deltas[i - 1 ]
1676
+ if normalize:
1677
+ if use_deltas:
1678
+ old_wt *= old_wt_factor ** sub_deltas[i - 1 ]
1679
+ else :
1680
+ old_wt *= old_wt_factor
1676
1681
else :
1677
- old_wt * = old_wt_factor
1682
+ weighted = old_wt_factor * weighted
1678
1683
if is_observation:
1679
-
1680
- # avoid numerical errors on constant series
1681
- if weighted_avg != cur:
1682
- weighted_avg = ((old_wt * weighted_avg) +
1683
- (new_wt * cur)) / (old_wt + new_wt)
1684
- if adjust:
1685
- old_wt += new_wt
1684
+ if normalize:
1685
+ # avoid numerical errors on constant series
1686
+ if weighted != cur:
1687
+ weighted = old_wt * weighted + new_wt * cur
1688
+ weighted /= (old_wt + new_wt)
1689
+ if adjust:
1690
+ old_wt += new_wt
1691
+ else :
1692
+ old_wt = 1.
1686
1693
else :
1687
- old_wt = 1.
1694
+ weighted += cur
1688
1695
elif is_observation:
1689
- weighted_avg = cur
1696
+ weighted = cur
1690
1697
1691
- sub_output[i] = weighted_avg if nobs >= minp else NaN
1698
+ sub_output[i] = weighted if nobs >= minp else NaN
1692
1699
1693
1700
output[s:e] = sub_output
1694
1701
1695
1702
return output
1696
1703
1697
1704
1698
- # ----------------------------------------------------------------------
1699
- # Exponentially weighted moving covariance
1700
-
1701
-
1702
1705
def ewmcov (const float64_t[:] input_x , const int64_t[:] start , const int64_t[:] end ,
1703
1706
int minp , const float64_t[:] input_y , float64_t com , bint adjust ,
1704
1707
bint ignore_na , bint bias ) -> np.ndarray:
0 commit comments