Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Numerical algorithms and computational techniques for statistics
.claude/skills/brycewang-stanford-numerical-methods/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-06 | ✗→✓ | ▲ Improved | 112% | 0% |
| case-07 | ✗→✓ | ▲ Improved | 126% | 0% |
| case-09 | ✓→✗ | ▼ Worse | 111% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 91% | 0% |
| case-03 | ✓→✓ | = Same ✓ | 124% | 0% |
You are an expert in numerical stability and computational aspects of statistical methods.
r.Machine$double.eps # ~2.22e-16 (machine epsilon) .Machine$double.xmax # ~1.80e+308 (max finite) .Machine$double.xmin # ~2.23e-308 (min positive normalized) .Machine$double.neg.eps # ~1.11e-16 (negative epsilon)
When subtracting nearly equal numbers:
r# BAD: loses precision x <- 1e10 + 1 y <- 1e10 result <- x - y # Should be 1, may have errors # BETTER: reformulate to avoid subtraction # Example: Computing variance var_bad <- mean(x^2) - mean(x)^2 # Can be negative! var_good <- sum((x - mean(x))^2) / (n-1) # Always non-negative
r# BAD: overflow prod(1:200) # Inf # GOOD: work on log scale sum(log(1:200)) # Then exp() if needed # BAD: underflow in probabilities prod(dnorm(x)) # 0 for large x # GOOD: sum log probabilities sum(dnorm(x, log = TRUE))
Essential for working with log probabilities:
rlog_sum_exp <- function(log_x) { max_log <- max(log_x) if (is.infinite(max_log)) return(max_log) max_log + log(sum(exp(log_x - max_log))) } # Example: log(exp(-1000) + exp(-1001)) log_sum_exp(c(-1000, -1001)) # Correct: ~-999.69 log(exp(-1000) + exp(-1001)) # Wrong: -Inf
r# BAD softmax_bad <- function(x) exp(x) / sum(exp(x)) # GOOD softmax <- function(x) { x_max <- max(x) exp_x <- exp(x - x_max) exp_x / sum(exp_x) }
The condition number κ(A) measures sensitivity to perturbation:
r# Check condition number kappa(X, exact = TRUE) # For regression: check X'X conditioning kappa(crossprod(X))
Prefer: Decomposition methods over explicit inversion
r# BAD: explicit inverse beta <- solve(t(X) %*% X) %*% t(X) %*% y # GOOD: QR decomposition beta <- qr.coef(qr(X), y) # BETTER for positive definite: Cholesky R <- chol(crossprod(X)) beta <- backsolve(R, forwardsolve(t(R), crossprod(X, y))) # For ill-conditioned: SVD/pseudoinverse beta <- MASS::ginv(X) %*% y
Always use specialized methods:
r# Cholesky for SPD L <- chol(Sigma) # Eigendecomposition eig <- eigen(Sigma, symmetric = TRUE) # Check positive definiteness all(eigen(Sigma, symmetric = TRUE, only.values = TRUE)$values > 0)
r# Numerical gradient (for verification) numerical_grad <- function(f, x, h = sqrt(.Machine$double.eps)) { sapply(seq_along(x), function(i) { x_plus <- x_minus <- x x_plus[i] <- x[i] + h x_minus[i] <- x[i] - h (f(x_plus) - f(x_minus)) / (2 * h) }) } # Central difference is O(h²) accurate # Forward difference is O(h) accurate
r# Check Hessian is positive definite at optimum check_hessian <- function(H, tol = 1e-8) { eigs <- eigen(H, symmetric = TRUE, only.values = TRUE)$values min_eig <- min(eigs) list( positive_definite = min_eig > tol, min_eigenvalue = min_eig, condition_number = max(eigs) / min_eig ) }
For gradient descent stability:
rbacktracking_line_search <- function(f, x, d, grad, alpha = 1, rho = 0.5, c = 1e-4) { # Armijo condition while (f(x + alpha * d) > f(x) + c * alpha * sum(grad * d)) { alpha <- rho * alpha if (alpha < 1e-10) break } alpha }
r# Adaptive quadrature (default choice) integrate(f, lower, upper) # For infinite limits integrate(f, -Inf, Inf) # For highly oscillatory or peaked functions # Increase subdivisions integrate(f, lower, upper, subdivisions = 1000) # For known singularities, split the domain
rmc_integrate <- function(f, n, lower, upper) { x <- runif(n, lower, upper) fx <- sapply(x, f) estimate <- (upper - lower) * mean(fx) se <- (upper - lower) * sd(fx) / sqrt(n) list(value = estimate, se = se) }
rnewton_raphson <- function(f, df, x0, tol = 1e-8, max_iter = 100) { x <- x0 for (i in 1:max_iter) { fx <- f(x) dfx <- df(x) # Check for near-zero derivative if (abs(dfx) < .Machine$double.eps * 100) { warning("Near-zero derivative") break } x_new <- x - fx / dfx if (abs(x_new - x) < tol) break x <- x_new } x }
For robust root finding without derivatives:
runiroot(f, interval = c(lower, upper), tol = .Machine$double.eps^0.5)
r# Always work with log-likelihood log_lik <- function(theta, data) { # Compute log-likelihood, not likelihood sum(dnorm(data, mean = theta[1], sd = theta[2], log = TRUE)) }
r# Sandwich estimator with numerical stability sandwich_se <- function(score, hessian) { # Check Hessian conditioning H_inv <- tryCatch( solve(hessian), error = function(e) MASS::ginv(hessian) ) meat <- crossprod(score) V <- H_inv %*% meat %*% H_inv sqrt(diag(V)) }
rsafe_bootstrap <- function(data, statistic, R = 1000) { results <- numeric(R) failures <- 0 for (i in 1:R) { boot_data <- data[sample(nrow(data), replace = TRUE), ] result <- tryCatch( statistic(boot_data), error = function(e) NA ) results[i] <- result if (is.na(result)) failures <- failures + 1 } if (failures > 0.1 * R) { warning(sprintf("%.1f%% bootstrap failures", 100 * failures / R)) } list( estimate = mean(results, na.rm = TRUE), se = sd(results, na.rm = TRUE), failures = failures ) }
any(is.nan(x)), any(is.infinite(x))kappa(matrix)r# Trace NaN/Inf sources debug_numeric <- function(x, name = "x") { cat(sprintf("%s: range [%.3g, %.3g], ", name, min(x), max(x))) cat(sprintf("NaN: %d, Inf: %d, -Inf: %d\n", sum(is.nan(x)), sum(x == Inf), sum(x == -Inf))) } # Check relative error rel_error <- function(computed, true) { abs(computed - true) / max(abs(true), 1) }
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-06 | fail→pass | 14,662 | 17,599 | +20% | 1 | 1 | 0% | 2,867 | 6,067 | +112% | 0 | 0 | — |
case-01 | fail→fail | 16,623 | 17,302 | +4% | 1 | 1 | 0% | 3,505 | 6,091 | +74% | 0 | 0 | — |
case-02 | pass→pass | 13,525 | 13,756 | +2% | 1 | 1 | 0% | 2,864 | 5,482 | +91% | 0 | 0 | — |
case-03 | pass→pass | 9,426 | 9,226 | -2% | 1 | 1 | 0% | 2,001 | 4,475 | +124% | 0 | 0 | — |
case-04 | pass→pass | 8,761 | 6,908 | -21% | 1 | 1 | 0% | 1,827 | 3,862 | +111% | 0 | 0 | — |
case-05 | pass→pass | 12,962 | 14,096 | +9% | 1 | 1 | 0% | 2,420 | 5,485 | +127% | 0 | 0 | — |
case-07 | fail→pass | 14,326 | 20,332 | +42% | 1 | 1 | 0% | 2,983 | 6,733 | +126% | 0 | 0 | — |
case-08 | pass→pass | 10,297 | 10,681 | +4% | 1 | 1 | 0% | 1,992 | 4,610 | +131% | 0 | 0 | — |
case-09 | pass→fail | 10,814 | 10,573 | -2% | 1 | 1 | 0% | 2,284 | 4,825 | +111% | 0 | 0 | — |
case-10 | pass→pass | 16,937 | 16,273 | -4% | 1 | 1 | 0% | 3,645 | 5,931 | +63% | 0 | 0 | — |
case-11 | pass→pass | 10,565 | 8,312 | -21% | 1 | 1 | 0% | 2,096 | 4,263 | +103% | 0 | 0 | — |
case-12 | pass→pass | 10,079 | 13,596 | +35% | 1 | 1 | 0% | 1,991 | 5,130 | +158% | 0 | 0 | — |
case-13 | pass→pass | 9,849 | 10,711 | +9% | 1 | 1 | 0% | 1,996 | 4,428 | +122% | 0 | 0 | — |
case-14 | pass→pass | 16,852 | 18,376 | +9% | 1 | 1 | 0% | 3,957 | 6,689 | +69% | 0 | 0 | — |
case-15 | fail→fail | 12,785 | 9,020 | -29% | 1 | 1 | 0% | 2,489 | 4,277 | +72% | 0 | 0 | — |
case-16 | pass→pass | 12,642 | 8,453 | -33% | 1 | 1 | 0% | 2,410 | 4,248 | +76% | 0 | 0 | — |
case-17 | pass→pass | 3,781 | 5,342 | +41% | 1 | 1 | 0% | 706 | 3,565 | +405% | 0 | 0 | — |
case-18 | pass→pass | 9,749 | 7,616 | -22% | 1 | 1 | 0% | 1,720 | 3,963 | +130% | 0 | 0 | — |
case-19 | pass→pass | 10,376 | 10,885 | +5% | 1 | 1 | 0% | 2,307 | 4,911 | +113% | 0 | 0 | — |
case-20 | pass→pass | 10,595 | 13,472 | +27% | 1 | 1 | 0% | 2,073 | 5,075 | +145% | 0 | 0 | — |
case-21 | pass→pass | 7,934 | 8,662 | +9% | 1 | 1 | 0% | 1,479 | 4,244 | +187% | 0 | 0 | — |
case-22 | pass→pass | 8,468 | 7,329 | -13% | 1 | 1 | 0% | 1,834 | 3,938 | +115% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +5 percentage points is the difference between those two pass rates over the 22 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.