anml.model¶
anml.model.prototype¶
- class anml.model.prototype.ModelPrototype(data, parameters, df)[source]¶
Bases:
abc.ABC
An abstract class that contains data and parameters information. The class provide interface to the optimization problem that comes from minimizing the negative log-likelihood, including the objective, gradient and hessian function. And the class also provide interface for the users, including fit and predict. Developers can inherit this class and overwrite the optimization interface for their use. For an example please check
anml.model.example.ModelExample
.- Parameters
data (DataPrototype) – Given data object.
parameters (List[Parameter]) – Given list of instances of
Parameter
.df (DataFrame) –
- property data¶
Given data object.
- Raises
TypeError – Raised when the input data is not an instance of
DataPrototype
.
- property parameters¶
Given list of instances of
Parameter
.- Raises
TypeError – Raised when the input parameters are not a list of instances of
Parameter
.
- attach(df)[source]¶
Attach data frame to model attributes including data and parameters.
- Parameters
df (DataFrame) – Given data frame.
- abstract objective(x)[source]¶
Objective function for negative log-likelihood.
- Parameters
x (NDArray) – Input coefficients.
- Returns
Objective value of the negative likelihood.
- Return type
float
- abstract gradient(x)[source]¶
Gradient function for negative log-likelihood.
- Parameters
x (NDArray) – Input coefficients.
- Returns
Gradient of the negative likelihood.
- Return type
NDArray
- abstract hessian(x)[source]¶
Hessian function for negative log-likelihood.
- Parameters
x (NDArray) – Input coefficients.
- Returns
Hessian of the negative likelihood.
- Return type
NDArray
- abstract jacobian2(x)[source]¶
Jacobian square function for negative log-likelihood. This function is used for sandwich estimation for posterior variance-covariation matrix.
- Parameters
x (NDArray) – Input coefficients.
- Returns
Jacobian square of the negative likelihood.
- Return type
NDArray
- fit(x0=None, **options)[source]¶
Fit the model. The current implementation uses Scipy optimize solver. More specifically we are using the trust-region solver. In the future the choice of solver might become an addtional option of the fit function. After fitting the model the result will be saved in class attribute result, which contains x as the optimal coefficients, vcov as the posterior variance-covariance matrix and info for more solver results details.
- Parameters
x0 (Optional[NDArray]) – The initial guess for the coefficients. Default is None. If x0=None, the protected function _get_initial_x will be called to get the initialization of the coefficients.
- predict(x=None, df=None)[source]¶
Predict the parameters using the coefficients and the data. User can choose to provide the coefficients or use optimal solution. And use can choose to provide new prediction data frame or use the training data.
- Parameters
x (Optional[NDArray]) – Provided coefficients. Default is None. If x=None, the function will use the optimal coefficients stored in result. If the optimal coefficients does not exist, an error will be raised.
df (Optional[DataFrame]) – Provided prediction data frame. Default is None. If df=None, the function will predict for the training data.
- Returns
The function will returns prediction for parameters. It is a list of arrays. The order of the list is consistent with the order of the parameters list.
- Return type
List[NDArray]
- Raises
ValueError – Raised when input x=None, and the model has not been fitted.
anml.model.example¶
- class anml.model.example.ModelExample(data, variables, df)[source]¶
Bases:
anml.model.prototype.ModelPrototype
An example model class for simple least-square problem. This class provides implementations of the optimization problems including the objective, gradient and hessian function. And provide a Jacobian matrix computation for the posterior variance-covariance matrix. For least-square problem we only have one parameter that can contain multiple variables.
- Parameters
data (DataExample) – Given data object. Here we use instance from
anml.data.example.DataExample
.variables (List[Variable]) – Given list of variables to parameterize the mean in the linear regression problem.
df (DataFrame) – Given data frame contains the observations and covariates.
- objective(x)[source]¶
Objective function of the least square negative likelihood.
\[\frac{1}{2} (y - Ax)^\top \Sigma^{-1} (y - Ax)\]- Parameters
x (NDArray) – Input coefficients.
- Returns
Objective value of the negative likelihood.
- Return type
float
- gradient(x)[source]¶
Gradient function of the least square negative likelihood.
\[A^\top \Sigma^{-1} (Ax - y)\]- Parameters
x (NDArray) – Input coefficients.
- Returns
Gradient of the negative likelihood.
- Return type
NDArray