balance.stats_and_plots.impact_of_weights_on_outcome¶
- balance.stats_and_plots.impact_of_weights_on_outcome.compare_adjusted_weighted_outcome_ss(adjusted0: Sample, adjusted1: Sample, method: str = 't_test', conf_level: float = 0.95, round_ndigits: int | None = 3) pd.DataFrame[source]¶
Compare two adjusted Samples by testing outcomes under each set of weights.
- Parameters:
adjusted0 – First adjusted Sample (w0).
adjusted1 – Second adjusted Sample (w1).
method – Statistical test to use (“t_test”).
conf_level – Confidence level for the mean difference interval.
round_ndigits – Optional rounding for numeric outputs.
- Returns:
Outcome-by-statistic table comparing weighted outcomes.
- Return type:
pd.DataFrame
Examples: .. code-block:: python
import pandas as pd
from balance.sample_class import Sample from balance.stats_and_plots.impact_of_weights_on_outcome import (
compare_adjusted_weighted_outcome_ss,
)
- sample = Sample.from_frame(
- pd.DataFrame(
- {
“id”: [1, 2, 3], “x”: [0.1, 0.2, 0.3], “weight”: [1.0, 1.0, 1.0], “outcome”: [1.0, 2.0, 3.0],
}
), id_column=”id”, weight_column=”weight”, outcome_columns=(“outcome”,),
) target = Sample.from_frame(
- pd.DataFrame(
- {
“id”: [4, 5, 6], “x”: [0.1, 0.2, 0.3], “weight”: [1.0, 1.0, 1.0], “outcome”: [1.0, 2.0, 3.0],
}
), id_column=”id”, weight_column=”weight”, outcome_columns=(“outcome”,),
) adjusted_a = sample.set_target(target).adjust(method=”null”) adjusted_b = sample.set_target(target).adjust(method=”null”) adjusted_b.set_weights(pd.Series([1.0, 2.0, 3.0], index=adjusted_b.df.index))
- impact = compare_adjusted_weighted_outcome_ss(
adjusted_a, adjusted_b, round_ndigits=3
) print(impact.to_string())
mean_yw0 mean_yw1 mean_diff diff_ci_lower diff_ci_upper t_stat p_value n
outcome outcome 2.0 4.667 2.667 -4.922 10.256 1.512 0.27 3.0
- balance.stats_and_plots.impact_of_weights_on_outcome.weights_impact_on_outcome_ss(y: Iterable[float] | pd.Series | np.ndarray, w0: Iterable[float] | pd.Series | np.ndarray, w1: Iterable[float] | pd.Series | np.ndarray, method: str = 't_test', conf_level: float = 0.95) pd.Series[source]¶
Evaluate whether weighting changes the outcome by testing y*w0 vs y*w1.
Note: Weights are normalized to have mean 1 before computing the weighted products. In the balance package, weights are typically normalized to sum to the sample size, so this additional normalization ensures comparability.
- Parameters:
y – Outcome values.
w0 – Baseline weights.
w1 – Alternative weights.
method – Statistical test to use (“t_test”).
conf_level – Confidence level for the mean difference interval.
- Returns:
Summary statistics for the weighted outcome comparison.
- Return type:
pd.Series
Examples: .. code-block:: python
import pandas as pd
- from balance.stats_and_plots.impact_of_weights_on_outcome import (
weights_impact_on_outcome_ss,
)
- result = weights_impact_on_outcome_ss(
y=pd.Series([1.0, 2.0, 3.0, 4.0]), w0=pd.Series([1.0, 1.0, 1.0, 1.0]), w1=pd.Series([1.0, 2.0, 1.0, 2.0]), method=”t_test”,
) print(result.round(3).to_string())
mean_yw0 2.500 mean_yw1 4.000 mean_diff 1.500 diff_ci_lower -1.547 diff_ci_upper 4.547 t_stat 1.567 p_value 0.215 n 4.000