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

Vectorize operations for propensity score matching #1179

Merged
merged 2 commits into from
Jun 4, 2024

Conversation

rahulbshrestha
Copy link
Contributor

@rahulbshrestha rahulbshrestha commented May 12, 2024

This PR addresses this issue by introducing vectorized operations instead of the existing for-loops. This should speed up operations for large datasets.

This PR is a work in progress, and the remaining tasks include:

  • Add test cases to verify matching still works properly
  • Further vectorize one of the arrays
  • Cleanup

@rahulbshrestha rahulbshrestha marked this pull request as draft May 12, 2024 21:49
@amit-sharma
Copy link
Member

Thanks for starting this, @rahulbshrestha . Let us know once the PR is ready for review.

@rahulbshrestha
Copy link
Contributor Author

rahulbshrestha commented May 14, 2024

I ran some tests to check if the values of att and atc are the same before and after changes made in this PR:


### PREVIOUS IMPLEMENTATION
        att = 0
        numtreatedunits = treated.shape[0]
        treated_outcomes_old = []
        control_outcomes_old = []

        for i in range(numtreatedunits):

            treated_outcome = treated.iloc[i][self._target_estimand.outcome_variable[0]].item()
            control_outcome = control.iloc[indices[i]][self._target_estimand.outcome_variable[0]].item()
            treated_outcomes_old.append(treated_outcome)
            control_outcomes_old.append(control_outcome)
            att += treated_outcome - control_outcome

        att /= numtreatedunits


        print('Checking values of ATT: ')
        print('ATT (before): ', att)

        outcome_variable = self._target_estimand.outcome_variable[0]
        treated_outcomes = treated[outcome_variable]
        control_outcomes = list(control.iloc[indices.flatten()][outcome_variable])

        att = (treated_outcomes - control_outcomes).mean()

        print('ATT (after): ', att)
        print('Treated outcomes ', treated_outcomes_old == treated_outcomes)
        print('Control outcomes', control_outcomes_old == control_outcomes)

and the results when running on some test data:

Checking values of ATT: 
ATT (before):  10.923190922091228
ATT (after):  10.923190922091242
Treated outcomes  True
Control outcomes True
Checking values of ATC: 
ATC (before):  10.506587873468016
ATC (after):  10.506587873468012
Treated outcomes  True
Control outcomes True

Both lists, treated outcomes and control outcomes are the same before and after the changes I made. The ATT and ATC seems to be off by a couple digits after averaging (check last 3 digits in the example above), which is probably a rounding error. Is this a problem @amit-sharma?

@rahulbshrestha rahulbshrestha marked this pull request as ready for review May 14, 2024 12:17
Added todo comment

Signed-off-by: Rahul Shrestha <rahulshrestha0101@gmail.com>

formatting fix

Signed-off-by: Rahul Shrestha <rahulshrestha0101@gmail.com>

bug fix with string name

Signed-off-by: rahulbshrestha <rahulshrestha0101@gmail.com>
Signed-off-by: rahulbshrestha <rahulshrestha0101@gmail.com>
@rahulbshrestha
Copy link
Contributor Author

Hey @amit-sharma! I think this PR is ready to be merged :)

Copy link
Member

@amit-sharma amit-sharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @rahulbshrestha . The changes look good!

@amit-sharma
Copy link
Member

@all-contributors please add @rahulbshrestha for code.

Copy link
Contributor

@amit-sharma

I've put up a pull request to add @rahulbshrestha! 🎉

@amit-sharma amit-sharma merged commit 72e3ba0 into py-why:main Jun 4, 2024
30 checks passed
@rahulbshrestha rahulbshrestha deleted the vectorize branch June 4, 2024 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants