Models

Core data structures for building expert scorecards. ExpertScorecard is natively sklearn-compatible.

Scorecard

class risk_kit.expert_scorecard.models.ExpertScorecard(features, validation_registry=None)[source]

Bases: BaseEstimator, RegressorMixin

__init__(features, validation_registry=None)[source]
validate()[source]
property feature_names_: list[str]

Sklearn-compatible feature names property.

get_params(deep=True)[source]

Get parameters for this estimator.

fit(X, y=None, **fit_params)[source]

Fit is a no-op for expert scorecards - they are pre-trained.

predict(X)[source]

Calculate risk scores for input data (sklearn-compatible).

predict_single(X)[source]

Score a single record (original interface for backward compatibility).

score(X, y)[source]

Return the predicted scores (sklearn-compatible).

get_table_data()[source]
get_score_range()[source]

Features

class risk_kit.expert_scorecard.models.BaseFeature(*, name, family, description, buckets, weight, default_score=0.0)[source]

Bases: BaseModel, ABC

Base class for all feature types.

name: str
family: str
description: str
buckets: list[NumericBucket] | list[ObjectBucket]
weight: float
default_score: float
get_score(value)[source]

Get the score for a given value.

get_score_range()[source]

Get the range of scores for a given feature.

order_buckets()[source]
has_overlapping_buckets()[source]

Check if any buckets in this feature overlap with each other

get_overlapping_buckets()[source]

Get all pairs of overlapping buckets in this feature

get_feature_rows()[source]

Get table rows for visualization purposes.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class risk_kit.expert_scorecard.models.NumericFeature(*, name, family, description, buckets, weight, default_score=0.0)[source]

Bases: BaseFeature

Feature for numeric values using NumericBucket instances.

buckets: list[NumericBucket]
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class risk_kit.expert_scorecard.models.ObjectFeature(*, name, family, description, buckets, weight, default_score=0.0)[source]

Bases: BaseFeature

Feature for categorical/string values using ObjectBucket instances.

buckets: list[ObjectBucket]
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

Buckets

class risk_kit.expert_scorecard.models.Bucket(*, definition, score)[source]

Bases: BaseModel, ABC, Generic[T]

Abstract base class for all bucket types. bucket types can be whatever you want them to be. We have two concrete implementations: NumericBucket and ObjectBucket. Subclasses must implement get_score get_sort_key and overlaps_with methods. TODO: Can we do away with sort keys and display_definition or make them nicer/easier to work with since it is only a visual thing? TODO: think about whether an ABC is the best way to go here. Do we need BaseModel?

definition: BucketDefinitionType
score: float
abstractmethod contains(value)[source]

Check if this bucket contains the given value

abstractmethod get_sort_key()[source]

Generate a sort key for natural ordering of buckets

abstractmethod overlaps_with(other)[source]

Check if this bucket overlaps with another bucket of the same type

abstractmethod display_definition()[source]

Return a human-readable string representation of the bucket definition

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class risk_kit.expert_scorecard.models.NumericBucket(*, definition, score, left_inclusive=True, right_inclusive=False)[source]

Bases: Bucket[NumericBucket]

Implementation of a Bucket class for numeric values. Numerical buckets are made up of ranges or exact values. Usually exact values are used for “special cases” such as a number than has a special meaning like 99999 or 0. Ranges are used for “normal cases” such as 18-25, 26-65, 66-100.

definition: NumericDefinitionType
left_inclusive: bool
right_inclusive: bool
contains(value)[source]

Check if this bucket contains the given value

get_sort_key()[source]

Sort ranges first by lowest score, then exact values

overlaps_with(other)[source]

Check if this numeric bucket overlaps with another numeric bucket

display_definition()[source]

Return a human-readable string representation of the bucket definition

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class risk_kit.expert_scorecard.models.ObjectBucket(*, definition, score)[source]

Bases: Bucket[ObjectBucket]

Bucket for string/categorical values. Supports single strings or lists of strings.

definition: ObjectDefinitionType
contains(value)[source]

Check if this bucket contains the given value

get_sort_key()[source]

Sort lists before strings

overlaps_with(other)[source]

Check if this object bucket overlaps with another object bucket

display_definition()[source]

Return a human-readable string representation of the bucket definition

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].