<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <h1 style="font-size
Step1:  Step 2
Step2:  Step 3
Step3:  Step 4
Step4:  Rather than looping through each region and product within the data set, the following cell creates a subset of data to forecast. If you'd like to forecast for an alternate region/product combination this is where you're able to make the change by subbing out Region <i>13</i> and Product <i>315</i>.
Step5:  Step 5
<ASSISTANT_TASK:>
Python Code:
#import tm1 service module
from TM1py.Services import TM1Service
#import tm1 utils module
from TM1py.Utils import Utils
#import pandas
import pandas as pd
#import matplotlib
import matplotlib.pyplot as plt
#inline plotting for matplotlib
%matplotlib inline
#import statsmodels package
from fbprophet import Prophet
Explanation: <h1 style="font-size:42px; text-align:center; margin-bottom:30px;"><span style="color:SteelBlue">TM1py:</span> Time Series Forecasting</h1>
<hr>
The first step you should complete before actually stepping through this code is to install all required dependencies. Assuming you already have Anaconda installed, you'll only need to install TM1py. To do this run the following command:
<pre>pip install TM1py</pre>
You'll also need to install pystan and the facebook Prophet time series package. To do this run the following commands: 
<pre>pip install pystan</pre>
<pre>pip install fbprophet</pre>
Step 1: Import the required modules
<p> Before stepping through the process there are a number of modules that need to be imported. This model relies heavily on <a href="https://pandas.pydata.org/">Pandas</a> for data manipulation and transformation and <a href="https://code.cubewise.com/tm1py/">TM1py</a> for interacting with the TM1 API.
End of explanation
#Server address
address = 'localhost'
#HTTP port number - this can be found in your config file
port = '8892'
#username
user = 'admin'
#password
password = 'apple'
#SSL parameter - this can be found in your config file
ssl = True
Explanation: Step 2: Setup your TM1 parameters
TM1 Server Parameters
Populate your TM1 server parameters below. In this example the script is connecting to the local server and 8882 for the HTTP port.
End of explanation
#specify the cube
cube_name = 'Retail'
#specify the view
view_name = 'Time Series'
with TM1Service(address= address, port=port, user=user, password=password, ssl=ssl) as tm1:
    
    # Extract pnl data from specified cube view
    raw_data = tm1.cubes.cells.get_view_content(cube_name=cube_name, view_name=view_name, private=False)
    # Build pandas DataFrame fram raw cellset data
    df = Utils.build_pandas_dataframe_from_cellset(raw_data, multiindex=False)
Explanation: Step 3: Get TM1 Data
The following code obtains a data set from TM1 based on the cube and view specified above. The resulting dataset is transformed into a pandas dataframe for statistical analysis. 
For more on pandas and dataframes check out this <a href="https://pandas.pydata.org/pandas-docs/stable/tutorials.html">pandas tutorial</a>.
Before importing any data, create the view you would like to import into the data frame. Populate the fields below.
End of explanation
ts = df
ts.dtypes
ts['Date'] = ts['Year'] + '-' + ts['Period']
ts['Date'] = pd.DatetimeIndex(ts['Date'])
ts = ts.rename(columns={'Date': 'ds',
                        'Values': 'y'})
Explanation: Step 4: Time Series Forecast with Prophet
End of explanation
region = '13'
product = '315'
sub = ts[ts['Region']==region]
sub = sub[sub['Product']==product]
sub_model = Prophet(interval_width=0.95)
sub_model.fit(sub)
#specify the number of future periods
future_dates = sub_model.make_future_dataframe(periods=24, freq='MS')
forecast = sub_model.predict(future_dates)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
sub_model.plot(forecast,
              uncertainty=True)
sub_model.plot_components(forecast)
Explanation: Rather than looping through each region and product within the data set, the following cell creates a subset of data to forecast. If you'd like to forecast for an alternate region/product combination this is where you're able to make the change by subbing out Region <i>13</i> and Product <i>315</i>.
End of explanation
#Combine the forecast dataframe and our original subset
sub = sub.set_index('ds')
forecast = forecast.set_index('ds')
result = pd.concat([sub, forecast], axis = 1)
#fill out other dimensions
result['Version'] = result.Version.fillna('3')
result['Currency'] = result.Currency.fillna('Local')
result['Region'] = result.Region.fillna(region)
result['Product'] = result.Product.fillna(product)
result['Retail Measure'] = result['Retail Measure'].fillna('Sales Amount')
#make ds accessible
result = result.reset_index()
result['Year'] = pd.DatetimeIndex(result['ds']).year
result['Period'] = pd.DatetimeIndex(result['ds']).month
with TM1Service(address= address, port=port, user=user, password=password, ssl=ssl) as tm1:
    
    # cellset to store the new data
    cellset = {}
    # Populate cellset with coordinates and value pairs
    for index, row in result.iterrows():
        cellset[(row['Year'], row['Period'], 'Forecast', row['Product'], row['Currency'], row['Region'],row['Retail Measure'])] = row['yhat']        
        tm1.cubes.cells.write_values('Retail', cellset)
Explanation: Step 5: Send the data back into TM1
End of explanation
<END_TASK>