30

私は日付の範囲とそれらの日付ごとの測定値を持っています。各日付の指数移動平均を計算したいと思います。誰もこれを行う方法を知っていますか?

私はpythonが初めてです。平均が標準の python ライブラリに組み込まれているようには見えませんが、これは少し奇妙に思えます。多分私は正しい場所を探していません。

では、次のコードが与えられた場合、カレンダーの日付の IQ ポイントの移動加重平均を計算するにはどうすればよいでしょうか?

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

(おそらく、データを構造化するためのより良い方法があります。アドバイスをいただければ幸いです)

4

16 に答える 16

14

少しグーグルしてみたところ、次のサンプルコード(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
于 2009-01-28T18:12:07.030 に答える
13

私はいつも Pandas で 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-docs/stable/generated/pandas.ewma.html

于 2015-10-04T12:42:55.487 に答える
6

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日以降の秒数などの浮動小数点数に変換してください。

于 2009-01-28T18:10:09.187 に答える
3

上記の @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() は、消費したい次の値を生成します)。

于 2014-02-12T20:35:05.880 に答える