Source code for biogeme.latent_variables.positive_parameter_factory
"""Factory utilities for strictly positive parameters.This module provides a generic mechanism to define positive model parametersin a way that is compatible with both maximum likelihood and Bayesianestimation.In a maximum likelihood context, positivity is typically enforced byestimating the logarithm of the parameter and exponentiating it. This avoidsexplicit constraints and generally improves numerical stability.In a Bayesian context, it is often preferable to work directly with theparameter itself and impose positivity through a lower bound set to a smallpositive value. This leads to more interpretable posterior draws and avoidsartificial symmetry in the parameter space.The use of factories abstracts away these implementation details: componentsthat rely on positive parameters do not need to know whether a log-transformedor directly constrained representation is used."""from__future__importannotationsfromtypingimportProtocolfrombiogeme.expressionsimportBeta,Expression,expfrombiogeme.floating_pointimportSMALL_POSITIVE
[docs]classPositiveParameterFactory(Protocol):"""Protocol for factories creating strictly positive model parameters. Implementations return a Biogeme :class:`~biogeme.expressions.Expression` representing a parameter constrained to be strictly positive. """def__call__(self,name:str,prefix:str,value:float,)->Expression:"""Create a strictly positive parameter expression. :param name: Base name of the parameter (for example ``"sigma"`` or ``"delta_0"``). :param prefix: Prefix used to namespace parameter names (for example an indicator or type label). :param value: Initial value used when creating the underlying Biogeme parameter. :return: An expression that evaluates to a strictly positive value. """...
[docs]classSigmaFactory(Protocol):"""Protocol for factories creating strictly positive sigma (scale) parameters."""def__call__(self,prefix:str)->Expression:"""Create a strictly positive sigma parameter expression. :param prefix: Prefix used to namespace the sigma parameter name. :return: An expression that evaluates to a strictly positive sigma. """...
[docs]defmake_positive_parameter_factory(*,use_log:bool)->PositiveParameterFactory:"""Create a factory for strictly positive parameters. Two parameterizations are supported: - If ``use_log`` is True, the returned factory creates an unconstrained parameter in log-space (named ``"<prefix>_<name>_log"``) and returns its exponential. This is typically used for maximum-likelihood estimation. - If ``use_log`` is False, the returned factory creates a directly constrained parameter (named ``"<prefix>_<name>"``) with a lower bound set to :data:`~biogeme.floating_point.SMALL_POSITIVE`. This is typically used for Bayesian estimation. :param use_log: If True, define parameters in log-space and exponentiate them. :return: A callable factory compatible with :class:`PositiveParameterFactory`. """deffactory(name:str,prefix:str,value:float,)->Expression:ifuse_log:returnexp(Beta(f"{prefix}_{name}_log",value,None,None,0))returnBeta(f"{prefix}_{name}",value,SMALL_POSITIVE,None,0)returnfactory
[docs]defmake_sigma_factory(*,use_log:bool)->SigmaFactory:"""Create a sigma factory (specialization of the positive-parameter factory). The sigma factory creates a strictly positive scale parameter named ``"<prefix>_sigma"`` (or ``"<prefix>_sigma_log"`` in log-space) using the same parameterization rules as :func:`make_positive_parameter_factory`. :param use_log: If True, define sigma in log-space and exponentiate it. :return: A callable factory compatible with :class:`SigmaFactory`. """positive_factory=make_positive_parameter_factory(use_log=use_log)value=-1ifuse_logelse1defsigma_factory(prefix:str)->Expression:returnpositive_factory("sigma",prefix,value=value,)returnsigma_factory