Time shapes and kernels#

A time shape is a value that depends on the period: on the model year, the period, or the calendar month. A kernel says how one amount spreads over its own and later periods, and drives settle=, recognize=, used=, and Series.spread.

Values over time#

boki.by_year(mapping, default=None)#

Return values for model years counted from zero.

Integer keys count model years from the model start, with each model year spanning twelve months. Date and period-label keys select the model year containing that date or period. A range selects model years with Python’s stop-exclusive semantics, and "terminal" selects the last period. A selected value is used in every period of that model year. Periods without a matching key use default.

Parameters:
mappingdict

Map a model-year index (int), range, date, period label (str), or "terminal" to a numeric scalar, Quantity, or Series. Date and period-label keys select the model year that contains them. Values and default must have compatible units.

defaultscalar, Quantity, or Series, optional

A scalar, Quantity, Series, or None. Use a value with units compatible with mapping; the default is None.

Returns:
Series

A value for each model period. The series has the common unit of the mapping values and default.

Raises:
UnitError

At construction, if mapping values and default have incompatible units.

ValueError

At evaluation, if an integer key is outside the model’s year range. Integer keys count from zero, not from the calendar year.

See also

boki.step

Select values by model-period index.

boki.by_month

Select values by calendar month.

Examples

Use a calendar-year label or a zero-based model-year index in an annual model:

>>> import boki as b
>>> m = b.Model("Revenue", start="2027-01", periods=3, step=b.year)
>>> value = b.by_year({"2027": 10, 1: 20}, default=0)
>>> r = m.render()
>>> [r.value(value, period) for period in ("2027", "2028", "2029")]
[10, 20, 0]

Model years count twelve months from the model start, even with monthly periods:

>>> m = b.Model("Monthly revenue", start="2027-01", periods=24, step=b.month)
>>> value = b.by_year({0: 5, 1: 7})
>>> r = m.render()
>>> [r.value(value, period) for period in ("2027-01", "2028-01")]
[5, 7]
boki.step(mapping)#

Return a value that takes effect at each key and holds until the next key.

Each key is a period of the model. A key’s value applies from that period up to the period before the next key, so a price list or a rate that changes on given dates is one step. The order of the keys does not matter. Periods before the first key have no value (None), and a series computed from a missing value is missing too.

Parameters:
mappingdict

Map a period key to a numeric scalar, Quantity, or Series. A key is a zero-based period index (int; a negative index counts from the last period), a date, or a period label such as "2027-04" or "2028" (annual periods). A date selects the period that contains it. A Series value is evaluated in each period in which its key is in effect. Values must have compatible units; a literal 0 fits any unit.

Returns:
Series

The value in effect in each period. The series has the common unit of the values.

Raises:
UnitError

At construction, if the values have incompatible units.

IndexError

At evaluation, if a key is outside the model’s timeline.

See also

boki.by_year

Select values by model year.

boki.by_month

Select values by calendar month.

Notes

Integer keys are period indexes, not model years: b.step({1: x}) starts in the second period whatever the period length, while b.by_year({1: x}) starts in the second twelve months.

Examples

A price that changes on given months of a monthly model:

>>> import boki as b
>>> m = b.Model("Price list", start="2027-01", periods=6, step=b.month)
>>> price = b.step({"2027-01": 100, "2027-04": 120})
>>> r = m.render()
>>> r[price]
[100, 100, 100, 120, 120, 120]

Periods before the first key have no value:

>>> late = b.step({"2027-03": 5})
>>> r[late]
[None, None, 5, 5, 5, 5]

Integer keys count periods, so with quarterly periods 2 is the third quarter:

>>> m = b.Model("Quarterly", start="2027-01", periods=4, step=3 * b.month)
>>> level = b.step({0: 1, 2: 2})
>>> r = m.render()
>>> [r.value(level, quarter) for quarter in ("2027-01", "2027-04", "2027-07", "2027-10")]
[1, 1, 2, 2]
boki.by_month(values, default=None)#

Return a value that depends on the calendar month of each period, for seasonality in a monthly model.

The model must have dated periods of one month each. A month’s value applies in every period that starts in that month, in every year.

Parameters:
valuesdict, list, or tuple

Either a dict that maps a calendar month (int, 1 for January to 12 for December) to a numeric scalar, Quantity, or Series, or a list or tuple of exactly twelve such values from January. Values must have compatible units with each other and with default.

defaultscalar, Quantity, or Series, optional

Value of the months that values does not list. If it is None, values must give all twelve months. The default is None.

Returns:
Series

The value for the calendar month of each period. The series has the common unit of values and default.

Raises:
TypeError

At construction, if values is not a dict, list, or tuple.

ValueError

At construction, if a list does not have twelve values, a key is not an int from 1 to 12, or default is None and a month has no value. At evaluation, if a period is not a dated period of one month, as in a quarterly model or a model that starts with an integer.

UnitError

At construction, if the values and default have incompatible units.

See also

boki.by_year

Select values by model year.

boki.step

Select values by period.

Notes

The month is the month in which a period starts, so by_month needs monthly periods; use by_year or step for longer ones.

Examples

A seasonal factor that is 1.2 in November, 1.4 in December, and 1.0 in other months:

>>> import boki as b
>>> m = b.Model("Season", start="2027-11", periods=4, step=b.month)
>>> season = b.by_month({11: 1.2, 12: 1.4}, default=1.0)
>>> r = m.render()
>>> r[season]
[1.2, 1.4, 1.0, 1.0]

Twelve values from January give every month a value, so no default is needed:

>>> sales = b.by_month([10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 30])
>>> r[sales]
[20, 30, 10, 10]

Kernels#

A kernel counts periods, not months, so b.delay(1) is a quarter in a quarterly model.

class boki.shapes.Kernel(weights, name)#

A kernel: the shares of one amount that fall in the same period and in later periods.

weights[k] is the share that falls k periods after the period of the amount, so delay(2) has the weights (0, 0, 1). The unit of k is the model’s period, not a month. A kernel sets when cash moves (settle=), when profit is recognized (recognize=), when bought inventory is used (used=), and how a series spreads over later periods (Series.spread). Build kernels with delay, even, curve, mix, and the tax schedules of boki.lib (macrs, straight_line) rather than by calling the class.

Parameters:
weightsIterable[float]

Share of the amount that falls k periods later, for k = 0, 1, .... The shares are dimensionless fractions of the amount. At least one weight is required, and every weight must be finite. The weights are stored as a tuple.

namestr

Label for messages and for the name of series built with the kernel. The label "curve" also makes boki.shapes.spread keep applying the last weight in all later periods.

Attributes:
weightstuple[float, …]

Share of the amount that falls k periods after the amount, for k = 0, 1, ....

namestr

The kernel’s label, such as "delay(2)".

Raises:
ValueError

At construction, if there are no weights or a weight is not finite.

See also

boki.delay

A kernel that moves an amount a number of periods later.

boki.even

A kernel that spreads an amount evenly.

boki.curve

A kernel from arbitrary weights.

boki.mix

A weighted mix of kernels.

boki.shapes.spread

Apply a kernel to a series.

Notes

Two kernels are equal when their weights are equal, whatever their names, and a kernel hashes by its weights. settle=, recognize=, and used= accept only kernels whose weights sum to 1 (a distribution), and reject a curve, which keeps applying its last weight, unless it ends in 0.

Examples

Read the weights of a kernel:

>>> import boki as b
>>> kernel = b.delay(2)
>>> kernel.weights
(0.0, 0.0, 1.0)
>>> kernel.name
'delay(2)'
>>> b.delay(1) == b.curve([0, 1])
True
boki.delay(n)#

Return a kernel that moves the whole amount n periods later.

Use it for settle= to collect or pay cash after a fixed lag, such as b.delay(2) for 60-day terms in a monthly model.

Parameters:
nint

Number of periods later, counted in the model’s periods, not in months. 0 keeps the amount in the same period. It must not be negative.

Returns:
Kernel

A kernel whose weights are 0 for the first n periods and 1 in period n.

Raises:
ValueError

At construction, if n is negative.

See also

boki.even

Spread an amount evenly instead of moving it all at once.

boki.mix

Split an amount between kernels.

boki.shapes.spread

Apply a kernel to a series.

Notes

A period is whatever length the model uses. b.delay(1) is one month in a monthly model and three months in a quarterly one. With settle=, a revenue line is booked into the shared receivable (an expense line into the payable) and the cash follows the kernel; see the account and settlement table.

Examples

Revenue booked in each month of a monthly model and collected one month later:

>>> import boki as b
>>> months = ["2027-01", "2027-02", "2027-03"]
>>> m = b.Model("Collections", start="2027-01", periods=3, step=b.month)
>>> co = m.entity("A", standard=b.IFRS)
>>> sales = co.pl.revenue("Sales", 100, settle=b.delay(1))
>>> r = m.render()
>>> [round(r.value(sales, month)) for month in months]
[100, 100, 100]
>>> [round(r.value(co.bs.cash, month)) for month in months]
[0, 100, 200]

The lag counts periods: one period is one quarter in a quarterly model.

>>> m = b.Model("Quarterly", start="2027-01", periods=3, step=3 * b.month)
>>> co = m.entity("A", standard=b.IFRS)
>>> sales = co.pl.revenue("Sales", 100, settle=b.delay(1))
>>> r = m.render()
>>> [round(r.value(co.bs.cash, quarter)) for quarter in ("2027-01", "2027-04", "2027-07")]
[0, 100, 200]
boki.even(n)#

Return a kernel that spreads an amount evenly over n periods, starting with the period of the amount.

Use it for recognize= to recognize a prepayment over the months it covers, for settle= to pay in equal instalments, or with Series.spread to smear an amount over time.

Parameters:
nint

Number of periods, counted in the model’s periods, not in months. It must be at least 1; 1 keeps the amount in its own period.

Returns:
Kernel

A kernel with n equal weights of 1 / n, so the shares sum to 1.

Raises:
ValueError

At construction, if n is less than 1.

See also

boki.delay

Move an amount all at once.

boki.curve

Spread by arbitrary weights.

boki.mix

Split an amount between kernels.

boki.shapes.spread

Apply a kernel to a series.

Notes

The shares are dimensionless fractions of the amount. With recognize= the amount is billed in its own period into deferred revenue (revenue lines) or prepaid expenses (expense lines) and released to profit by the kernel; see the account and settlement table.

Examples

An annual insurance premium of 300 paid up front in January and recognized as an expense over three months:

>>> import boki as b
>>> months = ["2027-01", "2027-02", "2027-03", "2027-04"]
>>> m = b.Model("Insurance", start="2027-01", periods=4, step=b.month)
>>> co = m.entity("A", standard=b.IFRS)
>>> co.bs.opening(cash=1000)
>>> premium = co.pl.sga("Premium", {0: 300}, recognize=b.even(3))
>>> r = m.render()
>>> [round(r.value(premium, month)) for month in months]
[100, 100, 100, 0]
>>> [round(r.value(co.bs.cash, month)) for month in months]
[700, 700, 700, 700]

Spread a one-off amount of 100 over two periods:

>>> m = b.Model("Spread", start="2027-01", periods=3, step=b.month)
>>> amount = m.series("Amount", data={0: 100})
>>> r = m.render()
>>> r[amount.spread(b.even(2))]
[50.0, 50.0, 0.0]
boki.curve(weights)#

Return a kernel from arbitrary weights by periods elapsed, such as a ramp or an S-curve.

Applied with Series.spread, the last weight keeps applying in every later period, so a ramp that ends at 1 stays at full. To use a curve as a distribution for settle= or recognize=, end it with a weight of 0 and make the weights sum to 1.

Parameters:
weightsiterable of float

Share of the amount that falls k periods later, for k = 0, 1, ..., as dimensionless fractions of the amount. At least one weight is required and every weight must be finite. The weights do not have to sum to 1 for Series.spread.

Returns:
Kernel

A kernel named "curve" with these weights.

Raises:
ValueError

At construction, if there are no weights or a weight is not finite.

See also

boki.delay

Move an amount all at once.

boki.even

Spread an amount evenly.

boki.mix

Split an amount between kernels.

boki.shapes.spread

Apply a kernel to a series.

Notes

Only a kernel made by curve holds its last weight. A mix of curves, or a kernel from delay, even, or the tax schedules, applies its weights once and then contributes nothing.

Examples

A ramp-up: a one-off amount of 100 reaches the output at 25%, then 50%, then in full and keeps contributing:

>>> import boki as b
>>> m = b.Model("Ramp", start="2027-01", periods=4, step=b.month)
>>> amount = m.series("Amount", data={0: 100})
>>> r = m.render()
>>> r[amount.spread(b.curve([0.25, 0.5, 1]))]
[25.0, 50.0, 100.0, 100.0]

A curve that ends in 0 does not keep contributing:

>>> r[amount.spread(b.curve([0.25, 0.5, 0.25, 0]))]
[25.0, 50.0, 25.0, 0.0]
boki.mix(parts)#

Return a kernel that is a weighted mix of kernels, such as 70% in the same period and 30% a period later.

Parameters:
partsdict

Map each Kernel to its share of the amount, a dimensionless fraction. The shares must add up to 1 (within 1e-9).

Returns:
Kernel

A kernel named "mix" whose weight in each period is the sum over the parts of the share times the part’s weight. It is as long as the longest part.

Raises:
ValueError

At construction, if the shares do not add up to 1.

See also

boki.delay

Move an amount all at once.

boki.even

Spread an amount evenly.

boki.curve

Spread by arbitrary weights.

Notes

The result is a plain "mix" kernel, so a curve inside it stops after its last weight instead of holding it. Individual shares are not checked beyond the total.

Examples

Customers pay 70% in the month of the sale and 30% a month later:

>>> import boki as b
>>> months = ["2027-01", "2027-02", "2027-03"]
>>> m = b.Model("Collections", start="2027-01", periods=3, step=b.month)
>>> co = m.entity("A", standard=b.IFRS)
>>> sales = co.pl.revenue("Sales", 100, settle=b.mix({b.delay(0): 0.7, b.delay(1): 0.3}))
>>> r = m.render()
>>> [round(r.value(co.bs.cash, month)) for month in months]
[70, 170, 270]
>>> b.mix({b.delay(0): 0.7, b.delay(1): 0.3}).weights
(0.7, 0.3)
boki.shapes.spread(src, kernel)#

Return a series in which each amount of src is spread over its own and later periods by kernel.

The value in period t is the sum over earlier or equal periods s of kernel.weights[t - s] times the value of src in period s. A scalar src counts as the same amount in every period. Series.spread is the method form of this function.

Parameters:
srcSeries, Quantity, or number

The amounts to spread, in any unit. A Series is read in every period, a scalar counts as the same amount in every period.

kernelKernel

The shares by periods elapsed. A kernel made by curve keeps applying its last weight in later periods; any other kernel contributes nothing after its last weight.

Returns:
Series

The spread amounts in each period. The series has the unit of src.

See also

boki.delay

Move an amount all at once.

boki.even

Spread an amount evenly.

boki.curve

Spread by arbitrary weights.

boki.mix

Split an amount between kernels.

Notes

The result adds up to the amounts spread only if the weights add up to 1 and nothing is cut off by the end of the model: amounts in the last periods are not fully spread.

Examples

A one-off amount of 100 spread evenly over two months:

>>> import boki as b
>>> m = b.Model("Spread", start="2027-01", periods=4, step=b.month)
>>> amount = m.series("Amount", data={0: 100})
>>> r = m.render()
>>> r[b.shapes.spread(amount, b.even(2))]
[50.0, 50.0, 0.0, 0.0]

Amounts in several periods add up where their spreads overlap:

>>> orders = m.series("Orders", data={0: 100, 1: 60})
>>> r[orders.spread(b.delay(1))]
[0.0, 100.0, 60.0, 0.0]