<jupyter_start><jupyter_text>Basic time series models Mean model<jupyter_code>model_mean_pred = data.priceModLog.mean()
data["priceMean"] = np.exp(model_mean_pred)
data.plot(kind="line", x="date", y = ["priceMod", "priceMean"])
def RMSE(predicted, actual):
    mse = (predicted - actual)**2
    rmse = np.sqrt(mse.sum()/mse.count())
    return rmse

print('model_mean_RMSE : {}'.format(RMSE(data.priceMean, data.priceMod)))<jupyter_output>model_mean_RMSE : 674.5351987589808<jupyter_text>Linear Model<jupyter_code>data["timeIndex"] = data.date - data.date.min()
data.head()
# Convert to months using the timedelta function
data["timeIndex"] =  data["timeIndex"]/np.timedelta64(1, 'M')

# Round the number to 0
data["timeIndex"] = data["timeIndex"].round(0).astype(int)
data.head()
# Plot linear regression between priceMod and timeIndex
model_linear = smf.ols('priceModLog ~ timeIndex', data = data).fit()
model_linear.summary()
model_linear_pred = model_linear.predict()
data["priceLinear"] = np.exp(model_linear_pred)
data.plot(kind="line", x="date", y = ["priceMod", "priceLinear"])
print('model_linear_RMSE : {}'.format(RMSE(data.priceLinear, data.priceMod)))<jupyter_output>model_linear_RMSE : 583.845151178354<jupyter_text>Random walk model<jupyter_code>data["priceModLogShift1"] = data.priceModLog.shift()
data.head()
data["priceRandom"] = np.exp(data.priceModLogShift1)
data["priceModLogDiff"] = data.priceModLog - data.priceModLogShift1
data.plot(kind="line", x="timeIndex", y = ["priceMod","priceRandom"])
print('model_random_RMSE : {}'.format(RMSE(data.priceRandom, data.priceMod)))<jupyter_output>model_random_RMSE : 329.67094766591094<jupyter_text>Time series decomposition<jupyter_code>from statsmodels.tsa.seasonal import seasonal_decompose

data.index = data.index.to_datetime()
decomposition = seasonal_decompose(data.priceModLog, model = "additive")

decomposition.plot()
trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid

data["priceDecomp"] = np.exp(trend + seasonal)
data.plot(kind="line", x="timeIndex", y = ["priceMod", "priceDecomp"])
print('model_decomp_RMSE : {}'.format(RMSE(data.priceDecomp, data.priceMod)))<jupyter_output>model_decomp_RMSE : 365.3364693923517<jupyter_text>ARIMA<jupyter_code>ts = data.priceModLog
ts_diff = data.priceModLogDiff
ts_diff.dropna(inplace = True)
from statsmodels.tsa.arima_model import ARIMA

model_AR1MA = ARIMA(ts_diff, order=(1,0,1))
results_ARIMA = model_AR1MA.fit(disp = -1)

ts_diff.plot()
results_ARIMA.fittedvalues.plot()
predictions_ARIMA_diff = pd.Series(results_ARIMA.fittedvalues, copy=True)
predictions_ARIMA_diff_cumsum = predictions_ARIMA_diff.cumsum()

predictions_ARIMA_log = pd.Series(ts.ix[0], index=ts.index)
predictions_ARIMA_log = predictions_ARIMA_log.add(predictions_ARIMA_diff_cumsum,fprint('model_arima_RMSE : {}'.format(RMSE(data.priceARIMA, data.priceMod)))ill_value=0)

data['priceARIMA'] = np.exp(predictions_ARIMA_log)
data.plot(kind="line", x="timeIndex", y = ["priceMod", "priceARIMA"])
print('model_arima_RMSE : {}'.format(RMSE(data.priceARIMA, data.priceMod)))
data.plot(kind="line", x="timeIndex", y = ["priceMod", "priceMean", "priceLinear", "priceRandom",
                                           "priceARIMA"])<jupyter_output>/opt/infilect/dev/py3.5discoverenv/lib/python3.5/site-packages/pandas/plotting/_core.py:1714: UserWarning: Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
  series.name = label
{'fraction_non_alphanumeric': 0.1488162344983089, 'fraction_numerical': 0.025084554678692222, 'mean_word_length': 7.330985915492958, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '384707', 'license': 'MIT', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1285, 'n_words': 187, 'path': 'ml-notebooks/time-series.ipynb', 'repo_name': 'skasturi/ml-course1', 'size': '3548'}