Skip to content

Reliability and Calibrated Prediction #491

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

Merged
merged 8 commits into from
Jan 16, 2023

Conversation

NathanielF
Copy link
Contributor

@NathanielF NathanielF commented Jan 6, 2023

This will be an example notebook demonstrating the techniques of survival analysis used in reliability context with a focus on the predictive distribution and calibrated prediction intervals from a Bayesian and MLE perspective. Relates to issue: #474

Adding this draft version here, because i'm having a little trouble with the pre-commit checks

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@NathanielF
Copy link
Contributor Author

Hi @drbenvincent and @OriolAbril

I think this is ready for review now. It's longer than i anticipated and i've cut out the idea to compare against conformal prediction as there is already quite a bit of detail in there.

The broader structure has three parts:

image

In the first part i'm just introducing time-to-failure data and the descriptive statistical analogues of survival and cdf functions. I follow a trajectory of first showing how we can analyse this kind of data with MLE or bootstrap style inference and then show on a trickier example with sparser data why the ability to add Bayesian priors to help calibrate the risk the profile is useful. Then i conclude with a "lets all be friends" kind of ending.

I'm not sure who is best placed to review, but since i deal with censored data and I know @drbenvincent recently updated the censored data notebook, i thought you might find it interesting? Curious about what you think too @OriolAbril? Happy to take any feedback on the content or structure...

@NathanielF NathanielF marked this pull request as ready for review January 6, 2023 13:23
Copy link
Member

@OriolAbril OriolAbril left a comment

Choose a reason for hiding this comment

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

I have commented on the myst file, but the changes should be done in ipynb and then use pre-commit to update myst one.

General comment: I would use https://myst-nb.readthedocs.io/en/latest/render/hiding.html to hide the source of some of the code. i.e. the stringIO ones can be hidden, we would only need to split them so the df_name to show the rendered table is in its own cell right below (not hidden so readers can follow the notebook and get the variable name even without expanding the cell). Some of the plots I think could also have their source hidden.

We'll use the posterior predictive distribution of the uniformative model. We show here how to derive the uncertainty in the estimates of the 95% prediction interval for the number of failures in a time interval.

```{code-cell} ipython3
def PI_failures(joint_draws, lp, up, n_at_risk):
Copy link
Member

@OriolAbril OriolAbril Jan 9, 2023

Choose a reason for hiding this comment

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

I'll update the code to use xarray-einstats, the inputs are xarray dataset and scalars IIUC, so there should be no need for looping in order to use scipy distributions. I can also try rerunning the notebook and finding other similar instances.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On this one... i think i kind of disagree. While it's not the most efficient way to do this operation, i wanted the parallel with the bootstrapping looping procedures to be clear. We're instantiating in a loop a reasonable model fit to the data in each iteration of the loop, and seeing the variation induced in downstream statistics... i think pedagogically this is simpler than hiding the similarity of the procedure in more efficient code....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops I think I misread this comment yesterday, did you want to try and streamline the loop? My only point was that I'd like to keep the analogy with the bootstrap procedure clear....

But I think I could just add more text explaining that too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, i've added some more text to highlight the relationship between the bootstrap and posterior predictive distributions if you want to work some xarray magic, that'd be cool! Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Updated, and found a bug in xarray-einstats while doing it 😄 (I have fixed it already)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it this one:

image

I updated to my xarray einstats version, but still getting an error when i try to re-run your new function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can recreate with just this call:
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh!!!! Sorry, i see the issue, the latest change is not on pip!!!! Sorry, slow this morning.

image

Ehm, that's cool. I'm happy with the changes and you can merge if you want. But for reproducibility should we be concerned that the user can't easily install the required packages?

Copy link
Member

Choose a reason for hiding this comment

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

I still have one PR I to get into 0.5 release, but I plan to release it soon. We can wait until I actually make the release one of these days, with the watermark having the 0.5.0dev flag though I don't think it is a deal breaker to merge it even before that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, cool! Let's do it. Thanks again for all your help on this one! I'm happy with it now if you are.

…hide data import and plotting funcs

Signed-off-by: Nathaniel <[email protected]>
@NathanielF
Copy link
Contributor Author

Thanks so much for the review. @OriolAbril. I've quibbled with one of the requests but i think i've addressed the rest. I've added hide-input tags to a bunch of the data loads and some of the more involved plotting functions.

…diction intervals with posterior predictive intervals

Signed-off-by: Nathaniel <[email protected]>
@NathanielF NathanielF requested a review from OriolAbril January 12, 2023 09:29
Copy link
Member

@OriolAbril OriolAbril left a comment

Choose a reason for hiding this comment

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

I reran the notebook locally to update that sampling snippet. And did a couple extra changes that I have commented here in the review. Let me know what you think, they are all quite minor except for the sampling one.

joint_draws = az.extract_dataset(idata, group="posterior", num_samples=1000)[
["alpha", "beta"]
].to_dataframe()
joint_draws = az.extract(idata, num_samples=1000)[["alpha", "beta"]]
Copy link
Member

Choose a reason for hiding this comment

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

The fact that was a dataframe wasn't being used anywhere, so I changed that because now below we take advantage of the fact joint_draws is an xarray dataset.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair.

We'll use the posterior predictive distribution of the uniformative model. We show here how to derive the uncertainty in the estimates of the 95% prediction interval for the number of failures in a time interval.

```{code-cell} ipython3
def PI_failures(joint_draws, lp, up, n_at_risk):
Copy link
Member

Choose a reason for hiding this comment

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

Updated, and found a bug in xarray-einstats while doing it 😄 (I have fixed it already)

@NathanielF
Copy link
Contributor Author

NathanielF commented Jan 16, 2023

Thanks @OriolAbril I'm fine with all the changes you made, i understand the logic of the xarray-einstats version too. I think the code is fine, just using the dev version of xarray-einstats makes reproducibility harder. We could add a note to the effect that this uses a dev version of xarrray. What do you think?

@NathanielF
Copy link
Contributor Author

NathanielF commented Jan 16, 2023

Just fixing a typo at the beginning.

@OriolAbril OriolAbril merged commit 462700a into pymc-devs:main Jan 16, 2023
@NathanielF
Copy link
Contributor Author

Woop, woop! Thanks 😊!

@NathanielF NathanielF deleted the reliability_calibration_clean branch January 16, 2023 20:28
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