# Sales Forecasting with LSTM, Custom Loss Function, and Hyperparameter Optimization: A Case Study
## 3 Results
### Results by Product  
Based on the genetic algorithm designed in this paper to optimize the LSTM network, we obtained the hyperparameter configurations shown in Table 8. Subsequently, we trained the network with these configurations, to obtain a model for each lumber mill product considered.  
Figure 7: Comparison of original instance and balanced instance.  
Figure 11: Comparison of actual and forecasted sales of product 4.  
Figure 12: Comparison of actual and forecasted sales of product 5.  
Figure 10: Comparison of actual and forecasted sales of product 3.  
The results demonstrate that the LSTM network aimed to balance the total cost of the forecasts. Forecasts that exceeded actual values incurred expenses due to the operating cost of storing products in the lumber mill. Conversely, a forecast below actual sales led to expenses from producing additional products to meet demand. Table 9 presents each model's total cost calculated per product, the Root Mean Square Error (RMSE), and the Mean Absolute Percentage Error (MAPE) obtained for the LSTM network. The calculations were made with the forecast and actual values in the original scale of the time series.  
Although Table 9 shows an average MAPE of 49.99%, which is moderately high, we consider that the MAPE obtained was produced by the volatility of the sales for the products that the LSTM network could not adequately generalize. However, the LSTM network achieved an acceptable forecast of sales trends for all products. Based on MAPE, products 1 to 3 had similar behavior and were more complicated for the network to predict than products 4 and 5.  
Moreover, we can observe that product 1 produced the lowest total cost and RMSE despite the MAPE obtained. In contrast, Product 2 had the second-highest total cost and the highest RMSE, suggesting that the network struggled to accurately forecast sales for this product, resulting in higher storage and production costs.  
Products 3 and 4 produced moderately similar and intermediate results on cost and RMSE, indicating that the LSTM network reasonably well-identified sales trends for these products despite the considerable differentiation on the MAPE obtained for both. Product 5 incurred the highest cost, due to the network's difficulty accurately forecasting high sales values, though it still maintained an acceptable RMSE and the second-lowest MAPE.  
We observed that the smoothing forecasts achieved by the LSTM network were able to produce a reasonable cost balance, which is essential for minimizing both storage costs and the cost of producing unplanned products. Figures 13-17 illustrate the behavior observed for the five products analyzed. We note higher forecasts higher than actual sales can lead to high storage costs. Maintaining an adequate inventory level can help address cases where forecasts fall short of actual sales.  
Finally, to assess the efficiency of the LSTM network results obtained with the best hyperparameters found with the genetic algorithm, we decided to compare the loss costs calculated for each product with the costs obtained by the LSTM network with the hyperparameters of the first execution of the genetic algorithm for each product. Table 10 presents the comparison and the relative variation of each product concerning the Initial Solution Found Cost (ISFC) obtained and the Best Solution Found Cost (BSFC) obtained by the LSTM network.  
Figure 14: Cost of forecasts and inventory of product 2.  
Figure 15: Cost of forecasts and inventory of product 3.  
Figure 16: Cost of forecasts and inventory of product 4.  
The results presented in Table 10 demonstrate that the genetic algorithm hyperparameter optimization improved the LSTM network performance in forecasting the sales trends of the D'Gallery lumber mill in Tamaulipas, Mexico, obtaining an average general improvement of 38.73%.