apply_impacts(baseline, multiplicative_impacts, additive_impacts)

Apply the impacts on top of the basline to get yhat.

\[ yhat = (baseline*\underset{}{\overset{m}{\prod }}({multiplicativeEffect}_{m})) + \underset{}{\overset{a}{\sum }}({additiveEffect}_{a}) \]

Parameters:

Name Type Description Default
baseline Tensor

The baseline impact. This will be the starting point where impacts are applied on.

required
multiplicative_impacts list[Tensor]

These impacts scales the baseline multiplicatively (larger effect).

required
additive_impacts list[Tensor]

These impacts, increase the baseline additively (smaller effect).

required

Returns:

Type Description
Tensor

tf.Tensor: The yhat after applying all the impacts on the baseline.

Source code in wt_ml/layers/impact_utils.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def apply_impacts(
    baseline: tf.Tensor,
    multiplicative_impacts: list[tf.Tensor] | tuple[tf.Tensor, ...],
    additive_impacts: list[tf.Tensor] | tuple[tf.Tensor, ...],
) -> tf.Tensor:
    """Apply the impacts on top of the basline to get yhat.

    $$
    yhat = (baseline*\\underset{}{\\overset{m}{\\prod }}({multiplicativeEffect}_{m}))
        + \\underset{}{\\overset{a}{\\sum }}({additiveEffect}_{a})
    $$

    Args:
        baseline (tf.Tensor): The baseline impact. This will be the starting point where impacts are applied on.
        multiplicative_impacts (list[tf.Tensor]): These impacts scales the baseline multiplicatively (larger effect).
        additive_impacts (list[tf.Tensor]): These impacts, increase the baseline additively (smaller effect).

    Returns:
        tf.Tensor: The yhat after applying all the impacts on the baseline.
    """  # noqa: E501
    yhat = baseline
    for impact in multiplicative_impacts:
        yhat = yhat * impact
    for impact in additive_impacts:
        yhat = yhat + impact
    return yhat

apply_inverse_impacts(y, multiplicative_impacts, additive_impacts)

Remove the impacts from y to get the baseline back. baseline = (yhat - Σ(additive effects)) / ∏(multiplicative effects)

Here its inverse impacts, i.e.,

inverse multiplicative effect = 1/multiplicative effect inverse additive effect = -1 * additive effect

Parameters:

Name Type Description Default
y Tensor

The y total impact.

required
multiplicative_impacts list[Tensor]

Multiplicative impacts, are scaled down (larger effect).

required
additive_impacts list[Tensor]

Additive impacts, are subtracted off the y (smaller effect).

required

Returns:

Type Description
Tensor

tf.Tensor: The baseline after all the impacts are removed from y.

Source code in wt_ml/layers/impact_utils.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def apply_inverse_impacts(
    y: tf.Tensor, multiplicative_impacts: list[tf.Tensor], additive_impacts: list[tf.Tensor]
) -> tf.Tensor:
    """Remove the impacts from `y` to get the baseline back.
    baseline = (yhat - Σ(additive effects)) / ∏(multiplicative effects)

    NOTE: Here its inverse impacts, i.e.,
        inverse multiplicative effect = 1/multiplicative effect
        inverse additive effect = -1 * additive effect

    Args:
        y (tf.Tensor): The `y` total impact.
        multiplicative_impacts (list[tf.Tensor]): Multiplicative impacts, are scaled down (larger effect).
        additive_impacts (list[tf.Tensor]): Additive impacts, are subtracted off the `y` (smaller effect).

    Returns:
        tf.Tensor: The baseline after all the impacts are removed from `y`.
    """
    baseline = y
    for impact in additive_impacts:
        baseline = baseline - impact
    for impact in multiplicative_impacts:
        baseline = tf.math.divide_no_nan(baseline, impact)
    return baseline