anml.parameter package
Submodules
Likelihood
The Likelihood
keeps track of all information
related to probability distributions that can be used for priors or data distributions.
- class Likelihood[source]
Bases:
object
- get_neg_log_likelihood(vals, parameters=None)[source]
Gets the objective value based on parameters of the distribution and current values in vals.
- Parameters
vals (
Union
[float
,ndarray
]) – location at which to evaluate the oracleparameters (
Union
[List
[float
],List
[ndarray
],None
]) – parameters of the oracle, optional. If not passed it will use the parameters attached to self, otherwise return an error if there are no parameters.
- Returns
- Return type
float representing the objective value evaluated based on vals and parameters.
Prior Distributions
Gives prior specifications that can be attached to fixed and/or random effect components of a variable and may be used in the solver optimization.
The default prior only includes upper and lower bounds for box constraints and defaults
to [-np.inf, np.inf]. Alternative priors include
GaussianPrior
.
In order to get the error that should be added to the objective function
value, each prior is associated with an Likelihood
.
- class GaussianPrior(lower_bound=<factory>, upper_bound=<factory>, mean=<factory>, std=<factory>)[source]
Bases:
anml.parameter.prior.Prior
- mean: List[float]
- std: List[float]
Variables
Variables are the most granular object for constructing a model specification.
At the simplest level, a variable is just Intercept
,
which is a column of ones (indicating that it does not change based on the data row, except through
an optional random effect).
Each Variable has a collection of methods (e.g., build_design_matrix_fe()
)
that gets the design matrices, constraint matrices and bounds for that single covariate.
- class Intercept(var_link_fun=<function Variable.<lambda>>, fe_prior=Prior(lower_bound=[-inf], upper_bound=[inf], _likelihood=<anml.parameter.likelihood.Likelihood object>), add_re=False, col_group=None, re_var_prior=Prior(lower_bound=[-inf], upper_bound=[inf], _likelihood=<anml.parameter.likelihood.Likelihood object>), re_prior=Prior(lower_bound=[0.0], upper_bound=[inf], _likelihood=<anml.parameter.likelihood.Likelihood object>))[source]
Bases:
anml.parameter.variables.Variable
An intercept variable.
- class ParameterBlock[source]
Bases:
object
- constr_lb_fe: Optional[numpy.ndarray] = None
- constr_lb_re: Optional[numpy.ndarray] = None
- constr_lb_re_var: Optional[numpy.ndarray] = None
- constr_matrix_fe: Optional[numpy.ndarray] = None
- constr_matrix_re: Optional[numpy.ndarray] = None
- constr_matrix_re_var: Optional[numpy.ndarray] = None
- constr_ub_fe: Optional[numpy.ndarray] = None
- constr_ub_re: Optional[numpy.ndarray] = None
- constr_ub_re_var: Optional[numpy.ndarray] = None
- design_matrix_fe: Optional[numpy.ndarray] = None
- design_matrix_re: Optional[numpy.ndarray] = None
- num_fe: int = 0
- property num_re
- num_re_var: int = 0
- re_priors: Optional[numpy.ndarray] = None
- re_var_padding: Optional[numpy.ndarray] = None
- variables: List[anml.parameter.variables.Variable]
- class Variable(covariate=None, var_link_fun=<function Variable.<lambda>>, fe_prior=Prior(lower_bound=[-inf], upper_bound=[inf], _likelihood=<anml.parameter.likelihood.Likelihood object>), add_re=False, col_group=None, re_var_prior=Prior(lower_bound=[-inf], upper_bound=[inf], _likelihood=<anml.parameter.likelihood.Likelihood object>), re_prior=Prior(lower_bound=[0.0], upper_bound=[inf], _likelihood=<anml.parameter.likelihood.Likelihood object>))[source]
Bases:
object
A class that stores information about a variable.
- Parameters
covariate (str) – name of the covariate for this variable.
var_link_fun (callable) – link function for this variable.
fe_prior (Prior, optional) – a prior of class
Prior
for fixed effects coefficientadd_re (bool, optional) – whether to add random effects to this variable
col_group (str, optional) – name for group column
re_var_prior (Prior, optional) – a prior of class
Prior
for random effect variancere_prior (Prior, optional) – a prior of class
Prior
for random effects.
- add_re: bool = False
- build_design_matrix_fe(df, **kwargs)[source]
Build design matrix corresponding to fixed effects.
- Parameters
df (pd.DataFrame) – input dataframe
- build_design_matrix_re(df)[source]
Build design matrix corresponding to random effects covariances.
- Parameters
df (pd.DataFrame) – input dataframe
- col_group: str = None
- covariate: str = None
- design_matrix_fe: Optional[numpy.ndarray]
- design_matrix_re: Optional[numpy.ndarray]
- encode_groups(df)[source]
Convert a categorical column into ordinal numbers.
- Parameters
df (pd.DataFrame) – input dataframe
- Returns
a list of ints indicating category of each datapoint.
- Return type
List[int]
- Raises
ValueError – Only one group in the entire input dataframe.
- fe_prior: anml.parameter.prior.Prior = Prior(lower_bound=[-inf], upper_bound=[inf], _likelihood=<anml.parameter.likelihood.Likelihood object>)
- re_prior: anml.parameter.prior.Prior = Prior(lower_bound=[0.0], upper_bound=[inf], _likelihood=<anml.parameter.likelihood.Likelihood object>)
- re_var_prior: anml.parameter.prior.Prior = Prior(lower_bound=[-inf], upper_bound=[inf], _likelihood=<anml.parameter.likelihood.Likelihood object>)
- var_link_fun()
Spline Variable
A subclass of anml.parameter.variables.Variable
that handles spline related computations.
- class Spline(covariate=None, var_link_fun=<function Variable.<lambda>>, col_group=None, re_var_prior=Prior(lower_bound=[-inf], upper_bound=[inf], _likelihood=<anml.parameter.likelihood.Likelihood object>), re_prior=Prior(lower_bound=[0.0], upper_bound=[inf], _likelihood=<anml.parameter.likelihood.Likelihood object>), knots_type='frequency', knots_num=3, degree=3, l_linear=False, r_linear=False, include_intercept=False, derivative_constr=<factory>, constr_grid_size_global=None)[source]
Bases:
anml.parameter.variables.Variable
Spline variable.
- Parameters
knots_type (str) – type of knots. can only be ‘frequency’ or ‘domain’
knots_num (int) – number of knots
degree (int) – degree of spines
l_linear (bool) – whether left tail is linear
r_linear (bool) – whether right tail is linear
include_intercept (bool) – whether to include intercept in design matrix
derivative_constr (List[~anml.parameter.spline_variable.SplineLinearConstr]) – constraints on derivatives
constr_grid_size_global (int, optional) – number of points to use when building constraint matrix. used only when grid_size for individual ~anml.parameter.spline_variable.SplineLinearConstr is not available
- Raises
VariableError – unknown knot type
VariableError – no covariate has been set
- build_constraint_matrix_fe()[source]
build constrain matrix and bounds for constr_lb <= constr_matrix <= constr_ub.
- Returns
constraint matrix, lower bounds and upper bounds.
- Return type
List[np.ndarray]
- constr_grid_size_global: int = None
- constr_lb_fe: Optional[Union[List[float], numpy.ndarray]]
- constr_matrix_fe: Optional[numpy.ndarray]
- constr_ub_fe: Optional[Union[List[float], numpy.ndarray]]
- degree: int = 3
- derivative_constr: List[anml.parameter.spline_variable.SplineLinearConstr]
- include_intercept: bool = False
- knots_num: int = 3
- knots_type: str = 'frequency'
- l_linear: bool = False
- r_linear: bool = False
- spline: Optional[xspline.core.XSpline]
- x: Optional[numpy.ndarray]
- class SplineLinearConstr(order, y_bounds, x_domain=<factory>, grid_size=None)[source]
Bases:
object
Constraints on spline derivatives. The general form is lb <= Ax <= ub where x is in some interval domain x_domain, and A can be 0th, 1st or 2nd order derivative matrix of the splines evaluated at some discretization points. A is not known at the initialization of this object, but will have dimension grid_size by number of spline basis. lb and ub are vectors of multiples of ones.
This type of constraints can be used to impose monotonicity and convexity constraints. For instance, for splines defined on [0, 5], one can specify monotonically decreasing on [0,1] with constr = SplineLinearConstr(x_domain=[0, 1], y_bounds=[-np.inf, 0.0], order=1), monotonically increasing on [4, 5] with constr = SplineLinearConstr(x_domain=[4, 5], y_bounds=[0.0, np.inf], order=1), and overall convexity with constr = SplineLinearConstr(x_domain=[0, 5], y_bounds=[0.0, np.inf], order=2).
- Parameters
order (int) – order of the derivative
y_bounds (List[float, float]) – bounds for y = Ax
x_domain (List[float, float], optional) – domain for x, default to be -inf to inf
grid_size (int, optional) – size of grid
- Raises
ValueError – domain for x is not valid.
ValueError – bounds for y = Ax is not valid.
ValueError – invalid derivative order
ValueError – invalid grid size
- grid_size: int = None
- order: int
- x_domain: List[float]
- y_bounds: List[float]
- class Parameter(variables, param_name, link_fun=<function Parameter.<lambda>>)[source]
Bases:
anml.parameter.variables.ParameterBlock
A class for parameters.
- Parameters
param_name (str) – name of the parameter
link_fun (callable) – link function for the parameter
variables (List[
Variable
]) – a list of variables
- All attributes from :class:`~anml.parameter.parameter.Variable`s in `variables`
- are carried over but are put into a list.
- link_fun()
- property num_re
- param_name: str
- variables: List[anml.parameter.variables.Variable]
- class ParameterFunction(param_function_name, param_function, param_function_fe_prior=Prior(lower_bound=[-inf], upper_bound=[inf], _likelihood=<anml.parameter.likelihood.Likelihood object>))[source]
Bases:
object
A class for function on parameters.
- Parameters
param_function_name (str) – name of the parameter function
param_function (callable) – parameter function
param_function_fe_prior (List[float]) – a list of two floats specifying mean and std for Gaussian prior on the function.
- param_function: Callable
- param_function_fe_prior: anml.parameter.prior.Prior = Prior(lower_bound=[-inf], upper_bound=[inf], _likelihood=<anml.parameter.likelihood.Likelihood object>)
- param_function_name: str
- class ParameterSet(parameters, parameter_functions=None)[source]
Bases:
anml.parameter.variables.ParameterBlock
A class for a set of parameters.
- Parameters
parameters (List[
Parameter
]) – a list of parameters.parameter_functions (List[
ParameterFunction
]) – a list of parameter functions.
- get_param_function_index(param_function_name)[source]
A function that returns index of a given parameter function.
- Parameters
param_function_name (str) – name of the parameter function
- Returns
index of the parameter function
- Return type
int
- Raises
RuntimeError – parameter function not found in the parameter set.
- get_param_index(param_name)[source]
A function that returns index of a given parameter.
- Parameters
param_name (str) – name of the parameter
- Returns
index of the parameter
- Return type
int
- Raises
RuntimeError – parameter not found in the parameter set.
- property num_re
- param_name: List[str]
- parameter_functions: List[anml.parameter.parameter.ParameterFunction] = None
- parameters: List[anml.parameter.parameter.Parameter]
- variables: List[anml.parameter.variables.Variable]