CS 230 — Mathematics, Statistics and Data for AI — Integrated project

Optional integrated project

Produce a reproducible analysis of fictional service jobs. Include a feature-unit schema, a hand-derived gradient update, two base-rate tables, a grouped/time-aware partition plan, training-only preprocessing and a threshold decision under error costs and review capacity. Supply code and independently calculated expected results.

  1. Dimensionally valid matrix calculations with documented feature order and units.
  2. A derivative derivation and numerical before/after loss comparison at two learning rates.
  3. Natural-frequency tables distinguishing sensitivity from predictive value.
  4. An explicit sampling unit, estimand and dependence-aware evaluation design.
  5. A fitted preprocessing pipeline confined to development partitions.
  6. A confusion matrix, operational cost calculation, capacity constraint and limitations.
Worked approach and limits

For the two-job matrix, obtain 3700 and 4700 cents with the 200-cent bias. Show that converting hours to minutes requires dividing the matching coefficient by 60. Derive the squared-loss gradient before implementing it and compare the resulting losses rather than only the parameter values. In the rare-event screen, 90 true and 495 false alerts yield about 15.38% predictive value; show the changed-population calculation separately. Partition customers according to the deployment estimand and learn imputation inside each training fold. Compare A’s 2050 error cost with B’s 1200 while recognizing B’s 80-alert workload. The report must distinguish causal claims from predictive or descriptive findings.

Runnable reference implementation

The reference demonstrates the bounded mechanisms described in the course; use the project criteria to identify what a complete independent implementation still needs.

Download the runnable reference implementation
"""Original Lawfare Academy mathematics lab. All arithmetic uses fictional data."""
import math
from statistics import mean

def dot(x, w):
    if len(x) != len(w):
        raise ValueError('Mismatched shapes')
    return sum(a*b for a,b in zip(x,w))

def central_difference(f, x, h=1e-4):
    return (f(x+h)-f(x-h))/(2*h)

def counts(prevalence, sensitivity, specificity, n=10000):
    positives = n*prevalence
    tp = positives*sensitivity
    fp = (n-positives)*(1-specificity)
    return tp, fp, tp/(tp+fp)

def metrics(truth, scores, threshold):
    predicted = [s >= threshold for s in scores]
    tp = sum(y == 1 and p for y,p in zip(truth,predicted))
    fp = sum(y == 0 and p for y,p in zip(truth,predicted))
    fn = sum(y == 1 and not p for y,p in zip(truth,predicted))
    return {'alerts':tp+fp, 'precision':tp/(tp+fp) if tp+fp else None,
            'recall':tp/(tp+fn) if tp+fn else None}

def main():
    assert dot([2,3],[10,5]) == 35
    try:
        dot([1],[2,3])
        raise AssertionError('shape guard failed')
    except ValueError:
        pass
    loss = lambda w:(2*w-6)**2
    assert math.isclose(central_difference(loss,1),-16,rel_tol=1e-8)
    assert math.isclose(counts(.01,.8,.9)[2],80/1070)
    assert math.isclose(counts(.2,.8,.9)[2],2/3)
    training, heldout = [2,None,6], [100,None]
    fill = mean(x for x in training if x is not None)
    assert [fill if x is None else x for x in heldout] == [100,4]
    truth, scores = [0,1,0,1], [.2,.4,.6,.8]
    low,high=metrics(truth,scores,.3),metrics(truth,scores,.7)
    assert low == {'alerts':3,'precision':2/3,'recall':1}
    assert high == {'alerts':1,'precision':1,'recall':.5}
    print('PASS: shapes, gradient check, prevalence sensitivity, train-only imputation and threshold metrics')

if __name__ == '__main__':
    main()