Functionality for Generalized Additive Models in Liesel
Project description
Generalized Additive Models Functionality in Liesel
This package provides functionality to make the setup of semiparametric generalized additive distributional regression models in Liesel convenient. It works nicely with liesel-devs/smoothcon, which can be used to obtain basis and penalty matrices from the R package mgcv.
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!
- There is currently no documentation beyond this readme.
In any case, this package comes with no warranty or guarantees.
Installation
You can install the development version from GitHub via pip:
pip install git+https://github.com/liesel-devs/liesel_gam.git
Illustration
This is a short pseudo-code illustration without real data. For full examples, please consider the notebooks.
import liesel.model as lsl
import liesel.goose as gs
import liesel_gam as gam
import jax.numpy as jnp
Set up the response model.
loc = gam.AdditivePredictor("loc")
scale = gam.AdditivePredictor("scale", inv_link=jnp.exp) # terms will be added on the linked level
y = lsl.Var.new_obs(
value=...,
distribution=lsl.Dist(..., loc=loc, scale=scale),
name="y"
)
Add intercept terms
loc += gam.Intercept(
value=0.0, # this is the default
distribution=None, # this is the default
inference=gs.MCMCSpec(gs.IWLSKernel), # supply inference information here
name="b0"
)
scale += gam.Intercept( # this term will be applied on the log link level
value=0.0,
distribution=None,
inference=gs.MCMCSpec(gs.IWLSKernel),
name="g0"
)
Add a smooth term, which can be any structured additive term defined by a basis matrix and a penalty matrix. A potentially rank-deficient multivariate normal prior will be set up for the coefficient of this term.
loc += gam.SmoothTerm(
basis=...,
penalty=...,
scale=lsl.Var.new_param(..., name="tau"),
inference=gs.MCMCSpec(gs.IWLSKernel),
name="s(x)"
)
Add a linear term.
loc += gam.LinearTerm(
x=..., # 1d-array or 2d-array are both allowed
distribution=lsl.Dist(...),
inference=gs.MCMCSpec(gs.IWLSKernel),
name="x"
)
Get a Liesel EngineBuilder instance to set up MCMC sampling.
model = lsl.Model([y])
eb = gs.LieselMCMC(model).get_engine_builder() # get your engine builder instance
Contents
import liesel.model as lsl
import liesel.goose as gs
import liesel_gam as gam
This package provides the following classes and functions:
gam.AdditivePredictor: Alsl.Varobject that provides a convenient way to define an additive predictor.gam.SmoothTerm: Alsl.Varobject that provides a convenient way to set up a structured additive term with a singular multivariate normal prior, given a basis matrix, a penalty matrix, and alsl.Varrepresenting the prior scale parameter.- The alternative constructor
gam.SmoothTerm.new_igcan be used to quickly set up a term with an inverse gamma prior on the prior variance parameter. This variance parameter will be initialized with a suitable Gibbs kernel.
- The alternative constructor
gam.LinearTerm: Alsl.Varobject that provides a convenient way to set up a linear term.gam.Intercept: Alsl.Varparameter object that represents an intercept.gam.Basis: An observedlsl.Varobject that represents a basis matrix.
A bit more behind the scenes:
gam.MultivariateNormalSingular: An implementation of the singular multivariate normal distribution in thetensorflow_probabilityinterface.gam.star_ig_gibbsandgam.init_star_ig_gibbs: Shortcuts for setting up ags.GibbsKernelfor a variance parameter with an inverse gamma prior.
Usage
Usage is illustrated in the following notebooks.
- notebooks/test_gam_gibbs.ipynb: Uses the
gam.SmoothTerm.new_igconstructor for the quickest and most convenient setup. - notebooks/test_gam_manual.ipynb: Uses
gam.SmoothTermwith a manually initialized scale parameter. This is less convenient, but demonstrates how to use anylsl.Varfor the scale parameter.
Usage with bases and penalties from mgcv via smoothcon
We can get access to a large class of possible basis and penalty matrices by interfacing with the wonderful R package mgcv via liesel-devs/smoothcon.
Example notebooks that illustrate smoothcon usage are provided in the smoothcon repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file liesel_gam-0.0.4.tar.gz.
File metadata
- Download URL: liesel_gam-0.0.4.tar.gz
- Upload date:
- Size: 67.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
968a3b07530b4d6be87add06dd0e34596959ee35cd5789822f8808b82b032440
|
|
| MD5 |
9880adea836825244cb023d2e8c87880
|
|
| BLAKE2b-256 |
0591db7cc83d628ca90c41f952db7f895728de700c3e40573105e7a5d95ab883
|
File details
Details for the file liesel_gam-0.0.4-py3-none-any.whl.
File metadata
- Download URL: liesel_gam-0.0.4-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29af1ef807f37ea7d77b6683bdcd12de01abe8aca2f1176709caeeb9c0245669
|
|
| MD5 |
1cc61e4771bce09b00e626cef285d215
|
|
| BLAKE2b-256 |
cea23e3a748f06492920a1bfb9608b8b8607ec25d006749c2b479d0f57fadbb4
|