我有一个日期范围以及每个日期的测量值。我想计算每个日期的指数移动平均值。有人知道怎么做这个吗?

我是蟒蛇新手。标准 python 库中似乎没有内置平均值,这让我觉得有点奇怪。也许我没有找对地方。

那么,给定以下代码,我如何计算日历日期的 IQ 点的移动加权平均值?

from datetime import date
days = [date(2008,1,1), date(2008,1,2), date(2008,1,7)]
IQ = [110, 105, 90]

(可能有更好的方法来构建数据,任何建议将不胜感激)

有帮助吗?

解决方案

编辑:它似乎 mov_average_expw() 从功能 scikits.时间序列。lib。moving_funcs 模块从 SciKits (附加工具包,以补充 )更适合的措辞的问题。


计算一个 指数平滑 你的数据有平滑的因素 alpha (它是 (1 - alpha) 在维基百科的条款):

>>> alpha = 0.5
>>> assert 0 < alpha <= 1.0
>>> av = sum(alpha**n.days * iq 
...     for n, iq in map(lambda (day, iq), today=max(days): (today-day, iq), 
...         sorted(zip(days, IQ), key=lambda p: p[0], reverse=True)))
95.0

上面是不是很漂亮,因此,让我们重构这一点:

from collections import namedtuple
from operator    import itemgetter

def smooth(iq_data, alpha=1, today=None):
    """Perform exponential smoothing with factor `alpha`.

    Time period is a day.
    Each time period the value of `iq` drops `alpha` times.
    The most recent data is the most valuable one.
    """
    assert 0 < alpha <= 1

    if alpha == 1: # no smoothing
        return sum(map(itemgetter(1), iq_data))

    if today is None:
        today = max(map(itemgetter(0), iq_data))

    return sum(alpha**((today - date).days) * iq for date, iq in iq_data)

IQData = namedtuple("IQData", "date iq")

if __name__ == "__main__":
    from datetime import date

    days = [date(2008,1,1), date(2008,1,2), date(2008,1,7)]
    IQ = [110, 105, 90]
    iqdata = list(map(IQData, days, IQ))
    print("\n".join(map(str, iqdata)))

    print(smooth(iqdata, alpha=0.5))

例如:

$ python26 smooth.py
IQData(date=datetime.date(2008, 1, 1), iq=110)
IQData(date=datetime.date(2008, 1, 2), iq=105)
IQData(date=datetime.date(2008, 1, 7), iq=90)
95.0

其他提示

我做了一些谷歌搜索,发现了以下示例代码(http://osdir.com/ml/python.matplotlib.general/2005-04/msg00044.html):

def ema(s, n):
    """
    returns an n period exponential moving average for
    the time series s

    s is a list ordered from oldest (index 0) to most
    recent (index -1)
    n is an integer

    returns a numeric array of the exponential
    moving average
    """
    s = array(s)
    ema = []
    j = 1

    #get n sma first and calculate the next n period ema
    sma = sum(s[:n]) / n
    multiplier = 2 / float(1 + n)
    ema.append(sma)

    #EMA(current) = ( (Price(current) - EMA(prev) ) x Multiplier) + EMA(prev)
    ema.append(( (s[n] - sma) * multiplier) + sma)

    #now calculate the rest of the values
    for i in s[n+1:]:
        tmp = ( (i - ema[j]) * multiplier) + ema[j]
        j = j + 1
        ema.append(tmp)

    return ema

我总是计算均线与熊猫:

下面是一个例子如何做到这一点:

import pandas as pd
import numpy as np

def ema(values, period):
    values = np.array(values)
    return pd.ewma(values, span=period)[-1]

values = [9, 5, 10, 16, 5]
period = 5

print ema(values, period)

约熊猫EWMA更多的相关信息:

http://pandas.pydata.org/pandas-文档/稳定/生成/ pandas.ewma.html

我的Python是有点有点生疏(任何人都可以随意编辑该代码改正的,如果我莫名其妙地搞砸语法),但在这里不用....

def movingAverageExponential(values, alpha, epsilon = 0):

   if not 0 < alpha < 1:
      raise ValueError("out of range, alpha='%s'" % alpha)

   if not 0 <= epsilon < alpha:
      raise ValueError("out of range, epsilon='%s'" % epsilon)

   result = [None] * len(values)

   for i in range(len(result)):
       currentWeight = 1.0

       numerator     = 0
       denominator   = 0
       for value in values[i::-1]:
           numerator     += value * currentWeight
           denominator   += currentWeight

           currentWeight *= alpha
           if currentWeight < epsilon: 
              break

       result[i] = numerator / denominator

   return result

此功能向后移动,从列表中的开头的端部,通过向后工作直到一个元素的权重系数小于所述给定的ε-计算的指数移动平均的每个值。

目前的功能结束时,其返回的列表(以使它们是按照正确的顺序对呼叫者)之前反转值。

(边注:如果我用比Python语言其他,我首先创建一个全尺寸的空数组,然后填充它向后顺序,使我不会有在最后扭转它。但我不认为你可以在python宣布一个大的空数组。而在Python列表,追加是比前面加上便宜得多,这就是为什么我建立以相反的顺序列表,请纠正我,如果我错了。)

在“阿尔法”的说法是在每次迭代的衰减因子。例如,如果使用的0.5的α,那么今天的移动平均值将被由下列加权值的:

today:        1.0
yesterday:    0.5
2 days ago:   0.25
3 days ago:   0.125
...etc...

当然,如果你已经有了一个巨大的值数组,从十到十五天前值不会多么有助于今天的加权平均。该“小量”的说法,您可以设置一个分界点,低于这个你就不再关心旧值(因为他们今天的价值贡献是微不足道的)。

您会调用该函数是这样的:

result = movingAverageExponential(values, 0.75, 0.0001)

在matplotlib.org实例( http://matplotlib.org/examples/pylab_examples/finance_work2。 HTML )提供了一种使用numpy的移动平均(EMA)功能指数的一个很好的例子:

def moving_average(x, n, type):
    x = np.asarray(x)
    if type=='simple':
        weights = np.ones(n)
    else:
        weights = np.exp(np.linspace(-1., 0., n))

    weights /= weights.sum()

    a =  np.convolve(x, weights, mode='full')[:len(x)]
    a[:n] = a[n]
    return a

我不知道Python,但对于平均化部,你的意思是形式的指数衰减的低通滤波器

y_new = y_old + (input - y_old)*alpha

其中alpha = DT / tau蛋白,DT =过滤器,tau的时间步长=滤波器的时间常数τ (这个可变时间步形式如下,只是夹DT / tau蛋白以不超过1.0)

y_new = y_old + (input - y_old)*dt/tau

如果您要过滤像一个日期,一定要转换为浮点量像秒#自1970年1月1日。

您也可以使用过滤器SciPy的方法,因为EMA是IIR滤波器。相比枚举()方法

当这将有被快约64倍的益处与使用 timeit 大型数据集在我的系统测得的
import numpy as np
from scipy.signal import lfilter

x = np.random.normal(size=1234)
alpha = .1 # smoothing coefficient
zi = [x[0]] # seed the filter state with first value
# filter can process blocks of continuous data if <zi> is maintained
y, zi = lfilter([1.-alpha], [1., -alpha], x, zi=zi)

我发现了上述代码段由@earino非常有用的 - 但我需要的东西,可以连续平滑值的流 - 所以我它重构为这样:

def exponential_moving_average(period=1000):
    """ Exponential moving average. Smooths the values in v over ther period. Send in values - at first it'll return a simple average, but as soon as it's gahtered 'period' values, it'll start to use the Exponential Moving Averge to smooth the values.
    period: int - how many values to smooth over (default=100). """
    multiplier = 2 / float(1 + period)
    cum_temp = yield None  # We are being primed

    # Start by just returning the simple average until we have enough data.
    for i in xrange(1, period + 1):
        cum_temp += yield cum_temp / float(i)

    # Grab the timple avergae
    ema = cum_temp / period

    # and start calculating the exponentially smoothed average
    while True:
        ema = (((yield ema) - ema) * multiplier) + ema

和我使用它是这样的:

def temp_monitor(pin):
    """ Read from the temperature monitor - and smooth the value out. The sensor is noisy, so we use exponential smoothing. """
    ema = exponential_moving_average()
    next(ema)  # Prime the generator

    while True:
        yield ema.send(val_to_temp(pin.read()))

(其中pin.read()产生的下一个值,我想消耗)。

下面是一个简单的示例中,我后处理根据 HTTP:// stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_averages

请注意,不同于他们的电子表格,我不计算SMA,我不等待后10个样品产生的EMA。这意味着,我值略有不同,但如果你图表,它经过10个样品准确如下。在第一个10个样品,我计算EMA适当地平滑。

def emaWeight(numSamples):
    return 2 / float(numSamples + 1)

def ema(close, prevEma, numSamples):
    return ((close-prevEma) * emaWeight(numSamples) ) + prevEma

samples = [
22.27, 22.19, 22.08, 22.17, 22.18, 22.13, 22.23, 22.43, 22.24, 22.29,
22.15, 22.39, 22.38, 22.61, 23.36, 24.05, 23.75, 23.83, 23.95, 23.63,
23.82, 23.87, 23.65, 23.19, 23.10, 23.33, 22.68, 23.10, 22.40, 22.17,
]
emaCap = 10
e=samples[0]
for s in range(len(samples)):
    numSamples = emaCap if s > emaCap else s
    e =  ema(samples[s], e, numSamples)
    print e

我使用的列表和作为输入衰减速率。我希望这个只有两行的小功能,可以帮助你在这里,考虑到深递归是不是在蟒蛇稳定。

def expma(aseries, ratio):
    return sum([ratio*aseries[-x-1]*((1-ratio)**x) for x in range(len(aseries))])
scroll top