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.4.tar.gz (167.7 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.4-cp314-cp314-win_amd64.whl (304.8 kB view details)

Uploaded CPython 3.14Windows x86-64

greybox-1.0.4-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (315.4 kB view details)

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

greybox-1.0.4-cp314-cp314-macosx_11_0_arm64.whl (278.5 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

greybox-1.0.4-cp313-cp313-win_amd64.whl (300.3 kB view details)

Uploaded CPython 3.13Windows x86-64

greybox-1.0.4-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (315.1 kB view details)

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

greybox-1.0.4-cp313-cp313-macosx_11_0_arm64.whl (278.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

greybox-1.0.4-cp313-cp313-macosx_10_13_x86_64.whl (286.7 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

greybox-1.0.4-cp312-cp312-win_amd64.whl (300.2 kB view details)

Uploaded CPython 3.12Windows x86-64

greybox-1.0.4-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (315.0 kB view details)

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

greybox-1.0.4-cp312-cp312-macosx_11_0_arm64.whl (278.1 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

greybox-1.0.4-cp312-cp312-macosx_10_13_x86_64.whl (286.6 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

greybox-1.0.4-cp311-cp311-win_amd64.whl (295.2 kB view details)

Uploaded CPython 3.11Windows x86-64

greybox-1.0.4-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (311.3 kB view details)

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

greybox-1.0.4-cp311-cp311-macosx_11_0_arm64.whl (275.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

greybox-1.0.4-cp311-cp311-macosx_10_13_x86_64.whl (281.6 kB view details)

Uploaded CPython 3.11macOS 10.13+ x86-64

File details

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

File metadata

  • Download URL: greybox-1.0.4.tar.gz
  • Upload date:
  • Size: 167.7 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.4.tar.gz
Algorithm Hash digest
SHA256 122c6e6a477eea2af180f40ee5412e7efd9576416fe615c7dc402e11d8807b79
MD5 4860ce6d55cf74f122021b3039fa0891
BLAKE2b-256 40a7de5ca07ddd1d88765ce16ae32d83986276e464c58f44b34152e77dda58a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4.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.4-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: greybox-1.0.4-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 304.8 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.4-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 713ad9d430cde4d24a728c09118af8f0d18e18f24dada91421354ecfe5c8c269
MD5 69f77fedfa54f11d7abbdb74140563ec
BLAKE2b-256 e04ba6af49b0ae7b60f8573b06d0ddddc5a29ac1584dc68a8af22175ffb8326d

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.4-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 aaee358b9c3045c900976d62109115b3760c73968865df369968064533d1b4a3
MD5 63aa3ffc1c4fe4c451af0f0e8d197e24
BLAKE2b-256 d0b7ca1e7be878a5729016a617b34d4faa0b959f442f4b8a438043e0d37b15a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for greybox-1.0.4-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1dbaf95287d70596e3fc2474a740200205a02d9bf1123fe3f6af940b52438339
MD5 3625767ff831d899dddbaa3addeccbfb
BLAKE2b-256 ad4b5591107025b7c6f5b97a4ceb4cebbbbbd70d67390f94cf4a2412ebff3897

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: greybox-1.0.4-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 300.3 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.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c0169034bac5c2dc19d8cf36173b0b6d11fed30d6734d3fcc6a16cad960403a4
MD5 9fcbe526374bad20c318589095d08f6a
BLAKE2b-256 53a97a52a8d607148c2ec21484c54fa72f19eefb89688e9b3fe666ab82fdee83

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.4-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e0946549e53476f261d5667cc3b016d627de6fd814d472b7cbc6e43d878a7d74
MD5 a562cb9a2a0f89f30f73be7eac2e12d7
BLAKE2b-256 644ccc3d8ebef4bdcaf482ca88de7f226adab56e97c5476ecdd451a15b4e524c

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for greybox-1.0.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 253bdfe3cfd689806c42cf333f599de3682168ecd1557007360cec6e3336c55c
MD5 eb9bfb88e1fa669cf5f8b2b00fb05dc1
BLAKE2b-256 740e15419d2f3879e6dea4bbb3f87c47a8edb689b06a0db5e18e084e8431a885

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.4-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 fe0a69ae1727a4a33771e06af1abd77d96b71dabdf5634b3e89eb382c18b8249
MD5 d708a8fabe786354d4edec4e9cf55d53
BLAKE2b-256 d487b1aed9a0a4ce2da1bb7f8a737630c891287b96d17ad153cc9dbf838d8bd4

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: greybox-1.0.4-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 300.2 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.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 733f38bb60ca063c56c6116568b21bd8c100edf58063e6a6ecd632fed85570ff
MD5 945c07a7909890b901bb3ebe4dd6dc0d
BLAKE2b-256 ae8dfd779c6d1744342a706dcc11831a4562d42ad73c03dbc336f162ed9e57e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.4-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 df4a5dd599f6929441adef5ce9db9085836bd58d64c4a29e42403a9b0d3c07f6
MD5 02f7341314888f74378576098077e31d
BLAKE2b-256 7231a09f434d95644ec5011b5c5836aed34d2ef4d522eec77e5f611b4e3231dd

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for greybox-1.0.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4d9cb8d169d41fecd741d49b057949f3782b0f4e0456aa7aa93cd69303f3eb77
MD5 f4058624902e5f3b18ca152ebaa417da
BLAKE2b-256 0b190fc8bd392a06fce2822b26773a34dfbfd08ccfc6e23de39ec924056d46de

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.4-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d45af8809a46a62f7210064aaa13a1cd58bbd24c382b201f171369990efedb0d
MD5 a8b2091d0bbf067b95440b990e4db71b
BLAKE2b-256 5becc5ebbffae4ce029e5dbf358bdc575e1741f5f8f887d45cf28fbedfae0d08

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: greybox-1.0.4-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 295.2 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.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d33356ab990a05523acdf91c7fa129a8c743d2764e46d7341e76b6aff87bec74
MD5 d415051ee7c5a2857c91eaae2a6d650c
BLAKE2b-256 2a6fc2355e287e046ab879c10581fb350c7ff0123948491cfae4c7051f91a473

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.4-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c3b51de1a2b10fef0a7d18458fe8c99a17fb9ffce27c7604a8ef185ea9b41554
MD5 3dc5f8522b99d890123abd65711fb47f
BLAKE2b-256 e903b272070cd256dfa58edc1c0710696441d81913f177ebb7de4e9e8b504b06

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for greybox-1.0.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a51bf670b3dfbe296b0a383ce8d2077ef795e7316ba8e62c1318678d2645aa75
MD5 8b12a107d2d050b11c807d59a8b54fe0
BLAKE2b-256 23ac3042f1e0aab5096e480f2e14f104a2ab9a6b42dd22551a1c22b9b4be5e4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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.4-cp311-cp311-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.4-cp311-cp311-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 917be974165cb9310cf656e693b40bf7b9eaaa49fd0f823595748b6bf4220fa8
MD5 a0bcc557559fdbe98aaff11c00fa9b3c
BLAKE2b-256 a5160bb574488439c6b3be100f61537e361a48af72924ab81acbaf7b66568122

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.4-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