天天看點

python 狀态空間模型_替代r的python/scikit/numpy中的指數平滑狀态空間模型

John Moutafi..

7

經過一番搜尋,我沒有發現任何看起來很有希望作為etspython 的替代品.雖然有一些嘗試:StatsModels和pycast的預測方法,您可以檢查它們是否适合您的需求.

可用于解決缺少的實作的一個選項是使用子程序子產品從python運作R腳本.這裡是如何做到這一點很好的文章在這裡.

為了做到以後:

您需要建立一個R腳本(例如my_forecast.R),它将

計算(使用ets)并列印檔案上的預測,或者stdout(使用cat()指令),以便在腳本運作後使用它們.

您可以從python腳本運作R腳本,如下所示:

import subprocess

# You need to define the command that will run the Rscript from the subprocess

command = 'Rscript'

path2script = 'path/to/my_forecast.R'

cmd = [command, path2script]

# Option 1: If your script prints to a file

subprocess.run(cmd)

f = open('path/to/created/file', 'r')

(...Do stuff from here...)

# Option 2: If your script prints to stdout

forecasts = subprocess.check_output(cmd, universal_newlines=True)

(...Do stuff from here...)

您還可以為您的參數添加參數,您cmd的Rscript将将其用作指令行參數,如下所示:

args = [arg0, arg1, ...]

cmd = [command, path2script] + args

Then pass cmd to the subprocess

編輯:

我發現了一個示範一系列的霍爾特-溫特斯預測文章:第一部分,第2部分和第三部分.除了這些文章中易于了解的分析外,Gregory Trubetskoy(作者)提供了他開發的代碼:

初步趨勢:

def initial_trend(series, slen):

sum = 0.0

for i in range(slen):

sum += float(series[i+slen] - series[i]) / slen

return sum / slen

# >>> initial_trend(series, 12)

# -0.7847222222222222

最初的季節性成分:

def initial_seasonal_components(series, slen):

seasonals = {}

season_averages = []

n_seasons = int(len(series)/slen)

# compute season averages

for j in range(n_seasons):

season_averages.append(sum(series[slen*j:slen*j+slen])/float(slen))

# compute initial values

for i in range(slen):

sum_of_vals_over_avg = 0.0

for j in range(n_seasons):

sum_of_vals_over_avg += series[slen*j+i]-season_averages[j]

seasonals[i] = sum_of_vals_over_avg/n_seasons

return seasonals

# >>> initial_seasonal_components(series, 12)

# {0: -7.4305555555555545, 1: -15.097222222222221, 2: -7.263888888888888,

# 3: -5.097222222222222, 4: 3.402777777777778, 5: 8.069444444444445,

# 6: 16.569444444444446, 7: 9.736111111111112, 8: -0.7638888888888887,

# 9: 1.902777777777778, 10: -3.263888888888889, 11: -0.7638888888888887}

最後算法:

def triple_exponential_smoothing(series, slen, alpha, beta, gamma, n_preds):

result = []

seasonals = initial_seasonal_components(series, slen)

for i in range(len(series)+n_preds):

if i == 0: # initial values

smooth = series[0]

trend = initial_trend(series, slen)

result.append(series[0])

continue

if i >= len(series): # we are forecasting

m = i - len(series) + 1

result.append((smooth + m*trend) + seasonals[i%slen])

else:

val = series[i]

last_smooth, smooth = smooth, alpha*(val-seasonals[i%slen]) + (1-alpha)*(smooth+trend)

trend = beta * (smooth-last_smooth) + (1-beta)*trend

seasonals[i%slen] = gamma*(val-smooth) + (1-gamma)*seasonals[i%slen]

result.append(smooth+trend+seasonals[i%slen])

return result

# # forecast 24 points (i.e. two seasons)

# >>> triple_exponential_smoothing(series, 12, 0.716, 0.029, 0.993, 24)

# [30, 20.34449316666667, 28.410051892109554, 30.438122252647577, 39.466817731253066, ...

您可以将它們放在一個檔案中,例如:holtwinters.py在具有以下結構的檔案夾中:

forecast_folder

|

??? __init__.py

|

??? holtwinters.py

從這裡開始,這是一個python子產品,您可以将其置于所需的每個項目結構中,并在該項目内的任何位置使用它,隻需導入它即可.

我給你賞金,因為你付出了很多努力來回答這個問題,但這仍然不是我想要的.如果将來有人發現(或建立)所有30個Rob Hyndmans狀态空間指數平滑模型,我将很樂意制作并獎勵另一個賞金:https://www.otexts.org/fpp/7/ 7 (2認同)