acturtle / cashflower

An open-source Python framework for actuarial cash flow models
https://cashflower.acturtle.com
MIT License
38 stars 9 forks source link

Bidirectional variables #351

Closed zchmielewska closed 11 months ago

zchmielewska commented 11 months ago

In actuarial models, it's common to use iteration/recursion but in one direction (usually forward).

However, the user might define a model variable that uses bidirectional iteration.

The package should either:

Example of bidirectional iteration within the variable:

@variable()
def my_var(t):
    if t == 0:
        return 0

    if t == 12:
        return 12

    if t < 6:
        return my_var(t-1)

    if t >= 6:
        return my_var(t+1)
settings = {
    ...
    "T_MAX_CALCULATION": 12,
}

Currently, the package calculates the incorrect values. Model assumes it's forward recursion because of the existence of my_var(t-1) in the definition.

While fixing this issue, take into account cycles.

zchmielewska commented 11 months ago

Implemented in 652da2df49d983fab82aedf48026463437eb34db

Bidirectional recursion is disallowed because it does not seem to be used within actuarial cash flow models.

However, if a user provides a real-life example that it is needed then it will be implemented.