Linear Algebra with NumPy

ECON 3209 · Week 3, Lecture 2 · Kerala Agricultural University

Department of Development Economics, KAU

Autumn 2026

Learning Outcomes

By the end of this lecture, you will be able to:

  1. Represent vectors and matrices with NumPy arrays
  2. Perform matrix addition, transpose, and multiplication
  3. Compute dot products and interpret them economically
  4. Solve linear systems with np.linalg.solve()
  5. Obtain eigenvalues and connect them to variance structure

Why Linear Algebra Matters in Econometrics

  • Regression models are easiest to write in matrix form
  • Input-output analysis, portfolio models, and optimisation all use matrices
  • Linear algebra lets us represent many equations compactly
  • NumPy gives direct tools for these operations
  • Understanding the algebra helps you interpret software output better

Ordinary least squares can be written as:

\[\hat{\beta} = (X'X)^{-1}X'y\]

Vectors and Matrices in NumPy

  • A vector is a one-dimensional array
  • A matrix is a two-dimensional array
  • Shapes matter because multiplication follows strict rules
  • In regression, columns of X are explanatory variables

Matrix Arithmetic

  • Addition and subtraction are element-wise
  • Scalar multiplication multiplies every entry by the same number
  • .T gives the transpose
  • Matrix shapes must match for addition and subtraction

Dot Product and Matrix Multiplication

  • np.dot() computes the dot product of two vectors
  • @ is Python’s matrix multiplication operator
  • Dot products are weighted sums — common in economics
  • Matrix multiplication is not the same as element-wise multiplication

Economic Interpretation of a Dot Product

  • Suppose quantities are the output of three crops
  • Prices are the per-unit market prices
  • The dot product adds price × quantity across crops
  • This is a compact way to compute total value of production
  • Similar weighted sums appear in index numbers and portfolios

Solving a System of Equations

  • np.linalg.solve(A, b) solves Ax = b
  • This is better than manually computing the inverse
  • Systems like this appear in equilibrium and cost allocation problems
  • The coefficient matrix must be square and non-singular

Determinant and Inverse

  • The determinant helps tell whether a matrix is invertible
  • If the determinant is zero, the inverse does not exist
  • In practice, solve() is usually preferred to inv() for equations
  • But knowing the inverse concept is important for theory

Eigenvalues and Eigenvectors

  • Eigenvalues measure how much variation lies along key directions
  • They are central in principal component analysis
  • In finance, they help summarise common risk factors
  • In econometrics, they also appear in matrix stability analysis

Regression Preview in Matrix Form

  • In a simple regression, the design matrix contains a column of ones and a column for x
  • Matrix algebra lets software estimate coefficients efficiently
  • Even if software hides the algebra, the concepts remain important
  • NumPy is the first step toward understanding what regression packages do
  • We will revisit this when we reach OLS

When you understand matrices, regression output becomes less mysterious and more interpretable.

Exercise

Use NumPy to solve the system:

  • x + 2y = 100
  • 3x - y = 40

Then print the values of x and y.

Bonus: compute the determinant of the coefficient matrix.

Summary

  • ✅ NumPy represents vectors and matrices naturally
  • @ and np.dot() perform key linear algebra operations
  • ✅ Dot products appear as weighted sums in economics and finance
  • np.linalg.solve() is the standard way to solve linear systems
  • ✅ Determinants and inverses describe matrix structure
  • ✅ Eigenvalues help summarise variation and system behaviour

Next Lecture

Statistical Operations with NumPy

  • We will move from matrix algebra to descriptive statistics and random numbers
  • You will learn how NumPy supports simulation and exploratory data analysis
  • These ideas lead directly to inference and econometric modelling