# 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 data file. The Ionic 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"
read "/sample/Lokad_Orders.tsv" as Orders
read "/sample/Lokad_PurchaseOrders.tsv" as PO
path:="/sample/"
// Forecasting lead time distribution with purchase orders history
Leadtime = forecast.leadtime(
category: Brand, Category, SubCategory
supplier: 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)
Demand = forecast.demand(
category: Brand, Category, SubCategory
horizon: Leadtime
offset: 0
present: (max(Orders.Date) by 1) + 1
demandDate: Orders.Date
demandValue: Orders.Quantity)
// Persisting the distributions into a Ionic data file
show table "Distributions" export:"\{path}Lokad_Distrib.ion" with Id, Demand
//Extending the demand distribution into a grid
table Grid = extend.distrib(Demand)
Grid.Probability = int(Demand, Grid.Min, Grid.Max)
show table "Grid" with
Id
Grid.Min as "Min"
Grid.Max as "Max"
Grid.Probability as "Probability"
```