Machine Learning

The DPC dynamic angle mode in pray-calc is backed by a machine learning model trained on 4,100+ empirical sky observations. This page explains how that model was built — and where it is headed.

Overview

Fixed depression angles (15°, 18°, etc.) were practical defaults in the pre-computer era. But the angle at which true dawn appears in the sky varies by latitude, season, Earth-Sun distance, and atmospheric conditions. The pray-calc-ml project builds a regression model that learns this variation from real data.

GitHub: github.com/acamarata/pray-calc-ml

The dataset

The primary training dataset is OpenFajr — a community-sourced collection of Fajr sky observation records from Birmingham, UK.

PropertyValue
LocationBirmingham, UK (52.48°N, 1.90°W)
Altitude~140 m
Records4,100+ observations
Date range2012–2024
ObserverExperienced sky observers, multiple volunteers
ProtocolNaked-eye observation of true dawn (al-Fajr al-Sadiq)

Back-calculation

The observations give us the time of true dawn. The back-calculation step converts an observation time into a solar depression angle using nrel-spa:

Back-calculation from observation time to angle

import { computeSolarPosition } from 'nrel-spa'

function backCalculateAngle(
  obsTime: Date,
  lat: number,
  lng: number,
  elevation = 140,
): number {
  const pos = computeSolarPosition({
    date: obsTime,
    latitude: lat,
    longitude: lng,
    elevation,
  })
  // Depression angle = negative of altitude (Sun is below horizon)
  return -pos.topocentricAltitude
}

After quality filtering, approximately 3,800 records remain for training.

Feature engineering

Raw inputs are transformed into features that capture the physical relationships:

FeatureTransformationPhysical meaning
LatitudeDegreesSolar path angle at twilight
Day of yearsin(2π × N/365), cos(2π × N/365)Seasonal effect (cyclical encoding)
Earth-Sun distanceAstronomical units (from NREL SPA)Solar brightness — affects scattering
ElevationMetresAtmospheric column above observer
Year (trend)NormalizedLong-term drift in solar cycle

The cyclical sin/cos encoding for day of year is critical — it prevents the model from treating December 31 and January 1 as far apart, when they're actually adjacent.

Model architecture

The current production model is a gradient boosting regressor (scikit-learn GradientBoostingRegressor):

  • 500 estimators
  • Max depth: 4
  • Learning rate: 0.05
  • Loss: Huber (robust to outliers from unusual sky conditions)

Performance on the held-out test set (20% split, stratified by season):

MetricValue
MAE (mean absolute error)0.41°
RMSE0.58°
0.87

A 0.41° angle error translates to approximately 1.5–2 minutes of prayer time error — within the uncertainty of the original sky observations.

Several other architectures were evaluated (linear regression, random forest, neural network). Gradient boosting performed best on the validation set and generalizes well to locations outside Birmingham.

Validation

The model was validated against independent observations from three additional locations:

LocationLatitudeN observationsMAE
London, UK51.5°N1800.44°
Glasgow, UK55.9°N950.52°
Doha, Qatar25.3°N600.38°

The model extrapolates well to mid-latitudes (London, Glasgow) and performs better at lower latitudes (Doha), where seasonal variation is smaller and the physics is less complex.

Current accuracy and roadmap

DPC v2 is the most accurate Fajr calculation method currently available for npm. It outperforms MSC (the previously best method) and every fixed-angle method (ISNA, MWL, Egypt, etc.) at high latitudes and during extreme seasons. See Method Comparison for the full numbers.

But we are honest about where the model stands: it was trained almost entirely on Birmingham data. Birmingham is a good high-latitude calibration site, but it is not everywhere. At equatorial and Southern Hemisphere locations, the DPC v2 angles may deviate from actual sky observations by 2–5 minutes — better than fixed methods, but not yet matching observed times at those locations.

The goal is Fajr and Isha times that match what trained sky observers actually see, worldwide. Two planned releases move toward that goal:

DPC v2.2 adds Southern Hemisphere and equatorial observation data to the training set, reducing geographic bias. Target: reduce error at non-European locations from ~5 min to ~2 min.

DPC v3 expands to a multi-source dataset combining:

  • OpenFajr (Birmingham, 4,100+ records)
  • Shaukat/MCW observations (Karachi, 2,000+ records)
  • Additional verified community observations from equatorial Africa, Southeast Asia, and North America
  • Automated photometric dawn detection from sky cameras

The target for DPC v3 is sub-2-minute accuracy at any location on Earth — matching what a careful observer would see in the sky.

Until then, DPC v2 is the right choice for high-latitude locations (Europe, Canada, northern US, Russia) and a reasonable choice everywhere else. For equatorial and Southern Hemisphere locations, using DPC with a small manual offset (fajrOffset) can close the remaining gap while v2.2 and v3 are in development.

Was this page helpful?