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

Adds code for pregnancy due date modeling blog post #1081

Merged
merged 1 commit into from
Aug 5, 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
22 changes: 22 additions & 0 deletions examples/due_date_probabilities/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Modeling Pregnancy Due Dates using Hamilton

This is an example of developing and applying a simple statistical model using Hamilton
for a very common problem. What is the probability that a baby will be born (before, on, after)
X date?


You can find the notebook on google collab here:
<a target="_blank" href="https://colab.research.google.com/github/DAGWorks-Inc/hamilton/blob/main/examples/due_date_probabilities/notebook.ipynb">
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open Me In Google Colab"/>
</a>

You can read the full description in the [post](https://blog.dagworks.io/p/181bb751-2e58-4e76-8d53-5a8c81ea16cb).

In this, you'll find the following files:

1. [notebook.ipynb](notebook.ipynb) - The Jupyter notebook that walks through the process of developing/running the model.
2. [base_dates.py] -- a Hamilton module that generates date-related series
3. [probability_estimation.py] -- a Hamilton module that computes a statistical model (estimating parameters for probability due dates)
4. [probabilities.py] -- a Hamilton module that runs that model over a set of dates

Run the notebook to see the process of developing the model and running it.
21 changes: 21 additions & 0 deletions examples/due_date_probabilities/base_dates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import datetime

import pandas as pd


def due_date(start_date: datetime.datetime) -> datetime.datetime:
"""The due date is start_date + 40 weeks. Start date is the date of the expecting mother's last period"""
return start_date + datetime.timedelta(weeks=40)


def possible_dates(
due_date: datetime.datetime,
buffer_before_due_date: int = 8 * 7,
buffer_after_due_date: int = 4 * 7,
) -> pd.Series:
"""Gets all the reasonably possible dates (-8 weeks, + 4 weeks) of delivery"""
idx = pd.date_range(
due_date - datetime.timedelta(days=buffer_before_due_date),
due_date + datetime.timedelta(days=buffer_after_due_date),
)
return pd.Series(data=idx, index=idx)
Loading