The 4 Core Concepts
1. Inputs (what the user gives you)
Your library needs only one thing: periodStarts[]
A list of period start dates:
[
{ date: "2024-12-01" },
{ date: "2024-12-29" },
{ date: "2025-01-26" }
]
From this, your library calculates cycle lengths like: 28 days, 28 days, ...
2. Engine (PredictionEngine class)
Users interact with PredictionEngine:
const engine = new PredictionEngine({ strategy: "calendar" });
engine.predictNextPeriod(history);
engine.predictOvulation(history);
engine.predictFertileWindow(history);
The engine:
- reads the cycle history
- chooses a prediction strategy
- calculates expected dates
- builds confidence
- adds human-friendly notes
3. Strategies (the brain of predictions)
Right now you support two:
✔️ calendar strategy
Simple average: take all cycle lengths, average them, and add to the last period to predict the next. Works well for regular cycles.
✔️ wma strategy
Weighted moving average: recent cycles have more importance, while old cycles have less. Better accuracy for slight irregularities.
4. What the predictions return
Every prediction returns the same type of structured output:
{
likely: "2025-02-10",
window: { start: "2025-02-09", end: "2025-02-11" },
confidence: 0.76,
notes: [
"Your average cycle length is 28 days.",
"Based on 7 cycles.",
"Your cycles are very regular. Prediction reliability is high."
]
}
likely: The most likely predicted date.
window: A small ± range for safety.
confidence: 0 → low, 1 → high. Shows how stable the user’s cycles are.
notes: Explains the prediction in human language.
Irregularity Check (anomalyCheck)
The simplest part of the library. You calculate cycle lengths: [28, 29, 28, 27, 29]. Then you check if these numbers are close (regular) or jumping (irregular).
const intervals = [20, 40, 35];
anomalyCheck(intervals);
{
irregular: true,
message: "Your cycles vary more than usual."
}
This improves the confidence score and the notes.
The Simplest "Full Explanation" Ever
You give your library a history of period start dates. It calculates cycle lengths. Then it predicts the next period, ovulation, and fertile window. It checks if the cycles are regular. It returns a date, a window, confidence, and human notes. And everything is TypeScript, clean, and accurate.