private ProductDemandEstimated[] ExtractEstimatedValue(Dictionary <string, string>[] output)
        {
            var result = new List <ProductDemandEstimated>();

            foreach (var dic in output)
            {
                var pd = new ProductDemandEstimated();

                try
                {
                    pd.Quantity = float.Parse(dic[MlSettings.ScoredValueFieldName]);

                    pd.Locationid = int.Parse(dic["Locationid"]);
                    pd.Plu        = int.Parse(dic["PLU"]);
                    pd.Year       = int.Parse(dic["Year"]);
                    pd.Month      = int.Parse(dic["Month"]);
                    pd.Day        = int.Parse(dic["Day"]);
                }
                catch (FormatException e)
                {
                    var message = JsonConvert.SerializeObject(dic);
                    throw new FormatException(message, e);
                }

                result.Add(pd);
            }
            return(result.ToArray());
        }
        public ProductDemandEstimated[] GetMovingAveragePredictions(ProductDemand[] data)
        {
            var result       = new List <ProductDemandEstimated>();
            var salesHistory = DataSource.GetSalesHistory();

            foreach (var demand in data)
            {
                // 4 weeks average
                var productSales = salesHistory.Where(s =>
                                                      s.Plu == demand.Plu &&
                                                      s.Locationid == demand.Locationid &&
                                                      s.Salesdate < demand.Salesdate &&
                                                      s.Salesdate >= demand.Salesdate.AddDays(-4 * 7)).ToArray();

                var salesAverage = 0f;
                if (productSales.Length > 0)
                {
                    salesAverage = productSales
                                   .Select(s => s.Quantity)
                                   .Sum() / (4 * 7); // 28 sale days
                }
                var estimatedDemand = new ProductDemandEstimated
                {
                    Locationid = demand.Locationid,
                    Plu        = demand.Plu,
                    Year       = demand.Year,
                    Month      = demand.Month,
                    Day        = demand.Day,
                    Quantity   = salesAverage // estimation
                };
                result.Add(estimatedDemand);
            }
            return(result.ToArray());
        }