Skip to main content

Get basis and penalty matrices from mgcv and convert them to numpy arrays

Project description

SmoothCon

pre-commit pytest pytest-cov

This is a small wrapper that pulls basis and penalty matrices from the R packge mgcv and converts them to numpy arrays via ryp.

Although smoothcon is adjacent to the liesel ecosystem, it has no dependence on Liesel and can be used independently. smoothcon works well together with liesel_gam for building generalized additive distributional regresion models, see also the notebooks for examples.

Disclaimer

This package is experimental and under active development. That means:

  • The API cannot be considered stable. If you depend on this package, pin the version.
  • Testing has not been extensive as of now. Please check and verify!
  • Smoothcon is currently tested only for simple, univariate bases. For tensor products or markov random fields, you will need to be very careful and know what you are doing.
  • There is currently no documentation beyond this readme.

In any case, this package comes with no warranty or guarantees.

Installation

You can install smoothcon from pypi:

pip install smoothcon

You can install the development version from GitHub via pip:

pip install git+https://github.com/liesel-devs/smoothcon.git

Smoothcon requires the following R packages:

install.packages("arrow") # for general usage of ryp
install.packages("svglite") # for plotting in jupyter notebooks

Usage

We illustrate usage with random data:

# import packages
import numpy as np
from smoothcon import SmoothCon


# generate some random data
rng = np.random.default_rng(seed=1)
n = 100
x = rng.uniform(-2.0, 2.0, size=n)
y = x + rng.normal(loc=0.0, scale=1.0, size=n)
mcycle = {"accel": y, "times": x}  # imitating the MASS:mcycle dataset

Now we initialize the smooth. What's special here is that the spec argument of the SmoothCon class can simply be a string containing the R code that you would usually use to specify a smooth in mgcv. Any smooth specification accepted by mgcv::SmoothCon is permitted.

# construct smooth
smooth = SmoothCon(
    spec="s(times, bs='ps', k=20, m=c(3,2))",   # mgcv smooth specification
    data=mcycle,                # dictionary, pandas dataframe, or polars dataframe
    knots=None,                 # knots; if None (default), mgcv will create the knots
    absorb_cons=True,           # If True, constraints (e.g. sum-to-zero) will be absorbed into the basis matrix
    diagonal_penalty=True,      # If True, the penalty will be diagonalized
    pass_to_r=None,             # dictionary of data that should be made available to the R environment
)

Access smooth information:

# shortcuts to smooth information
smooth.basis        # if there is only one basis in the smooth
smooth.penalty      # if there is only one penalty in the smooth
smooth.knots

# full smooth information
smooth.all_bases()      # list of all bases in the smooth
smooth.all_penalties()  # list of all penalties in the smooth

# prediction
new_x = rng.uniform(-1.0, 2.0, size=5)
newdata = {"times": new_x}
smooth.predict(data=newdata)            # compute single basis at new covariate values
smooth.predict_all_bases(data=newdata)  # compute all bases at new covariate values
smooth(new_x)                           # alternative syntax for .predict

SmoothFactory

If you want to initialize several smooths, you might not want to pass the data each time to SmoothCon. Passing the data each time is not only cumbersome, but also inefficient, because it will be converted to an R dataframe each time. So you probably want to use the SmoothFactory class to initialize your SmoothCon objects in most cases:

from smoothcon import SmoothFactory

sf = SmoothFactory(data=df, pass_to_r=None) # pass data to R only once
smooth_x = sf("s(x, bs='ps', k=20)")        # call to initialize a SmoothCon object

Usage with liesel_gam: Example Notebooks

Advanced usage for building generalized additive distributional regression models with liesel and liesel_gam is illustrated in the following notebooks.

  • notebooks/test_gam_gibbs.ipynb: A generalized addition location-scale model, using inverse gamma priors an Gibbs kernels for the inverse smoothing parameters.
  • notebooks/test_gam_manual.ipynb: A generalized addition location-scale model, using a manually initialized inverse smoothing parameter with a Weibull prior.

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

smoothcon-0.0.7.tar.gz (72.9 kB view details)

Uploaded Source

Built Distribution

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

smoothcon-0.0.7-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file smoothcon-0.0.7.tar.gz.

File metadata

  • Download URL: smoothcon-0.0.7.tar.gz
  • Upload date:
  • Size: 72.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for smoothcon-0.0.7.tar.gz
Algorithm Hash digest
SHA256 c0f4eb1b3e925e5a1ea480abd71d7ece0fec4fdfa9e3ddd946934d41146be2fd
MD5 3a8b19d9b4e5acd53f1fd04ac4dd7e8c
BLAKE2b-256 86837d8dd7802de37bb73154d842b9b8aa8ba2db343a747e176216324453e20b

See more details on using hashes here.

File details

Details for the file smoothcon-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: smoothcon-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for smoothcon-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 3ae3e8f931b4c825bda0ac4cabd44849692e91e936b44f5c5761e6ae6f151726
MD5 c7d3b83b52dbe58cb3b29559bcfdddf0
BLAKE2b-256 99b12c41603ca409080e748cafd4acbbbdfb6062bee181267d8f5061058d8765

See more details on using hashes here.

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