wp_R_dataset_url = 'https://github.com/facebookincubator/prophet/blob/master/examples/example_wp_R.csv'
wp_R_filename = '../datasets/example_wp_R.csv'

import pandas as pd
import numpy as np
from fbprophet import Prophet

# NB: this didn't work as of 8/22/17
#import io
#import requests
#s=requests.get(peyton_dataset_url).content
#df=pd.read_csv(io.StringIO(s.decode('utf-8')))#df = pd.read_csv(peyton_dataset_url)

"""
Explanation: Working with FB Prophet
begin with Forecasting Growth example from FB page
Lok at time series of daily page views for the Wikipedia page of the R programming language. The csv is available here
End of explanation
"""


df = pd.read_csv(wp_R_filename)
# transform to log scale
df['y']=np.log(df['y'])
df.head()

"""
Explanation: import the data and transform to log-scale
End of explanation
"""


df['cap']=8.5

df.tail()

"""
Explanation: By default, Prophet uses a linear model for its forecast. When forecasting growth, there is usually some maximum achievable point: total market size, total population size, etc. This is called the carrying capacity, and the forecast should saturate at this point.
Prophet allows you to make forecasts using a logistic growth trend model, with a specified carrying capacity. We illustrate this with the log number of page visits to the R (programming language) page on Wikipedia.
We must specify the carrying capacity in a column cap. Here we will assume a particular value, but this would usually be set using data or expertise about the market size.
End of explanation
"""


m = Prophet(growth='logistic')
m.fit(df)

"""
Explanation: The important things to note are that cap must be specified for every row in the dataframe, and that it does not have to be constant. If the market size is growing, then cap can be an increasing sequence.
We then fit the model as before, except pass in an additional argument to specify logistic growth:
End of explanation
"""


future = m.make_future_dataframe(periods=3*365+1) # covers leap year of 2016 this way

%matplotlib inline

future['cap'] = 8.5
fcst = m.predict(future)
m.plot(fcst);

m.plot?

"""
Explanation: We make a dataframe for future predictions as before, except we must also specify the capacity in the future. Here we keep capacity constant at the same value as in the history, and forecast 3 years into the future.
End of explanation
"""


m_linear = Prophet()
m_linear.fit(df);
future_lin = m_linear.make_future_dataframe(periods=3*365+1) # covers leap year of 2016 this way
fcst_lin = m_linear.predict(future_lin)
m_linear.plot(fcst_lin);

"""
Explanation: go back and make a prediction using simple linear growth model
End of explanation
"""


