Structural Equation Model Estimators

class pgmpy.estimators.IVEstimator(model)[source]

Initialize IVEstimator object.

Parameters:

model (pgmpy.models.SEM) – The model for which estimation need to be done.

Examples

fit(X, Y, data, ivs=None, civs=None)[source]

Estimates the parameter X -> Y.

Parameters:
  • X (str) – The covariate variable of the parameter being estimated.

  • Y (str) – The predictor variable of the parameter being estimated.

  • data (pd.DataFrame) – The data from which to learn the parameter.

  • ivs (List (default: None)) – List of variable names which should be used as Instrumental Variables (IV). If not specified, tries to find the IVs from the model structure, fails if can’t find either IV or Conditional IV.

  • civs (List of tuples (tuple form: (var, coditional_var))) – List of conditional IVs to use for estimation. If not specified, tries to find the IVs from the model structure, fails if can’t find either IV or Conditional IVs.

Examples

>>> from pgmpy.estimators import IVEstimator # TODO: Finish example.
class pgmpy.estimators.SEMEstimator(model)[source]

Base class of SEM estimators. All the estimators inherit this class.

fit(data, method, opt='adam', init_values='random', exit_delta=0.0001, max_iter=1000, **kwargs)[source]

Estimate the parameters of the model from the data.

Parameters:
  • data (pandas DataFrame or pgmpy.data.Data instance) – The data from which to estimate the parameters of the model.

  • method (str ("ml"|"uls"|"gls"|"2sls")) – The fitting function to use. ML : Maximum Likelihood ULS: Unweighted Least Squares GLS: Generalized Least Squares 2sls: 2-SLS estimator

  • init_values (str or dict) – Options for str: random | std | iv dict: dictionary with keys B and zeta.

  • **kwargs (dict) –

    Extra parameters required in case of some estimators. GLS:

    W: np.array (n x n) where n is the number of observe variables.

    2sls:

    x: y:

Returns:

pgmpy.model.SEM instance

Return type:

Instance of the model with estimated parameters

References

get_init_values(data, method)[source]

Computes the starting values for the optimizer.

Reference

gls_loss(params, loss_args)[source]

Method to compute the Weighted Least Squares fitting function. The optimizer calls this method after each iteration with updated params to compute the new loss.

The fitting function for ML is: .. math:: F_{ULS} = tr { [(S - Sigma(theta)) W^{-1}]^2 }

Parameters:
  • params (dict) – params contain all the variables which are updated in each iteration of the optimization.

  • loss_args (dict) – loss_args contain all the variable which are not updated in each iteration but are required to compute the loss.

Returns:

torch.tensor

Return type:

The loss value for the given params and loss_args

ml_loss(params, loss_args)[source]

Method to compute the Maximum Likelihood loss function. The optimizer calls this method after each iteration with updated params to compute the new loss.

The fitting function for ML is: .. math:: F_{ML} = log |\Sigma(\theta)| + tr(S Sigma^{-1}(theta)) - log S - (p+q)

Parameters:
  • params (dict) – params contain all the variables which are updated in each iteration of the optimization.

  • loss_args (dict) – loss_args contain all the variable which are not updated in each iteration but are required to compute the loss.

Returns:

torch.tensor

Return type:

The loss value for the given params and loss_args

uls_loss(params, loss_args)[source]

Method to compute the Unweighted Least Squares fitting function. The optimizer calls this method after each iteration with updated params to compute the new loss.

The fitting function for ML is: .. math:: F_{ULS} = tr[(S - Sigma(theta))^2]

Parameters:
  • params (dict) – params contain all the variables which are updated in each iteration of the optimization.

  • loss_args (dict) – loss_args contain all the variable which are not updated in each iteration but are required to compute the loss.

Returns:

torch.tensor

Return type:

The loss value for the given params and loss_args