retail_datafile = '../datasets/example_retail_sales.csv'

from fbprophet import Prophet
import numpy as np
import pandas as pd
%matplotlib inline

sales_df = pd.read_csv(retail_datafile)
sales_df.head()

"""
Explanation: Another example from the web of using FB Prophet package
Source url
Data corresponds to retail_sales example csv
End of explanation
"""


sales_df['y_orig'] = sales_df['y'] # to save a copy of the original data..you'll see why shortly. 
# log-transform y
sales_df['y'] = np.log(sales_df['y'])
sales_df.tail()

sales_df.y_orig.hist(bins=30)

sales_df.y.hist(bins=30)

"""
Explanation: NB: Prophet expects the dataframe to have one column named 'ds' and another named 'y'
It is recommended to transform 'y' in to log('y') before modeling in order to attempt to convert non-stationary data into stationary data. It also converts possible (expoential) trends into linear ones.
End of explanation
"""


model = Prophet()
model.fit(sales_df)

"""
Explanation: Start modeling
End of explanation
"""


future_data = model.make_future_dataframe(periods=6, freq = 'm')

forecast_data = model.predict(future_data)
forecast_data.tail()

model.plot(forecast_data)

model.plot_components(forecast_data)

"""
Explanation: create a future timeseries
End of explanation
"""


forecast_data_orig = forecast_data # make sure we save the original forecast data
forecast_data_orig['yhat'] = np.exp(forecast_data_orig['yhat'])
forecast_data_orig['yhat_lower'] = np.exp(forecast_data_orig['yhat_lower'])
forecast_data_orig['yhat_upper'] = np.exp(forecast_data_orig['yhat_upper'])

model.plot(forecast_data_orig)

sales_df['y_log']=sales_df['y'] #copy the log-transformed data to another column
sales_df['y']=sales_df['y_orig'] #copy the original data to 'y'

model.plot(forecast_data_orig)

"""
Explanation: From the trend and seasonality, we can see that the trend is a playing a large part in the underlying time series and seasonality comes into play more toward the beginning and the end of the year.
So far so good.  With the above info, we’ve been able to quickly model and forecast some data to get a feel for what might be coming our way in the future from this particular data set.
<hr>

Here is a little tip for getting your forecast plot to display your ‘original’ data so you can see the forecast in ‘context’ and in the original scale rather than the log-transformed data. You can do this by using np.exp() to get our original data back.
End of explanation
"""


