Skip to content

constant_distribution

ConstantDistribution

Bases: BaseDistribution

This class represents a constant distribution, a type of statistical distribution where all outcomes are equally likely. A constant distribution is defined by a single value that is returned for all inputs.

Parameters:

Name Type Description Default
value float
  • The constant value that this distribution returns.
  • The value should not be negative.
required

Raises:

Type Description
ValueError

If the 'value' argument is negative.

Example

Create a ConstantDistribution object.

cd = ConstantDistribution(5)

The value attribute can be accessed and updated.

cd.value 5 cd.value = 10 cd.value 10

Source code in emod_api/utils/distributions/constant_distribution.py
class ConstantDistribution(BaseDistribution):
    """
    This class represents a constant distribution, a type of statistical distribution where all outcomes are equally
    likely. A constant distribution is defined by a single value that is returned for all inputs.

    Args:
        value (float):
            - The constant value that this distribution returns.
            - The value should not be negative.

    Raises:
        ValueError: If the 'value' argument is negative.

    Example:
        >>> # Create a ConstantDistribution object.
        >>> cd = ConstantDistribution(5)
        >>> # The value attribute can be accessed and updated.
        >>> cd.value
        5
        >>> cd.value = 10
        >>> cd.value
        10
    """
    DEMOGRAPHIC_DISTRIBUTION_FLAG = DemographicDistributionFlag.CONSTANT.value

    def __init__(self, value: float):
        super().__init__()
        if value < 0:
            raise ValueError("The 'value' argument should not be negative.")
        self.value = value

    def set_intervention_distribution(self, intervention_object: s2c.ReadOnlyDict, prefix: str):
        """
        Set the distribution parameters to the object.

        Args:
            intervention_object (s2c.ReadOnlyDict):
                - The object to set.
            prefix (str):
                - The prefix of the parameters.
        """
        self._set_parameters(intervention_object, f"{prefix}_Distribution", DistributionType.CONSTANT_DISTRIBUTION.value)
        self._set_parameters(intervention_object, f"{prefix}_Constant", self.value)

    def get_demographic_distribution_parameters(self) -> dict:
        """
        Yield the flag and relevant values necessary for setting a demographics constant distribution

        Returns:
            a dict of the form: {'flag': X, 'value1': Y, 'value2': Z}
        """
        return {"flag": self.DEMOGRAPHIC_DISTRIBUTION_FLAG, "value1": self.value, "value2": None}  # value 2 not used

get_demographic_distribution_parameters()

Yield the flag and relevant values necessary for setting a demographics constant distribution

Returns:

Type Description
dict

a dict of the form: {'flag': X, 'value1': Y, 'value2': Z}

Source code in emod_api/utils/distributions/constant_distribution.py
def get_demographic_distribution_parameters(self) -> dict:
    """
    Yield the flag and relevant values necessary for setting a demographics constant distribution

    Returns:
        a dict of the form: {'flag': X, 'value1': Y, 'value2': Z}
    """
    return {"flag": self.DEMOGRAPHIC_DISTRIBUTION_FLAG, "value1": self.value, "value2": None}  # value 2 not used

set_intervention_distribution(intervention_object, prefix)

Set the distribution parameters to the object.

Parameters:

Name Type Description Default
intervention_object ReadOnlyDict
  • The object to set.
required
prefix str
  • The prefix of the parameters.
required
Source code in emod_api/utils/distributions/constant_distribution.py
def set_intervention_distribution(self, intervention_object: s2c.ReadOnlyDict, prefix: str):
    """
    Set the distribution parameters to the object.

    Args:
        intervention_object (s2c.ReadOnlyDict):
            - The object to set.
        prefix (str):
            - The prefix of the parameters.
    """
    self._set_parameters(intervention_object, f"{prefix}_Distribution", DistributionType.CONSTANT_DISTRIBUTION.value)
    self._set_parameters(intervention_object, f"{prefix}_Constant", self.value)