Code
import re
from aggregate import build, qd, __version__
from style import wprint, GT, figures
figures()
print(__version__)1.43.1
Display labels on statements, layers, reinsurance, expenses, and premium with the DecL as clause
Stephen J. Mildenhall
2026-10-30
Every DecL statement that defines an object has a name, the bare word after its kind, which identifies the object and is the handle another program uses to refer to it. A label is the text shown for it in output. The clause as LABEL follows the item it labels: the name of a statement, an occurrence layer, a reinsurance layer, an expense group, or a premium. A label is presentation only and changes no computed value. The label property returns the label, or a default, or the name, so it is never blank, and qd shows it in the heading of its description.
A label that is a single word, such as as GWP, needs no quotes. A label with spaces or punctuation is quoted, as "Motor Deal", and may contain any character except a double quote or a line break; a reserved word such as premium must also be quoted. Without a label the output uses a default: Gross for the first step of a P&L walk, Premium and Loss for its premium and loss legs, the basis for an expense group, such as fixed expense, and the layer, such as occ 1750 xs 250, for a reinsurance step.
xpnl Deal as "Motor Deal" 10000 premium as GWP less ... # statement and premium
agg Deal.Book as "Motor Book" ... # the wrapped aggregate
2000 xs 0 as "Policy Limit" # occurrence layer
occurrence net of 1750 xs 250 rate 0.325 as "Occ Re" # reinsurance layer
0.15 premium expenses as Acquisition # expense group1.43.1
The example is the ceding commission treaty of Using pnl and xpnl, with a label at every site and the expense split into two groups. In the qd description the first step is the label of the xpnl, Motor Deal, and the cover step is the label of the reinsurance layer, Occ Re.
program = '''xpnl Deal as "Motor Deal"
10000 premium as GWP
less
agg Deal.Book as "Motor Book"
10000 premium at 0.7 lr
2000 xs 0 as "Policy Limit"
sev lognorm 150 cv 2.5
occurrence net of
1750 xs 250 rate 0.325 cede 0.1 as "Occ Re"
mixed gamma 0.25
less
0.15 premium expenses as Acquisition
1000 fixed expenses as "General Expense"'''
x = build(program)
qd(x)
print(x.name, x.label, sep=', ')PnL('Motor Deal': 2 groups, 7 legs -> 'margin')
EX SD CV Skew P01 Median P99
Step Side
Motor Deal Consideration 10000 0 0 10000 10000 10000
Obligation -9500 2706.5 -0.28489 -0.67617 -17060 -9180 -4620
Margin 500 2706.5 5.4129 -0.67617 -7060 820 5380
Occ Re Consideration -3250 0 0 -3250 -3250 -3250
Obligation 2844 1619.5 0.56944 0.94221 405 2605 7645
Margin -406 1619.5 -3.9889 0.94221 -2845 -645 4395
Net 93.999 1426.7 15.178 -0.5251 -3745 215 2855
All Consideration 6750 0 0 6750 6750 6750
Obligation -6656 1426.7 -0.21435 -0.5251 -10495 -6535 -3895
Margin 93.999 1426.7 15.178 -0.5251 -3745 215 2855
Impact -406 1619.5 -3.9889 0.94221 -2845 -645 4395
Deal, Motor Deal
The ledger economic_df shows the labels in its Label level. The premium leg is GWP, the loss leg and the margin of the first step take the label of the wrapped aggregate, Motor Book, each expense group is its own leg, and the legs of the cover are Occ Re premium, Occ Re recovery, and Occ Re commission. The rows Total, Net, and Impact are derived and keep their names. The rows of economic_ratios_df are the steps, under the same labels.
| Step | Side | Label | EX | SD |
|---|---|---|---|---|
| Motor Deal | Consideration | GWP | 10,000 | 0 |
| Obligation | Motor Book | -7,000 | 2,706 | |
| Acquisition | -1,500 | 0 | ||
| General Expense | -1,000 | 0 | ||
| Total | -9,500 | 2,706 | ||
| Margin | Motor Book | 500 | 2,706 | |
| Occ Re | Consideration | Occ Re premium | -3,250 | 0 |
| Obligation | Occ Re recovery | 2,519 | 1,619 | |
| Occ Re commission | 325 | 0 | ||
| Total | 2,844 | 1,619 | ||
| Margin | Total | -406 | 1,619 | |
| Net | 94 | 1,427 | ||
| All | Consideration | Net | 6,750 | 0 |
| Obligation | Net | -6,656 | 1,427 | |
| Margin | Net | 94 | 1,427 | |
| Impact | -406 | 1,619 |
The same program with every as clause removed shows the defaults, first for the steps and then in the Label level of the ledger. The labels of the occurrence layer and of the reinsurance layer are stored on the wrapped aggregate, in its labels.
['Gross', 'occ 1750 xs 250', 'All']
['Premium', 'Loss', 'premium expense', 'fixed expense', 'Total', 'Gross', 'occ
1750 xs 250 premium', 'occ 1750 xs 250 recovery', 'occ 1750 xs 250 commission',
'Net', 'Impact']
labels({'layer': 'Policy Limit', 'occ_reins': {0: 'Occ Re'}})
---
title: Using labels
subtitle: "Display labels on statements, layers, reinsurance, expenses, and premium with the DecL as clause"
categories: [DecL, output, P&L]
date: last-modified
jupyter:
jupytext:
text_representation:
extension: .qmd
format_name: quarto
format_version: '1.0'
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
---
Every DecL statement that defines an object has a name, the bare word after its kind, which identifies the object and is the handle another program uses to refer to it. A **label** is the text shown for it in output. The clause `as LABEL` follows the item it labels: the name of a statement, an occurrence layer, a reinsurance layer, an expense group, or a premium. A label is presentation only and changes no computed value. The `label` property returns the label, or a default, or the name, so it is never blank, and `qd` shows it in the heading of its description.
A label that is a single word, such as `as GWP`, needs no quotes. A label with spaces or punctuation is quoted, `as "Motor Deal"`, and may contain any character except a double quote or a line break; a reserved word such as `premium` must also be quoted. Without a label the output uses a default: `Gross` for the first step of a P&L walk, `Premium` and `Loss` for its premium and loss legs, the basis for an expense group, such as `fixed expense`, and the layer, such as `occ 1750 xs 250`, for a reinsurance step.
```decl
xpnl Deal as "Motor Deal" 10000 premium as GWP less ... # statement and premium
agg Deal.Book as "Motor Book" ... # the wrapped aggregate
2000 xs 0 as "Policy Limit" # occurrence layer
occurrence net of 1750 xs 250 rate 0.325 as "Occ Re" # reinsurance layer
0.15 premium expenses as Acquisition # expense group
```
```{python}
import re
from aggregate import build, qd, __version__
from style import wprint, GT, figures
figures()
print(__version__)
```
## Labels in a P&L walk
The example is the ceding commission treaty of [Using pnl and xpnl](pnls.qmd), with a label at every site and the expense split into two groups. In the `qd` description the first step is the label of the `xpnl`, `Motor Deal`, and the cover step is the label of the reinsurance layer, `Occ Re`.
```{python}
program = '''xpnl Deal as "Motor Deal"
10000 premium as GWP
less
agg Deal.Book as "Motor Book"
10000 premium at 0.7 lr
2000 xs 0 as "Policy Limit"
sev lognorm 150 cv 2.5
occurrence net of
1750 xs 250 rate 0.325 cede 0.1 as "Occ Re"
mixed gamma 0.25
less
0.15 premium expenses as Acquisition
1000 fixed expenses as "General Expense"'''
x = build(program)
qd(x)
print(x.name, x.label, sep=', ')
```
The ledger `economic_df` shows the labels in its `Label` level. The premium leg is `GWP`, the loss leg and the margin of the first step take the label of the wrapped aggregate, `Motor Book`, each expense group is its own leg, and the legs of the cover are `Occ Re premium`, `Occ Re recovery`, and `Occ Re commission`. The rows `Total`, `Net`, and `Impact` are derived and keep their names. The rows of `economic_ratios_df` are the steps, under the same labels.
```{python}
GT(x.economic_df[['EX', 'SD']], float_format=',.0f')
```
```{python}
GT(x.economic_ratios_df[['P', 'L', 'E', 'M', 'LR', 'CR']],
float_format=',.0f', ratio_cols=['LR', 'CR'])
```
## Default labels and labels on the wrapped aggregate
The same program with every `as` clause removed shows the defaults, first for the steps and then in the `Label` level of the ledger. The labels of the occurrence layer and of the reinsurance layer are stored on the wrapped aggregate, in its `labels`.
```{python}
plain = build(re.sub(r' as ("[^"]*"|\w+)', '', program).replace('Deal', 'Plain'))
print(list(plain.economic_ratios_df.index))
wprint(list(plain.economic_df.index.get_level_values('Label').unique()))
print(x.engine.labels)
```