Skip to content

parameterspace.transformations.log_zero_one

LogZeroOneFloat (BaseTransformation)

Maps bounded interval to [0,1] via a logarithmic transformation.

This means that all the priors used with a parameter effectively model the exponent of the actual quantity.

This class should be used for ContinuousParameters.

Source code in parameterspace/transformations/log_zero_one.py
class LogZeroOneFloat(BaseTransformation):
    """Maps bounded interval to [0,1] via a logarithmic transformation.

    This means that all the priors used with a parameter effectively model the
    exponent of the actual quantity.

    This class should be used for ContinuousParameters.
    """

    @store_init_arguments
    def __init__(self, bounds: Optional[Tuple]):
        """
        [summary]

        Args:
            bounds: Bounds must be positive for log-transformation.
        """
        assert min(bounds) > 0, "bounds must be positive for log-transformation"
        super().__init__(bounds, (0, 1))
        self.log_bounds = np.log(self.input_bounds)
        self.log_interval_size = self.log_bounds[1] - self.log_bounds[0]

    def inverse(self, numerical_value: float) -> float:
        return float(
            np.clip(
                np.exp(self.log_bounds[0] + numerical_value * (self.log_interval_size)),
                self.input_bounds[0],
                self.input_bounds[1],
            )
        )

    def __call__(self, value: Any) -> float:
        return float((np.log(value) - self.log_bounds[0]) / self.log_interval_size)

    def jacobian_factor(self, numerical_value: float) -> float:
        return 1.0 / (self.log_interval_size * self.inverse(numerical_value))

    def __eq__(self, other):
        return np.allclose(self.input_bounds, other.input_bounds)

inverse(self, numerical_value)

Convert the numerical representation back to the true value with the proper type.

Parameters:

Name Type Description Default
numerical_value float

Transformed/Numerical representation of a value.

required

Returns:

Type Description
float

The value corresponding to the given value. Type depends no the kind of transformation.

Source code in parameterspace/transformations/log_zero_one.py
def inverse(self, numerical_value: float) -> float:
    return float(
        np.clip(
            np.exp(self.log_bounds[0] + numerical_value * (self.log_interval_size)),
            self.input_bounds[0],
            self.input_bounds[1],
        )
    )

jacobian_factor(self, numerical_value)

Factor to correct the likelihood based on the non-linear transformation.

Parameters:

Name Type Description Default
numerical_value float

Transformed/Numerical representation of a value.

required

Returns:

Type Description
float

Jacobian factor to properly transform the likelihood.

Source code in parameterspace/transformations/log_zero_one.py
def jacobian_factor(self, numerical_value: float) -> float:
    return 1.0 / (self.log_interval_size * self.inverse(numerical_value))

LogZeroOneInteger (BaseTransformation)

Maps a bounded interval of integers to [0, 1] via a logarithmic transformation.

This means that all the priors used with a parameter effectively model the exponent of the actual quantity.

This class should be used for IntegerParameters.

Source code in parameterspace/transformations/log_zero_one.py
class LogZeroOneInteger(BaseTransformation):
    """Maps a bounded interval of integers to [0, 1] via a logarithmic transformation.

    This means that all the priors used with a parameter effectively model the
    exponent of the actual quantity.

    This class should be used for IntegerParameters.
    """

    @store_init_arguments
    def __init__(self, bounds: Optional[Tuple]):
        """
        [summary]

        Args:
            bounds: Bounds must be positive for log-transformation.
        """
        assert min(bounds) > 0, "bounds must be positive for log-transformation"
        super().__init__(bounds, (0, 1))
        self.log_bounds = np.log(self.input_bounds + np.array([-0.5, 0.5]))
        self.log_interval_size = self.log_bounds[1] - self.log_bounds[0]

    def inverse(self, numerical_value: float) -> int:
        """
        [summary]
        """
        integer_value = np.around(
            np.exp(self.log_bounds[0] + numerical_value * (self.log_interval_size))
        )
        # clip result to bound due to rounding problems when the numerical value is 0.0
        return int(np.clip(integer_value, *self.input_bounds))

    def __call__(self, value: Any) -> float:
        return float((np.log(value) - self.log_bounds[0]) / self.log_interval_size)

    def jacobian_factor(self, numerical_value: float) -> float:
        return 1.0 / (self.log_interval_size * self.inverse(numerical_value))

    def __eq__(self, other):
        return np.allclose(self.input_bounds, other.input_bounds)

inverse(self, numerical_value)

[summary]

Source code in parameterspace/transformations/log_zero_one.py
def inverse(self, numerical_value: float) -> int:
    """
    [summary]
    """
    integer_value = np.around(
        np.exp(self.log_bounds[0] + numerical_value * (self.log_interval_size))
    )
    # clip result to bound due to rounding problems when the numerical value is 0.0
    return int(np.clip(integer_value, *self.input_bounds))

jacobian_factor(self, numerical_value)

Factor to correct the likelihood based on the non-linear transformation.

Parameters:

Name Type Description Default
numerical_value float

Transformed/Numerical representation of a value.

required

Returns:

Type Description
float

Jacobian factor to properly transform the likelihood.

Source code in parameterspace/transformations/log_zero_one.py
def jacobian_factor(self, numerical_value: float) -> float:
    return 1.0 / (self.log_interval_size * self.inverse(numerical_value))