Choose and check an input model
Goal: go from raw observations to a source you can defend.
Look at the data first
A few questions decide the family of source:
If the data… |
consider |
|---|---|
are continuous, independent and you want a closed form |
|
are small integers (counts, extra days) |
|
are autocorrelated (today looks like yesterday) |
|
have trend or seasonality you want to keep or extrapolate |
|
are mostly zeros with occasional values (intermittent demand) |
|
are several related series (sites, products) |
|
Fit distributions
cimba.inputs.fit() fits candidates by maximum likelihood and returns
them ranked by AIC, each with its Kolmogorov–Smirnov distance:
from cimba import inputs
for candidate in inputs.fit(service_times):
print(candidate.source.describe()["method"],
round(candidate.aic, 1), round(candidate.ks, 3))
dist.exponential 974.7 0.027
dist.weibull 976.6 0.023
dist.gamma 976.7 0.026
dist.lognormal 1035.8 0.071
dist.normal 1419.2 0.165
The default candidates are exponential, gamma, lognormal, normal and
Weibull; pass candidates=[inputs.dist.gamma, inputs.dist.weibull] to
restrict them. Candidates that don’t support the data (a gamma with zeros)
are skipped. Each result’s .source is ready to bind:
best = inputs.fit(service_times)[0]
model.service_time = best.source
Here exponential, Weibull and gamma are within two AIC points of each other: the data can’t tell them apart, and the simplest, the exponential, is a fine choice.
Check before you trust
cimba.analysis.check_input() draws a sample from any source (n
values, with its own seed) and compares it with reference data:
from cimba import analysis
source = inputs.bootstrap.stationary(history, mean_block=7)
report = analysis.check_input(source, reference=history, n=4096, lags=10)
report.reference_mean, report.generated_mean
report.reference_variance, report.generated_variance
report.reference_quantiles, report.generated_quantiles # 5, 25, 50, 75, 95 %
report.reference_acf, report.generated_acf # lags 0..10
report.reference_pacf, report.generated_pacf
report.ks_statistic, report.ks_pvalue
What to look for:
Means and quantiles agree: the marginal distribution is right.
ACF/PACF agree: the time structure is right. An iid bootstrap of autocorrelated data shows near-zero ACF where the reference has structure. That tells you to use a block method.
A small KS statistic says the distributions are close. With thousands of samples even tiny differences become “significant”, so read the statistic, not just the p-value.
For a joint source, pass the joint object and a panel (a dict keyed like the joint, or a 2-D array). The report adds cross-correlation matrices and a per-member report:
joint = inputs.bootstrap.joint(panel, mean_block=14)
report = analysis.check_input(joint, reference=panel)
report.reference_correlation, report.generated_correlation
report.members[0].ks_statistic
Choosing block lengths
Block bootstraps keep the dependence within a block and break it between
blocks. Longer blocks keep more structure but give less variety. A common
starting point is a mean block of about n1/3 for n observations
(22 for 10,000 days). Then confirm with check_input that the ACF matches
at the lags that matter to your model.
Let the study decide
When two input models are both plausible, run both and see whether the answer changes. See Compare scenarios fairly.