The formula Module

mogp_emulator.formula.mean_from_patsy_formula(formula, inputdict={})

Create a mean function from a patsy formula

This is the functional interface to creating a mean function from a patsy/R formula. The higher level function MeanFunction in the MeanFunction module is preferred over this one, but this potentially gives the user slightly more control as a patsy ModelDesc object can be passed directly, rather than giving a string.

This method takes a string or a patsy ModelDesc object as an input and an optional dictionary that map strings to integer indices in the input data. The formula is then parsed with patsy, and the individual terms resulting from that are converted to mean functions and composed using the provided operations.

The string formulas can be specified in several ways. The formula LHS is implicitly always "y = " or "y ~ ", though these can be explicitly provided as well (though it is ignored in the conversion). The RHS may contain a set of terms containing the add, multiply, power, and call operations much in the same way that the operations would be entered as regular python code. Parentheses are used to indicated prececence as well as the call operation, and square brackets indicate an indexing operation on the inputs. Inputs may be specified as either a string such as "x[0]", "inputs[0]", or a string that can be mapped to an integer index with the optional dictionary passed to the function. Any strings not representing operations or inputs as described above are interpreted as follows: if the string can be converted into a number, then it is interpreted as a ConstantMean fixed mean function object; otherwise it is assumed to represent a fitting coefficient. Note that this means many characters that do not represent operations within this mean function language but would not normally be considered as python variables will nonetheless be converted into fitting coefficients – it is up to the user to get this right.

Expressions that are repeated or redundant will not be simplified beyond the parsing done by patsy, so the user should take care that the provided expression is sensible as a mean function and will not cause problems when fitting.

Examples:

>>> from mogp_emulator.formula import mean_from_patsy_formula
>>> mf1 = mean_from_patsy_formula("x[0]")
>>> print(mf1)
c + c*x[0]
>>> mf2 = mean_from_patsy_formula("a*b", {"a": 0, "b": 1})
>>> print(mf2)
c + c*x[0] + c*x[1] + c*x[0]*x[1]
Parameters:
  • formula (str or ModelDesc) – string representing the desired mean function formula or a patsy ModelDesc object
  • inputdict (dict) – dictionary used to map variables to input indices. Maps strings to integer indices (must be non-negative). Optional, default is {}.
Returns:

New subclass of MeanBase implementing the given formula

Return type:

subclass of MeanBase (exact type will depend on the formula that is provided)

mogp_emulator.formula.mean_from_string(formula, inputdict={})

Create a mean function from a string formula

This is the functional interface to creating a mean function from a string formula. The higher level function MeanFunction in the MeanFunction module is preferred over this one, but this can also be used and the output is identical.

This method takes a string as an input and an optional dictionary that map strings to integer indices in the input data.

The string formulas can be specified in several ways. The formula LHS is implicitly always "y = " or "y ~ ", though these can be explicitly provided as well. The RHS may contain a set of terms containing the add, multiply, power, and call operations much in the same way that the operations would be entered as regular python code. Parentheses are used to indicated prececence as well as the call operation, and square brackets indicate an indexing operation on the inputs. Inputs may be specified as either a string such as "x[0]", "inputs[0]", or a string that can be mapped to an integer index with the optional dictionary passed to the function. Any strings not representing operations or inputs as described above are interpreted as follows: if the string can be converted into a number, then it is interpreted as a ConstantMean fixed mean function object; otherwise it is assumed to represent a fitting coefficient. Note that this means many characters that do not represent operations within this mean function language but would not normally be considered as python variables will nonetheless be converted into fitting coefficients – it is up to the user to get this right.

Expressions that are repeated or redundant will not be simplified, so the user should take care that the provided expression is sensible as a mean function and will not cause problems when fitting.

Examples:

>>> from mogp_emulator.formula import mean_from_string
>>> mf1 = mean_from_string("y = a + b*x[0]")
>>> print(mf1)
c + c*x[0]
>>> mf2 = mean_from_string("c*a*b", {"a": 0, "b": 1})
>>> print(mf2)
c*x[0]*x[1]
Parameters:
  • formula (str) – string representing the desired mean function formula
  • inputdict (dict) – dictionary used to map variables to input indices. Maps strings to integer indices (must be non-negative). Optional, default is {}.
Returns:

New subclass of MeanBase implementing the given formula

Return type:

subclass of MeanBase (exact type will depend on the formula that is provided)