Skip to main content

Functionality for Generalized Additive Models in Liesel

Project description

Generalized Additive Models Functionality in Liesel

pre-commit pytest pytest-cov

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 liesel_gam from pypi:

pip install liesel_gam

You can also 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: A lsl.Var object that provides a convenient way to define an additive predictor.
  • gam.SmoothTerm: A lsl.Var object 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 a lsl.Var representing the prior scale parameter.
    • The alternative constructor gam.SmoothTerm.new_ig can 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.
  • gam.LinearTerm: A lsl.Var object that provides a convenient way to set up a linear term.
  • gam.Intercept: A lsl.Var parameter object that represents an intercept.
  • gam.Basis: An observed lsl.Var object that represents a basis matrix.

A bit more behind the scenes:

  • gam.MultivariateNormalSingular: An implementation of the singular multivariate normal distribution in the tensorflow_probability interface.
  • gam.star_ig_gibbs and gam.init_star_ig_gibbs: Shortcuts for setting up a gs.GibbsKernel for 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_ig constructor for the quickest and most convenient setup.
  • notebooks/test_gam_manual.ipynb: Uses gam.SmoothTerm with a manually initialized scale parameter. This is less convenient, but demonstrates how to use any lsl.Var for 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

liesel_gam-0.0.5.tar.gz (67.2 kB view details)

Uploaded Source

Built Distribution

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

liesel_gam-0.0.5-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file liesel_gam-0.0.5.tar.gz.

File metadata

  • Download URL: liesel_gam-0.0.5.tar.gz
  • Upload date:
  • Size: 67.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for liesel_gam-0.0.5.tar.gz
Algorithm Hash digest
SHA256 cc24ef0ddfb848bf848ebc2418cc522409eb323791e6e85f82f18426b79c0822
MD5 660faef1d73eda41941f19b1bbbe5068
BLAKE2b-256 ead4f1ef89dc8c789e8ba7c53d75230b4adc8f511af3fdafaba00ed004f2fd87

See more details on using hashes here.

File details

Details for the file liesel_gam-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: liesel_gam-0.0.5-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.23

File hashes

Hashes for liesel_gam-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ae3d51a0c39ac2de046ab499f1025bbce91a456882393adf2037f08878841a75
MD5 96390167633227482d1a84e28752819a
BLAKE2b-256 5d99f6cee8d6b8db4068140c9405b95f2d81fbbf1f88f9f0c6156a9c5b99a917

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