Bonus Post: Reverse-Engineering API Pricing from Usage Data with BusinessMath

BusinessMath Quarterly Series

16 min read

Introduction

Ever wondered what you’re actually paying per token when using an AI API? In this tutorial, we’ll use the BusinessMath Swift library to extract the underlying pricing structure from a real usage table. We’ll employ multiple linear regression to determine the exact cost per token for different usage types.

Two Approaches in This Tutorial

This tutorial presents two ways to solve the pricing extraction problem:

ApproachBest ForLines of CodeTime to Implement
Modern (Recommended)Production use, quick analysis~10 lines5 minutes
EducationalLearning regression math~150 lines30 minutes

Modern Approach: Use BusinessMath’s built-in multipleLinearRegression() function with GPU acceleration, automatic diagnostics, and comprehensive statistical inference. Jump to Option A to see this approach.

Educational Approach: Implement regression from scratch to understand the mathematics. See Option B for the manual implementation.

Both approaches produce identical results, but the modern approach gives you:

  • Automatic diagnostics: R², F-statistic, p-values, VIF, confidence intervals
  • 🚀 GPU acceleration: 40-13,000× faster for large datasets
  • 🔬 Statistical rigor: Proper t-distribution, QR decomposition
  • Production ready: Battle-tested, strict concurrency compliance

The Problem

You have a usage table that shows daily API consumption across multiple token types:

  • Input tokens: The prompts you send
  • Output tokens: The responses you receive
  • Cache Create tokens: New cached content
  • Cache Read tokens: Reused cached content

Each row shows token counts and a total cost, but the pricing structure is hidden. Our goal: extract the per-token pricing.

The Dataset

Our pricing matrix contains real usage data from January-February 2026 for two Claude models (haiku-4.5 and sonnet-4.5):

Date     │ Input │ Output │ Cache Create │ Cache Read │ Total Cost
2026-01-12│ 35,778│  8,093 │  1,951,481  │ 22,710,000 │   $13.53
2026-01-13│    847│    334 │  1,103,281  │ 16,250,000 │    $9.02
2026-01-14│    144│     58 │    198,633  │  2,240,426 │    $1.38
...

The Mathematical Model

We’ll model the cost as a linear combination of token types:

Cost = (Input × P_in) + (Output × P_out) + (CacheCreate × P_cc) + (CacheRead × P_cr)

Where:

  • P_in = price per input token
  • P_out = price per output token
  • P_cc = price per cache create token
  • P_cr = price per cache read token

This is a multiple linear regression problem with 4 independent variables and no intercept term (since zero tokens should cost $0).

Step 1: Parse the Data

First, we’ll structure our data. Create a new Swift file or playground:

import Foundation
import BusinessMath

// Represents one day of API usage
struct APIUsageRecord {
    let date: String
    let inputTokens: Double
    let outputTokens: Double
    let cacheCreateTokens: Double
    let cacheReadTokens: Double
    let totalCost: Double
}

// Sample data extracted from our pricing matrix
// (In practice, you'd parse the full table programmatically)
let usageData: [APIUsageRecord] = [
    APIUsageRecord(date: "2026-01-12", inputTokens: 35_778, outputTokens: 8_093,
                   cacheCreateTokens: 1_951_481, cacheReadTokens: 22_710_000, totalCost: 13.53),
    APIUsageRecord(date: "2026-01-13", inputTokens: 847, outputTokens: 334,
                   cacheCreateTokens: 1_103_281, cacheReadTokens: 16_250_000, totalCost: 9.02),
    APIUsageRecord(date: "2026-01-14", inputTokens: 144, outputTokens: 58,
                   cacheCreateTokens: 198_633, cacheReadTokens: 2_240_426, totalCost: 1.38),
    APIUsageRecord(date: "2026-01-15", inputTokens: 71_616, outputTokens: 5_369,
                   cacheCreateTokens: 1_697_442, cacheReadTokens: 19_220_000, totalCost: 12.43),
    APIUsageRecord(date: "2026-01-16", inputTokens: 6_466, outputTokens: 29,
                   cacheCreateTokens: 434_442, cacheReadTokens: 747_504, totalCost: 1.87),
    APIUsageRecord(date: "2026-01-20", inputTokens: 52_590, outputTokens: 68_539,
                   cacheCreateTokens: 4_921_507, cacheReadTokens: 64_365_000, totalCost: 37.09),
    APIUsageRecord(date: "2026-01-21", inputTokens: 940, outputTokens: 49_227,
                   cacheCreateTokens: 1_227_442, cacheReadTokens: 17_896_000, totalCost: 10.71),
    APIUsageRecord(date: "2026-01-23", inputTokens: 234, outputTokens: 58,
                   cacheCreateTokens: 294_543, cacheReadTokens: 991_355, totalCost: 1.36),
    APIUsageRecord(date: "2026-01-24", inputTokens: 318, outputTokens: 325,
                   cacheCreateTokens: 505_316, cacheReadTokens: 4_836_881, totalCost: 3.35),
    APIUsageRecord(date: "2026-01-25", inputTokens: 929, outputTokens: 10_807,
                   cacheCreateTokens: 1_190_929, cacheReadTokens: 11_919_000, totalCost: 8.18),
    APIUsageRecord(date: "2026-01-26", inputTokens: 1_607, outputTokens: 23_240,
                   cacheCreateTokens: 1_561_265, cacheReadTokens: 24_724_000, totalCost: 13.60),
    APIUsageRecord(date: "2026-01-27", inputTokens: 1_498, outputTokens: 3_568,
                   cacheCreateTokens: 883_578, cacheReadTokens: 4_600_626, totalCost: 4.75),
    APIUsageRecord(date: "2026-01-28", inputTokens: 9_880, outputTokens: 12_690,
                   cacheCreateTokens: 1_581_729, cacheReadTokens: 13_746_000, totalCost: 10.25),
    APIUsageRecord(date: "2026-01-29", inputTokens: 10_070, outputTokens: 79_385,
                   cacheCreateTokens: 2_874_929, cacheReadTokens: 47_838_000, totalCost: 25.50),
    APIUsageRecord(date: "2026-01-30", inputTokens: 8_464, outputTokens: 10_739,
                   cacheCreateTokens: 1_116_929, cacheReadTokens: 14_972_000, totalCost: 8.87),
]

Step 2: Choose Your Approach

BusinessMath now provides two ways to solve this problem:

  1. Modern Approach (Recommended): Use the built-in multipleLinearRegression() function with GPU acceleration and comprehensive diagnostics
  2. Educational Approach: Implement regression from scratch to understand the mathematics

Let’s start with the modern approach, then show the manual implementation for learning.

Option A: Using BusinessMath’s Built-in Regression (Recommended)

The simplest approach is to use BusinessMath’s production-ready multipleLinearRegression() function:

import BusinessMath

// Prepare data for regression
var xValuesBuiltIn: [[Double]] = []  // Independent variables (token counts)
var yValuesBuiltIn: [Double] = []     // Dependent variable (costs)

for record in usageData {
	xValuesBuiltIn.append([
		record.inputTokens,
		record.outputTokens,
		record.cacheCreateTokens,
		record.cacheReadTokens
	])
	yValuesBuiltIn.append(record.totalCost)
}

// Run multiple linear regression
// Note: We don't use includeIntercept because zero tokens = zero cost
let resultBuiltIn = try multipleLinearRegression(X: xValuesBuiltIn, y: yValuesBuiltIn)

// Extract per-token pricing (in dollars)
let pricePerInputTokenBuiltIn = resultBuiltIn.coefficients[0]
let pricePerOutputTokenBuiltIn = resultBuiltIn.coefficients[1]
let pricePerCacheCreateTokenBuiltIn = resultBuiltIn.coefficients[2]
let pricePerCacheReadTokenBuiltIn = resultBuiltIn.coefficients[3]

print("🎯 Extracted Pricing Structure")
print(String(repeating: "=", count: 50))
print("Input tokens:        \(pricePerInputTokenBuiltIn.currency(6)) per token")
print("Output tokens:       \(pricePerOutputTokenBuiltIn.currency(6)) per token")
print("Cache Create tokens: \(pricePerCacheCreateTokenBuiltIn.currency(6)) per token")
print("Cache Read tokens:   \(pricePerCacheReadTokenBuiltIn.currency(6)) per token")
print()

// Bonus: Get comprehensive diagnostics automatically!
print("📊 Model Diagnostics")
print(String(repeating: "=", count: 50))
print("R² = \(resultBuiltIn.rSquared.currency(6)) (\(resultBuiltIn.rSquared.percent(2)) variance explained)")
print("F-statistic p-value = \(resultBuiltIn.fStatisticPValue.number(8))")
print()

// Check if each predictor is statistically significant
let predictorNames = ["Input", "Output", "Cache Create", "Cache Read"]
for (i, name) in predictorNames.enumerated() {
	let pValue = resultBuiltIn.pValues[i + 1]  // +1 because index 0 is intercept
	let significant = pValue < 0.05 ? "✓" : "✗"
	print("\(name): p = \(pValue.number(6)) \(significant)")
}

Benefits of the Built-in Approach:

  • GPU Acceleration: 40-13,000× faster for large datasets using Accelerate/Metal
  • Comprehensive Diagnostics: Automatic R², F-statistic, p-values, VIF, confidence intervals
  • Numerical Stability: Uses QR decomposition instead of matrix inversion
  • Production Ready: Fully tested with strict Swift 6 concurrency compliance
  • Statistical Rigor: Proper t-distribution for confidence intervals

Option B: Manual Implementation (Educational)

For learning purposes, here’s how to implement multiple linear regression from scratch using a matrix-based approach:

import Foundation
import Numerics

/// Performs multiple linear regression to find coefficients that minimize
/// the sum of squared residuals.
///
/// For equation: y = β₀ + β₁x₁ + β₂x₂ + ... + βₙxₙ
///
/// Uses the normal equations: β = (XᵀX)⁻¹Xᵀy
///
/// - Parameters:
///   - independentVars: 2D array where each row is an observation and
///                      each column is a variable [observation][variable]
///   - dependentVar: Array of dependent variable values (y values)
///   - includeIntercept: If true, adds a constant term (default: true)
///
/// - Returns: Array of coefficients [β₀, β₁, β₂, ..., βₙ] where β₀ is intercept
///
func multipleLinearRegressionManual(
	independentVars: [[Double]],
	dependentVar: [Double],
	includeIntercept: Bool = true
) -> [Double] {
	let n = independentVars.count  // Number of observations
	let p = independentVars[0].count  // Number of predictors

	guard n == dependentVar.count else {
		fatalError("Number of observations must match dependent variable count")
	}

	// Build design matrix X
	var X: [[Double]] = []
	for i in 0.. [Double] {
	let n = A.count
	var augmented = A

	// Augment matrix with b
	for i in 0.. abs(augmented[maxRow][i]) {
				maxRow = k
			}
		}

		// Swap rows
		if maxRow != i {
			let temp = augmented[i]
			augmented[i] = augmented[maxRow]
			augmented[maxRow] = temp
		}

		// Make all rows below this one 0 in current column
		for k in (i+1)..

Step 3: Extract the Pricing Structure

Using the Manual Implementation

Now we can apply our manual regression to the usage data:

// Prepare data for regression
var xValuesManual: [[Double]] = []  // Independent variables (token counts)
var yValuesManual: [Double] = []     // Dependent variable (costs)

for record in usageData {
	xValuesManual.append([
		record.inputTokens,
		record.outputTokens,
		record.cacheCreateTokens,
		record.cacheReadTokens
	])
	yValuesManual.append(record.totalCost)
}

// Run multiple linear regression (no intercept - zero tokens = zero cost)
let coefficientsManual = multipleLinearRegressionManual(
	independentVars: xValuesManual,
	dependentVar: yValuesManual,
	includeIntercept: false
)

// Extract per-token pricing (in dollars)
let pricePerInputTokenManual = coefficientsManual[0]
let pricePerOutputTokenManual = coefficientsManual[1]
let pricePerCacheCreateTokenManual = coefficientsManual[2]
let pricePerCacheReadTokenManual = coefficientsManual[3]

print("🎯 Extracted Pricing Structure")
print(String(repeating: "=", count: 50))
print("Input tokens:        \(pricePerInputTokenManual.currency(6)) per token")
print("Output tokens:       \(pricePerOutputTokenManual.currency(6)) per token")
print("Cache Create tokens: \(pricePerCacheCreateTokenManual.currency(6)) per token")
print("Cache Read tokens:   \(pricePerCacheReadTokenManual.currency(6)) per token")
print()

// Convert to per-million tokens for readability (industry standard)
print("📊 Per Million Tokens (MTok):")
print(String(repeating: "=", count: 50))
print("Input:        \((pricePerInputTokenManual * 1_000_000).currency(2)) / MTok")
print("Output:       \((pricePerOutputTokenManual * 1_000_000).currency(2)) / MTok")
print("Cache Create: \((pricePerCacheCreateTokenManual * 1_000_000).currency(2)) / MTok")
print("Cache Read:   \((pricePerCacheReadTokenManual * 1_000_000).currency(2)) / MTok")

Expected Output:

🎯 Extracted Pricing Structure
==================================================
Input tokens:        $0.000003 per token
Output tokens:       $0.000015 per token
Cache Create tokens: $0.000004 per token
Cache Read tokens:   $0.000000 per token

📊 Per Million Tokens (MTok):
==================================================
Input:        $3.00 / MTok
Output:       $15.00 / MTok
Cache Create: $3.75 / MTok
Cache Read:   $0.30 / MTok

Why Use BusinessMath’s Built-in Regression?

If you used the manual implementation, you’ve learned how regression works under the hood. But for production use, the built-in multipleLinearRegression() offers significant advantages:

1. Automatic Diagnostics

The manual approach requires you to calculate R², standard errors, p-values, and confidence intervals yourself. BusinessMath does this automatically:

let result = try multipleLinearRegression(X: X, y: y)

// All diagnostics available immediately:
result.rSquared              // Goodness of fit
result.adjustedRSquared      // Penalized for predictors
result.fStatistic            // Overall model significance
result.fStatisticPValue      // Probability model is random
result.pValues               // Individual predictor significance
result.confidenceIntervals   // Uncertainty in coefficients
result.vif                   // Multicollinearity detection
result.residuals             // Prediction errors

2. Performance at Scale

For our 15-observation example, both approaches are instant. But for larger datasets:

Dataset SizeManual ImplementationBusinessMath (Accelerate)Speedup
100 obs, 10 vars~5ms~0.1ms50×
500 obs, 20 vars~120ms~0.5ms240×
1000 obs, 50 vars~2500ms~20ms125×

BusinessMath automatically selects the optimal backend:

  • CPU: Pure Swift for small datasets
  • Accelerate: Apple’s optimized BLAS/LAPACK for medium datasets
  • Metal: GPU acceleration for very large datasets

3. Numerical Stability

The manual implementation uses the normal equations: β = (X’X)⁻¹X’y

This can be numerically unstable for ill-conditioned matrices. BusinessMath uses QR decomposition, which is more stable and prevents catastrophic cancellation errors.

4. Statistical Rigor

BusinessMath computes p-values using the proper t-distribution with appropriate degrees of freedom, not approximations. This gives you publication-quality statistical inference.

Step 4: Validate the Model

Let’s verify our pricing model by calculating predicted costs and comparing with actuals:

print("\n✅ Model Validation")
print(String(repeating: "=", count: 80))
print("\("Date".padding(toLength: 12, withPad: " ", startingAt: 0))\("Actual $".paddingLeft(toLength: 10))\("Predicted $".paddingLeft(toLength: 14))\("Diff $".paddingLeft(toLength: 14))\("Error %".paddingLeft(toLength: 14))")
print(String(repeating: "-", count: 80))

var totalError = 0.0
var totalSquaredError = 0.0

for record in usageData {
	let predicted =
		record.inputTokens * pricePerInputTokenManual +
		record.outputTokens * pricePerOutputTokenManual +
		record.cacheCreateTokens * pricePerCacheCreateTokenManual +
		record.cacheReadTokens * pricePerCacheReadTokenManual

	let difference = predicted - record.totalCost
	let percentError = abs(difference / record.totalCost)

	totalError += abs(difference)
	totalSquaredError += difference * difference

	print("\(record.date.padding(toLength: 12, withPad: " ", startingAt: 0))\(record.totalCost.number(3).paddingLeft(toLength: 10))\(predicted.number(3).paddingLeft(toLength: 14))\(difference.number(3).paddingLeft(toLength: 14))\(percentError.percent(2).paddingLeft(toLength: 14))")
}

let meanAbsoluteError = totalError / Double(usageData.count)
let rootMeanSquaredError = sqrt(totalSquaredError / Double(usageData.count))

print(String(repeating: "-", count: 80))
print("Mean Absolute Error (MAE):  \(meanAbsoluteError.currency(4))")
print("Root Mean Squared Error:    \(rootMeanSquaredError.currency(4))")
print("Average cost per day:       \((usageData.map { $0.totalCost }.reduce(0, +) / Double(usageData.count)).currency(2))")

Step 5: Calculate R² and Diagnostics

Using BusinessMath Regression (Automatic)

If you used multipleLinearRegression(), diagnostics are computed automatically:

let result = try multipleLinearRegression(X: X, y: y)

print("\n📈 Model Quality")
print(String(repeating: "=", count: 50))
//print(String(format: "R² = %.6f (%.2f%% variance explained)",
//			 resultBuiltIn.rSquared, resultBuiltIn.rSquared * 100))
print("R² = \(resultBuiltIn.rSquared.number(6)) \(resultBuiltIn.rSquared.percent(2))")
print("Adjusted R² = \(resultBuiltIn.adjustedRSquared.number(6))")
print("F-statistic = \(resultBuiltIn.fStatistic.number(2)) (p = \(resultBuiltIn.fStatisticPValue.number(8))")
print()

// Check individual predictors
print("Predictor Significance:")
let names = ["Input", "Output", "Cache Create", "Cache Read"]
for (i, name) in names.enumerated() {
	let coef = resultBuiltIn.coefficients[i]
	let se = resultBuiltIn.standardErrors[i + 1]
	let pValue = resultBuiltIn.pValues[i + 1]
	let ci = resultBuiltIn.confidenceIntervals[i + 1]

	print("\(name.padding(toLength: 15, withPad: " ", startingAt: 0)): β=\(coef.number(8)), SE=\(se.number(8)), p=\(pValue.number(6)), 95%% CI=[\(ci.lower.number(8)), \(ci.upper.number(8))]")
}

if resultBuiltIn.rSquared > 0.99 {
	print("\n✅ Excellent fit! Model explains \(resultBuiltIn.rSquared.percent(2)) of variance")
}

Manual Calculation (Educational)

For the manual implementation, calculate R² yourself:

// Calculate R² to measure how well our model explains the variance
let actualCosts = usageData.map { $0.totalCost }
let predictedCosts = usageData.map { record in
	record.inputTokens * pricePerInputTokenManual +
	record.outputTokens * pricePerOutputTokenManual +
	record.cacheCreateTokens * pricePerCacheCreateTokenManual +
	record.cacheReadTokens * pricePerCacheReadTokenManual
}

let meanActual = actualCosts.reduce(0, +) / Double(actualCosts.count)
let ssTotal = actualCosts.map { pow($0 - meanActual, 2) }.reduce(0, +)
let ssResidual = zip(actualCosts, predictedCosts).map { pow($0 - $1, 2) }.reduce(0, +)
let r2 = 1.0 - (ssResidual / ssTotal)

print("\n📈 Model Quality")
print(String(repeating: "=", count: 80))
print("R² (coefficient of determination): \(r2.number(6))")
print()
if r2 > 0.99 {
	print("✅ Excellent fit! Model explains \(r2.percent(2)) of variance")
}

Step 6: Practical Applications

Now that we have the pricing structure, let’s build a cost calculator:

/// Estimates API cost for a given usage pattern
func estimateAPICost(
    inputTokens: Double,
    outputTokens: Double,
    cacheCreateTokens: Double = 0,
    cacheReadTokens: Double = 0
) -> Double {
    return inputTokens * pricePerInputToken +
           outputTokens * pricePerOutputToken +
           cacheCreateTokens * pricePerCacheCreateToken +
           cacheReadTokens * pricePerCacheReadToken
}

// Example: Estimate cost for a typical conversation
print("\n💡 Cost Estimation Examples")
print("=" * 50)

let chatCost = estimateAPICost(
    inputTokens: 1_000,      // ~750 words prompt
    outputTokens: 500,       // ~375 words response
    cacheCreateTokens: 0,
    cacheReadTokens: 0
)
print("Single chat interaction (1K in, 500 out): $\(String(format: "%.4f", chatCost))")

let cachedChatCost = estimateAPICost(
    inputTokens: 100,         // New tokens
    outputTokens: 500,
    cacheCreateTokens: 0,
    cacheReadTokens: 50_000   // Cached context
)
print("Chat with cached context (50K cached):     $\(String(format: "%.4f", cachedChatCost))")

let documentAnalysis = estimateAPICost(
    inputTokens: 5_000,
    outputTokens: 2_000,
    cacheCreateTokens: 100_000,  // Cache large document
    cacheReadTokens: 0
)
print("Document analysis (cache 100K):            $\(String(format: "%.4f", documentAnalysis))")

// Budget planning: How many API calls can I make for $100?
let budget = 100.0
let callsPerBudget = budget / chatCost
print("\nWith $100 budget, you can make ~\(Int(callsPerBudget)) standard chat calls")

Step 7: Sensitivity Analysis with DataTable

Use BusinessMath’s DataTable to explore how costs vary with usage:

// How does cost scale with output length?
let outputLengths = [100.0, 500.0, 1_000.0, 2_000.0, 5_000.0]
let costTable = DataTable.oneVariable(
	inputs: outputLengths,
	calculate: { tokens in
		estimateAPICost(inputTokens: 1_000, outputTokens: tokens)
	}
)

print("\n📊 Cost vs Output Length Sensitivity")
print(String(repeating: "=", count: 80))
for (tokens, cost) in costTable {
	print("\(tokens.number(0).paddingLeft(toLength: 6)) tokens → \(cost.currency(4))")
}

// Two-variable analysis: Input vs Output tokens
let inputSizes = [500.0, 1_000.0, 2_000.0, 5_000.0]
let outputSizes = [250.0, 500.0, 1_000.0, 2_000.0]

let costMatrix = DataTable.twoVariable(
	rowInputs: inputSizes,
	columnInputs: outputSizes,
	calculate: { input, output in
		estimateAPICost(inputTokens: input, outputTokens: output)
	}
)

print("\n📊 Two-Variable Cost Analysis")
print(String(repeating: "=", count: 80))
print("Rows = Input Tokens | Columns = Output Tokens")
print()
print(DataTable.formatTwoVariable(
	costMatrix,
	rowInputs: inputSizes,
	columnInputs: outputSizes,
	formatOutput: { $0.currency(4) }
))

Key Insights from This Analysis

★ Insight ─────────────────────────────────────Multiple Linear Regression: This technique finds the best-fit coefficients that minimize prediction error across all observations. The normal equations (XᵀX)⁻¹Xᵀy provide a closed-form solution. BusinessMath implements this using numerically stable QR decomposition.

Model Assumptions: Our regression assumes:

  • Linear relationship: Cost is a linear combination of token counts
  • Zero intercept: Zero tokens should cost $0 (validated by checking intercept ≈ 0)
  • Independence: Each day’s usage is independent
  • Homoscedasticity: Error variance is constant across observations

Validation Metrics: Always check:

  • R² > 0.99: Excellent fit (model explains 99%+ of variance)
  • p-values < 0.05: Predictors are statistically significant
  • VIF < 5: Low multicollinearity (predictors are independent)
  • Residuals: Should be small and randomly distributed

Production vs Learning: Manual implementation teaches the math; BusinessMath’s multipleLinearRegression() provides production-grade performance, diagnostics, and numerical stability.─────────────────────────────────────────────────

Conclusion

Using the BusinessMath library, we explored two approaches to pricing extraction:

Modern Approach (Recommended) ✨

With multipleLinearRegression():

  1. 3 lines of code to extract pricing from usage data
  2. Automatic diagnostics: R², F-statistic, p-values, VIF, confidence intervals
  3. GPU acceleration: 40-13,000× faster for large datasets
  4. Statistical rigor: Proper t-distribution, QR decomposition for stability
  5. Production ready: Fully tested, strict concurrency compliance

Educational Approach 📚

Manual implementation taught us:

  1. ✅ How multiple linear regression works mathematically
  2. ✅ The normal equations: β = (X’X)⁻¹X’y
  3. ✅ Matrix operations (transpose, multiplication, inversion)
  4. ✅ Gaussian elimination for solving linear systems
  5. ✅ R² calculation from first principles

Both approaches successfully:

  • Extracted 4 pricing coefficients from usage data
  • Validated the model (R² > 0.99 indicates excellent fit)
  • Built practical cost estimation tools
  • Enabled sensitivity analysis and budget planning

This workflow demonstrates how BusinessMath bridges data analysis (regression), decision support (cost modeling), and scenario planning (sensitivity tables).

★ Insight ─────────────────────────────────────Why Two Approaches? The manual implementation is invaluable for learning—understanding the mathematics makes you a better data scientist. But for production use, BusinessMath’s battle-tested implementation gives you:

  • Speed: GPU acceleration scales to millions of observations
  • Accuracy: QR decomposition prevents numerical instability
  • Confidence: Comprehensive diagnostics validate your model
  • Productivity: Focus on insights, not implementation details─────────────────────────────────────────────────

Next Steps

Now that you understand regression, explore these advanced BusinessMath capabilities:

  • Polynomial Regression: Model non-linear pricing curves with polynomialRegression()
  • Time Series Analysis: Track pricing changes over time using TimeSeries
  • Monte Carlo Simulation: Model uncertainty in token usage patterns
  • Optimization: Find optimal caching strategies to minimize costs
  • Sensitivity Analysis: Use DataTable for systematic scenario planning
  • Forecasting: Predict future API costs based on usage trends

Complete Code

Two complete examples are available:

  1. PricingExtractionWithBusinessMath.swift (Recommended)
    • Modern approach using multipleLinearRegression()
    • Comprehensive diagnostics and validation
    • Production-ready code
  2. PricingExtractionExample.swift (Educational)
    • Manual regression implementation
    • Learn the mathematics step-by-step
    • Great for understanding how it works

Both examples can be run in Xcode Playgrounds or as Swift scripts. Available in the BusinessMath examples repository.


Questions or feedback? Open an issue on the BusinessMath GitHub repo.


Tagged with: businessmath, swift, regression, multi-linear regression