Python 简单指数平滑

Python Simple Exponential Smoothing

我从 www.nasdaq.com 下载了 TESLA 股票;下载 CSV 文件后,我意识到我需要使用 Microsoft Excel 2016 转换 CSV。我使用数据选项卡;并单击分栏文本。 header 现在清楚了,它们是:日期、收盘价、成交量、开盘价、最高价、最低价。请在此处查看 csv 文件。 LinK: https://drive.google.com/open?id=1cirQi47U4uumvA14g6vOmgsXbV-YvS4l

Preview (The CSV data is from 02/02/2017 until 02/02/2018):

 1. date        | close  |  volume  | open   | high   | low   |
 2. 02/02/2018  | 343.75 |  3696157 | 348.44 | 351.95 | 340.51|
 3. 01/02/2018  | 349.25 |  4187440 | 351.00 | 359.66 | 348.63|

我面临的挑战是创建一个尽可能接近每月第一天的每个月的数据点。我在 excel 文件中过滤,这是我得到的数据。

 - date | close
 - 01/02/2018 | 349.25
 - 02/01/2018 | 320.53
 - 01/12/2017 | 306.53
 - 01/11/2017 | 321.08
 - 02/10/2017 | 341.53
 - 01/09/2017 | 355.40
 - 01/08/2017 | 319.57
 - 03/07/2017 | 352.62
 - 01/06/2017 | 340.37
 - 01/05/2017 | 322.83
 - 03/04/2017 | 298.52
 - 01/03/2017 | 250.02
 - 02/02/2017 | 251.55

如果我创建一个数据点,它会变成这样,需要创建一个图表。用简单的指数平滑或有时称为单指数平滑来显示原始数据和“平滑数据”的图形。这是关于使用 python-ggplot.

的时间序列预测的更多信息
 - x | y
 - 01/02/2018 | 349.25
 - 02/01/2018 | 320.53
 - 01/12/2017 | 306.53
 - 01/11/2017 | 321.08
 - 02/10/2017 | 341.53
 - 01/09/2017 | 355.40
 - 01/08/2017 | 319.57
 - 03/07/2017 | 352.62
 - 01/06/2017 | 340.37
 - 01/05/2017 | 322.83
 - 03/04/2017 | 298.52
 - 01/03/2017 | 250.02
 - 02/02/2017 | 251.55

我写的python程序是:

# -*- coding: utf-8 -*-

"""
Created on Sat Feb  3 13:20:28 2018

@author: johannesbambang
"""

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

my_data = pd.read_csv('C:\TESLA Exponential Smoothing\TSLA.csv',dayfirst=True,index_col=0)
my_data.plot()

plt.show()

我的问题是我的 python 程序应该改进什么?任何帮助都会很棒。提前谢谢你。

在 Python 中使用简单指数平滑。

预测是使用加权平均值计算的,其中权重随着观察来自过去的时间而呈指数下降,最小的权重与最旧的观察相关联:

'''simple exponential smoothing go back to last N values
 y_t = a * y_t + a * (1-a)^1 * y_t-1 + a * (1-a)^2 * y_t-2 + ... + a*(1-a)^n * 
y_t-n'''


def exponential_smoothing(panda_series, alpha_value):
    ouput=sum([alpha_value * (1 - alpha_value) ** i * x for i, x in 
                enumerate(reversed(panda_series))])
    return ouput
panda_series=mydata.y
smoothing_number=exponential_smoothing(panda_series,0.6) # use a=0.6 or 0.5 your choice, which gives less rms error
estimated_values=testdata.copy() # replace testdata with your test dataset
estimated_values['SES'] = smoothing_number
error=sqrt(mean_squared_error(testdata.y, estimated_values.SES))
print(error)

statsmodels ExponentialSmoothing 怎么样?

statsmodels 包在 python 中有很多用于时间序列分析的工具。

from statsmodels.tsa.api import ExponentialSmoothing

此外,请查看这篇关于 python 中时间序列分析的文章:

https://www.analyticsvidhya.com/blog/2018/02/time-series-forecasting-methods/