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.3.tar.gz (167.4 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.3-cp313-cp313-win_amd64.whl (300.3 kB view details)

Uploaded CPython 3.13Windows x86-64

greybox-1.0.3-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (314.1 kB view details)

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

greybox-1.0.3-cp313-cp313-macosx_11_0_arm64.whl (278.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.13+ x86-64

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

Uploaded CPython 3.12Windows x86-64

greybox-1.0.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (314.0 kB view details)

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

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.13+ x86-64

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

Uploaded CPython 3.11Windows x86-64

greybox-1.0.3-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (310.5 kB view details)

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

greybox-1.0.3-cp311-cp311-macosx_11_0_arm64.whl (275.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.13+ x86-64

greybox-1.0.3-cp310-cp310-win_amd64.whl (294.5 kB view details)

Uploaded CPython 3.10Windows x86-64

greybox-1.0.3-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (308.5 kB view details)

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

greybox-1.0.3-cp310-cp310-macosx_11_0_arm64.whl (273.2 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

greybox-1.0.3-cp310-cp310-macosx_10_13_x86_64.whl (279.7 kB view details)

Uploaded CPython 3.10macOS 10.13+ x86-64

File details

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

File metadata

  • Download URL: greybox-1.0.3.tar.gz
  • Upload date:
  • Size: 167.4 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.3.tar.gz
Algorithm Hash digest
SHA256 b625b10596ddd7617e8b1de0bb2bd74ef1c9e99704e695d92ae20057d10ebfa3
MD5 4d2e40439b68ad9d3c3524b8a3fbe627
BLAKE2b-256 58abf2dc1e93e02e25c646ea6c4ca20909c68322d40b9f0edb06773952726da7

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: greybox-1.0.3-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.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 78efef62d2a49b3040058a706969b3dfdce0784759a174388e8dcbe7659d533e
MD5 c3e4bf6765fd60576da96d904aff13e8
BLAKE2b-256 fdeed00f23224bf2fca06d1f60fcb803846c34bef29e4915e542da33e95ce3fb

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.3-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cbb597d1d975260809a8701ba9ae832f8ddd77ae2a915cea98bbf7294a3fdc3e
MD5 717a77641cf7f80699f79db530a09c14
BLAKE2b-256 813e07ac33ec6c1329987bbf454ad87b63bf27bb85634564820485d41fb1ce7c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 118e2e48c0e2f20dc3e0c023962883b85779cb525b5506efa9d9e2673395da0a
MD5 37f347bb7ee2abd47cb62a6de41b73fb
BLAKE2b-256 041e27a0566ebd4146e16b2ea55e3faf4a2df55bee2714d2215ca72b3a027df1

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.3-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 96b583a026da7092234329fe5f5865907ad5cd769c39c141edbb1bd2bbf158f4
MD5 13a009df70f6e33b789842dbf548849b
BLAKE2b-256 3d10053bb681770858bee503fb60e7928c5af1b0c3b7e620c818961aaa1e7016

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: greybox-1.0.3-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.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 20c8bc143c48df663daa68208a70aa84cfa3245f9b2c953dc0522f498a3cb230
MD5 168be6bdf5c75b542d32e76cd1e016e4
BLAKE2b-256 fbb9967b522855f7789903d2229af670253df5e5d9026b05b433bbc58d27ae22

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 409f7b46b2d2a200417820264091c53cb5281c0bb2b0265db1aeed25e53bd3a2
MD5 ecd09c832a5b2baf580bdd5874a384d3
BLAKE2b-256 765a716753864e28d16ec086e7eaef201c581b8278eb1c5173c38526067f4d63

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a74229c3f2df8735e46cf9beb2a87025023cb97677f0b12d714b2ac5ddc2694e
MD5 f691f8f6745b95065b0b7dc1df64b6ea
BLAKE2b-256 d02327b1b2b83c5c96e2f3ed4aa70220c884837ea9704f5d0b4839d3f470d223

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.3-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 ad34a3281f846025e9cf919bfce3e36d1b084a46cdc397062ec1ba918f50659f
MD5 8f22c7ff9fc11b4a7c10deee3164dd41
BLAKE2b-256 965857400e726a70bd546c785f8d36b4f696e87ce28da321dc8b698aa5c3e99b

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: greybox-1.0.3-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.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f03478f548c10c9aba595bda34524ff8f742aee8db2dade667acf0c8aa0362e3
MD5 17728e1dae4e3fd8af82d75f1ffb6fd4
BLAKE2b-256 564ec27b45149fc2fd762ae5f7d4c5dfaa67814ce33eeb0a930670ce675de9dc

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.3-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4000580bbcd1086597e20ea6123b9bdbe2c35f36913746b98bae4dfba630d97e
MD5 2f147db85ecd389e47b3851afdc8017b
BLAKE2b-256 14a66024d57e3bb514e7981c349cfd87fe949bcacaa84778d3b2b265cb491e00

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 478c9186d7c4fbaa388c71b9b543ab1693a6a513cb0ec445c1dcb07326d98b1d
MD5 56f0ecbbf9dac0414804d594b099314c
BLAKE2b-256 befa8f0b690dfb3769e2aeb5c61aeb90b9e9c95da0ad8f38f2a3e9d0a9c97520

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for greybox-1.0.3-cp311-cp311-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 e40d448b4d37fec81d186f94b81b2530974cb5c4a5be4eeb10f948bab358b2d3
MD5 910eb67161de4ce4c55ea97282436fbb
BLAKE2b-256 0b9105e63280f1bcfdb46cebfbd8863e766989cdf3a3ece00058bbbc5acbab9c

See more details on using hashes here.

Provenance

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

File details

Details for the file greybox-1.0.3-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: greybox-1.0.3-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 294.5 kB
  • Tags: CPython 3.10, 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.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ed62ed9eb51db3801886f1bfba1c3e39aa78736ff0c4861ff40f9f0c5268bd93
MD5 14cfe7b2078755de433bc8e6a736b673
BLAKE2b-256 a2e70c262125b12f14d1696f1b318d94b2434e6b450b9c747f436f6743755e95

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.3-cp310-cp310-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.3-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.3-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7273aac031fbd3d64d57fd194b175ef20cbe55ebdd57e4d1f37d04339d5bd644
MD5 762a92bbef2680c0b7b38f789df5f33a
BLAKE2b-256 c5c80b61839446a0e9a92ee04fcf809ff49e2af2916e5b08a4d918751af3a70f

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.3-cp310-cp310-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.3-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for greybox-1.0.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 da41ef9561341bccd4163a80d986db06c1f732703540780c04af071d4a6faea3
MD5 3b804d65f5ebd7676f6e18c164ed79d7
BLAKE2b-256 c8182e031825dd0c12d34dbf8b6f9fc9f8f10bef437993bb83c025bc4636717f

See more details on using hashes here.

Provenance

The following attestation bundles were made for greybox-1.0.3-cp310-cp310-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.3-cp310-cp310-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for greybox-1.0.3-cp310-cp310-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 b14f64171a098b01d2b72788b6c436b0bc26d7d21afdd706bf2af3613c0e7f24
MD5 046f50462faf250ed33d4efccb8df991
BLAKE2b-256 2e884ca94187219627a91ded181758d0cc026689ac79025a85e7b7b04c946eee

See more details on using hashes here.

Provenance

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