A while back I was staring at the code that turns a list of period start dates into a prediction, and I realized the hard part wasn't the averaging. It was deciding what to do with the records that don't fit.
Most examples use tidy 28-day cycles. Real logs don't. People forget to log a month, travel, get sick, stop taking something, start taking something. The gap between two start dates can come out at 19 days, or 63.
That leaves an app with three options:
- Throw the odd cycle away, so the average stays pretty.
- Clamp it to something "normal" before doing the math.
- Keep it, and be honest that the estimate just got less certain.
The first two are tempting because they make the output look stable. They also quietly overrule the person who was there. If someone logged a period after 63 days, that's what happened. An app that deletes it has decided its own output matters more than the record.
We went with the third option, and it shaped a few parts of the design.
Separate the fact from the estimate
The record is simple: a set of dates. One boolean per day, grouped into runs. No predicted_start column, no cached average, no confidence score stored next to it.
The prediction is derived on read. Every screen that needs it calls the same function with the same inputs: the recent complete cycles, the settings, and today's date. Nothing is persisted, because a derived value that lives in two places is a derived value that will eventually disagree with itself.
That separation is also what makes the odd cycle survivable. Lowering confidence doesn't mutate the history. Widening a window doesn't edit a date. The user's record stays exactly as entered, and the estimate is free to be wobbly.
Count days, not timestamps
Every cycle date is stored as a calendar date, not a timestamp. This sounds fussy until someone flies from Shanghai to Berlin and the app starts showing the period one day early, because midnight local time became 18:00 the previous day in UTC.
Date-only storage removes that entire class of bug. It also means "cycle length" is a subtraction between two dates, not a division of milliseconds by 86,400,000. Month boundaries and leap years are handled where they belong, in date arithmetic, not in presentation formatting.
Use complete cycles, and say how many
The prediction looks at complete cycles only: a start date and the next start date. The cycle currently in progress is not a cycle yet.
It uses at most the six most recent complete ones. Six is a compromise. More history smooths noise but also keeps honoring data from a year ago that may not describe the present. Fewer than six makes the window jump around. And when there aren't enough complete cycles, the app says so instead of dressing up two data points as a forecast.
The same function takes asOfDate explicitly. It never reads the system clock internally. That one rule makes the whole thing testable: you can ask what the app would have predicted on any given day, and get a deterministic answer.
What "lower confidence" actually means
When the recent cycles disagree with each other, the predicted range gets wider. That's it. The start of the next period is still shown as a range, because a single day would be a precision we haven't earned.
There's also a short note in plain language when the history looks unusual, something like "your recent cycles vary a lot, so this estimate is rough." No diagnosis, no advice, no red warning. The app is not a clinician and shouldn't pretend to be one.
The only input we refuse is data that can't be stored or parsed. A short gap or a long cycle is not an error. It's a fact.
The tests that keep it honest
The invariants are boring on purpose:
- a period that spans a month boundary keeps its length
- February in a leap year doesn't shift a date
- deleting a middle day splits one run into two
- a prediction computed with the same inputs twice returns the same result
- an unusual interval is preserved and lowers confidence rather than being dropped
None of these need a model. They need a date library and the discipline to keep facts and guesses in different boxes.
That's the whole argument. A period tracker will always be guessing about the future. It has no business guessing about the past.
I work on Mori, a free period tracker for Android that runs offline, needs no account, and keeps its data on the device.
Written with AI assistance.
