-
Notifications
You must be signed in to change notification settings - Fork 59
[WIP] add test implementation of carrillo and rosenbaum #208
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
Open
ljwolf
wants to merge
1
commit into
pysal:main
Choose a base branch
from
ljwolf:cf
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.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # From Carillo | ||
| # 1. estimate the probability that an observation is in time 0 using | ||
| # P(T=t) = N_t/(\sum_k N_k) | ||
| # 2. estimate P(T=t0 | X=x) with dependent variate is_t0 using all data. | ||
| # should be able to give a predicted probabilty T=t0 for any X | ||
| # 3. only for observations where T=t1, compute the probability that T=t0. This is P(T=t0 | X=x) | ||
| # This should be the predicted probability for observations in t1 using the model from before. | ||
| # 4. Then, compute the weights using the tau swap function: | ||
| # tau t1 -> t0 := ( P(T=t0|X=x) / (1 - P(T=t0|X=x) ) / (P(T=t0)/(1-P(T=t0))) | ||
| # 5. Re-weight the distribution of T1 using tau weights. This is the new distribution in T=1 | ||
|
|
||
| # In plain english, this re-weights the observed pattern in T1 using the odds of seeing x in t0. | ||
|
|
||
| from sklearn.linear_model import LogisticRegression | ||
| from sklearn.base import BaseEstimator, TransformerMixin | ||
|
|
||
| class Spatial_Counterfactual(BaseEstimator, TransformerMixin): | ||
| def __init__(self, y0=None, exog0=None, geometry=None, predictor=LogisticRegression): | ||
| self.y0 = y0 | ||
| self.exog0 = exog0 | ||
| self.geometry = geometry | ||
| self.predictor = predictor | ||
|
|
||
| def fit(self, y, X, *, **predictor_kwargs): | ||
|
|
||
| # 1 | ||
| n0, n1 = len(self.y0), len(y) | ||
| p0, p1 = n0/(n0 + n1), n1/(n0 + n1) | ||
| assert self.exog0.shape[0] == n0, "exog0 and y0 are not aligned!" | ||
| assert X.shape[0] == n1, "exog1 and y1 are not aligned!" | ||
| # is this necessary: | ||
| # assert n0 == n1, "spatial support changes between time periods!" | ||
| # 2 | ||
| y_pooled = numpy.hstack((self.y0, y)) | ||
| exog_pooled = numpy.row_stack((self.exog0, X)) | ||
| is_t0 = numpy.hstack((numpy.ones_like((y0)), numpy.zeros_like((y)))) | ||
|
|
||
| self.predictor_ = self.predictor(**predictor_kwards).fit(is_t0, exog_pooled) | ||
| # 3 | ||
| t1_p = self.predictor_.predict(X) | ||
| # 4 | ||
| self.tau_ = ( | ||
| (t1_p/(1 - t1_p)) | ||
| / | ||
| (p0 / (1 - p0)) | ||
| ) | ||
| # 5 | ||
| self.actual_ = y | ||
| self.counterfactual_ = self.tau_ * self.actual_ | ||
|
|
||
| def predict(self, X, *,): | ||
| n0, n1 = len(self.y0), X.shape[0] | ||
| p0, p1 = n0/(n0 + n1), n1/(n0 + n1) | ||
| tk_p = self.predictor_.predict(X) | ||
| tau_ = ( | ||
| (tk_p/(1 - tk_p)) | ||
| / | ||
| (p0 / (1 - p0)) | ||
| ) | ||
| return self.actual_ * tau_ | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not yet certain that this does the "right" thing. C&R say you need to apply tau against P(y1,y2|tau), and the empirical distribution of that is
actual_(y). But, I'm not sure where the kernel density function re-weighting needs to come in? Need to continue working on it.