The Lawfare Academy

Lawfare Group → Knowledge Institute → Lawfare Academy

← CS 230 — Mathematics, Statistics and Data for AI

Weeks 1–2

Vectors, matrices and dimensional reasoning

Learning objectives

  • Explain and trace the mechanisms in vectors, matrices and dimensional reasoning.
  • Apply the method to the worked example and changed-condition exercise.
  • Identify assumptions, failure cases and the evidence needed to justify a result.

A vector is an ordered collection of components; a matrix can represent a collection of observations or a linear transformation. Meaning depends on the declared axes. If X has n rows and d columns, rows may be observations and columns features. Multiplying X by a d-component coefficient vector produces n predictions. A dimension mismatch often reveals a conceptual mistake before code runs.

A dot product sums componentwise products. For a linear model it combines features with weights, but the units matter. A weight multiplying hours differs from a weight multiplying dollars. Feature scaling changes coefficient interpretation and can affect optimization. Broadcasting is a programming convenience that expands compatible dimensions implicitly; it can create a numerically valid but unintended calculation. Never use successful execution as proof that shapes mean what you intended.

Linear independence concerns whether one vector can be built from others. Redundant features can make coefficient estimation unstable or nonunique even if predictions remain similar. Matrix multiplication is generally not commutative. Track the interpretation of AB rather than assuming BA describes the same process. Begin with tiny hand calculations and verify them in code before using large tensors.

Worked reasoning Two hours at weight 10 and three visits at weight 5 contribute 20 and 15, giving 35 in the model’s output units. zip alone would silently truncate unequal inputs, so the explicit length check is part of correctness. The function computes a mathematical operation, not a fitted or validated prediction system.

Extended teaching and laboratory

A matrix product is meaningful when both dimensions and represented units align. Shape compatibility prevents one class of error but cannot establish that features are in the correct order or measured in the intended units. A dot product silently using dollars against a coefficient trained for cents can be numerically valid and semantically wrong.

Keep a feature schema alongside the parameters. Batch rows represent observations; columns represent features in an explicit order. A bias term is an additional model component, not a reason to ignore units.

Worked problem Two jobs have features [hours,visits]: [2,3] and [4,1]. The coefficient vector is [1000 cents/hour,500 cents/visit], with bias 200 cents. Compute predictions and dimensions.

Step-by-step reasoning 1. X has shape 2×2 and w has shape 2×1. Xw has shape 2×1, one prediction per job.

  1. The first dot product is 2000+1500=3500 cents; the second is 4000+500=4500 cents.
  1. Add the 200-cent bias to each result: 3700 and 4700 cents. The bias is not multiplied by the number of features.
  1. Record feature order and units. Swapping hours and visits produces a different model even though the array dimensions still match.

Apply the idea

Two jobs have features [hours,visits]: [2,3] and [4,1]. The coefficient vector is [1000 cents/hour,500 cents/visit], with bias 200 cents. Compute predictions and dimensions.

The hours feature is converted to minutes. What coefficient preserves the same prediction, and what error occurs if the old coefficient is retained?

Read the worked reasoning

Divide the hours coefficient by 60: 1000/60 cents per minute. Then 120 minutes contributes the same 2000 cents as two hours. Retaining 1000 as cents per minute inflates that contribution by a factor of 60. Matching dimensions alone will not detect the unit error.

Check understanding

Why label matrix dimensions before implementing a model?

  1. To check whether each operation matches its intended meaning
    Feedback

    Shape reasoning catches many representation errors early.

  2. Because matching shapes guarantee a good model
    Feedback

    Mathematically compatible shapes can still encode the wrong task.

  3. To eliminate all numerical error
    Feedback

    Finite precision remains a separate numerical issue.

Further reading

Study the complete on-site source sections below, then work the original example, changed-condition problem and transfer laboratory. Study Dive into Deep Learning: Data Manipulation and Linear Algebra in full; complete their exercises. Write shapes beside every operation.

My learning →