pyINLA · Random Effects ยท Constrained

Clinear Model

Constrained linear effect with bounded regression coefficient. The coefficient is restricted to a specified range [a, b].

Overview

The clinear model implements a linear effect where the regression coefficient is constrained to lie within a specified range. Unlike the standard linear model which uses a Gaussian prior, clinear uses a logit transformation to enforce bounds.

This model is useful when:

Mathematical Formulation

The clinear model defines a linear effect:

\[\eta_i = \beta \cdot z_i + \cdots\]

where the coefficient \(\beta\) is constrained to lie in the interval \([a, b]\) via a logit transformation:

\[\beta = a + (b - a) \cdot \frac{\exp(\theta)}{1 + \exp(\theta)}\]

where:

Key Property

The logit transformation ensures \(\beta\) always stays strictly within \((a, b)\). The internal parameter \(\theta\) is estimated on an unconstrained scale, making optimization straightforward.

When to Use Clinear

Positive Effects Only

When you know a covariate can only have a positive effect (e.g., range=[0, 10]).

Negative Effects Only

When you know a covariate can only have a negative effect (e.g., range=[-10, 0]).

Bounded Coefficients

When physical or domain constraints require the coefficient to stay within specific bounds.

Proportion Effects

When the coefficient represents a proportion or probability-like quantity (e.g., range=[0, 1]).

Specification in pyINLA

Allowed Parameters

Parameter Required Description
id Yes Column name for the covariate (used as index)
model Yes Must be 'clinear'
range Yes List [a, b] specifying the bounds for \(\beta\)
hyper No Prior specification for the internal parameter \(\theta\)
diagonal No Small value added to precision diagonal for numerical stability (default: 1e-4)

Basic Syntax

Shared setup. Every snippet on this page assumes this block has been run first; it builds a small synthetic df with the columns the examples use.

# shared setup for every snippet on this page (run this first)
import numpy as np
import pandas as pd
from pyinla import pyinla, inla_surv

rng = np.random.default_rng(42)
n = 80
x = rng.standard_normal(n); x1 = rng.standard_normal(n)
age = rng.uniform(20, 70, size=n); weight = rng.uniform(50, 100, size=n)
df = pd.DataFrame({'x': x, 'x1': x1, 'age': age, 'weight': weight})
df['y'] = 1.0 + 1.5 * x + rng.normal(0, 0.3, n)           # a positive slope to constrain
df['response'] = df['y']
df['signal_strength'] = 3.0 - 0.8 * x1 + rng.normal(0, 0.3, n)
df['distance'] = rng.integers(1, 11, size=len(df))     # index column used by a snippet below
df['dose'] = rng.integers(1, 11, size=len(df))     # index column used by a snippet below
df['x2'] = rng.integers(1, 11, size=len(df))     # index column used by a snippet below
from pyinla import pyinla

# Constrained linear effect: coefficient must be in [0, 5]
formula = {
    'response': 'y',
    'fixed': ['x1'],
    'random': [
        {
            'id': 'x2',
            'model': 'clinear',
            'range': [0, 5]  # beta constrained to [0, 5]
        }
    ]
}

result = pyinla(formula=formula, family='gaussian', data=df)

Examples

Example 1: Positive Coefficient

Constrain a dose-response coefficient to be positive:

# Dose must have a positive effect on response
formula = {
    'response': 'response',
    'fixed': ['age', 'weight'],
    'random': [
        {
            'id': 'dose',
            'model': 'clinear',
            'range': [0, 10]  # Positive effect only
        }
    ]
}

result = pyinla(formula=formula, family='gaussian', data=df)

Example 2: Negative Coefficient

Constrain a coefficient to be negative (e.g., decay rate):

# Distance effect must be negative (decay)
formula = {
    'response': 'signal_strength',
    'random': [
        {
            'id': 'distance',
            'model': 'clinear',
            'range': [-5, 0]  # Negative effect only
        }
    ]
}

result = pyinla(formula=formula, family='gaussian', data=df)

Hyperparameters

The clinear model has one hyperparameter:

Parameter Internal Name Description
\(\beta\) beta (or b) The constrained coefficient, transformed via logit to stay in range [a, b]

Prior Specification

The prior is specified on the internal (logit-transformed) scale:

'random': [{
    'id': 'x',
    'model': 'clinear',
    'range': [0, 5],
    'hyper': [{
        'prior': 'normal',      # Prior on internal theta
        'param': [0, 1],       # mean=0, precision=1
        'initial': 0            # Starting value (theta=0 means beta at midpoint)
    }]
}]

Initial Value Interpretation

When initial=0, the coefficient starts at the midpoint of the range: \(\beta = (a + b) / 2\). That holds for a finite range \([a, b]\), where the internal map is a logit. With an open upper bound, \([a, \infty)\), the map is \(\beta = a + e^{\theta}\), so initial=0 starts at \(a + 1\) instead; the engine's own default there is initial=1 with a normal prior of mean 1 and precision 10. Positive initial values push \(\beta\) toward the upper bound \(b\), negative values push toward lower bound \(a\).

Fixed Coefficient

To fix the coefficient at a specific value within the range:

import math

# Fix beta at 2.5 within range [0, 5]
a, b = 0, 5
beta_target = 2.5
# Inverse logit transformation
p = (beta_target - a) / (b - a)  # = 0.5
theta_fixed = math.log(p / (1 - p))  # = 0

'hyper': [{
    'initial': theta_fixed,
    'fixed': True
}]

Interpreting Results

The hyperparameter summary reports the coefficient \(\beta\) on the original (user) scale:

# View the constrained coefficient
print(result.summary_hyperpar)

# Typical output:
#                    mean       sd   0.025quant   0.5quant   0.975quant
# Beta for x         2.34     0.45         1.52       2.33         3.21

The reported values are already transformed to the user scale (within the specified range). No additional back-transformation is needed.

Clinear vs Linear

Feature linear clinear
Coefficient range \((-\infty, +\infty)\) \([a, b]\) (user-specified)
Prior Gaussian on \(\beta\) Gaussian on logit-transformed \(\theta\)
Use case General linear effects Bounded/constrained coefficients
Key parameter mean.linear, prec.linear range

Related Models