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.dev0.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.dev0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: liesel_gam-0.0.5.dev0.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.dev0.tar.gz
Algorithm Hash digest
SHA256 0171e5d614e25479e45db2497bb1bc3466dbf211caf5014167c111b32c7a57e0
MD5 d91dc895cc6183a2ed2b1d124f3e709f
BLAKE2b-256 10c5c058957cca4f1e8df5116aeafc5fcf08ad50af68f4aaedd673d4701b9c6b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for liesel_gam-0.0.5.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 425eeab03262cd4b578d0bf6afa986a52ae9c7b64bbd9287661825a07740a41c
MD5 8f0cab57bd44580663b1967f65a0d42c
BLAKE2b-256 ec9bd36086023aecf0110db89f2d062f9073ebd918adebca5ac25e0a7083569d

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