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

Feature p value #214

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion backend-python/media_impact_monitor/impact.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import pandas as pd

from media_impact_monitor.events import get_events
Expand Down Expand Up @@ -87,6 +86,7 @@ def get_impact(q: ImpactSearch) -> Impact:
mean=impact["mean"].loc[n_days],
ci_upper=impact["ci_upper"].loc[n_days],
ci_lower=impact["ci_lower"].loc[n_days],
p_value=impact["p_value"].loc[n_days],
),
absolute_impact_time_series=[
DatedMeanWithUncertainty(**d)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,10 @@ def estimate_mean_impact(
lambda x: confidence_interval_ttest(x.dropna(), 0.95)[1], axis=1
)
return pd.DataFrame(
{"mean": average, "ci_lower": ci_lower, "ci_upper": ci_upper}
{
"mean": average,
"ci_lower": ci_lower,
"ci_upper": ci_upper,
"p_value": 1, # TODO
}
), warnings
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_mean_impact_estimates():
assert impacts_df.index[0] == -4
assert impacts_df.index[-1] == 6
assert len(impacts_df) == 4 + 7
assert set(impacts_df.columns) == {"mean", "ci_lower", "ci_upper"}
assert set(impacts_df.columns) == {"mean", "ci_lower", "ci_upper", "p_value"}
for i in range(-4, -1):
mean = impacts_df.loc[i, "mean"]
assert -50 <= mean <= 50
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import pandas as pd
import statsmodels.api as sm

Expand Down Expand Up @@ -69,7 +68,7 @@ def regress(
return {
"date": day,
"mean": model.params[treatment],
"p": model.pvalues[treatment],
"p_value": model.pvalues[treatment],
"ci_lower": model.conf_int(alpha=alpha)[0][treatment],
"ci_upper": model.conf_int(alpha=alpha)[1][treatment],
}
Expand Down Expand Up @@ -103,5 +102,5 @@ def estimate_impact(
for day in outcome_days
]
)
impacts = impacts.set_index("date")[["mean", "ci_lower", "ci_upper"]]
impacts = impacts.set_index("date")[["mean", "ci_lower", "ci_upper", "p_value"]]
return impacts, limitations
2 changes: 2 additions & 0 deletions backend-python/media_impact_monitor/types_.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,15 @@ class MeanWithUncertainty(BaseModel):
mean: float = Field(description="Mean estimate.")
ci_upper: float = Field(description="Upper bound of the 95% confidence interval.")
ci_lower: float = Field(description="Lower bound of the 95% confidence interval.")
p_value: float = Field(description="P-value.")


class DatedMeanWithUncertainty(BaseModel):
date: int | date
mean: float
ci_upper: float
ci_lower: float
p_value: float


class ImpactEstimate(BaseModel):
Expand Down
Loading