/// <summary> /// Should forcast using 12 months data, falling back to 6 month if unavailable /// //ForcastSlope = _ForcastVals[0]; //ForcastIntercept = _ForcastVals[1]; //ForcastValue = _ForcastVals[2]; //ForcastRangeUpperBount = _ForcastVals[3]; /// </summary> /// <param name="SKUID"></param> private double[] Forcast(int SkuId, DateTime EndDate) { StatusMonContext db = new StatusMonContext(); List <double> SalesData; double[] xdata; double[] ydata; DateTime EndDate12 = EndDate.AddMonths(-12); DateTime EndDate6 = EndDate.AddMonths(-6); int date = (EndDate.Date.Year * 12 + EndDate.Date.Month); //Test if 12 Month of Data Exists if (db.SalesOrderDetails.Where(o => o.SalesOrder.OrderDate <= EndDate12).Count() > 0) { SalesData = MonthlySalesD(SkuId, EndDate.AddMonths(-11), EndDate); xdata = new double[] { date - 11, date - 10, date - 9, date - 8, date - 7, date - 6, date - 5, date - 4, date - 3, date - 2, date - 1, date }; //12 Months! } else if (db.SalesOrderDetails.Where(o => o.SalesOrder.OrderDate.Date <= EndDate6).Count() > 0) { //6 Months SalesData = MonthlySalesD(SkuId, EndDate.AddMonths(-5), EndDate); xdata = new double[] { date - 5, date - 4, date - 3, date - 2, date - 1, date }; //Actual Values are nominal } else { //Not enough Data to forcast accurately db.Dispose(); return(new double[] { 0, 0, 0, 0 }); } db.Dispose(); ydata = SalesData.ToArray(); return(Linear.Forcast(date + 1, xdata, ydata)); }
static void Main(string[] args) { //new List<double> {0.0, 0.0, 0.0, 0.0}; double[] yValues = new double[] { 0, 1, 1, 0, 1, 0 }; double[] xValues = new double[] { 8, 7, 6, 5, 4, 3 }; double xValueToPredict = 9; double[] t = Linear.Forcast(xValueToPredict, xValues, yValues); System.Console.WriteLine(t[2]); System.Console.ReadLine(); System.Console.WriteLine((DateTime.Now.Date.Year * 12 * DateTime.Now.Date.Month)); System.Console.ReadLine(); }
public static void GenerateSingleLinearForcast(int id) { ApplicationDbContext db = new ApplicationDbContext(); MonthlyTotal MT = db.MonthlyTotals.Where(p => p.SKU == id).OrderBy(g => g.Date).ToArray().Last(); DateTime ForcastDate = MT.Date.AddMonths(1); int forcast_month_id = (12 * ForcastDate.Year) + ForcastDate.Month; List <MonthlyTotal> Totals = db.MonthlyTotals.Where(P => P.SKU == id && P.Date < ForcastDate).OrderByDescending(O => O.Date).Take(12).ToList(); List <LinearDataSet> DS = new List <LinearDataSet>(); double Intercept = 0; double Slope = 0; double Absolute_Quantity_Forcast = 0; double Quantity_Forcast = 0; string JSON_MonthlyTotals = JsonConvert.SerializeObject(DS.ToArray()); int Sample_Size = Totals.Count(); string SkuClass = "Unknown"; bool Valid = false; if (Sample_Size > 3) { Valid = true; double[] YTotals = Totals.Select(P => P.Absolute_Quantity_Sold).ToArray(); double[] YTotalsR = Totals.Select(P => P.Quantity_Sold).ToArray(); double[] XMonth = Totals.Select(P => (double)(P.Date.Year * 12) + P.Date.Month).ToArray(); double[] t = Linear.Forcast(forcast_month_id, XMonth, YTotals); DS.Add(new LinearDataSet() { label = "Actual Sales", y = YTotalsR, x = XMonth }); DS.Add(new LinearDataSet() { label = "Adjusted Sales", y = YTotals, x = XMonth }); List <double> TrendYVals = new List <double>(); List <double> TrendYValsSeasonal = new List <double>(); foreach (double x in XMonth) { double val = x * t[1] + t[0]; TrendYVals.Add(val); } DS.Add(new LinearDataSet() { label = "Trend Sales", y = TrendYVals.ToArray(), x = XMonth }); JSON_MonthlyTotals = JsonConvert.SerializeObject(DS.ToArray()); Intercept = t[0]; Slope = t[1]; Absolute_Quantity_Forcast = t[2]; Quantity_Forcast = MonthlyTotal.AddSeasonality(Absolute_Quantity_Forcast, ForcastDate.Month); SkuClass = SingleLinearForcast.GetSkuClass((int)Totals.Select(P => P.Quantity_Sold).ToArray().Sum()); } SingleLinearForcast Forcast = new SingleLinearForcast() { SKU = id, Month_Id = (12 * ForcastDate.Year) + ForcastDate.Month, //Month * Year Date = ForcastDate, //Forcasted Date Quantity_Forcast = Quantity_Forcast, Absolute_Quantity_Forcast = Absolute_Quantity_Forcast, Slope = Slope, Intercept = Intercept, JSON_MonthlyTotals = JSON_MonthlyTotals, Sample_Size = Sample_Size, SkuClass = SkuClass, Valid = Valid, }; db.SingleLinearForcasts.Add(Forcast); db.SaveChanges(); }