-
Notifications
You must be signed in to change notification settings - Fork 380
Multiplicative MMM (Log and Log-Log Models) #2477
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
Draft
juanitorduz
wants to merge
25
commits into
main
Choose a base branch
from
multiplicative-mmm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3b66901
init
juanitorduz f9bf9c6
Merge branch 'main' into multiplicative-mmm
juanitorduz 24358d2
nb init
juanitorduz 5717658
serialization
juanitorduz a08409c
fix bugbot
juanitorduz 27f4851
rerun nb
juanitorduz a4fff27
fix test
juanitorduz deb47de
add docs and remove plan (design)
juanitorduz 76f6fa9
nb enhacement
juanitorduz 3652220
Merge branch 'main' into multiplicative-mmm
juanitorduz 92d8ca6
original scale
juanitorduz a7898a0
docs init
juanitorduz 2832fa7
ccleanup
juanitorduz a962f5b
better decomposition logic
juanitorduz 24feb15
tests
juanitorduz 5e96733
Merge branch 'main' into multiplicative-mmm
juanitorduz 138fd28
improvements
juanitorduz 65fbdc0
update
juanitorduz 12c2084
undo
juanitorduz 4ae2d36
undo mmm.py branch-specific scaling refactor
juanitorduz bb37747
little cleanup
juanitorduz 6065cca
cleanup
juanitorduz 9840152
rerun nb
juanitorduz 81bf64c
Merge branch 'main' into multiplicative-mmm
juanitorduz 158a639
rerun
juanitorduz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3,744 changes: 3,744 additions & 0 deletions
3,744
docs/source/notebooks/mmm/mmm_multiplicative.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Copyright 2022 - 2026 The PyMC Labs Developers | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Shared decomposition math helpers for MMM counterfactuals.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import numpy as np | ||
| import xarray as xr | ||
|
|
||
|
|
||
| def original_scale_prediction_from_mu( | ||
| mu: xr.DataArray, | ||
| target_scale: xr.DataArray, | ||
| ) -> xr.DataArray: | ||
| """Convert log-link linear predictor samples to original scale.""" | ||
| return np.exp(mu) * target_scale | ||
|
|
||
|
|
||
| def log_counterfactual_remove_component( | ||
| mu_total: xr.DataArray, | ||
| component: xr.DataArray, | ||
| target_scale: xr.DataArray, | ||
| ) -> xr.DataArray: | ||
| """Per-draw log-link counterfactual contribution for a component.""" | ||
| y_hat = original_scale_prediction_from_mu(mu_total, target_scale) | ||
| y_hat_without_component = np.exp(mu_total - component) * target_scale | ||
| return y_hat - y_hat_without_component | ||
|
|
||
|
|
||
| def identity_counterfactual_component( | ||
| component: xr.DataArray, | ||
| target_scale: xr.DataArray, | ||
| ) -> xr.DataArray: | ||
| """Per-draw identity-link contribution for a component.""" | ||
| return component * target_scale | ||
|
|
||
|
|
||
| def safe_proportional_share( | ||
| numerator: xr.DataArray, | ||
| denominator: xr.DataArray, | ||
| ) -> xr.DataArray: | ||
| """Compute a finite proportional share, guarding against zero denominators.""" | ||
| denom_safe = xr.where(np.abs(denominator) > 1e-12, denominator, np.nan) | ||
| share = numerator / denom_safe | ||
| return share.fillna(0.0).where(np.isfinite(share), 0.0) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.