"Real math, not vibes" is the philosophy behind every algorithm in Busara AI. This article walks through the actual TypeScript implementations of the core statistical algorithms that power the platform's 50-agent pipeline.

Holt-Winters Triple Exponential Smoothing

Time series forecasting is one of the most valuable capabilities for any data analysis platform. I implemented Holt-Winters triple exponential smoothing, which captures three components: level (the current average), trend (the direction of change), and seasonality (repeating patterns).

The algorithm iterates through the data, updating level, trend, and seasonal components using three smoothing parameters: alpha (level), beta (trend), and gamma (seasonality). For agricultural applications, seasonality is critical — rainfall in Kenya follows bimodal patterns that the algorithm must capture.

The implementation also computes RMSE (Root Mean Square Error) and MAPE (Mean Absolute Percentage Error) as accuracy metrics, and generates 95% confidence intervals as 1.96 × sigma × sqrt(h) where h is the forecast horizon.

OLS Regression via Normal Equation

For linear regression, I implemented Ordinary Least Squares via the normal equation: β = (XᵀX)⁻¹Xᵀy. This requires matrix inversion, which I implemented using Gauss-Jordan elimination with partial pivoting. The algorithm transforms the augmented matrix [A|I] into [I|A⁻¹] through row operations, with partial pivoting to improve numerical stability.

K-Means++ with Silhouette Scoring

The K-Means implementation uses K-Means++ initialization, which spreads the initial centroids more evenly than random initialization. After clustering, I compute the silhouette score to evaluate cluster quality. The silhouette score measures how similar each point is to its own cluster compared to other clusters, ranging from -1 to 1.

To find the optimal k, I run K-Means with k=2,3,4,5 and use the elbow method — selecting the k with the largest marginal inertia drop. This gives a data-driven recommendation for the number of clusters.

Anomaly Detection Ensemble

The anomaly detection module runs three algorithms in ensemble: Z-Score (flags points >3σ from mean), IQR (flags points outside Q1-1.5×IQR or Q3+1.5×IQR), and EWMA (Exponentially Weighted Moving Average detects deviations from recent trend). Each detected anomaly gets an ensemble score (sum of method hits) and is classified as critical (score ≥3), warning (≥2), or info.

Granger Causality

For causal inference, I implemented a simplified Granger causality test: regress Δy_t on y_{t-1} (restricted model) and on y_{t-1} + x_{t-1} (unrestricted model), then compute an F-statistic. If the F-statistic exceeds the critical value at p=0.05, we conclude that x Granger-causes y — meaning past values of x help predict y beyond what y's own past values provide.

Why TypeScript?

You might wonder why I implemented all these algorithms in TypeScript rather than Python, which has NumPy, pandas, and scikit-learn. The answer is twofold: (1) TypeScript provides type safety that catches bugs at compile time, and (2) the same code runs on both the server (Node.js) and the browser (for client-side computation). For a production platform like Busara AI, type safety and code sharing are worth the extra implementation effort.

← Back to Blog