-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
686f893
commit 86f4a6d
Showing
6 changed files
with
231 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,94 @@ | ||
"""Power-scaling sensitivity diagnostics.""" | ||
|
||
from typing import cast | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import xarray as xr | ||
from arviz_base.labels import BaseLabeller | ||
from arviz_base.sel_utils import xarray_var_iter | ||
|
||
labeller = BaseLabeller() | ||
|
||
|
||
def psens(dt, group="log_likelihood"): | ||
""" | ||
Compute power-scaling sensitivity values. | ||
dt : DataTree | ||
group : str | ||
"log_likelihood" or "log_prior". | ||
""" | ||
# calculate lower and upper alpha values | ||
delta = 0.1 | ||
lower_alpha = 1 / (1 + delta) | ||
upper_alpha = 1 + delta | ||
|
||
# calculate importance sampling weights for lower and upper alpha power-scaling | ||
lower_w = np.exp(dt.azstats.power_scale_lw(alpha=lower_alpha, group=group)) | ||
lower_w = lower_w / np.sum(lower_w) | ||
|
||
upper_w = np.exp(dt.azstats.power_scale_lw(alpha=upper_alpha, group=group)) | ||
upper_w = upper_w / np.sum(upper_w) | ||
|
||
# calculate the sensitivity diagnostic based on the importance weights and draws | ||
return dt.azstats.power_scale_sens( | ||
lower_w=lower_w[group]["obs"].values.flatten(), # FIXME | ||
upper_w=upper_w[group]["obs"].values.flatten(), # FIXME | ||
delta=delta, | ||
) | ||
|
||
|
||
def psens_summary(data, threshold=0.05, round_to=3): | ||
""" | ||
Compute the prior/likelihood sensitivity based on power-scaling perturbations. | ||
Parameters | ||
---------- | ||
data : DataTree | ||
threshold : float, optional | ||
Threshold value to determine the sensitivity diagnosis. Default is 0.05. | ||
round_to : int, optional | ||
Number of decimal places to round the sensitivity values. Default is 3. | ||
Returns | ||
------- | ||
psens_df : DataFrame | ||
DataFrame containing the prior and likelihood sensitivity values for each variable | ||
in the data. And a diagnosis column with the following values: | ||
- "prior-data conflict" if both prior and likelihood sensitivity are above threshold | ||
- "strong prior / weak likelihood" if the prior sensitivity is above threshold | ||
and the likelihood sensitivity is below the threshold | ||
- "-" otherwise | ||
""" | ||
pssdp = psens(data, group="log_prior") | ||
pssdl = psens(data, group="log_likelihood") | ||
|
||
joined = xr.concat([pssdp, pssdl], dim="component").assign_coords( | ||
component=["prior", "likelihood"] | ||
) | ||
n_vars = np.sum([joined[var].size // 2 for var in joined.data_vars]) | ||
|
||
psens_df = pd.DataFrame( | ||
(np.full((cast(int, n_vars), 2), np.nan)), columns=["prior", "likelihood"] | ||
) | ||
|
||
indices = [] | ||
for i, (var_name, sel, isel, values) in enumerate( | ||
xarray_var_iter(joined, skip_dims={"component"}) | ||
): | ||
psens_df.iloc[i] = values | ||
indices.append(labeller.make_label_flat(var_name, sel, isel)) | ||
psens_df.index = indices | ||
|
||
def _diagnose(row): | ||
if row["prior"] >= threshold and row["likelihood"] >= threshold: | ||
return "prior-data conflict" | ||
if row["prior"] > threshold > row["likelihood"]: | ||
return "strong prior / weak likelihood" | ||
|
||
return "-" | ||
|
||
psens_df["diagnosis"] = psens_df.apply(_diagnose, axis=1) | ||
|
||
return psens_df.round(round_to) |