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.6.tar.gz (182.1 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.6-cp314-cp314-win_amd64.whl (316.9 kB view details)

Uploaded CPython 3.14Windows x86-64

greybox-1.0.6-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (327.3 kB view details)

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

greybox-1.0.6-cp314-cp314-macosx_11_0_arm64.whl (290.4 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

greybox-1.0.6-cp313-cp313-win_amd64.whl (312.3 kB view details)

Uploaded CPython 3.13Windows x86-64

greybox-1.0.6-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (327.1 kB view details)

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

greybox-1.0.6-cp313-cp313-macosx_11_0_arm64.whl (290.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

greybox-1.0.6-cp313-cp313-macosx_10_13_x86_64.whl (298.6 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

greybox-1.0.6-cp312-cp312-win_amd64.whl (312.2 kB view details)

Uploaded CPython 3.12Windows x86-64

greybox-1.0.6-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (326.9 kB view details)

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

greybox-1.0.6-cp312-cp312-macosx_11_0_arm64.whl (292.0 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

greybox-1.0.6-cp312-cp312-macosx_10_13_x86_64.whl (299.6 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

greybox-1.0.6-cp311-cp311-win_amd64.whl (307.2 kB view details)

Uploaded CPython 3.11Windows x86-64

greybox-1.0.6-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (323.2 kB view details)

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

greybox-1.0.6-cp311-cp311-macosx_11_0_arm64.whl (287.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

greybox-1.0.6-cp311-cp311-macosx_10_13_x86_64.whl (293.5 kB view details)

Uploaded CPython 3.11macOS 10.13+ x86-64

File details

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

File metadata

  • Download URL: greybox-1.0.6.tar.gz
  • Upload date:
  • Size: 182.1 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.6.tar.gz
Algorithm Hash digest
SHA256 748f097c9819601a83407f3e320aa232e8743f4c5243f297a09d4b54aea47527
MD5 37abfdc0800fd4a14effd4f032986f4e
BLAKE2b-256 d6f82922c0f6ddc047f4f523bd56ea2fb6966b86d0e005c5849e5f8d90511261

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: greybox-1.0.6-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 316.9 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.6-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 ee0de5ae4d1d46e8dd8b3a97edd619cab014fb32a93a2bfe2917cb9d9fbe3f2b
MD5 f6c8353a07db306c880ed236c5733d4b
BLAKE2b-256 4d299ae88026ba36a3f634788f4b85f462a2d2970b06e11e1ca64b6f9dc4b2a0

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.6-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 72401448570de5d10018fd9a8efc744e6fe0a2b63302e400f035be0890e4db45
MD5 b593e897cbdaad3d26985c8382047826
BLAKE2b-256 767f36db5e049875c5481f5a6d07a60a3d2dd9cbe066d3564817e8b5d9740ef9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.6-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 32258c88b842a503c7c8d963dc2edb6d83a0b41b8212c4e587afaeb8bafddf39
MD5 2d3ed6e044cb29ba8a78a0a5fdb89e2d
BLAKE2b-256 f08d007d8f16152c3b13ab06f375931dfa19bf9d69bac95e732fa515a24c88a0

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: greybox-1.0.6-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 312.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.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 5979b0cce10d301cda6422413886f6a5f64088e436f8c9d78d1474b42945844d
MD5 1cb151eb626263534a0579e080a9d8c3
BLAKE2b-256 fe8d44249fee6585706e092a2bb33397378c63e44d4b425ae1cdf2b28f30731a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.6-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 44ef3e3ec4b7326c64ac5738f5b72d8485c0fc2e73f52c622695e03ae4ea71de
MD5 42044ac8b84759cdd890c632c406a221
BLAKE2b-256 678d2d402571e181dcb0061a0fa4773466d00657db92690bebde760bbbcf1a46

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.6-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8da52aa97da0ed5c0887eebe4673c532226d7e62f6cdb7bf4bcff940f2471bbf
MD5 5a580c51a56f5c5dff00e22d0946347a
BLAKE2b-256 a8a81d37b5af45d8a3bf08421ed2c39c9921c2839ff914fb4f76dea0179e896f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.6-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 77668fa2a481aee45210e5a2f86aa5de5d4fdba5bf697bc6c3d9f7fc6ca91f20
MD5 3e81d1fc501be0bc8d08630b8c6095a7
BLAKE2b-256 57c07238591353646250121fcac5afed3ba96be1a3050d44bf444b013f53bcf7

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: greybox-1.0.6-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 312.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.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 455cb0274679e1ddd9bb532aff5c6f37a16f9f4338c4f6ea22e2f477abc122f6
MD5 2a5086971453713b36d59e9bf044f2d6
BLAKE2b-256 bf4d244a71d41f4b417f88361f0c7aecc6a278093a7eb5be10b4392a7a24ec86

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.6-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1231da03ea705ee69f4407925141b8ffc664330e793cc91a1db09fc76605acd9
MD5 3f1e7479279bd7e9a93318d1d7369e1a
BLAKE2b-256 b7bd29f52847eddc4ae32fa54c81fe0a11d5f28cf24bbd64147b36b36f81bcd5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.6-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 597bbcbb92b1d02f5085bd785fc6548777498ed1577d841e802cf8c6aefd052b
MD5 d8e4c7078b9cd8d5eaa47ad55b689c4b
BLAKE2b-256 089799bb88dbefb83245a85bd7ddce36a255de161e76d930dc1af3d551f9a58b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.6-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 7e38bcb819fe474393d8a6f85e3245b2ec15c0e4c9b70da4e7b6b7ff3541f902
MD5 532dc6ebb3f679dab055513380af8b7d
BLAKE2b-256 cdbe50a4ece6f6a01e313e71b4dbf685117b8175384fd5cd1fe4efae1de6a366

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: greybox-1.0.6-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 307.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.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5e8b143b18f32532cf2ac0dd8bc8878e9541089cd8efd43a1cd1f48540ab5592
MD5 c4ec0a22324e49f4290182717cb01a30
BLAKE2b-256 c47d0b66ad0d1197a53fa2cbec18247b4c51f62fc361c60a2f6711afc81d9556

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.6-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d0c75b2d717289d07ab1251297428981d690d0ee38930cc0bdbaae1188ccf606
MD5 8e7189c5ac32550a463e4c101bd07199
BLAKE2b-256 86ed46027765996a506e68d2162ab3e14f7429f146b7cc32b5ee33c017c89fd5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.6-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 76381e920c0135040dde7d27f190c37cc454868c486150fc0f366508f42738e6
MD5 e6ec424205cf916974434cf90a49d0c5
BLAKE2b-256 a7581ab8f1b34a8a9d21ba0d7374ab04bcf1d21dd4059f7a727d47ffd03d4715

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.6-cp311-cp311-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 81a03b3d782483f85af4a1f567d1a7ae16f97bedb31d23a21c3956965dddea69
MD5 511bc16a6e5586faacae60b783be1f36
BLAKE2b-256 c191855365e7318e45bfb0d8ebba514f3b4b9e247692ab3a9d5f94c82b9d35c9

See more details on using hashes here.

Provenance

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