## 5 Our Approach
### Tuning ARIMA
#### 5.2.2 Algorithm  
This section describes how we applied ARIMA to our study. As mentioned above, we have different features (as time series) that can be used as exogenous variables for our ARIMA model. An exogenous variable would contain tuples in the form of (_date_, _value_). For instance, in terms of tuples, when merging the Amazon products' prices _Basic_ data with _Google Trends_ data we will have to join (_date_, _price_) with (_date_, _popularity_) so that we will obtain (_date_, _popularity_, _price_) tuples that can be fed to ARIMA. Thus, the algorithm we perform for each product of our collection is:  
* (_p_, \(q\), _d_) as stated in Section 5.2.1.
* Retrieve products' external data. For instance, if a product has Google Trends entries for its _Manufacturer_ we retrieve that and add it to the current external features.
* Fit a model for each combination of products' available basic and external features.  
Formally we can express the points above as follows: given an item \(I\), we retrieve its available data (_features_) \(F_{i}\) that can be split in two subsets of features such that:  
\[F_{B_{i}}\subseteq F_{i}\] \[F_{E_{i}}\subseteq F_{i}\] \[F_{B_{i}}\bigcap F_{E_{i}}=\{date\}\] \[F_{B_{i}}\bigcup F_{E_{i}}=F_{i}\] \[F_{B_{i}}=\{date,price\}\] \[F_{E_{i}}\in\mathbb{P}((F_{i}-F_{B_{i}})\bigcup\{date\})\]  
Figure 6: Example of a PACF plot. This plot shows a spike for lag values less than 2, based on 95% confidential criteria. So we choose a \(p\) value of 2 for the upper bound when we’re searching for best \((p,d,q)\) configuration.  
where \(P\) is a function that takes \(F_{B_{i}}\) and \(F_{E_{i}}\) as inputs and returns a new set of features \(F_{T_{i}}\) that contains all the elements of \(F_{B_{i}}\) and a possible combination of \(F_{E_{i}}\) (also \(\emptyset\)). The function \(C\) returns all the possible combinations of features in \(F_{E_{i}}\):  
\[C(F_{E_{i}})=\{f_{E_{i}}:f_{E_{i}}\in\mathbb{P}(F_{E_{i}})\}\] \[P(F_{B_{i}},f_{E_{i}})=F_{B_{i}}\bigcup f_{E_{i}}\] \[F_{T_{i}}=\{P(F_{B_{i}},f_{E_{i}}):f_{E_{i}}\in C(F_{E_{i}})\}\]  
Once we obtain all the features combinations \(F_{T_{i}}\) we proceed to fit the _ARIMA model_ with each of the features combinations above coupled with their correlated _MAPE_ and saving these results as a (_Map[Fitted Model, MAPE Score]_) defined as \(m[M_{T_{i}},E_{T_{i}}]\). Let \(O_{i}\) be the real trend described by \(F_{B_{i}}\).  
Although in some studies, such as that in [51], the _MAPE metric_ has been computed by using the median instead of the mean, during the experiments we preferred to adopt its canonical formulation (i.e., that based on the mean) in order to have the opportunity to compare our results to other works in literature, since it represents the most widely used evaluation metric in forecasting fields [52; 53].  
\(MAPE\) is defined as:  
\[MAPE=\frac{100}{n}\cdot\sum_{t=1}^{n}\left|\frac{O_{t_{j}}-R_{t_{j}}}{O_{t_{j }}}\right|\]  
Taking a real price trend \(O_{t}\) and a forecast \(R_{t}\) makes the difference for each point with the same _date_ field by accessing them with an index \(j\). It sums all of these differences and returns a percentage error of the average difference between all the points considered. More about _MAPE_ can be read in [54].  
To obtain the results, we simply look for the value in \(m\) having the lowest _value_. It means that the _key_ such that \(m[key]=min(value)\) is the best combination of features for the underlying product. Thus, different Amazon products might have different combinations of features as best configuration. The code we have developed to implement the proposed ARIMA model can be freely downloaded (github.com/andream16/price-probe-ml).