|
| 1 | +using Microsoft.ML; |
| 2 | +using System; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | + |
| 7 | +namespace eShopForecast |
| 8 | +{ |
| 9 | + public class TimeSeriesDataGenerator |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Supplements the data and returns the orignial list of months with addtional months |
| 13 | + /// prepended to total a full 36 months. |
| 14 | + public static IEnumerable<ProductData> SupplementData(MLContext mlContext, IDataView productDataSeries) |
| 15 | + { |
| 16 | + return SupplementData(mlContext, mlContext.Data.CreateEnumerable<ProductData>(productDataSeries, false)); |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Supplements the data and returns the orignial list of months with addtional months |
| 22 | + /// prepended to total a full 36 months. |
| 23 | + public static IEnumerable<ProductData> SupplementData(MLContext mlContext, IEnumerable<ProductData> singleProductSeries) |
| 24 | + { |
| 25 | + var supplementedProductSeries = new List<ProductData>(singleProductSeries); |
| 26 | + |
| 27 | + // Get the first month in series |
| 28 | + var firstMonth = singleProductSeries.FirstOrDefault(p => p.year == 2017 && p.month == singleProductSeries.Select(pp => pp.month).Min()); |
| 29 | + |
| 30 | + var referenceMonth = firstMonth; |
| 31 | + |
| 32 | + float randomCountDelta = 4; |
| 33 | + float randomMaxDelta = 10; |
| 34 | + |
| 35 | + if (singleProductSeries.Count() < 12) |
| 36 | + { |
| 37 | + var yearDelta = 12 - singleProductSeries.Count(); |
| 38 | + |
| 39 | + for (int i = 1; i <= yearDelta; i++) |
| 40 | + { |
| 41 | + var month = firstMonth.month - i < 1 ? 12 - MathF.Abs(firstMonth.month - i) : firstMonth.month - 1; |
| 42 | + |
| 43 | + var year = month > firstMonth.month ? firstMonth.year - 1 : firstMonth.year; |
| 44 | + |
| 45 | + var calculatedCount = MathF.Round(singleProductSeries.Select(p => p.count).Average()) - randomCountDelta; |
| 46 | + var calculatedMax = MathF.Round(singleProductSeries.Select(p => p.max).Average()) - randomMaxDelta; |
| 47 | + var calculatedMin = new Random().Next(1, 5); |
| 48 | + |
| 49 | + var productData = new ProductData |
| 50 | + { |
| 51 | + next = referenceMonth.units, |
| 52 | + productId = firstMonth.productId, |
| 53 | + year = year, |
| 54 | + month = month, |
| 55 | + units = referenceMonth.prev, |
| 56 | + avg = MathF.Round(referenceMonth.prev / calculatedCount), |
| 57 | + count = calculatedCount, |
| 58 | + max = calculatedMax, |
| 59 | + min = calculatedMin, |
| 60 | + prev = referenceMonth.prev - MathF.Round((referenceMonth.units - referenceMonth.prev) / 2) // subtract the delta from the previous month to this month |
| 61 | + }; |
| 62 | + |
| 63 | + supplementedProductSeries.Insert(0, productData); |
| 64 | + |
| 65 | + referenceMonth = productData; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return SupplementDataWithYear(SupplementDataWithYear(supplementedProductSeries)); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// If we have 12 months worth of data, this will suppliment the data with an additional 12 |
| 74 | + /// PREVIOUS months based on the growth exponent provided |
| 75 | + /// </summary> |
| 76 | + /// <param name="singleProductSeries">The initial 12 months of product data.</param> |
| 77 | + /// <param name="growth">The amount the values should grow year over year.</param> |
| 78 | + /// <returns></returns> |
| 79 | + static IEnumerable<ProductData> SupplementDataWithYear(IEnumerable<ProductData> singleProductSeries, float growth = 0.1f) |
| 80 | + { |
| 81 | + if (singleProductSeries.Count() < 12) |
| 82 | + { |
| 83 | + throw new NotImplementedException("fix this, currently only handles if there's already a full 12 months or more of data."); |
| 84 | + } |
| 85 | + |
| 86 | + var supplementedProductSeries = new List<ProductData>(); |
| 87 | + |
| 88 | + var growthMultiplier = 1 - growth; |
| 89 | + |
| 90 | + var firstYear = singleProductSeries.Take(12); |
| 91 | + |
| 92 | + foreach (var product in firstYear) |
| 93 | + { |
| 94 | + var newUnits = MathF.Floor(product.units * growthMultiplier); |
| 95 | + var newCount = new Random().Next((int)MathF.Floor(product.count * growthMultiplier), (int)product.count); |
| 96 | + var newMax = MathF.Floor(product.max * growthMultiplier); |
| 97 | + var newMin = new Random().Next(1, 4); |
| 98 | + |
| 99 | + var newProduct = new ProductData |
| 100 | + { |
| 101 | + next = MathF.Floor(product.next * growthMultiplier), |
| 102 | + productId = product.productId, |
| 103 | + year = product.year - 1, |
| 104 | + month = product.month, |
| 105 | + units = newUnits, |
| 106 | + avg = MathF.Round(newUnits / newCount), |
| 107 | + count = newCount, |
| 108 | + max = newMax, |
| 109 | + min = newMin, |
| 110 | + prev = MathF.Floor(product.prev * growthMultiplier) |
| 111 | + }; |
| 112 | + |
| 113 | + supplementedProductSeries.Add(newProduct); |
| 114 | + } |
| 115 | + |
| 116 | + supplementedProductSeries.AddRange(singleProductSeries); |
| 117 | + |
| 118 | + return supplementedProductSeries; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments