cleverly.inference.Transform#

class cleverly.inference.Transform(name: str, forward: Callable[[float], float], inverse: Callable[[float], float], derivative: Callable[[float], float] | None = None)[source]#

A monotone map that a contrast’s interval and test are computed on.

The interval is \(f^{-1}(f(\hat h) \pm z \cdot se)\), with \(se\) the standard error of \(f(\hat h)\) by the chain rule. This is the contrast = list(f, f_inv, h, fh_grad) form of R drtmle 1.1.2 ci() (R/confint.R, lines 146 to 167). The limits are sorted after the inverse map, so a decreasing f gives an ordered interval.

A transform built from lambdas does not pickle. log() and logit() use module-level functions and pickle. A fitted result never stores a transformed estimate, so result.save() does not depend on this.

Parameters:
namestr

A label for the transform, which to_dict reports.

forwardcallable()

The map \(f\), from the scale of the contrast to the inference scale.

inversecallable()

The inverse map \(f^{-1}\).

derivativecallable() or None, default=None

The derivative \(f'\). None takes a central difference.

See also

cleverly.estimators.TMLEResult.contrast

Takes transform=.

Examples

>>> from cleverly.inference import Transform
>>> log = Transform.log()
>>> log.name, round(log.forward(1.0), 6), round(log.inverse(0.0), 6)
('log', 0.0, 1.0)

Methods#

classmethod Transform.log() → Transform[source]#

The natural log, with exp as its inverse.

Returns:
Transform

The log transform, for a positive contrast such as a ratio.

classmethod Transform.logit() → Transform[source]#

The logit, with the logistic function as its inverse.

Returns:
Transform

The logit transform, for a contrast inside (0, 1) such as a probability.

classmethod Transform.range_logit(low: float, high: float) → Transform[source]#

The logit of a value’s position in the open interval (low, high).

The map is \(f(x) = \operatorname{logit}((x - a) / (b - a))\) with \((a, b)\) = (low, high). On (0, 1) it is the logit. On (-1, 1) it is \(2 \operatorname{atanh}(x)\), so a symmetric interval on its scale is the atanh interval of a difference of two probabilities.

Parameters:
lowfloat

The lower end of the range.

highfloat

The upper end of the range, above low.

Returns:
Transform

The transform, named "logit" on (0, 1) and "range_logit" otherwise.

Raises:
ValueError

If high is not above low, or either end is not finite.

Transform.slope(value: float, *, step: float = 1e-06) → float[source]#

The derivative of forward at value.

Parameters:
valuefloat

The point on the scale of the contrast.

stepfloat, default=1e-6

Relative step of the central difference when derivative is None.

Returns:
float

\(f'(value)\).

Attributes#

Transform.derivative: Callable[[float], float] | None = None#
Transform.name: str#
Transform.forward: Callable[[float], float]#
Transform.inverse: Callable[[float], float]#