# Sales Forecasting with LSTM, Custom Loss Function, and Hyperparameter Optimization: A Case Study
## 1 Introduction
However, despite their advantages, RNNs have some limitations that make them ineffective for specific problems. In this sense, LSTM networks have recently emerged as an advanced variant of RNNs for forecasting and time series analysis due to their ability to handle long-term dependencies and gradient stability. Thus, LSTMs are frequently found in the state-of-the-art instead of RNNs, due to their performance in complex time series forecasting, and they currently remain one of the most robust and versatile options. For example, [PERSON] and [PERSON] in [13] proposed an LSTM network for stock price prediction in the financial market; the authors used a three-layer network to predict the closing prices of stocks in the LQ45 financial sectors. The experimental results showed that the predictions were reasonable. Therefore, the authors recommended using LSTM networks in this context, based on the low MAPE and predictions obtained. In [14], [PERSON] et al. developed an effective foreign trade forecasting method, using an LSTM network to analyze the trade data of 10 countries. The proposed method was developed based on the economic theory of two-way causal relationships in trade data. The results showed that the technique could adequately model the temporal information on uncertainty trends in foreign trade data. In almost all cases, the MAPE and RMSE errors calculated were lower than those of the other models analyzed. For their part, [PERSON] and [PERSON] in [15] developed three RNN models to predict short-term actual evapotranspiration. Additionally, they used an ensemble technique to improve the accuracy of the results. The models were trained and tested at two sites with different climates. The results indicated that LSTM models are more accurate with data from subtropical climates. In contrast, Non-linear AutoRegression with eXogenous inputs (NARX) models perform better when using data from semi-arid climates. Furthermore, some authors have adapted LSTM networks to be bidirectional and have combined them with other techniques, as in the case of [PERSON] et al. [16], where they proposed a method to predict the closing price of stocks for the next day, combining LSTM networks with Convolutional Neural Networks (CNNs) and an Attention Mechanism (AM). The authors compared the performance of the proposed model with seven other different methods. The authors calculated the following metrics for the comparison: MeanAbsolute Error (MAE), Root Mean Square Error (RMSE), and coefficient of determination (\(R^{2}\)). The experimentation showed that the CNN-BiLSTM model obtained the best results in predicting the closing price of stocks with an MAE of 21.952, an RMSE of 31.694, and an \(R^{2}\) of 0.9804. Similarly, [PERSON] et al. [17] developed a daily temperature forecasting model for the country of China, in which an approach combining seasonal and trend decomposition using the Seasonal-Trend decomposition employing the LOESS (STL) method and the Jumping Spectrum and Trend (JUST) algorithm, together with a bidirectional Long Short-Term Memory (Bi-LSTM) network, was proposed. The experimental results showed that the proposed model significantly outperformed other models, such as Bi-LSTM, END-LSTM, and STL-LSTM, in terms of accuracy and being the lowest in prediction errors, with an RMSE error of 0.2187 and an MAE of 0.1737. In this paper, we propose the design of an LSTM network to forecast the sales of a lumber mill, considering two scenarios: (1) costs of product construction when demand exceeded the available stock in the inventory, and (2) operating costs generated by the products stored when demand was less than the inventory. This paper aimed to accurately forecast the sales demand trend for D'Gallery lumber mill products in Tamaulipas, Mexico. The proposed LSTM network focused on reducing costs associated with constructing products during high demand and minimizing storage costs by maintaining optimal inventory levels.