Sequences
Three small LSTMs that forecast Jakarta's air, the rupiah and Wikipedia attention from data fetched the moment you open the page.
−15%
Air: error vs best simple guess
11.3 µg/m³
Air: average miss over 24 h
82.3%
Rupiah: inside the 80% band
50%
Wikipedia: typical miss, 7 days
Overview
Most forecasting demos replay an old dataset. This one doesn't: every visit pulls the newest hourly air quality for Jakarta, the latest ECB exchange rate and yesterday's Indonesian Wikipedia pageviews, and forecasts forward from now. The forecast it made one horizon ago is checked against what actually happened, live.
The three series behave very differently, and the results say so. For air, the LSTM cuts the error of the best simple guess by 15%. For Wikipedia attention, one model for all articles learns how spikes fade. For the rupiah, it does not beat "no change", and the page says that plainly: what does work there is an uncertainty band that widens when the currency gets jumpy.
How it works
From raw file to prediction.
Every number below comes from the training run.
- 01
History
37,409 h · 7,132 d
Plus 449 Wikipedia articles, cached once
- 02
Windows
72 h · 60 d · 28 d
Scaled inputs, split by time
- 03
Baselines
6 simple guesses
Same as now, yesterday, no change, drift, last week, 7-day mean
- 04
LSTM
32 · 16 · 32 units
Keras, Huber loss, early stopping
- 05
Ship
11,476 weights
JSON; TypeScript forward pass = NumPy
- 06
Live
fetch → forecast → check
Three public APIs, from your browser
Inside the model
A network with memory.
Scroll to watch the LSTM read three days of real Jakarta air, hour by hour, and forecast the next day, with its real weights.
The data
Three live signals, always the latest.
Air pollution, a currency and public attention: three series with very different rhythms. Each was trained on years of history, and each is fetched fresh every time you open this page.
Jakarta air, PM2.5
Fine particles in the air over Jakarta, every hour, from the Copernicus atmosphere model (CAMS) via Open-Meteo.
37,409
hourly values
4 Aug 2022
since
24 hours
forecast
CC BY 4.0 · non-commercial
Rupiah, USD/IDR
The European Central Bank's daily reference rate, crossed to rupiah per US dollar by the Frankfurter API.
7,132
business days
4 Jan 1999
since
5 days
forecast
ECB reference rates
Wikipedia attention
Daily human views of the articles that trended on Indonesian Wikipedia over the past year: what people suddenly want to know.
449
articles
121,483
daily windows
7 days
forecast
Wikimedia pageviews API
01
Fetch today
Your browser calls the three public APIs directly: no key, no server of mine
02
Window
The last 72 hours, 60 business days or 28 days, scaled as in training
03
LSTM
A small recurrent network reads the window step by step, in TypeScript
04
Forecast
The next 24 hours, 5 business days or 7 days, from now
05
Check yesterday
The forecast it made one horizon ago, against what then happened
Try it
Forecasts from right now.
Pick a series. The page pulls the newest data, the LSTM forecasts forward from this moment, and the check below each chart shows how its forecast from one horizon ago actually did.
Live demo
runs in your browser
Fetching the latest data from Open-Meteo (CAMS)…
PM2.5 over Jakarta: the last 72 hours and the LSTM's next 24
Right now
75
µg/m³ · 5.0× the WHO daily guideline
LSTM · next 24 h
54
average · peak 77 at 00:00
CAMS forecast
–
shown when live
Check: the forecast made 24 hours ago, against what happened
12
LSTM avg miss
17
same as then miss
The LSTM was closer this time. One window is luck either way; the test results below average 2,136 windows over the next 24 hours.
Data: Open-Meteo Air Quality API, built on CAMS (Copernicus Atmosphere Monitoring Service), CC BY 4.0, non-commercial use. The “actual” line is CAMS' own model estimate for the grid cell over Jakarta, not a sensor reading, so this LSTM learns to forecast that estimate.
What the history says
Three rhythms: a daily cycle, a random walk, a spike that fades.
Air is dirtiest around 07:00 and cleanest around 16:00. A daily cycle this strong is why "same hour yesterday" is a tough baseline.
The dry season is the worst: May has the highest median. Over the whole history, 99% of days averaged above the WHO daily guideline of 15 µg/m³.
From about 8,549 rupiah per dollar in 1999 to 18,108 now. The long climb is visible; next week's move is not.
Attention fades fast: by day 4 the typical article has lost half of its spike-day views. "Same as yesterday" assumes no fading at all, which is why it loses after spikes.
How good is it, honestly
Air: 15% better than the best simple guess. Rupiah: no better than “no change”.
Every model was tested on the most recent stretch of history it never saw (from 12 Aug 2026 for air, 22 Nov 2024 for the rupiah, 11 Sep 2026 for Wikipedia), against the simple guesses a forecaster has to beat.
Averaged over the 24 hours, the LSTM misses by 11.3 µg/m³, against 13.3 for "same hour yesterday" and 18.2 for "same as now". Over 2,136 test windows.
Each blue point was forecast 24 hours before it happened. The LSTM follows the daily cycle and most swings; it smooths sharp peaks, a typical price of minimising average error.
Five days ahead the LSTM misses by Rp 92 on average, "no change" by Rp 93: within a few rupiah, because exchange rates move close to a random walk, and 495 test days are not enough to call a winner. The 61% "right direction" score is the trend, not insight: the rupiah weakened in 62% of test weeks, and the drift guess scores 62%.
| Model | Day 1 | Day 5 | Direction, day 5 |
|---|---|---|---|
| LSTM | Rp 44.9 | Rp 91.6 | 61.2% |
| No change (random walk) | Rp 44.8 | Rp 93.4 | – |
| Long-run drift | Rp 44.7 | Rp 91.6 | 61.6% |
82.3%
inside the 80% band
±131
day-5 band, calm weeks
±205
day-5 band, jumpy weeks
What does work is the uncertainty: the band, scaled by the last 20 days' volatility, held 77.1% of outcomes in calm weeks and 87.5% in jumpy ones, against a target of 80%.
One LSTM for all 449 articles. Its typical miss is 50% against 60% for the best simple guess (same as yesterday). Right after a spike the gap widens: 89% vs 167% for "same as yesterday".
Three small LSTMs trained by me in TensorFlow, run on live data in your browser.
| Here | |
|---|---|
| Models | Three LSTMs (32, 16 and 32 units) plus a dense output layer, trained in ml/train.py, weights shipped as JSON |
| Runs on | Your browser: the LSTM is written in TypeScript and checked against NumPy; the data comes straight from the public APIs |
| Air | Beats both simple guesses on 90 test days. Its “actuals” are CAMS model estimates, not sensors, so it learns to forecast that estimate |
| Rupiah | Does not beat “no change”. It is shown because an honest forecaster says so; the volatility band is the useful part |
| Wikipedia | One model for every article; it can't see news coming, so it can't predict a new spike, only how an existing one fades |
| Not for | Health, trading or editorial decisions: a portfolio demonstration of the method |
Build notes
How it's built, step by step.
The real code behind this page, taken from the repository when the site is built and split at each step of the training run. Copy any step, or the whole script with its imports and helpers.
Run it yourself
live_forecasts() from ml/train.py + 2 helpers (r, save)
pip install numpy pandas scikit-learn tensorflow · data in ml/data/raw/ · dataset source
Training · Python
- 1
Set up the LSTM recipe
Seeds TensorFlow and defines one shared recipe: an LSTM layer plus a dense head that outputs all forecast steps at once, trained with Huber loss and early stopping.
ml/train.py:3324 · 20 lines
import keras import tensorflow as tf from keras import layers print("live-forecasts") keras.utils.set_random_seed(SEED) tf.config.experimental.enable_op_determinism() LIVE = RAW / "live" parity: dict = {} def build(L, F, units, H): return keras.Sequential([keras.Input((L, F)), layers.LSTM(units, name="lstm"), layers.Dense(H, name="out")]) def fit(model, Xtr, ytr, Xva, yva, epochs=60, batch=256): model.compile(optimizer=keras.optimizers.Adam(1e-3), loss=keras.losses.Huber(delta=1.0)) h = model.fit( Xtr, ytr, validation_data=(Xva, yva), epochs=epochs, batch_size=batch, verbose=0, shuffle=True, callbacks=[keras.callbacks.EarlyStopping(patience=6, restore_best_weights=True), keras.callbacks.ReduceLROnPlateau(factor=0.5, patience=3, min_lr=1e-5)], ) return {"loss": [r(v, 5) for v in h.history["loss"]], "val_loss": [r(v, 5) for v in h.history["val_loss"]]} - 2
Mirror the browser's forward pass
Exports LSTM weights as plain numbers, re-runs them in NumPy exactly as src/lib/lstm.ts does, and measures the gap to Keras to prove the gate order matches.
ml/train.py:3346 · 35 lines
def export(model): k, rk, b = model.get_layer("lstm").get_weights() w, bd = model.get_layer("out").get_weights() rnd = lambda a: [float(f"{v:.6g}") for v in np.asarray(a).ravel()] # noqa: E731 return {"units": int(rk.shape[0]), "inputDim": int(k.shape[0]), "horizon": int(w.shape[1]), "kernel": rnd(k), "recurrent": rnd(rk), "bias": rnd(b), "dense": {"w": rnd(w), "b": rnd(bd)}} def run_np(m, X): """The browser's forward pass (src/lib/lstm.ts) on the rounded exported weights. X: n x L x F.""" U, F, H = m["units"], m["inputDim"], m["horizon"] K = np.array(m["kernel"]).reshape(F, 4 * U) R = np.array(m["recurrent"]).reshape(U, 4 * U) B = np.array(m["bias"]) W = np.array(m["dense"]["w"]).reshape(U, H) Bd = np.array(m["dense"]["b"]) sig = lambda x: 1 / (1 + np.exp(-x)) # noqa: E731 X = np.asarray(X, dtype=np.float64) - 3
Prepare Jakarta PM2.5 windows
Hourly readings are log-scaled and cut into 72-hour windows with hour-of-day features to predict the next 24; the last 90 days are test, the 30 before validation.
ml/train.py:3383 · 20 lines
out: dict = {} # ---------------------------------------------------------------- PM2.5, Jakarta, hourly d = json.load(open(LIVE / "pm25_jakarta.json")) s = pd.Series(d["pm2_5"], index=pd.to_datetime(d["time"]), dtype="float64") s = s[s.index < pd.Timestamp(d["fetched"])] # the rest of the fetch day is CAMS forecast, not analysis s = s[s.first_valid_index() :].interpolate(limit=6, limit_area="inside") v = s.to_numpy() y = np.log1p(v) hours = s.index.hour.to_numpy() L, H = 72, 24 n = len(s) test_from = n - 90 * 24 val_from = test_from - 30 * 24 mu, sd = float(np.nanmean(y[:val_from])), float(np.nanstd(y[:val_from])) feats = np.stack([(y - mu) / sd, np.sin(2 * np.pi * hours / 24), np.cos(2 * np.pi * hours / 24)], 1) ts = np.array([t for t in range(L - 1, n - H) if np.isfinite(y[t - L + 1 : t + H + 1]).all()]) X = np.stack([feats[t - L + 1 : t + 1] for t in ts]).astype("float32") Y = np.stack([y[t + 1 : t + H + 1] - y[t] for t in ts]).astype("float32") tr_m, va_m, te_m = ts + H < val_from, (ts >= val_from) & (ts + H < test_from), ts >= test_from - 4
Train and test the PM2.5 model
A 32-unit LSTM learns the next 24 hourly changes; its exported-weights forecasts face same-as-now and same-hour-yesterday baselines and a 14-day backtest.
ml/train.py:3404 · 54 lines
mdl = build(L, 3, 32, H) hist_pm = fit(mdl, X[tr_m], Y[tr_m], X[va_m], Y[va_m]) m_pm = export(mdl) gap_pm = vs_keras(mdl, m_pm, X[te_m]) t_te = ts[te_m] P = run_np(m_pm, X[te_m]) actual = np.stack([v[t + 1 : t + H + 1] for t in t_te]) preds = { "LSTM": np.expm1(y[t_te][:, None] + P), "Same as now": np.repeat(v[t_te][:, None], H, 1), "Same hour yesterday": np.stack([v[t + 1 - 24 : t + H + 1 - 24] for t in t_te]), } pm_cmp = [] for k, p in preds.items(): e = np.abs(p - actual) pm_cmp.append({"model": k, "mae": r(e.mean(), 3), "by_h": [r(x, 3) for x in e.mean(0)]}) - 5
Prepare USD/IDR windows
Daily log returns, scaled by their long-run spread, form 60-day windows that predict the next 5 days; the last 500 days are test and the 250 before validation.
ml/train.py:3460 · 18 lines
# ---------------------------------------------------------------- USD/IDR, daily (ECB business days) d = json.load(open(LIVE / "usd_idr.json")) fx = pd.Series(d["rates"], dtype="float64") fx.index = pd.to_datetime(fx.index) v = fx.to_numpy() lr = np.log(v) ret = np.diff(lr, prepend=lr[0]) L, H, VOLW, VOLF = 60, 5, 20, 0.25 n = len(v) test_from = n - 500 val_from = test_from - 250 sd_r = float(ret[1:val_from].std()) feat = (ret / sd_r)[:, None] ts = np.arange(L, n - H) X = np.stack([feat[t - L + 1 : t + 1] for t in ts]).astype("float32") Y = np.stack([(lr[t + 1 : t + H + 1] - lr[t]) / sd_r for t in ts]).astype("float32") vol = np.maximum(X[:, -VOLW:, 0].astype(np.float64).std(1), VOLF) # recent volatility, in units of the long-run daily move tr_m, va_m, te_m = ts + H < val_from, (ts >= val_from) & (ts + H < test_from), ts >= test_from - 6
Train and test the USD/IDR model
A 16-unit LSTM faces random-walk and drift baselines; the 0.1 and 0.9 quantiles of its validation errors, scaled by recent volatility, set its forecast band.
ml/train.py:3479 · 60 lines
mdl = build(L, 1, 16, H) hist_fx = fit(mdl, X[tr_m], Y[tr_m], X[va_m], Y[va_m]) m_fx = export(mdl) gap_fx = vs_keras(mdl, m_fx, X[te_m]) resid = (Y[va_m] - run_np(m_fx, X[va_m])) / vol[va_m][:, None] q_lo, q_hi = np.quantile(resid, 0.1, axis=0), np.quantile(resid, 0.9, axis=0) t_te = ts[te_m] P = run_np(m_fx, X[te_m]) now = v[t_te][:, None] act = np.stack([v[t + 1 : t + H + 1] for t in t_te]) to_rate = lambda z: now * np.exp(z * sd_r) # noqa: E731 drift = float(ret[1:val_from].mean()) preds = {"LSTM": to_rate(P), "No change (random walk)": np.repeat(now, H, 1), "Long-run drift": now * np.exp(drift * np.arange(1, H + 1))[None, :]} moved = act[:, -1] != now[:, 0] fx_cmp = [] for k, p in preds.items(): - 7
Prepare Wikipedia attention windows
Every article feeds one global model: 28-day log windows with weekday features predict the next 7 days, skipping quiet windows under 20 views and flagging spikes.
ml/train.py:3541 · 27 lines
# ---------------------------------------------------------------- Indonesian Wikipedia attention, daily, one global model d = json.load(open(LIVE / "wiki_id.json")) L, H, MINV = 28, 7, 20 days = pd.date_range(end=pd.Timestamp(d["fetched"]) - pd.Timedelta(days=1), periods=2 * 365 + 30) dow = days.day_of_week.to_numpy() # Monday = 0, as in the browser's (getUTCDay() + 6) % 7 dsin, dcos = np.sin(2 * np.pi * dow / 7), np.cos(2 * np.pi * dow / 7) test_from_day, val_from_day = days[-60], days[-90] Xs, Ys, when, base_seas, base_mean, spike = [], [], [], [], [], [] for ser in d["series"].values(): vv = pd.Series({pd.Timestamp(k): x for k, x in ser.items()}, dtype="float64").reindex(days).fillna(0.0).to_numpy() lv = np.log1p(vv) for t in range(L - 1, len(vv) - H, 2): w = lv[t - L + 1 : t + 1] if np.expm1(w.mean()) < MINV: continue Xs.append(np.stack([w - w.mean(), dsin[t - L + 1 : t + 1], dcos[t - L + 1 : t + 1]], 1)) - 8
Train and test the Wikipedia model
A 32-unit LSTM is scored against yesterday, last-week and 7-day-average baselines, overall and on spike days, and its post-spike decay is compared with reality.
ml/train.py:3569 · 46 lines
mdl = build(L, 3, 32, H) hist_wk = fit(mdl, X[tr_m], Y[tr_m], X[va_m], Y[va_m], epochs=40, batch=512) m_wk = export(mdl) gap_wk = vs_keras(mdl, m_wk, X[te_m]) P = run_np(m_wk, X[te_m]) Yt = Y[te_m].astype(np.float64) sp = np.array(spike)[te_m] base = {"LSTM": P, "Same as yesterday": np.zeros_like(Yt), "Same weekday last week": np.array(base_seas)[te_m], "Last 7-day average": np.repeat(np.array(base_mean)[te_m][:, None], H, 1)} wk_cmp = [] for k, p in base.items(): e = np.abs(p - Yt) wk_cmp.append({"model": k, "mae_log": r(e.mean(), 4), "typical": r(np.expm1(e.mean()), 4), "by_h": [r(x, 4) for x in e.mean(0)], "spike_mae_log": r(e[sp].mean(), 4), "spike_typical": r(np.expm1(e[sp].mean()), 4)}) print(" wiki", [(c_["model"], c_["mae_log"], c_["spike_mae_log"]) for c_ in wk_cmp], "windows", len(X), "test", int(te_m.sum()), "spikes", int(sp.sum()), "epochs", len(hist_wk["loss"])) top = [a for a in sorted(d["top_days"], key=lambda a: -d["top_days"][a]) if a in d["series"]][:6] out["wiki"] = { "source": "Wikimedia REST API (pageviews, Indonesian Wikipedia, user agents only)", - 9
Save parity data and JSON
Writes test windows and their NumPy forecasts to parity.json for checking the browser code, then saves all three models and results to live-forecasts.json.
ml/train.py:3617 · 2 lines
json.dump(parity, open(LIVE / "parity.json", "w")) save("live-forecasts", {**out, "trained": str(pd.Timestamp.today().date()), "tf": {"tensorflow": tf.__version__, "keras": keras.__version__}})
In your browser · TypeScript
- 10
The LSTM in TypeScript
Keras' gate order (input, forget, cell, output), step by step over the window, then the dense layer that outputs every hour or day ahead.
src/lib/lstm.ts:18 · 36 lines
/** `seq`: timesteps × features. Returns `horizon` outputs (the model's own scale; callers turn them into values). */ export function lstmRun(m: LstmModel, seq: number[][]): number[] { const U = m.units; const G = 4 * U; let h = new Array<number>(U).fill(0); let c = new Array<number>(U).fill(0); const z = new Array<number>(G); for (const x of seq) { for (let g = 0; g < G; g++) { let s = 0; for (let i = 0; i < m.inputDim; i++) s += x[i] * m.kernel[i * G + g]; let r = 0; for (let j = 0; j < U; j++) r += h[j] * m.recurrent[j * G + g]; z[g] = s + r + m.bias[g]; } const hn = new Array<number>(U); - 11
Forecast Jakarta's air from the live data
Builds the same scaled window as in training (log PM2.5 plus hour of day) and turns the network's output back into µg/m³.
src/lib/live-forecast.ts:61 · 10 lines
/** 24 hourly forecasts from the window that ends at `end` (inclusive). */ export function pmForecastFrom(values: number[], times: string[], end: number): number[] { const seq: number[][] = []; for (let i = end - PM.L + 1; i <= end; i++) { const h = Number(times[i].slice(11, 13)); seq.push([(Math.log1p(values[i]) - PM.mu) / PM.sd, Math.sin((TAU * h) / 24), Math.cos((TAU * h) / 24)]); } const base = Math.log1p(values[end]); return lstmRun(pmModel, seq).map((o) => Math.expm1(base + o)); } - 12
Forecast the rupiah with an 80% band
Scaled daily returns in, cumulative returns out; the band is scaled by the last 20 days' volatility.
src/lib/live-forecast.ts:113 · 11 lines
/** 5 forecasts (rates) plus an 80% band, from the window that ends at `end`. */ export function fxForecastFrom(values: number[], end: number) { const seq: number[][] = []; for (let i = end - FX.L + 1; i <= end; i++) seq.push([(Math.log(values[i]) - Math.log(values[i - 1])) / FX.sd]); const recent = seq.slice(-FX.vol_window).map((x) => x[0]); const m = recent.reduce((a, b) => a + b, 0) / recent.length; const vol = Math.max(Math.sqrt(recent.reduce((a, b) => a + (b - m) ** 2, 0) / recent.length), FX.vol_floor); const out = lstmRun(fxModel, seq); const at = (z: number) => values[end] * Math.exp(z * FX.sd); return { mid: out.map(at), lo: out.map((o, k) => at(o + FX.band.lo[k] * vol)), hi: out.map((o, k) => at(o + FX.band.hi[k] * vol)), vol }; }
Data download scriptml/live_data.py
ml/live_data.py:1 · 157 lines
"""
Fetch and cache the history behind the Live Forecast Lab (no API keys needed):
- Jakarta PM2.5, hourly Open-Meteo Air Quality API (CAMS), CC BY 4.0, non-commercial use
- USD -> IDR, daily (ECB) Frankfurter API
- Indonesian Wikipedia pageviews Wikimedia REST API (top articles + per-article daily views)
Raw JSON goes to ml/data/raw/live/ (gitignored). Run: ml/.venv/Scripts/python -I ml/live_data.py [--force | --fill-wiki]
"""
from __future__ import annotations
import datetime as dt
import json
import re
import time
import urllib.error
import urllib.parse
import urllib.requestNotes
The decisions behind it.
Approach
- 1
Cached the history once: Jakarta PM2.5 since 2022, USD/IDR since 1999, and two years of daily views for every article that reached Indonesian Wikipedia's daily top list in the past year.
- 2
Split strictly by time: the newest stretch is the test set, the stretch before it decides when to stop training, and no window sees its own future.
- 3
Set the bar with simple guesses: same as now, same hour yesterday, no change, long-run drift, same weekday last week, last 7-day average.
- 4
Trained one small Keras LSTM per series that outputs the whole horizon at once; for the rupiah, added an 80% band from validation errors scaled by recent volatility.
- 5
Rewrote the LSTM in TypeScript and checked it against NumPy; the page calls the APIs directly and runs every forecast on your device.
Test error · LSTM vs the best simple guess
Honest note
The PM2.5 "actuals" are the Copernicus model's own estimates, not sensor readings, so the LSTM learns to forecast a model; the live demo shows the professional CAMS forecast next to it for that reason. The rupiah model is no better than "no change", as decades of research on exchange rates would predict; its 61.2% right-direction score only reflects a weakening trend in the test period. The Wikipedia model cannot see the news, so it cannot predict a new spike, only how an existing one fades.
Reproduce: ml/train.py → live_forecasts() · ml/live_data.py
dataset sourceNext project
Bilingual Sentiment