Skip to main content

HMM-based driving state classification and GLM-compatible risk scoring from raw telematics trip data

Project description

insurance-telematics

Raw telematics trip data to GLM-ready risk scores. Built for UK motor insurance pricing teams.

Most telematics scoring tools are either black-box APIs (you get a number, you cannot audit it) or one-off academic scripts that do not run on your data. This library gives you the full pipeline in Python: load 1Hz GPS/accelerometer data, classify driving behaviour using a Hidden Markov Model, aggregate to driver-level risk scores, and produce a feature DataFrame you can drop into your Poisson frequency GLM alongside traditional rating factors.

The academic basis is Jiang & Shi (2024) in NAAJ: HMM latent states capture driving regimes (cautious, normal, aggressive) and the fraction of time in the aggressive state is more predictive of claim frequency than raw speed or harsh event counts alone.

Five-line usage

from insurance_telematics import TripSimulator, TelematicsScoringPipeline

sim = TripSimulator(seed=42)
trips_df, claims_df = sim.simulate(n_drivers=100, trips_per_driver=50)

pipe = TelematicsScoringPipeline(n_hmm_states=3)
pipe.fit(trips_df, claims_df)
predictions = pipe.predict(trips_df)

No raw telematics data? Use TripSimulator to generate a realistic synthetic fleet — three driving regimes (cautious, normal, aggressive), Ornstein-Uhlenbeck speed processes, synthetic Poisson claims — so you can prototype the full workflow before your data is available.

What it does

Raw 1Hz trip data (CSV or Parquet)
  → clean_trips()         — GPS jump removal, acceleration derivation, road type
  → extract_trip_features()  — harsh braking rate, speeding fraction, night fraction, etc.
  → DrivingStateHMM       — classify each trip into a latent driving state
  → aggregate_to_driver() — Bühlmann-Straub credibility weighting to driver level
  → TelematicsScoringPipeline — Poisson GLM producing predicted claim frequency

Installation

pip install insurance-telematics

Requires Python 3.10+. Dependencies: polars, numpy, scipy, hmmlearn, statsmodels, scikit-learn.

Running the full pipeline on your data

from insurance_telematics import load_trips, clean_trips, extract_trip_features
from insurance_telematics import DrivingStateHMM, aggregate_to_driver

# Load from CSV (or Parquet)
trips_raw = load_trips("trips.csv")

# Clean: removes GPS jumps, derives acceleration and jerk, classifies road type
trips_clean = clean_trips(trips_raw)

# Extract trip-level features
features = extract_trip_features(trips_clean)

# Fit HMM and get driver-level state features
model = DrivingStateHMM(n_states=3)
model.fit(features)
states = model.predict_states(features)
driver_hmm_features = model.driver_state_features(features, states)

# Aggregate to driver level with credibility weighting
driver_risk = aggregate_to_driver(features, credibility_threshold=30)

Input data format

The library expects one row per second (1Hz) with these columns:

Column Type Notes
trip_id string Unique per trip
timestamp datetime ISO 8601 or Unix epoch
latitude float Decimal degrees
longitude float Decimal degrees
speed_kmh float GPS speed
acceleration_ms2 float Optional — derived from speed if absent
heading_deg float Optional — used for cornering estimation
driver_id string Optional — "unknown" if absent

Use the schema parameter to rename non-standard columns:

trips = load_trips("raw_data.csv", schema={"gps_speed": "speed_kmh"})

Features extracted per trip

  • harsh_braking_rate — events/km where deceleration < -3.5 m/s²
  • harsh_accel_rate — events/km where acceleration > +3.5 m/s²
  • harsh_cornering_rate — events/km (estimated from heading-change rate)
  • speeding_fraction — fraction of time exceeding road-type speed limit
  • night_driving_fraction — fraction of distance driven 23:00-05:00
  • urban_fraction — fraction of distance at speeds < 50 km/h
  • mean_speed_kmh, p95_speed_kmh, speed_variation_coeff

HMM state classification

from insurance_telematics import DrivingStateHMM, ContinuousTimeHMM

# Discrete-time (uniform 1Hz intervals) — wraps hmmlearn.GaussianHMM
hmm = DrivingStateHMM(n_states=3)
hmm.fit(trip_features_df)
states = hmm.predict_states(trip_features_df)

# Continuous-time — handles variable trip lengths via expm(Q * dt)
cthmm = ContinuousTimeHMM(n_states=3)
cthmm.fit(trip_features_df, time_deltas=time_intervals_minutes)

With three states the HMM typically produces:

  • State 0: cautious — low speed, low variance, urban driving
  • State 1: normal — mixed road types, moderate speed
  • State 2: aggressive — high speed variance, high harsh event rate

The fraction of time in state 2 per driver is the key GLM covariate. Following Jiang & Shi (2024), this outperforms raw feature averages as a predictor of claim frequency.

Composite risk score

aggregate_to_driver() produces a composite_risk_score (0-100) as a weighted combination of all features, scaled to the portfolio range. This is a summary diagnostic — use the individual features as GLM covariates for pricing, not the composite score directly.

Key papers

  • Jiang, Q. & Shi, Y. (2024). "Auto Insurance Pricing Using Telematics Data: Application of a Hidden Markov Model." NAAJ 28(4), pp.822-839.
  • Wüthrich, M.V. (2017). "Covariate Selection from Telematics Car Driving Data." European Actuarial Journal 7, pp.89-108.
  • Gao, G., Wang, H. & Wüthrich, M.V. (2021). "Boosting Poisson Regression Models with Telematics Car Driving Data." Machine Learning 111, pp.1787-1827.
  • Henckaerts, R. & Antonio, K. (2022). "The Added Value of Dynamically Updating Motor Insurance Prices with Telematics Data." Insurance: Mathematics and Economics 103, pp.79-95.
  • Guillen, M., Pérez-Marín, A.M. & Nielsen, J.P. (2024). "Pricing Weekly Motor Insurance Drivers with Behavioural and Contextual Telematics Data." Heliyon 10(17).

Databricks notebook demo

See notebooks/telematics_demo.py for a complete walkthrough on a synthetic fleet of 100 drivers — including HMM fitting, state feature extraction, and Poisson GLM training — designed to run on Databricks serverless compute.

Licence

MIT

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

insurance_telematics-0.1.0.tar.gz (32.3 kB view details)

Uploaded Source

Built Distribution

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

insurance_telematics-0.1.0-py3-none-any.whl (28.6 kB view details)

Uploaded Python 3

File details

Details for the file insurance_telematics-0.1.0.tar.gz.

File metadata

  • Download URL: insurance_telematics-0.1.0.tar.gz
  • Upload date:
  • Size: 32.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for insurance_telematics-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8054f0562684913bc211e15e406f7f05678d309a31c32353e014d9e244f25668
MD5 460ea061e4f69f72e8a8359b99e027d3
BLAKE2b-256 c797cc7738a8c277b56308dccb60d1794bafc38039443369b5e891ed24eb8544

See more details on using hashes here.

File details

Details for the file insurance_telematics-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: insurance_telematics-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 28.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for insurance_telematics-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be2a68320237acd8f10d5b717c2a0353727d02884ae18f7d5676a9939963e132
MD5 67fe7280332e5f2712528f40e51714e9
BLAKE2b-256 e0235b86493a04b8ff3b2d41cf1558b12e199f88ff23a78a78ef470e625f50ab

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