Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tanh saturation parameter class documentation #657

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 45 additions & 12 deletions pymc_marketing/mmm/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,34 +420,67 @@ def logistic_saturation(x, lam: npt.NDArray[np.float_] | float = 0.5):


class TanhSaturationParameters(NamedTuple):
b: pt.TensorLike
"""Saturation.
"""Container for tanh saturation parameters.

Parameters
----------
b : pt.TensorLike
Saturation
c : pt.TensorLike
Customer Aquisition Cost at 0.

"""

b: pt.TensorLike
c: pt.TensorLike
"""Customer Aquisition Cost at 0.
"""

def baseline(self, x0: pt.TensorLike) -> "TanhSaturationBaselinedParameters":
"""Change the parameterization to baselined at :math:`x_0`."""
"""Change the parameterization to baselined at :math:`x_0`.

Parameters
----------
x0 : pt.TensorLike
Baseline spend.

Returns
-------
TanhSaturationBaselinedParameters
Baselined parameters.

"""
y_ref = tanh_saturation(x0, self.b, self.c)
gain_ref = y_ref / x0
r_ref = y_ref / self.b
return TanhSaturationBaselinedParameters(x0, gain_ref, r_ref)


class TanhSaturationBaselinedParameters(NamedTuple):
x0: pt.TensorLike
"""Baseline spend.
"""Representation of tanh saturation parameters in baselined form.

Parameters
----------
x0 : pt.TensorLike
Baseline spend.
gain : pt.TensorLike
ROAS at :math:`x_0`.
r : pt.TensorLike
Overspend Fraction.

"""

x0: pt.TensorLike
gain: pt.TensorLike
"""ROAS at :math:`x_0`.
"""
r: pt.TensorLike
"""Overspend Fraction.
"""

def debaseline(self) -> TanhSaturationParameters:
"""Change the parameterization to baselined to be classic saturation and cac."""
"""Change the parameterization to baselined to be classic saturation and cac.

Returns
-------
TanhSaturationParameters
Classic saturation and cac parameters.

"""
saturation = (self.gain * self.x0) / self.r
cac = self.r / (self.gain * pt.arctanh(self.r))
return TanhSaturationParameters(saturation, cac)
Expand Down
Loading