Skip to main content

MLReserving

A machine learning-based probabilistic reserving model for (longitudinal data) insurance claims.

PyPI PyPI - License Downloads Documentation

Installation

pip install mlreserving

Usage

"""
Simple example using the RAA dataset with MLReserving
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt 
from mlreserving import MLReserving
from sklearn.ensemble import ExtraTreesRegressor, RandomForestRegressor
from sklearn.linear_model import RidgeCV

# Load the dataset
url = "https://raw.githubusercontent.com/Techtonique/datasets/refs/heads/main/tabular/triangle/raa.csv"
df = pd.read_csv(url)

print(df.head())
print(df.tail())


models = [RidgeCV(), ExtraTreesRegressor(), RandomForestRegressor()]

# Try both factor and non-factor approaches
for use_factors in [False, True]:
    print(f"\n{'='*50}")
    print(f"Using {'factors' if use_factors else 'log transformations'}")
    print(f"{'='*50}\n")
    
    for mdl in models: 
        # Initialize the model with prediction intervals
        model = MLReserving(model=mdl,
            level=80,  # 80% confidence level
            use_factors=use_factors,  # Use categorical encoding
            random_state=42
        )        

        # Fit the model
        model.fit(df, origin_col="origin", development_col="development", value_col="values")

        # Make predictions with intervals
        result = model.predict()
        ibnr = model.get_ibnr()

        print("\nMean predictions:")
        print(result.mean)
        print("\nIBNR per origin year (mean):")
        print(ibnr.mean)

        print("\nLower bound (95%):")
        print(result.lower)
        print("\nIBNR per origin year (lower):")
        print(ibnr.lower)

        print("\nUpper bound (95%):")
        print(result.upper)
        print("\nIBNR per origin year (upper):")
        print(ibnr.upper)

        print("\n Summary:")
        print(model.get_summary())

        # Display results
        print("\nMean predictions:")
        result.mean.plot()
        plt.title(f'Mean Predictions - {mdl.__class__.__name__} ({use_factors and "Factors" or "Log"})')
        plt.show()

        print("\nLower bound (95%):")
        result.lower.plot()
        plt.title(f'Lower Bound - {mdl.__class__.__name__} ({use_factors and "Factors" or "Log"})')
        plt.show()

        print("\nUpper bound (95%):")
        result.upper.plot()
        plt.title(f'Upper Bound - {mdl.__class__.__name__} ({use_factors and "Factors" or "Log"})')
        plt.show()

        # Plot IBNR
        plt.figure(figsize=(10, 6))
        plt.plot(ibnr.mean.index, ibnr.mean.values, 'b-', label='Mean IBNR')
        plt.fill_between(ibnr.mean.index, 
                         ibnr.lower.values, 
                         ibnr.upper.values, 
                         alpha=0.2, 
                         label='95% Confidence Interval')
        plt.title(f'IBNR per Origin Year - {mdl.__class__.__name__} ({use_factors and "Factors" or "Log"})')
        plt.xlabel('Origin Year')
        plt.ylabel('IBNR')
        plt.legend()
        plt.grid(True)
        plt.show()

Features

  • Machine learning based reserving model
  • Support for prediction intervals
  • Flexible model selection
  • Handles both continuous and categorical features

License

BSD Clause Clear License

Metadata

Release files for mlreserving 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mlreserving 0.5.2
File Size Uploaded
mlreserving-0.5.2.tar.gz 17.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mlreserving 0.5.2
File Interpreter ABI Platform
mlreserving-0.5.2-py3-none-any.whl Python 3 none any Details

Total release size: 33.2 kB

Release files / mlreserving-0.5.2.tar.gz

Download URL mlreserving-0.5.2.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9370d23aea8e9e0e6d56a5146ef59c935f174e570fd1fabb1de4f22f0681bfd9
BLAKE2b-256 checksum
How to use checksums
fbfff73ec18e0a93f4a8e1787b16f7b85952f91701e33f5f2425471d8a747161
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / mlreserving-0.5.2-py3-none-any.whl

Download URL mlreserving-0.5.2-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93194aa234ad02bbc72cfc1272b2bb33923b0a5519b64348cc1fdf060bb27439
BLAKE2b-256 checksum
How to use checksums
f11706a9fa60eac4dbb70d6c257095aba449cd96db5ed0e49c0577493d939012
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

0.5.2 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page