biogeme.models.piecewise module

Implements piecewise linear formulation

author:

Michel Bierlaire

date:

Wed Oct 25 09:43:41 2023

biogeme.models.piecewise.piecewiseFormula(variable, thresholds, betas=None)[source]

Warning

This function is deprecated. Use piecewise_formula() instead.

Return type:

Expression

Parameters:
  • variable (str | Variable)

  • thresholds (list[float])

  • betas (list[Beta] | None)

biogeme.models.piecewise.piecewiseFunction(x, thresholds, betas)[source]

Warning

This function is deprecated. Use piecewise_function() instead.

Return type:

float

Parameters:
  • x (float)

  • thresholds (list[float])

  • betas (list[float])

biogeme.models.piecewise.piecewiseVariables(variable, thresholds)[source]

Warning

This function is deprecated. Use piecewise_variables() instead.

Return type:

list[Expression]

Parameters:
  • variable (Expression | str)

  • thresholds (list[float])

biogeme.models.piecewise.piecewise_as_variable(variable, thresholds, betas=None)[source]

Generate the formula for a piecewise linear specification, seen as a transformed variable.

If there are K thresholds, K-1 variables are generated. The first and last thresholds can be defined as None, corresponding to \(-\infty\) and \(+\infty\), respectively. If \(t\) is the variable of interest, for each interval \([a:a+b[\), we define a variable defined as:

\[\begin{split}x_{Ti} =\left\{ \begin{array}{ll} 0 & \text{if } t < a \\ t-a & \text{if } a \leq t < a+b \\ b & \text{otherwise} \end{array}\right. \;\;\;x_{Ti} = \max(0, \min(t-a, b))\end{split}\]

The specification this is returned is

\[x_{T1} + \sum_{i=2}^{K-1} eta_i x_{Ti}\]
Parameters:
  • variable (string) – name of the variable for which we need the piecewise linear transform.

  • thresholds (list(float)) – list of thresholds

  • betas (list(biogeme.expresssions.Beta)) – list of Beta parameters to be used in the specification. The number of entries should be the number of thresholds, minus two. If None, for each interval, the parameter Beta(‘beta_VAR_interval’,0, None, None, 0) is used, where var is the name of the variable. Default: none.

Returns:

expression of the piecewise linear specification.

Return type:

biogeme.expressions.expr.Expression

Raises:
  • BiogemeError – if the thresholds are not defined properly, which means that only the first and the last threshold can be set to None.

  • BiogemeError – if the length of list initialexpr.Betas is not equal to the length of thresholds minus one.

biogeme.models.piecewise.piecewise_formula(variable, thresholds, betas=None)[source]

Generate the formula for a piecewise linear specification.

If there are K thresholds, K-1 variables are generated. The first and last thresholds can be defined as None, corresponding to \(-\infty\) and \(+\infty\), respectively. If \(t\) is the variable of interest, for each interval \([a:a+b[\), we define a variable defined as:

\[\begin{split}x_{Ti} =\left\{ \begin{array}{ll} 0 & \text{if } t < a \\ t-a & \text{if } a \leq t < a+b \\ b & \text{otherwise} \end{array}\right. \;\;\;x_{Ti} = \max(0, \min(t-a, b))\end{split}\]

New variables and new parameters are automatically created to obtain the specification

\[\sum_{i=1}^{K-1} \beta_i x_{Ti}\]
Parameters:
  • variable (str | Variable) – name of the variable for which we need the piecewise linear transform.

  • thresholds (list[float]) – list of thresholds

  • betas (list[Expression | float | int | bool] | None) – list of Beta parameters to be used in the specification. The number of entries should be the number of thresholds, minus one. If None, for each interval, the parameter Beta(‘beta_VAR_interval’,0, None, None, 0) is used, where var is the name of the variable. Default: none.

Return type:

Expression

Returns:

expression of the piecewise linear specification.

Raises:
  • BiogemeError – if the thresholds are not defined properly, which means that only the first and the last threshold can be set to None.

  • BiogemeError – if the length of list initialexpr.Betas is not equal to the length of thresholds minus one.

biogeme.models.piecewise.piecewise_function(x, thresholds, betas)[source]

Plot a piecewise linear specification.

If there are K thresholds, K-1 variables are generated. The first and last thresholds can be defined as None, corresponding to \(-\infty\) and \(+\infty\), respectively. If \(t\) is the variable of interest, for each interval \([a:a+b[\), we define a variable defined as:

\[\begin{split}x_{Ti} =\left\{ \begin{array}{ll} 0 & \text{if } t < a \\ t-a & \text{if } a \leq t < a+b \\ b & \text{otherwise} \end{array}\right. \;\;\;x_{Ti} = \max(0, \min(t-a, b))\end{split}\]
Parameters:
  • x (float) – value at which the piecewise specification must be avaluated

  • thresholds (list(float)) – list of thresholds

  • betas (list(float)) – list of the Beta parameters. The number of entries should be the number of thresholds, plus one.

Returns:

value of the numpy function

Return type:

float

Raises:

BiogemeError – if the thresholds are not defined properly, which means that only the first and the last threshold can be set to None.

biogeme.models.piecewise.piecewise_variables(variable, thresholds)[source]

Generate the variables to include in a piecewise linear specification.

If there are K thresholds, K-1 variables are generated. The first and last thresholds can be defined as None, corresponding to \(-\infty\) and \(+\infty\),respectively. If \(t\) is the variable of interest, for each interval \([a:a+b[\), we define a variable defined as:

\[\begin{split}x_{Ti} =\left\{ \begin{array}{ll} 0 & \text{if } t < a \\ t-a & \text{if } a \leq t < a+b \\ b & \text{otherwise} \end{array}\right. \;\;\;x_{Ti} = \max(0, \min(t-a, b))\end{split}\]
Parameters:
  • variable (Expression | str) – variable for which we need the piecewise linear transform. The expression itself or the name of the variable can be given.

  • thresholds (list[float]) – list of thresholds

Return type:

list[Expression]

Returns:

list of variables to for the piecewise linear specification.

Raises:

BiogemeError – if the thresholds are not defined properly, as only the first and the last thresholds can be set to None.