Variable types¶
Cashflower includes various types of variables. Here’s an overview:
# |
Type |
Definition |
Calling |
|---|---|---|---|
1 |
Regular |
@variable()
def regular_var(t):
return t
|
print(regular_var(t=5))
# 5.0
print(regular_var())
# np.array([0., 1., 2., ..., 720.])
|
2 |
Constant |
@variable()
def constant_var():
return 1
|
print(constant_var(t=5))
# 1.0
print(constant_var())
# 1.0
|
3 |
Array |
@variable(array=True)
def array_var():
return [*range(720)]
|
print(array_var(t=5))
# 5.0
print(array_var())
# np.array([0., 1., 2., ..., 720.])
|
4 |
Stochastic |
@variable()
def stoch_var(t, stoch):
return interest_rates[t, stoch]
|
print(stoch_var(t=5, stoch=2))
# 0.02
|
Description:
1. Regular
This is the default variable type with values that depend on
t(time).The function requires a parameter
tand should return a numeric value.You can call it for a specific period (e.g.,
t=5) to get a single float. When called without any parameters, it returns an entire array of results, useful for array-based calculations.It belongs to the
Variableclass.
2. Constant
Constant variable holds the same value for all periods.
The function doesn’t require any parameters and should return a numeric value.
It can be called for a specific period (e.g.,
t=5) or without any arguments, returning a float in both cases. It can still be used for array-based calculations thanks to broadcasting mechanisms.It belongs to the
ConstantVariableclass.
3. Array
Array variables significantly enhance runtime performance compared to regular ones.
The variable’s decorator should include an
arrayargument set toTrue(@variable(array=True)). The function doesn’t require any parameters and should return an array of numeric values, matching the projection horizon’s length.Similar to regular variables, it can be called for a specific period (e.g.,
t=5) to return a single float. When called without parameters, it provides the entire array of results for array-based calculations.It belongs to the
ArrayVariableclass.
4. Stochastic
Stochastic variables are for models involving multiple stochastic runs.
The function requires two parameters:
tandstoch, returning a numeric value.It can be called for a specific period and stochastic scenario (e.g.,
t=5andstoch=2) to return a single float.It belongs to the
StochasticVariableclass.
Data Type
All variables used in the model must be numeric. This is for two main reasons:
Performance – using numeric variables allows us to take advantage of NumPy’s optimized number types and operations, which makes the model run faster.
Organization – keeping the model focused on numbers and adding text (like names, categories, or descriptions) later helps keep things tidy and well-structured.