Skip to content

GAMLSS

GAM models for location scale and shape (GAMLSS) facilitate modelling additional parameters of the response distribution (such as a scale parameter), as a function of the explanatory variables. Below is a simple 1D example:

import matplotlib.pyplot as plt
import numpy as np

# Create toy dataset
import pandas as pd

import pymgcv.plot as gplt
from pymgcv.families import GauLSS
from pymgcv.gam import GAM
from pymgcv.terms import S

rng = np.random.default_rng(0)
x = np.linspace(-1, 1, 100)
data = pd.DataFrame({
    "x": x,
    "y": rng.normal(loc=np.pi*np.sin(4*x), scale=4*np.abs(x) + 0.1),
})

We use the GauLSS family, which supports modelling the log standard deviation

gam = GAM(
    {
        "y": S("x"),
        "scale": S("x"),
    },
    family=GauLSS(),
)

gam.fit(data=data)
pred = gam.predict(data, compute_se=True)

This gives us a dictionary of targets (response/family parameters) mapping to the fitted values and standard errors, e.g.

pred["y"].fit  # numpy vector predicted y values (link scale).
pred["y"].se  # Associated standard errors. 

pred["scale"].fit  # numpy vector predicted scale values (link scale).
pred["scale"].se;  # Associated standard errors.

Plotting the partial effects, we can see the model suggests a relationship between \(x\) and the response scale

gplt.plot(gam)
plt.show()

img