DEV Community

Michael Lip
Michael Lip

Posted on • Originally published at zovo.one

Menstrual Cycle Tracking Is Pattern Recognition With Biological Noise

The average menstrual cycle is 28 days. But averages hide enormous variation. Normal cycles range from 21 to 35 days and individual cycle length varies by several days from month to month. Prediction accuracy depends on capturing this variability.

The prediction model

The simplest prediction method uses a rolling average of recent cycle lengths:

function predictNextPeriod(cycleDates) {
  // cycleDates: array of period start dates, most recent last
  if (cycleDates.length < 2) return null;

  const cycleLengths = [];
  for (let i = 1; i < cycleDates.length; i++) {
    const diff = (cycleDates[i] - cycleDates[i-1]) / (1000 * 60 * 60 * 24);
    cycleLengths.push(diff);
  }

  const avgLength = cycleLengths.reduce((a, b) => a + b) / cycleLengths.length;
  const lastPeriod = cycleDates[cycleDates.length - 1];

  const nextPeriod = new Date(lastPeriod);
  nextPeriod.setDate(nextPeriod.getDate() + Math.round(avgLength));

  const stdDev = Math.sqrt(
    cycleLengths.reduce((sum, l) => sum + (l - avgLength) ** 2, 0) / cycleLengths.length
  );

  return {
    predicted: nextPeriod,
    confidence: stdDev < 2 ? 'high' : stdDev < 4 ? 'medium' : 'low',
    averageCycle: Math.round(avgLength),
    variation: Math.round(stdDev * 10) / 10
  };
}
Enter fullscreen mode Exit fullscreen mode

Accuracy improves with data

With 3 recorded cycles, predictions are rough. With 6+ cycles, the rolling average converges toward the individual's true cycle length, and the standard deviation indicates reliability.

A standard deviation under 2 days means the cycle is highly regular and predictions will be accurate to within a day or two. A standard deviation above 5 days means the cycle is irregular and calendar-based prediction has limited value.

Factors that shift cycles

Cycle length varies with stress, travel, illness, weight changes, exercise intensity, and medication. These factors make prediction inherently uncertain, which is why tracking apps should always show a range rather than a single date.

For tracking cycles and predicting future periods, I built a calculator at zovo.one/free-tools/period-calculator. Enter your recent period start dates and it calculates your average cycle length, predicts the next period, and estimates the prediction confidence.


I'm Michael Lip. I build free developer tools at zovo.one. 500+ tools, all private, all free.

Top comments (0)