Metadata-Version: 2.5
Name: actuarialnam
Version: 0.3.0
Summary: Actuarial Neural Additive Model: an interpretable deep learning model for insurance pricing
Project-URL: Repository, https://github.com/Pat-Laub/anam
Project-URL: Paper, https://doi.org/10.1016/j.insmatheco.2026.103270
Author: Patrick J. Laub
License-Expression: MIT
License-File: LICENSE
Keywords: actuarial,insurance pricing,interpretability,neural additive model,pytorch
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.12
Requires-Dist: matplotlib>=3.7
Requires-Dist: numpy>=1.24
Requires-Dist: pandas>=2.0
Requires-Dist: scikit-learn>=1.3
Requires-Dist: torch>=2.0
Description-Content-Type: text/markdown

# actuarialnam

The **Actuarial Neural Additive Model** (ANAM): an interpretable deep learning
model for general insurance pricing, written in PyTorch.

```python
from actuarialnam import ANAM   # pip install actuarialnam
```

Like a GAM, the model adds up one shape function per rating factor, plus a few
selected pairwise interactions:

```
g(mu) = bias + f_1(x_1) + ... + f_p(x_p) + sum of selected f_jk(x_j, x_k) + offset
```

Each shape function is a small neural network, so it learns its shape from the
data, yet every term can still be plotted and read on its own. On top of this
additive structure the package adds the requirements of a pricing model:

- **Sparsity:** a three-stage selection procedure keeps only the material main
  effects and interactions.
- **Smoothness:** a roughness penalty keeps continuous effects from overfitting.
- **Monotonicity:** a factor such as a bonus-malus score can be forced to only
  increase (or decrease), using lattice subnetworks that guarantee it everywhere,
  not just on the training data.
- **Exposure:** an offset column, exactly as in a Poisson GLM.

It supports Poisson, gamma and Gaussian responses. This is the reference
implementation of the model in
[Laub, Pho & Wong (2026)](https://doi.org/10.1016/j.insmatheco.2026.103270).

## Install

```bash
pip install actuarialnam
```

or, for the development version:

```bash
pip install "actuarialnam @ git+https://github.com/Pat-Laub/anam.git"
```

It needs Python 3.12 or newer.

## Quickstart

The model takes the raw data frame. Continuous and categorical columns are read
from the dtypes, and the standardising and encoding are stored in the model, so
`predict` and the plots work in the original units.

```python
from actuarialnam import ANAM

model = ANAM(
    distribution="poisson",         # log link and Poisson loss
    exposure="expo",                # log(expo) is added as an offset
    monotone={"bm": "increasing"},  # bonus-malus effect can only increase
    smoothness=1e-3,                # roughness penalty on continuous effects
)

# By default, fit() selects the main effects and interactions, then fits them.
model.fit(X_train, y_train, X_val=X_val, y_val=y_val,
          epochs=300, batch_size=5000, lr=0.01, patience=40, random_state=2000)

model.features_, model.interactions_   # the selected terms
model.predict(X_test)                  # expected claim counts, exposure included
```

## Choosing the terms yourself

Rather than trusting the automatic cutoffs, you can look at the rankings and then
decide:

```python
model.rank_main_effects(X_train, y_train, X_val=X_val, y_val=y_val, random_state=2000)
model.plot_main_effect_importance()

model.rank_interactions(X_train, y_train, X_val=X_val, y_val=y_val,
                        n_main=7, random_state=2000)
model.plot_interaction_importance()

model.choose(n_main=7, n_interactions=6)
model.fit(X_train, y_train, X_val=X_val, y_val=y_val,
          epochs=300, batch_size=5000, lr=0.01, patience=40, random_state=2000)
```

- `rank_main_effects` trains an ensemble of small main-effect models and scores
  each factor by how much its shape function varies over the data.
- `rank_interactions` scores each pair of kept main effects by how much it
  improves the validation loss.
- `choose` fixes the cutoffs, and `fit` then trains the chosen terms with the
  smoothness and monotonicity constraints.

## Reading the model

Each shape function is a contribution to the linear predictor. Under a log link,
`exp(f_j)` is a multiplier on the expected claim count.

```python
model.plot_effects(["ageph", "bm", "power"])   # several main effects
model.plot_effect("fuel")                      # a categorical factor, one bar per level
model.plot_effect("long", "lat")               # an interaction surface
grid, values = model.effect("bm")              # the numbers behind a plot
model.predict_components(X_test)               # each term's contribution, per policy
model.importance()                             # variance of each term over the data
```

Saving and loading keeps the preprocessing:

```python
model.save("model.pt")
model = ANAM.load("model.pt")
```

Categories not seen in training get zero effect.

## Development

```bash
git clone https://github.com/Pat-Laub/anam.git
cd anam
uv run pytest
```

## Citation

```bibtex
@article{laub2026anam,
  title   = {An interpretable deep learning model for general insurance pricing},
  author  = {Laub, Patrick J. and Pho, Tu and Wong, Bernard},
  journal = {Insurance: Mathematics and Economics},
  year    = {2026},
  pages   = {103270},
  doi     = {10.1016/j.insmatheco.2026.103270},
}
```

## License

MIT
