The RegularizedOptimization submodule#
This submodule implements RegularizedOptimization.R in Python.
It contains functions to learn an cMHN on the full state-space for a given data distribution and implements the L1 regularization.
- mhn.full_state_space.RegularizedOptimization.L1(theta: ndarray, eps: float = 1e-05) float#
Computes the L1 penalty for the given theta matrix.
- Parameters:
theta (np.ndarray) – The theta matrix representing the cMHN.
eps (float, optional) – A small epsilon value, mainly used for the derivative. Default is 1e-05.
- Returns:
The L1 penalty for the given theta matrix.
- Return type:
float
- mhn.full_state_space.RegularizedOptimization.L1_(theta: ndarray, eps: float = 1e-05) ndarray#
Computes the derivative of the L1 penalty for the given theta matrix.
- Parameters:
theta (np.ndarray) – The theta matrix representing the cMHN.
eps (float, optional) – A small epsilon value to prevent division by zero. Default is 1e-05.
- Returns:
The derivative of the L1 penalty.
- Return type:
np.ndarray
- mhn.full_state_space.RegularizedOptimization.grad_reg(theta: ndarray, pD: ndarray, lam: float, n: int = 0, pth_space: ndarray | None = None) ndarray#
Computes the gradient with L1 regularization for the given theta matrix.
- Parameters:
theta (np.ndarray) – The theta matrix representing the cMHN.
pD (np.ndarray) – Distribution provided by the training data.
lam (float) – Tuning parameter lambda for regularization.
n (int, optional) – The number of columns/rows of the theta matrix. Default is 0.
pth_space (np.ndarray, optional) – Optional parameter used to avoid recalculating pth, improving performance.
- Returns:
The gradient of the L1-regularized score.
- Return type:
np.ndarray
- mhn.full_state_space.RegularizedOptimization.learn_MHN(pD: ~numpy.ndarray, init: ~numpy.ndarray | None = None, lam: float = 0, maxit: int = 5000, trace: bool = False, reltol: float = 1e-07, round_result: bool = True, callback: ~typing.Callable | None = None, score_func: ~typing.Callable = <function score_reg>, jacobi: ~typing.Callable = <function grad_reg>) ndarray#
Trains a cMHN to fit a given probability distribution.
- Parameters:
pD (np.ndarray) – Probability distribution used to train the new model.
init (np.ndarray, optional) – Starting point for the training (initial theta). Default is None.
lam (float, optional) – Tuning parameter lambda for regularization. Default is 0.
maxit (int, optional) – Maximum number of training iterations. Default is 5000.
trace (bool, optional) – If True, convergence messages are printed (see scipy.optimize.minimize). Default is False.
reltol (float, optional) – Gradient norm must be less than reltol before successful termination. Default is 1e-07.
round_result (bool, optional) – If True, the result is rounded to two decimal places. Default is True.
callback (Callable, optional) – Function called after each iteration, must take theta as an argument. Default is None.
score_func (Callable, optional) – Score function used for training. Default is score_reg.
jacobi (Callable, optional) – Gradient function used for training. Default is grad_reg.
- Returns:
The trained model (theta matrix).
- Return type:
np.ndarray
- mhn.full_state_space.RegularizedOptimization.score_reg(theta: ndarray, pD: ndarray, lam: float, n: int | None = None, pth_space: ndarray | None = None) float#
Computes the score with L1 regularization for the given theta matrix.
- Parameters:
theta (np.ndarray) – The theta matrix representing the cMHN.
pD (np.ndarray) – Distribution provided by the training data.
lam (float) – Tuning parameter lambda for regularization.
n (int, optional) – The number of columns/rows of the theta matrix. Default is None.
pth_space (np.ndarray, optional) – Optional parameter used for communication with the gradient function to improve performance.
- Returns:
The score of the current cMHN, penalized with L1 regularization.
- Return type:
float