io
functions¶
Bermuda’s I/O functionality.
Binary files¶
- bermuda.io.binary_input.binary_to_triangle()¶
Read a Bermuda triangle from Bermuda’s binary file format.
- Parameters:
filename – The filename to read the triangle from.
compress – Whether the source file is compressed. If not provided explicitly, compression will be inferred from the file extension.
- bermuda.io.binary_output.triangle_to_binary(triangle: Triangle, filename: str, compress: bool = False) None ¶
Convert a Bermuda triangle to a binary file format.
- Parameters:
triangle – The Bermuda triangle to save out.
filename – The filename to save the triangle to.
compress – Whether to compress the file.
DataFrame
objects¶
- bermuda.io.data_frame_input.long_csv_to_triangle()¶
Convert a long-format CSV to a Triangle.
For more details on long-format CSVs/tables, see long_data_frame_to_triangle (which this function is a thin wrapper for).
- Parameters:
file_or_fname – Name of the CSV file or a handle to a file object.
detail_cols – Names of the columns that should be interpreted as metadata details. At least one of field_cols or detail_cols must be supplied.
loss_detail_cols – Names of the metadata columns that should be interpreted as metadata loss details, rather than standard metdata details. By default, nothing is assumed to be a loss detail.
**kwargs – Extra arguments to pd.read_csv
- bermuda.io.data_frame_input.long_data_frame_to_triangle()¶
Convert a long-format pd.DataFrame to a Triangle.
A long-format DataFrame requires the following columns to be present: period_start, period_end, evaluation_date, field and value. When the column ‘prev_evaluation_date’ is present, a triangle with incremental cells is generated. Otherwise, all triangle cells are cumulative.
Each row of the DataFrame represents a field of a single Cell of the Triangle. Any columns besides the three required columns are interpreted as metadata (i.e., per_occurrence_limit, currency, program_name).
The metadata argument can be used to supply common metadata that may not be directly present in the DataFrame. For example, every Cell in the Triangle could be accident-basis, gross of reinsurance, including DCC. This could be supplied by a single Metadata object in the function call instead of adding several constant columns to the DataFrame.
- Parameters:
df – Pandas DataFrame to convert to a Triangle.
loss_detail_cols – List of columns that belong to loss_details in Metadata, rather than details.
metadata – Default metadata to apply to every cell in the Triangle.
- bermuda.io.data_frame_input.wide_csv_to_triangle()¶
Convert a wide-format CSV to a Triangle.
For more details on wide-format CSVs/tables, see wide_data_frame_to_triangle (which this function is a thin wrapper for).
- Parameters:
file_or_fname – Name of the CSV file or a handle to a file object.
field_cols – Names of the columns that should be interpreted as fields.
detail_cols – Names of the columns that should be interpreted as metadata details. At least one of field_cols or detail_cols must be supplied.
loss_detail_cols – Names of the detail columns that should be interpreted as metadata loss details, rather than standard metadata details. By default, nothing is assumed to be a loss detail.
metadata – Default metadata to apply to every cell in the Triangle (can be overridden by detail_cols).
collapse_fields – A list of fields to collapse to a single value, rather than loading as an array with multiple values. This typically applies to earned_premium when multiple scenarios are being loaded.
**kwargs – Extra arguments to pd.read_csv
- bermuda.io.data_frame_input.wide_data_frame_to_triangle()¶
Convert a wide-format pd.DataFrame to a Triangle.
A wide-format DataFrame requires the following columns to be present: period_start, period_end, and evaluation_date. When the column ‘prev_evaluation_date’ is present, a triangle with incremental cells is generated. Otherwise, all triangle cells are cumulative.
Each row of the DataFrame represents a single Cell of the Triangle. Any columns besides the three required columns are interpreted as either fields (i.e., paid_loss, reported_loss) or metadata (i.e., per_occurrence_limit, currency, program_name).
This function doesn’t make any assumptions about which columns are fields and which are metadata based on their names. Instead, at least one of detail_cols or field_cols must be supplied to the function. If only one of those arguments is supplied, all other columns are assumed to belong to the other argument.
The metadata argument can be used to supply common metadata that may not be directly present in the DataFrame. For example, every Cell in the Triangle could be accident-basis, gross of reinsurance, including DCC. This could be supplied by a single Metadata object in the function call instead of adding several constant columns to the DataFrame.
- Parameters:
df – Pandas DataFrame to convert to a Triangle.
field_cols – Names of the columns that should be interpreted as fields.
detail_cols – Names of the columns that should be interpreted as metadata details. At least one of field_cols or detail_cols must be supplied.
loss_detail_cols – Names of the detail columns that should be interpreted as metadata loss details, rather than standard metadata details. By default, nothing is assumed to be a loss detail.
metadata – Default metadata to apply to every cell in the Triangle (can be overridden by detail_cols).
collapse_fields – A list of fields to collapse to a single value, rather than loading as an array with multiple values. This typically applies to earned_premium when multiple scenarios are being loaded.
Python ChainLadder
triangles¶
- bermuda.io.chain_ladder.chain_ladder_to_triangle()¶
Create a Triangle from a chainladder Triangle object.
Anything in the chainladder index will become details in the Bermuda Triangle.
- bermuda.io.chain_ladder.triangle_to_chain_ladder(triangle: Triangle) chainladder.core.triangle.Triangle ¶
Convert a Triangle to a chainladder Triangle object.
- Parameters:
triangle (Triangle) – The Bermuda Triangle to convert.
- Returns:
The chainladder Triangle object.
- Return type:
chainladder.core.triangle.Triangle
JSON files¶
- bermuda.io.json.triangle_to_json()¶
Convert a triangle to JSON.
- Parameters:
tri – A triangle to convert to a JSON representation.
file_or_fname – A filename, a file handle, or None.
- Returns:
If file_or_fname is None, then a string with the JSON representation of the triangle is returned. Otherwise, the JSON representation is written to the provided file and None is returned.
Matrix
types¶
- bermuda.io.matrix.triangle_to_matrix(tri: Triangle, eval_resolution: int | None = None, fields: list[str] | None = None) Matrix ¶
Convert a triangle to a matrix format.
The matrix format is well-suited for clean data with consistent experience period lengths and evaluation date deltas. If a triangle is not regular or if it has Predictions, it cannot be converted to a matrix.