Skip to main content

Toolbox for model building and forecasting

Project description

greybox

PyPI version PyPI - Downloads Python CI Python versions License: LGPL-2.1

Python port of the R greybox package — a toolbox for regression model building and forecasting.

hex-sticker of the greybox package for Python

Installation

pip install greybox

For more installation options, see the Installation wiki page.

Quick Example

import numpy as np
import pandas as pd
from greybox import ALM, formula

# Generate sample data
np.random.seed(42)
n = 200
data = pd.DataFrame({
    "y": np.random.normal(10, 2, n),
    "x1": np.random.normal(5, 1, n),
    "x2": np.random.normal(3, 1, n),
})
data["y"] = 2 + 0.5 * data["x1"] - 0.3 * data["x2"] + np.random.normal(0, 1, n)

# Parse formula and fit model
y, X = formula("y ~ x1 + x2", data=data)
model = ALM(distribution="dnorm")
model.fit(X, y)

# Summary
print(model.summary())

# Predict with intervals
pred = model.predict(result.data, interval="prediction", level=0.95)
print(pred.mean[:5])

# Include AR terms (ARIMA-like models)
# For example, ARIMA(1,1,0) model with Log-Normal distribution:
model = ALM(distribution="dlnorm", orders=(1, 1, 0))
model.fit(X, y)

Supported Distributions

Category Distributions
Continuous dnorm, dlaplace, ds, dgnorm, dlgnorm, dfnorm, drectnorm, dt
Positive dlnorm, dinvgauss, dgamma, dexp, dchisq
Count dpois, dnbinom, dgeom
Bounded dbeta, dlogitnorm, dbcnorm
CDF-based pnorm, plogis
Other dalaplace, dbinom

Smoothers (lowess, supsmu)

See the Smoothers wiki page for the full reference.

Non-parametric smoothers that reproduce R's stats::lowess and stats::supsmu to machine precision (native pybind11 implementations).

import numpy as np
from greybox import lowess, supsmu

rng = np.random.default_rng(0)
x = np.linspace(0, 6, 80)
y = np.sin(x) + rng.normal(0, 0.2, 80)

# Cleveland's LOWESS — robust local regression
lo = lowess(x, y, f=0.4)
print(lo["x"], lo["y"])   # sorted x, smoothed y

# Friedman's SuperSmoother — variable-span cross-validation
sm_cv    = supsmu(x, y)              # automatic span selection
sm_fixed = supsmu(x, y, span=0.3)    # fixed span

References: Cleveland (1979) for LOWESS, Friedman (1984) for SuperSmoother.

Automatic Identification of Demand (aid, aid_cat)

See the AID wiki page for the full reference.

Classifies a time series into one of six demand types and flags stockouts, new products, and obsolete products. Port of R's greybox::aid() / aidCat().

import numpy as np
import matplotlib.pyplot as plt
from greybox import aid, aid_cat

rng = np.random.default_rng(42)

# Intermittent count demand: Poisson(0.7)
y = rng.poisson(0.7, 120).astype(float)
result = aid(y)
print(result)                       # human-readable summary
print(result.name)                  # e.g. "smooth intermittent count"
print(result.type.type1)            # "count" or "fractional" — R-style attribute access
print(result.type["type1"])         # dict-style fallback also works

# Detect injected stockouts and plot them
y2 = rng.poisson(3, 100).astype(float)
y2[40:50] = 0
result2 = aid(y2)
print(result2.stockouts.start, result2.stockouts.end)  # 1-based, [41] [50]
ax = result2.plot()                 # series + grey-shaded stockout span
plt.show()

# Apply to multiple series at once
series = {
    "a": rng.poisson(1, 80).astype(float),
    "b": rng.poisson(5, 80).astype(float),
    "c": rng.normal(10, 2, 80),
}
cat = aid_cat(series)
print(cat.types)        # 2x3 demand-category frequency table
print(cat.anomalies)    # counts of new / stockouts / old products
ax = cat.plot()         # 2x3 demand-category panel
plt.show()

The nested type and stockouts fields are typed dataclasses that support both result.stockouts.start (R-style attribute access) and result.stockouts["start"] (dict-style fallback).

Features

  • ALM (Augmented Linear Model): Likelihood-based regression with 26 distributions
  • Formula parser: R-style formulas (y ~ x1 + x2, log(y) ~ ., y ~ 0 + x1) with support for backshift operator
  • stepwise(): IC-based variable selection with partial correlations
  • CALM(): Combine ALM models based on IC weights
  • Forecast error measures: MAE, MSE, RMSE, MAPE, MASE, MPE, sMAPE, and more
  • Variable processing: xreg_expander (lags/leads), xreg_multiplier (interactions), temporal_dummy
  • Distributions: 27 distribution families with density, CDF, quantile, and random generation
  • Association: Partial correlations and measures of association
  • Diagnostics: Model diagnostics and validation
  • Smoothers: lowess and supsmu matching R's stats::lowess and stats::supsmu to machine precision
  • Demand identification: aid() and aid_cat() for automatic classification of demand series and stockout detection

Links

License

LGPL-2.1

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

greybox-1.0.5.tar.gz (173.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

greybox-1.0.5-cp314-cp314-win_amd64.whl (309.4 kB view details)

Uploaded CPython 3.14Windows x86-64

greybox-1.0.5-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (320.0 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

greybox-1.0.5-cp314-cp314-macosx_11_0_arm64.whl (283.1 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

greybox-1.0.5-cp313-cp313-win_amd64.whl (304.9 kB view details)

Uploaded CPython 3.13Windows x86-64

greybox-1.0.5-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (319.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

greybox-1.0.5-cp313-cp313-macosx_11_0_arm64.whl (282.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

greybox-1.0.5-cp313-cp313-macosx_10_13_x86_64.whl (291.3 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

greybox-1.0.5-cp312-cp312-win_amd64.whl (304.8 kB view details)

Uploaded CPython 3.12Windows x86-64

greybox-1.0.5-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (319.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

greybox-1.0.5-cp312-cp312-macosx_11_0_arm64.whl (282.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

greybox-1.0.5-cp312-cp312-macosx_10_13_x86_64.whl (291.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

greybox-1.0.5-cp311-cp311-win_amd64.whl (299.8 kB view details)

Uploaded CPython 3.11Windows x86-64

greybox-1.0.5-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (315.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

greybox-1.0.5-cp311-cp311-macosx_11_0_arm64.whl (280.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

greybox-1.0.5-cp311-cp311-macosx_10_13_x86_64.whl (286.2 kB view details)

Uploaded CPython 3.11macOS 10.13+ x86-64

File details

Details for the file greybox-1.0.5.tar.gz.

File metadata

  • Download URL: greybox-1.0.5.tar.gz
  • Upload date:
  • Size: 173.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for greybox-1.0.5.tar.gz
Algorithm Hash digest
SHA256 a43fd2624ec14c489698112fa3b1a85f0d400a7e691f669557e0537a7c230c3d
MD5 8d82d7abdc0656a0dfba0ca459706f9e
BLAKE2b-256 ddec67e9f7769f609a77bf4630406b66a29d1647db734a921192dabc115462b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5.tar.gz:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: greybox-1.0.5-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 309.4 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for greybox-1.0.5-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 473948d580656fcd632b88af0ae544244f2ad511445bba34698fdd20869e0824
MD5 ebd6e36387715b7842c232ad50eb957e
BLAKE2b-256 6074afaf2bf589f01b8f9689779d2fc106f4bb6e72f755df3e587dd2217e9f66

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp314-cp314-win_amd64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.5-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0e147cd85789109d00ba46f74a768671654452f5cccbd009c0145374b2d1442e
MD5 b28d80b2e57b9c503e275ae247ee4144
BLAKE2b-256 b1d8a16089813147dd40f08a7087dc2339d9113fb18c3134f1e6fc43ed71d2ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for greybox-1.0.5-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b26ce9a0134ad9dab3efc45a8b80dad612d9da85228129972adbe082abd55b79
MD5 e346ef85f41e4ce3a68fbc92efc49b92
BLAKE2b-256 2b7dca74dd8e9e4b7b2200c92f7417135ecdbcd145eaa1f0b4957c185bf81399

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp314-cp314-macosx_11_0_arm64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: greybox-1.0.5-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 304.9 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for greybox-1.0.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 530784ef149b5ff7677b6e996f7fea77aab749fef6e74228204393f6e410bf3f
MD5 1c1fbe1b0fa5cb86a690aaf65cbdf02d
BLAKE2b-256 6aef899aaf3984055dff13532b244b926eaaec152b8c7983794fe86f628968f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp313-cp313-win_amd64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.5-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 57547de6de679e7a661bbb7a571709c60ec5b00e5be0128e314fc4ae824765e2
MD5 176a62efef68808510743211c3522218
BLAKE2b-256 16c8a4fe3b1ccdec704155aabbc4c6d6b500ced61331a170c750c6c35398b2e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for greybox-1.0.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 28f9be0475c8fa8807466921d1ba58def40d9158ebec58534f9b538849116558
MD5 30620b49e1cae87fdca9fc3b0b21bd37
BLAKE2b-256 118d2359bc34a7846e2f7162458d1bd2d23c7b80142354a6d666272f9fe8a4f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.5-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 f9998a5717fe322f39295cdfbbe3cd341a3477f667649b7c8e6629f061848b2f
MD5 2bd2043050fe3f042f818f59e53ff2ed
BLAKE2b-256 66b0af0ed3d176e588d8f7e487eaedb4603f71ec343df4ea8392898f1cf6153a

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp313-cp313-macosx_10_13_x86_64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: greybox-1.0.5-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 304.8 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for greybox-1.0.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0bf19da0df2461431604f8b80a9c4dd8b5e1a7744d78894a26cb59e2f357ed54
MD5 526ea43a454e10583ad44716ec6fd33a
BLAKE2b-256 ab889020318cb72a76b35ee332776035c5d4cbfc82f83a0e018e1f173b3cafbe

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp312-cp312-win_amd64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.5-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f3228f248cd351fc821c17e637dd39dfb86a99ef6c8b111ce32c44dfe581b015
MD5 03c2c22f024be8bf9d7f5b884e3d4925
BLAKE2b-256 dd04f87e930077ec094e176c711307d817a96259f5a9ba344ea2bf2df004fdaf

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for greybox-1.0.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 816b04df070199bb21bf284008e30e773ece559d48bdf71b35ba9959474fbb91
MD5 7f49039e22a2b6330d067ef75de57082
BLAKE2b-256 1c24e18af39abc6d9d530a53a56aaafdae9f615d163ec441a1ccea876af4cc2f

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.5-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 315680904816c4f7edec971e28215624c12db58c0b3a3af7ccb1f5bedef810b4
MD5 503cd6766a772afc3b3534d64b82034c
BLAKE2b-256 3a32b02778d261133c758eba23d242360a941c7df947f21147b77abea9eb31c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp312-cp312-macosx_10_13_x86_64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: greybox-1.0.5-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 299.8 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for greybox-1.0.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1f8f70dd53389a567e2e318cef62985bdd988eb79a66d7d51caec950819e8481
MD5 36492e38b5f57b6f2996cdce8f80b433
BLAKE2b-256 b8338dbff425037deb25b1d51a79418f93397281e23ce65860a8e786deb5e14f

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp311-cp311-win_amd64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.5-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ddf13463f91be7eef75a84276c43475b378dba4a1e1cec2c8a78f99697cdec03
MD5 27b6fbd5bf2efa1b9d934fd168055bdd
BLAKE2b-256 58f69bebc804507328f253ea1354f49061728a382685a1c97667656ef9e5c906

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for greybox-1.0.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a5753de6f4c6db5788ca6589f543e1eeb82f9048e47b880a5a1dd09ae9447f4a
MD5 b7491b4b6ea92f8910434d80b81f3c83
BLAKE2b-256 edab6e531c086d753f5ec87b768e577887b72fff05d8097525299a7ae698404d

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file greybox-1.0.5-cp311-cp311-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.5-cp311-cp311-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 ebbb23349569098f4fd711d3f5b928c40d75de5ed5f7a55953f69e2200ccff98
MD5 e8f961b401c42ce0aef9a6adecf13296
BLAKE2b-256 b982959769ec1d3939823fd84311cd88b4fceca308c460c5cf800c78c816dd71

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.5-cp311-cp311-macosx_10_13_x86_64.whl:

Publisher: release.yml on config-i1/greybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page