Skip to main content

Model-agnostic Probabilistic Machine Learning Reserving

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlreserving-0.3.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlreserving-0.3.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file mlreserving-0.3.1.tar.gz.

File metadata

  • Download URL: mlreserving-0.3.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mlreserving-0.3.1.tar.gz
Algorithm Hash digest
SHA256 f70e3e030469e4964cc5692201b66acc73a76567931e7bb37857f45e65e904ee
MD5 0679a6f4e91504bf8a027f456a5c9c51
BLAKE2b-256 0d15c22c6f990d6847e708d50f9b7c3f67692b6450f9dc277753b5205c5150bd

See more details on using hashes here.

File details

Details for the file mlreserving-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: mlreserving-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mlreserving-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6103912cdc6443a2a74158b261047d890522b834c9b05dd17ee4b3c7a47f9c21
MD5 96342876c49f910f556de0524e2710a6
BLAKE2b-256 14af763f5aba2c631cf217cdc51355f73801e77cab63fccc728675c16629bcc3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page