Triangle class

class bermuda.triangle.Triangle(cells: Sequence[Cell])

The main triangle class.

The Triangle class inherits from collections.abc.Set. It holds a sequence of Cell objects, which are automatically sorted on class instance creation.

The Triangle class has a number of methods, properties and static methods for triangle manipulation, visualization, and I/O. Consult the documentation for more details.

cells

the sequence of Cell objects.

clip(*, min_eval: date | None = None, max_eval: date | None = None, min_period: date | None = None, max_period: date | None = None, min_dev: timedelta | int | float | None = None, max_dev: timedelta | int | float | None = None, dev_lag_unit: str = 'month') → Triangle

Create a new Triangle that has been clipped/censored as specified.

Parameters:
  • min_eval – Minimum evaluation date to permit.

  • max_eval – Maximum evaluation date to permit.

  • min_period – Minimum period start date to permit.

  • max_period – Maximum period end date to permit.

  • min_dev – Minimum development lag to permit.

  • max_dev – Maximum development lag to permit.

  • dev_lag_unit – Unit of min_dev or max_dev, required if either is specified.

property common_metadata: Metadata

A single metadata element representing the metadata attributes common to all Cells in the Triangle.

derive_fields(**definitions: Callable[[Cell], Any] | Any) → Triangle

Create a new Triangle with additional derived fields defined.

Parameters:

**definitions – New field definitions. Argument names are the names of the new fields to be defined, values are either constants or functions that accept cells and return field values. Later definitions can depend on quantities computed via earlier definitions.

derive_metadata(**definitions: Callable[[Cell], Any] | Any) → Triangle

Create a new Triangle with additional metadata attributes or fields derived.

Parameters:

**definitions – Values are plain values or functions that accept a Cell and return a value. If keys are top-level attributes in metadata, their values will replace the existing values of the top-level attributes. Otherwise, the key-value pair will be added to the details field of the metadata.

dev_lags(unit='month') → list[float | int | timedelta]

A sorted list of unique development lags. unit can be, ‘month’, ‘day’ or ‘timedelta’. See docs for Observation.dev_lag() or Prediction.dev_lag() for more information.

property eval_date_resolution: int

The resolution of the evaluation dates in the triangle.

property evaluation_date: date

The latest evaluation date in the triangle.

property evaluation_dates: list[date]

A sorted list of unique evaluation dates.

property experience_gaps: list[tuple[date, date]]

A sorted list of missing experience period ranges from the triangle

extract(attribute: Callable[[Cell], Any] | str) → ndarray

Extract cell attributes either via a function or cell field name, returning a numpy array of values.

Parameters:

attribute – A function applied to all cells or a cell field.

property field_cell_counts: dict[str, int]

For each triangle field return the number of cells the field appears in.

property field_slice_counts: dict[str, int]

For each triangle field return the number of slices the field appears in.

property fields: list[str]

A sorted list of unique fields in the triangle set.

filter(predicate: Callable[[Cell], bool]) → Triangle

Create a new Triangle with only the cells that satisfy the predicate.

Parameters:

predicate – A function that accepts a Cell and returns a boolean.

property has_consistent_currency: bool

Does every cell in the triangle use the same currency?

property has_consistent_risk_basis: bool

Does every cell in the triangle use the same risk basis?

property has_consistent_values_shapes: bool

Does every field have the same shape in each cell?

property is_disjoint: bool

Is the triangle disjoint?

A triangle is disjoint if there is no overlap between any pair of experience periods.

property is_empty: bool

Is the triangle empty?

property is_incremental: bool

Is the traingle incremental?

property is_multi_slice: bool

Does this triangle have multiple slices?

is_regular(dev_lag_unit='month') → bool

Is the triangle regular?

A triangle is regular if it is semi-regular and the interval between consecutive development lags is always a constant.

property is_right_edge_ragged: bool

Returns True if any slice’s right edge encompasses different evaluation dates

is_semi_regular(dev_lag_unit='month') → bool

Is the triangle semi-regular?

A triangle is semi-regular if it is disjoint and every experience period has the same duration.

property is_slicewise_disjoint: bool

Returns True if each triangle slice is disjoint.

property metadata: list[Metadata]

A sorted list of all unique metadata in the Triangle.

property metadata_differences: list[Metadata]

A list of all unique metadata in the Triangle, with metadata attributes that are common to all Cells in the Triangle removed.

property num_samples: int

The number of samples in a Triangle of stochastic predictions. If the triangle has only observed data, num_samples is 1. If different cells or different fields have different values of num_samples, an error is raised.

property period_resolution: int

The resolution of the periods in the triangle.

property period_rows

Group by experience period and return an iterator over ‘rows’.

property periods: list[tuple[date, date]]

A sorted list of unique experience periods (as 2-tuples of start and end dates).

remove_static_details() → Triangle

Removes any metadata details and loss_details that are common to all cells in the triangle.

replace(**definitions: Callable[[Cell], Any] | Any) → Triangle

Replace cell member variables with a constant or via a function.

Parameters:

**definitions –

New variable definitions, which must be one of period_start, period_end, evaluation_date or values. Definition values can either be constants or functions of the cell itself. For example,

triangle.replace(values=dict(reported_loss=1) triangle.replace(values=lambda cell: dict(reported_loss = cell[“paid_loss”]))

property right_edge

Create a Triangle with only the most recent observation for each period within each triangle slice.

Note that right_edge doesn’t guarantee that every cell will have the same evaluation date!

select(keys: list[str]) → Triangle

Retain only the designated keys in values.

property slice_period_rows

Group by slice and experience period and return an iterator over ‘slice-rows’.

to_data_frame(dev_lag_unit='month')

Convert a triangle set to a Pandas dataframe.

class bermuda.triangle.TriangleSlice(cells: Sequence[Cell])