Forecasting leadtime and demand
The script below illustrates how probabilistic forecasts can be produced, first for leadtime, second for demand. The forecasts are persisted to a Ionic file. The Ionic file format is required because regular flat files - e.g. CSV - don’t support the direct export of the distribution data type. Finally, the distributions are extended into a grid.
/// Probabilistic forecasts both for leadtime and demand
read "/sample/Lokad_Items.tsv" as Items [Id] with
Id : text
Category : text
SubCategory : text
Brand : text
Supplier : text
LeadTime : number
read "/sample/Lokad_Orders.tsv" as Orders expect [Id, Date] with
Id : text
Date : date
Quantity : number
read "/sample/Lokad_PurchaseOrders.tsv" as PO expect [Id, Date] with
Id : text
Date : date
DeliveryDate : date
Supplier : text
path = "/sample/"
// Forecasting lead time distribution with purchase orders history
Items.Leadtime = forecast.leadtime(
category: Items.Brand, Items.Category, Items.SubCategory
supplier: Items.Supplier
offset: 0
present: (max(Orders.Date) by 1) + 1
leadtimeDate: PO.Date
leadtimeValue: PO.DeliveryDate - PO.Date + 1
leadtimeSupplier: PO.Supplier)
// Forecasting demand using varying lead times and sales history
// (in practice, the ordering leadtime needs to be factored in as well)
Items.Demand = forecast.demand(
category: Items.Brand, Items.Category, Items.SubCategory
horizon: Items.Leadtime
offset: 0
present: (max(Orders.Date) by 1) + 1
demandDate: Orders.Date
demandValue: Orders.Quantity)
// Persisting the ranvars into a Ionic data file
show table "Ranvars" write:"\{path}Lokad_Ranvars.ion" with Id, Demand
//Extending the demand ranvar into a grid
table Grid = extend.ranvar(Items.Demand)
Grid.Probability = int(Items.Demand, Grid.Min, Grid.Max)
show table "Grid" with
Id
Grid.Min as "Min"
Grid.Max as "Max"
Grid.Probability as "Probability"