actuarialnam 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ **/__pycache__/
2
+ **/.DS_Store
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Patrick J. Laub
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,164 @@
1
+ Metadata-Version: 2.5
2
+ Name: actuarialnam
3
+ Version: 0.3.0
4
+ Summary: Actuarial Neural Additive Model: an interpretable deep learning model for insurance pricing
5
+ Project-URL: Repository, https://github.com/Pat-Laub/anam
6
+ Project-URL: Paper, https://doi.org/10.1016/j.insmatheco.2026.103270
7
+ Author: Patrick J. Laub
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Keywords: actuarial,insurance pricing,interpretability,neural additive model,pytorch
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
15
+ Requires-Python: >=3.12
16
+ Requires-Dist: matplotlib>=3.7
17
+ Requires-Dist: numpy>=1.24
18
+ Requires-Dist: pandas>=2.0
19
+ Requires-Dist: scikit-learn>=1.3
20
+ Requires-Dist: torch>=2.0
21
+ Description-Content-Type: text/markdown
22
+
23
+ # actuarialnam
24
+
25
+ The **Actuarial Neural Additive Model** (ANAM): an interpretable deep learning
26
+ model for general insurance pricing, written in PyTorch.
27
+
28
+ ```python
29
+ from actuarialnam import ANAM # pip install actuarialnam
30
+ ```
31
+
32
+ Like a GAM, the model adds up one shape function per rating factor, plus a few
33
+ selected pairwise interactions:
34
+
35
+ ```
36
+ g(mu) = bias + f_1(x_1) + ... + f_p(x_p) + sum of selected f_jk(x_j, x_k) + offset
37
+ ```
38
+
39
+ Each shape function is a small neural network, so it learns its shape from the
40
+ data, yet every term can still be plotted and read on its own. On top of this
41
+ additive structure the package adds the requirements of a pricing model:
42
+
43
+ - **Sparsity:** a three-stage selection procedure keeps only the material main
44
+ effects and interactions.
45
+ - **Smoothness:** a roughness penalty keeps continuous effects from overfitting.
46
+ - **Monotonicity:** a factor such as a bonus-malus score can be forced to only
47
+ increase (or decrease), using lattice subnetworks that guarantee it everywhere,
48
+ not just on the training data.
49
+ - **Exposure:** an offset column, exactly as in a Poisson GLM.
50
+
51
+ It supports Poisson, gamma and Gaussian responses. This is the reference
52
+ implementation of the model in
53
+ [Laub, Pho & Wong (2026)](https://doi.org/10.1016/j.insmatheco.2026.103270).
54
+
55
+ ## Install
56
+
57
+ ```bash
58
+ pip install actuarialnam
59
+ ```
60
+
61
+ or, for the development version:
62
+
63
+ ```bash
64
+ pip install "actuarialnam @ git+https://github.com/Pat-Laub/anam.git"
65
+ ```
66
+
67
+ It needs Python 3.12 or newer.
68
+
69
+ ## Quickstart
70
+
71
+ The model takes the raw data frame. Continuous and categorical columns are read
72
+ from the dtypes, and the standardising and encoding are stored in the model, so
73
+ `predict` and the plots work in the original units.
74
+
75
+ ```python
76
+ from actuarialnam import ANAM
77
+
78
+ model = ANAM(
79
+ distribution="poisson", # log link and Poisson loss
80
+ exposure="expo", # log(expo) is added as an offset
81
+ monotone={"bm": "increasing"}, # bonus-malus effect can only increase
82
+ smoothness=1e-3, # roughness penalty on continuous effects
83
+ )
84
+
85
+ # By default, fit() selects the main effects and interactions, then fits them.
86
+ model.fit(X_train, y_train, X_val=X_val, y_val=y_val,
87
+ epochs=300, batch_size=5000, lr=0.01, patience=40, random_state=2000)
88
+
89
+ model.features_, model.interactions_ # the selected terms
90
+ model.predict(X_test) # expected claim counts, exposure included
91
+ ```
92
+
93
+ ## Choosing the terms yourself
94
+
95
+ Rather than trusting the automatic cutoffs, you can look at the rankings and then
96
+ decide:
97
+
98
+ ```python
99
+ model.rank_main_effects(X_train, y_train, X_val=X_val, y_val=y_val, random_state=2000)
100
+ model.plot_main_effect_importance()
101
+
102
+ model.rank_interactions(X_train, y_train, X_val=X_val, y_val=y_val,
103
+ n_main=7, random_state=2000)
104
+ model.plot_interaction_importance()
105
+
106
+ model.choose(n_main=7, n_interactions=6)
107
+ model.fit(X_train, y_train, X_val=X_val, y_val=y_val,
108
+ epochs=300, batch_size=5000, lr=0.01, patience=40, random_state=2000)
109
+ ```
110
+
111
+ - `rank_main_effects` trains an ensemble of small main-effect models and scores
112
+ each factor by how much its shape function varies over the data.
113
+ - `rank_interactions` scores each pair of kept main effects by how much it
114
+ improves the validation loss.
115
+ - `choose` fixes the cutoffs, and `fit` then trains the chosen terms with the
116
+ smoothness and monotonicity constraints.
117
+
118
+ ## Reading the model
119
+
120
+ Each shape function is a contribution to the linear predictor. Under a log link,
121
+ `exp(f_j)` is a multiplier on the expected claim count.
122
+
123
+ ```python
124
+ model.plot_effects(["ageph", "bm", "power"]) # several main effects
125
+ model.plot_effect("fuel") # a categorical factor, one bar per level
126
+ model.plot_effect("long", "lat") # an interaction surface
127
+ grid, values = model.effect("bm") # the numbers behind a plot
128
+ model.predict_components(X_test) # each term's contribution, per policy
129
+ model.importance() # variance of each term over the data
130
+ ```
131
+
132
+ Saving and loading keeps the preprocessing:
133
+
134
+ ```python
135
+ model.save("model.pt")
136
+ model = ANAM.load("model.pt")
137
+ ```
138
+
139
+ Categories not seen in training get zero effect.
140
+
141
+ ## Development
142
+
143
+ ```bash
144
+ git clone https://github.com/Pat-Laub/anam.git
145
+ cd anam
146
+ uv run pytest
147
+ ```
148
+
149
+ ## Citation
150
+
151
+ ```bibtex
152
+ @article{laub2026anam,
153
+ title = {An interpretable deep learning model for general insurance pricing},
154
+ author = {Laub, Patrick J. and Pho, Tu and Wong, Bernard},
155
+ journal = {Insurance: Mathematics and Economics},
156
+ year = {2026},
157
+ pages = {103270},
158
+ doi = {10.1016/j.insmatheco.2026.103270},
159
+ }
160
+ ```
161
+
162
+ ## License
163
+
164
+ MIT
@@ -0,0 +1,142 @@
1
+ # actuarialnam
2
+
3
+ The **Actuarial Neural Additive Model** (ANAM): an interpretable deep learning
4
+ model for general insurance pricing, written in PyTorch.
5
+
6
+ ```python
7
+ from actuarialnam import ANAM # pip install actuarialnam
8
+ ```
9
+
10
+ Like a GAM, the model adds up one shape function per rating factor, plus a few
11
+ selected pairwise interactions:
12
+
13
+ ```
14
+ g(mu) = bias + f_1(x_1) + ... + f_p(x_p) + sum of selected f_jk(x_j, x_k) + offset
15
+ ```
16
+
17
+ Each shape function is a small neural network, so it learns its shape from the
18
+ data, yet every term can still be plotted and read on its own. On top of this
19
+ additive structure the package adds the requirements of a pricing model:
20
+
21
+ - **Sparsity:** a three-stage selection procedure keeps only the material main
22
+ effects and interactions.
23
+ - **Smoothness:** a roughness penalty keeps continuous effects from overfitting.
24
+ - **Monotonicity:** a factor such as a bonus-malus score can be forced to only
25
+ increase (or decrease), using lattice subnetworks that guarantee it everywhere,
26
+ not just on the training data.
27
+ - **Exposure:** an offset column, exactly as in a Poisson GLM.
28
+
29
+ It supports Poisson, gamma and Gaussian responses. This is the reference
30
+ implementation of the model in
31
+ [Laub, Pho & Wong (2026)](https://doi.org/10.1016/j.insmatheco.2026.103270).
32
+
33
+ ## Install
34
+
35
+ ```bash
36
+ pip install actuarialnam
37
+ ```
38
+
39
+ or, for the development version:
40
+
41
+ ```bash
42
+ pip install "actuarialnam @ git+https://github.com/Pat-Laub/anam.git"
43
+ ```
44
+
45
+ It needs Python 3.12 or newer.
46
+
47
+ ## Quickstart
48
+
49
+ The model takes the raw data frame. Continuous and categorical columns are read
50
+ from the dtypes, and the standardising and encoding are stored in the model, so
51
+ `predict` and the plots work in the original units.
52
+
53
+ ```python
54
+ from actuarialnam import ANAM
55
+
56
+ model = ANAM(
57
+ distribution="poisson", # log link and Poisson loss
58
+ exposure="expo", # log(expo) is added as an offset
59
+ monotone={"bm": "increasing"}, # bonus-malus effect can only increase
60
+ smoothness=1e-3, # roughness penalty on continuous effects
61
+ )
62
+
63
+ # By default, fit() selects the main effects and interactions, then fits them.
64
+ model.fit(X_train, y_train, X_val=X_val, y_val=y_val,
65
+ epochs=300, batch_size=5000, lr=0.01, patience=40, random_state=2000)
66
+
67
+ model.features_, model.interactions_ # the selected terms
68
+ model.predict(X_test) # expected claim counts, exposure included
69
+ ```
70
+
71
+ ## Choosing the terms yourself
72
+
73
+ Rather than trusting the automatic cutoffs, you can look at the rankings and then
74
+ decide:
75
+
76
+ ```python
77
+ model.rank_main_effects(X_train, y_train, X_val=X_val, y_val=y_val, random_state=2000)
78
+ model.plot_main_effect_importance()
79
+
80
+ model.rank_interactions(X_train, y_train, X_val=X_val, y_val=y_val,
81
+ n_main=7, random_state=2000)
82
+ model.plot_interaction_importance()
83
+
84
+ model.choose(n_main=7, n_interactions=6)
85
+ model.fit(X_train, y_train, X_val=X_val, y_val=y_val,
86
+ epochs=300, batch_size=5000, lr=0.01, patience=40, random_state=2000)
87
+ ```
88
+
89
+ - `rank_main_effects` trains an ensemble of small main-effect models and scores
90
+ each factor by how much its shape function varies over the data.
91
+ - `rank_interactions` scores each pair of kept main effects by how much it
92
+ improves the validation loss.
93
+ - `choose` fixes the cutoffs, and `fit` then trains the chosen terms with the
94
+ smoothness and monotonicity constraints.
95
+
96
+ ## Reading the model
97
+
98
+ Each shape function is a contribution to the linear predictor. Under a log link,
99
+ `exp(f_j)` is a multiplier on the expected claim count.
100
+
101
+ ```python
102
+ model.plot_effects(["ageph", "bm", "power"]) # several main effects
103
+ model.plot_effect("fuel") # a categorical factor, one bar per level
104
+ model.plot_effect("long", "lat") # an interaction surface
105
+ grid, values = model.effect("bm") # the numbers behind a plot
106
+ model.predict_components(X_test) # each term's contribution, per policy
107
+ model.importance() # variance of each term over the data
108
+ ```
109
+
110
+ Saving and loading keeps the preprocessing:
111
+
112
+ ```python
113
+ model.save("model.pt")
114
+ model = ANAM.load("model.pt")
115
+ ```
116
+
117
+ Categories not seen in training get zero effect.
118
+
119
+ ## Development
120
+
121
+ ```bash
122
+ git clone https://github.com/Pat-Laub/anam.git
123
+ cd anam
124
+ uv run pytest
125
+ ```
126
+
127
+ ## Citation
128
+
129
+ ```bibtex
130
+ @article{laub2026anam,
131
+ title = {An interpretable deep learning model for general insurance pricing},
132
+ author = {Laub, Patrick J. and Pho, Tu and Wong, Bernard},
133
+ journal = {Insurance: Mathematics and Economics},
134
+ year = {2026},
135
+ pages = {103270},
136
+ doi = {10.1016/j.insmatheco.2026.103270},
137
+ }
138
+ ```
139
+
140
+ ## License
141
+
142
+ MIT
@@ -0,0 +1,40 @@
1
+ [project]
2
+ name = "actuarialnam"
3
+ version = "0.3.0"
4
+ description = "Actuarial Neural Additive Model: an interpretable deep learning model for insurance pricing"
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE"]
8
+ authors = [{ name = "Patrick J. Laub" }]
9
+ keywords = ["actuarial", "insurance pricing", "neural additive model", "interpretability", "pytorch"]
10
+ classifiers = [
11
+ "Programming Language :: Python :: 3",
12
+ "Operating System :: OS Independent",
13
+ "Intended Audience :: Science/Research",
14
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
15
+ ]
16
+ requires-python = ">=3.12"
17
+ dependencies = [
18
+ "torch>=2.0",
19
+ "numpy>=1.24",
20
+ "pandas>=2.0",
21
+ "matplotlib>=3.7",
22
+ "scikit-learn>=1.3",
23
+ ]
24
+
25
+ [project.urls]
26
+ Repository = "https://github.com/Pat-Laub/anam"
27
+ Paper = "https://doi.org/10.1016/j.insmatheco.2026.103270"
28
+
29
+ [dependency-groups]
30
+ dev = ["pytest>=8"]
31
+
32
+ [build-system]
33
+ requires = ["hatchling"]
34
+ build-backend = "hatchling.build"
35
+
36
+ [tool.hatch.build.targets.wheel]
37
+ packages = ["src/actuarialnam"]
38
+
39
+ [tool.hatch.build.targets.sdist]
40
+ only-include = ["src", "tests", "README.md", "LICENSE", "pyproject.toml"]
@@ -0,0 +1,110 @@
1
+ """
2
+ ANAM - Actuarial Neural Additive Model
3
+
4
+ A PyTorch implementation of Neural Additive Models with extensions for
5
+ actuarial applications including monotonicity constraints, smoothness
6
+ penalties, and interaction effects.
7
+
8
+ Quickstart::
9
+
10
+ from actuarialnam import ANAM
11
+ model = ANAM(distribution="poisson", exposure="expo",
12
+ monotone={"bm": "increasing"}, smoothness=1e-3)
13
+ model.fit(train, train["nclaims"], X_val=val, y_val=val["nclaims"])
14
+ model.plot_effects(["ageph", "bm", "power"]); model.predict(val)
15
+
16
+ The headline export ``ANAM`` is the user-facing wrapper: it owns preprocessing,
17
+ maps ``distribution`` to a loss + link, treats ``exposure`` as an offset column,
18
+ runs variable selection by default (``features="auto"``/``interactions="auto"``),
19
+ and plots shape functions in original units. Power users can build the raw
20
+ engine directly via ``ANAMModule`` and the lower-level helpers, all still
21
+ exported below.
22
+ """
23
+
24
+ from actuarialnam.model import ANAMModule
25
+
26
+ # The headline export ``ANAM`` is the user-facing wrapper (api.py). Power users
27
+ # who want the raw nn.Module engine can use ``ANAMModule``.
28
+ from actuarialnam.api import ANAM
29
+ from actuarialnam.losses import (
30
+ gamma_nll,
31
+ poisson_nll,
32
+ gaussian_nll,
33
+ smoothness_penalty,
34
+ marginal_clarity_penalty,
35
+ DISTRIBUTIONS,
36
+ resolve_distribution,
37
+ )
38
+ from actuarialnam.subnet import Subnet, EmbeddingSubnet
39
+ from actuarialnam.lattice import PWLCalibration, Lattice, Lattice1D, MonotonicSubnet
40
+ from actuarialnam.optimizer import train_anam, TrainingConfig, prepare_data
41
+ from actuarialnam.utils import (
42
+ generate_synthetic_data,
43
+ build_feature_configs,
44
+ remove_outliers_iqr,
45
+ train_val_test_split,
46
+ plot_shape_function,
47
+ plot_interaction_heatmap,
48
+ plot_variable_importance,
49
+ )
50
+ from actuarialnam.sparsity import (
51
+ SparsityConfig,
52
+ SelectionResult,
53
+ train_ensemble_main_effects,
54
+ compute_ensemble_importance,
55
+ select_main_effects,
56
+ train_main_effects_model,
57
+ evaluate_candidate_interactions,
58
+ select_interactions,
59
+ run_variable_selection,
60
+ plot_main_effect_importance,
61
+ plot_interaction_selection,
62
+ _find_elbow,
63
+ )
64
+
65
+ __version__ = "0.3.0"
66
+ __all__ = [
67
+ # Model
68
+ "ANAM",
69
+ "ANAMModule",
70
+ # Subnets
71
+ "Subnet",
72
+ "EmbeddingSubnet",
73
+ # Lattice
74
+ "PWLCalibration",
75
+ "Lattice",
76
+ "Lattice1D",
77
+ "MonotonicSubnet",
78
+ # Losses
79
+ "gamma_nll",
80
+ "poisson_nll",
81
+ "gaussian_nll",
82
+ "smoothness_penalty",
83
+ "marginal_clarity_penalty",
84
+ "DISTRIBUTIONS",
85
+ "resolve_distribution",
86
+ # Training
87
+ "train_anam",
88
+ "TrainingConfig",
89
+ "prepare_data",
90
+ # Sparsity / Variable Selection
91
+ "SparsityConfig",
92
+ "SelectionResult",
93
+ "train_ensemble_main_effects",
94
+ "compute_ensemble_importance",
95
+ "select_main_effects",
96
+ "train_main_effects_model",
97
+ "evaluate_candidate_interactions",
98
+ "select_interactions",
99
+ "run_variable_selection",
100
+ "plot_main_effect_importance",
101
+ "plot_interaction_selection",
102
+ # Utilities
103
+ "generate_synthetic_data",
104
+ "build_feature_configs",
105
+ "remove_outliers_iqr",
106
+ "train_val_test_split",
107
+ "plot_shape_function",
108
+ "plot_interaction_heatmap",
109
+ "plot_variable_importance",
110
+ ]