Bias Terms in Multiple Regression

Consider a matrix multiplication as shown below.

Where:

  • is the sample size
  • is the number of columns in the output, i.e. the number of response variables (or dependent variables) being modelled
  • is the number of columns in the input, i.e. the number of *features (or input/independent variables)

In this representation, each of the rows of and is an observation and the columns represent input and response variables respectively.

However, I have elected to use a column-major so let's instead represent this:

Now each observation is a column-vector instead of a row vector.

This corresponds to a matrix form thusly:

note the following points:

  1. This is a column-major representation, the vector represents a single observation and subsequent observations would become additional columns of .
    • A row major representation can be acheived by transposing the matrices.
    • R, Julia, Octave, Wolfram and Fortran all use column-major representations
    • Python, C(++), Go and Rust use a row-major representation
    • It's important to get this right, languages store values in a certain pattern in memory, cutting against the grain will be less performant.
  2. The use of sympy/octave/julia notation, whereby represents a vector composed of the first row of .

Let's now add a bias term (i.e. an intercept) so that the model reflects from simple linear regression:

The bias term could equally be expressed inside the matrix thusly:

That's why we don't often include a bias term when performing multiple linear regression, as it can be included as a component of the weights matrix.

More Observations

If there were observations:

Row Major

If we transformed this to a row-major representation, the 1s would now be a row of the input and the bias a row of the weights:

A note on Memory Layout in languages

Column major matrices are rooted in mathematical notation and conventions of linear algebra. In many textbooks and papers, 1D vectors are often treated as column vectors where convenient. This makes column-major order a natural and intuitive choice for mathematical languages where matrix operations are common. The reader may have already noted that many column-major languages are also 1-indexed, this is for a smiliar reason.

The C language was developed for systems programming, presumably row-major representation was chosen because it is consistent with how people usually layout data, there was no need to cater towards mathematical programming.