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 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.4.tar.gz (67.1 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.4-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

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

Hashes for liesel_gam-0.0.4.tar.gz
Algorithm Hash digest
SHA256 968a3b07530b4d6be87add06dd0e34596959ee35cd5789822f8808b82b032440
MD5 9880adea836825244cb023d2e8c87880
BLAKE2b-256 0591db7cc83d628ca90c41f952db7f895728de700c3e40573105e7a5d95ab883

See more details on using hashes here.

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

Hashes for liesel_gam-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 29af1ef807f37ea7d77b6683bdcd12de01abe8aca2f1176709caeeb9c0245669
MD5 1cc61e4771bce09b00e626cef285d215
BLAKE2b-256 cea23e3a748f06492920a1bfb9608b8b8607ec25d006749c2b479d0f57fadbb4

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