67
67
"""
68
68
69
69
70
- _ewm_kw = r"""com : float. optional
71
- Center of mass: :math:`\alpha = 1 / (1 + com)`,
70
+ _ewm_kw = r"""com : float, optional
71
+ Specify decay in terms of center of mass,
72
+ :math:`\alpha = 1 / (1 + com),\text{ for } com \geq 0`
72
73
span : float, optional
73
- Specify decay in terms of span, :math:`\alpha = 2 / (span + 1)`
74
+ Specify decay in terms of span,
75
+ :math:`\alpha = 2 / (span + 1),\text{ for } span \geq 1`
74
76
halflife : float, optional
75
- Specify decay in terms of halflife,
76
- :math:`\alpha = 1 - exp(log(0.5) / halflife)`
77
+ Specify decay in terms of half-life,
78
+ :math:`\alpha = 1 - exp(log(0.5) / halflife),\text{ for } halflife > 0`
79
+ alpha : float, optional
80
+ Specify smoothing factor :math:`\alpha` directly,
81
+ :math:`0 < \alpha \leq 1`
82
+
83
+ .. versionadded:: 0.18.0
84
+
77
85
min_periods : int, default 0
78
86
Minimum number of observations in window required to have a value
79
87
(otherwise result is NA).
92
100
_ewm_notes = r"""
93
101
Notes
94
102
-----
95
- Either center of mass, span or halflife must be specified
96
-
97
- EWMA is sometimes specified using a "span" parameter `s`, we have that the
98
- decay parameter :math:`\alpha` is related to the span as
99
- :math:`\alpha = 2 / (s + 1) = 1 / (1 + c)`
100
-
101
- where `c` is the center of mass. Given a span, the associated center of mass is
102
- :math:`c = (s - 1) / 2`
103
-
104
- So a "20-day EWMA" would have center 9.5.
103
+ Exactly one of center of mass, span, half-life, and alpha must be provided.
104
+ Allowed values and relationship between the parameters are specified in the
105
+ parameter descriptions above; see the link at the end of this section for
106
+ a detailed explanation.
105
107
106
108
When adjust is True (default), weighted averages are calculated using weights
107
109
(1-alpha)**(n-1), (1-alpha)**(n-2), ..., 1-alpha, 1.
121
123
True), and 1-alpha and alpha (if adjust is False).
122
124
123
125
More details can be found at
124
- http://pandas.pydata.org/pandas-docs/stable/computation.html#exponentially-weighted-moment-functions
126
+ http://pandas.pydata.org/pandas-docs/stable/computation.html#exponentially-weighted-windows
125
127
"""
126
128
127
129
_expanding_kw = """min_periods : int, default None
@@ -323,14 +325,15 @@ def rolling_corr(arg1, arg2=None, window=None, pairwise=None, **kwargs):
323
325
@Substitution ("Exponentially-weighted moving average" , _unary_arg , _ewm_kw ,
324
326
_type_of_input_retval , _ewm_notes )
325
327
@Appender (_doc_template )
326
- def ewma (arg , com = None , span = None , halflife = None , min_periods = 0 , freq = None ,
327
- adjust = True , how = None , ignore_na = False ):
328
+ def ewma (arg , com = None , span = None , halflife = None , alpha = None , min_periods = 0 ,
329
+ freq = None , adjust = True , how = None , ignore_na = False ):
328
330
return ensure_compat ('ewm' ,
329
331
'mean' ,
330
332
arg ,
331
333
com = com ,
332
334
span = span ,
333
335
halflife = halflife ,
336
+ alpha = alpha ,
334
337
min_periods = min_periods ,
335
338
freq = freq ,
336
339
adjust = adjust ,
@@ -341,14 +344,15 @@ def ewma(arg, com=None, span=None, halflife=None, min_periods=0, freq=None,
341
344
@Substitution ("Exponentially-weighted moving variance" , _unary_arg ,
342
345
_ewm_kw + _bias_kw , _type_of_input_retval , _ewm_notes )
343
346
@Appender (_doc_template )
344
- def ewmvar (arg , com = None , span = None , halflife = None , min_periods = 0 , bias = False ,
345
- freq = None , how = None , ignore_na = False , adjust = True ):
347
+ def ewmvar (arg , com = None , span = None , halflife = None , alpha = None , min_periods = 0 ,
348
+ bias = False , freq = None , how = None , ignore_na = False , adjust = True ):
346
349
return ensure_compat ('ewm' ,
347
350
'var' ,
348
351
arg ,
349
352
com = com ,
350
353
span = span ,
351
354
halflife = halflife ,
355
+ alpha = alpha ,
352
356
min_periods = min_periods ,
353
357
freq = freq ,
354
358
adjust = adjust ,
@@ -361,14 +365,15 @@ def ewmvar(arg, com=None, span=None, halflife=None, min_periods=0, bias=False,
361
365
@Substitution ("Exponentially-weighted moving std" , _unary_arg ,
362
366
_ewm_kw + _bias_kw , _type_of_input_retval , _ewm_notes )
363
367
@Appender (_doc_template )
364
- def ewmstd (arg , com = None , span = None , halflife = None , min_periods = 0 , bias = False ,
365
- freq = None , how = None , ignore_na = False , adjust = True ):
368
+ def ewmstd (arg , com = None , span = None , halflife = None , alpha = None , min_periods = 0 ,
369
+ bias = False , freq = None , how = None , ignore_na = False , adjust = True ):
366
370
return ensure_compat ('ewm' ,
367
371
'std' ,
368
372
arg ,
369
373
com = com ,
370
374
span = span ,
371
375
halflife = halflife ,
376
+ alpha = alpha ,
372
377
min_periods = min_periods ,
373
378
freq = freq ,
374
379
adjust = adjust ,
@@ -383,9 +388,9 @@ def ewmstd(arg, com=None, span=None, halflife=None, min_periods=0, bias=False,
383
388
@Substitution ("Exponentially-weighted moving covariance" , _binary_arg_flex ,
384
389
_ewm_kw + _pairwise_kw , _type_of_input_retval , _ewm_notes )
385
390
@Appender (_doc_template )
386
- def ewmcov (arg1 , arg2 = None , com = None , span = None , halflife = None , min_periods = 0 ,
387
- bias = False , freq = None , pairwise = None , how = None , ignore_na = False ,
388
- adjust = True ):
391
+ def ewmcov (arg1 , arg2 = None , com = None , span = None , halflife = None , alpha = None ,
392
+ min_periods = 0 , bias = False , freq = None , pairwise = None , how = None ,
393
+ ignore_na = False , adjust = True ):
389
394
if arg2 is None :
390
395
arg2 = arg1
391
396
pairwise = True if pairwise is None else pairwise
@@ -401,6 +406,7 @@ def ewmcov(arg1, arg2=None, com=None, span=None, halflife=None, min_periods=0,
401
406
com = com ,
402
407
span = span ,
403
408
halflife = halflife ,
409
+ alpha = alpha ,
404
410
min_periods = min_periods ,
405
411
bias = bias ,
406
412
freq = freq ,
@@ -414,8 +420,9 @@ def ewmcov(arg1, arg2=None, com=None, span=None, halflife=None, min_periods=0,
414
420
@Substitution ("Exponentially-weighted moving correlation" , _binary_arg_flex ,
415
421
_ewm_kw + _pairwise_kw , _type_of_input_retval , _ewm_notes )
416
422
@Appender (_doc_template )
417
- def ewmcorr (arg1 , arg2 = None , com = None , span = None , halflife = None , min_periods = 0 ,
418
- freq = None , pairwise = None , how = None , ignore_na = False , adjust = True ):
423
+ def ewmcorr (arg1 , arg2 = None , com = None , span = None , halflife = None , alpha = None ,
424
+ min_periods = 0 , freq = None , pairwise = None , how = None , ignore_na = False ,
425
+ adjust = True ):
419
426
if arg2 is None :
420
427
arg2 = arg1
421
428
pairwise = True if pairwise is None else pairwise
@@ -430,6 +437,7 @@ def ewmcorr(arg1, arg2=None, com=None, span=None, halflife=None, min_periods=0,
430
437
com = com ,
431
438
span = span ,
432
439
halflife = halflife ,
440
+ alpha = alpha ,
433
441
min_periods = min_periods ,
434
442
freq = freq ,
435
443
how = how ,
0 commit comments