Notes on Linear Regression, Basis Functions, and Bias-Variance Trade-Off

Logistics and course context

  • Homework: Homework 1 has two questions; two questions in the syllabus; reference to “six five” likely a course code/time mixed in the transcription.

  • Programming/practice: Question about whether a specific library is required or if a simple programming tool suffices; no firm requirement stated in the transcription.

  • Office hours and logistics: If you have questions about machine learning topics, feel free to visit the office. Logistics questions or IT/difficulty comments were acknowledged; no further concerns raised.

Recap: From linear regression to a general framework

  • Linear regression goal: Derive a linear model that predicts a target from features. Data consist of features X and target y; model is
    y^=wTx\hat{y} = w^T x

  • Optimization viewpoint: Translate learning into an optimization problem (minimize a loss). Commonly, with Gaussian/normal noise, maximum likelihood estimation (MLE) leads to a least-squares objective for linear models.

  • Typical objective (least squares):
    min⁡<em>w∑</em>i=1n(y<em>i−wTx</em>i)2=min⁡<em>w∣y−Xw∣</em>22\min<em>w \sum</em>{i=1}^n (y<em>i - w^T x</em>i)^2 = \min<em>w |\mathbf{y} - X w|</em>2^2
    where X is the design matrix with rows xi^T and \mathbf{y} = [y1, \dots, y_n]^T.

  • Connection with probability: For Gaussian noise, MLE corresponds to minimizing the squared error; the optimization problem is quadratic in w and can be solved by linear algebra or iterative methods.

  • Conceptual simplification: We can pose many learning problems as minimization problems over a vector space, giving a unifying framework for regression and related models.

Extending beyond linear regression: bias, variance, and feature maps

  • Main idea: Use a feature map (basis function) to map inputs into a higher-dimensional space and then fit a linear model in that space.

  • Feature map (basis function):
    ϕ:X→Rm,with ϕ(x)=[ϕ<em>1(x),ϕ</em>2(x),…,ϕm(x)]T.\phi: \mathcal{X} \to \mathbb{R}^m, \quad \text{with } \phi(x) = [\phi<em>1(x), \phi</em>2(x), \dots, \phi_m(x)]^T.

  • Generalized linear model with feature map:
    y^=wTϕ(x).\hat{y} = w^T \phi(x).

  • Special cases:

    • Identity map: if \phi(x) = x, we recover ordinary linear regression in the original space.

    • Enhanced feature maps: add bias (constant feature) or polynomial terms like x, x^2, etc., by expanding the feature space.

  • Dimensionality of feature space:

    • If the feature map increases dimension (e.g., from m to m+1 by adding a constant or polynomial terms), the linear model operates in this larger space.

    • In higher-dimensional settings (d dimensions for input, extended with features), the inner product in the new space yields the objective function (e.g., an expanded set of linear terms in the loss).

  • Infinite-dimensional feature spaces (conceptual):

    • You can think of the parameters as functions from \mathbb{R} to \mathbb{R} and the inner product as an integral over a continuum:
      ⟨ϕ<em>x,ϕ</em>x′⟩=∫w(t)ϕ<em>x(t)ϕ</em>x′(t)dt  (illustrative, general idea).\langle \phi<em>x, \phi</em>{x'} \rangle = \int w(t) \phi<em>x(t) \phi</em>{x'}(t) dt \; \text{(illustrative, general idea)}.

    • This allows very rich representations; you can write down many possible basis functions, even though in practice we use finite approximations.

  • Connection to neural networks: A neural network can be viewed as a large composition where a (possibly non-linear) feature map pi is applied to the input, and the final layer is linear in these features. In that view, learning involves choosing both the feature map (through network architecture/parameters) and the final linear weights w.

  • Practical takeaway: We will specify a feature map \phi (the basis functions) for a given model; the learning task reduces to optimizing w given \phi.

What basis functions do we actually use?

  • The simplest basis: linear (identity) basis. Then the model is a straight line (through the origin if no bias term is included).

  • To enrich the model, add more features:

    • Include a constant term (bias) and x to create a one-degree feature expansion; results in a model with an intercept and slope.

    • Include higher-degree terms (e.g., x^2) to capture curvature; the linear model in the expanded space corresponds to a polynomial in x.

  • General strategy: start with a small, manageable set of basis functions and increase the richness of \phi until the bias/variance trade-off is acceptable.

  • Important nuance: The choice of the basis functions is a design decision that greatly affects model capacity, learning dynamics, and generalization.

Sentiment analysis as a concrete example of feature maps

  • Input type: a sentence (not a real-valued scalar); the input space X is strings of words.

  • A natural mapping: map a sentence to a finite-dimensional vector space by counting word occurrences (bag-of-words style) or by word frequencies.

  • Example mapping (bag-of-words):

    • Let the vocabulary be a set of possible words; the feature vector has length equal to the number of distinct words in the vocabulary.

    • Each component corresponds to a word; the value is the frequency or count of that word in the sentence.

    • The model then computes a score as a weighted sum of word indicators:
      y^=∑<em>i=1∣V∣w</em>i1word i occurs in x\hat{y} = \sum<em>{i=1}^{|V|} w</em>i \mathbb{1}{\text{word } i \text{ occurs in } x}
      or using counts/frequencies: y^=∑<em>i=1∣V∣w</em>iϕ<em>i(x)\hat{y} = \sum<em>{i=1}^{|V|} w</em>i \phi<em>i(x) where \phii(x) is the count/frequency of word i in sentence x.

  • Observations from the discussion:

    • The dimension becomes the number of possible words in the vocabulary (potentially very large).

    • The inner product between feature vectors corresponds to a simple linear combination of word-based features.

    • The exact choice of phi determines how the sentence is represented and how the linear model will make predictions.

  • Practical note: Mapping from a sentence to a high-dimensional vector makes the optimization problem the same form as before: solve for w in a high-dimensional linear space with design matrix \Phi (rows are \phi(x_i)^T).

Training in the feature space: formulation and solution approach

  • Given a training set with inputs xi and targets yi, and the feature map \phi, define the design matrix \Phi with rows \phi(xi)^T: Φ=[ϕ(x</em>1)T ⋮ ϕ(x<em>n)T],y=[y</em>1,…,yn]T.\Phi = \begin{bmatrix} \phi(x</em>1)^T \ \vdots \ \phi(x<em>n)^T \end{bmatrix}, \quad \mathbf{y} = [y</em>1, \dots, y_n]^T.

  • Training objective in the feature-space representation:
    min⁡<em>w∣y−Φw∣</em>22.\min<em>{w} | \mathbf{y} - \Phi w |</em>2^2.

  • Solving the optimization:

    • Classic gradient-based methods (gradient descent) or closed-form normal equations can be used depending on conditioning and data size.

    • In standard linear regression in the original space, this is equivalent to solving for w in the expanded feature space, where the effective data dimension can be very large: the dimension of w equals the number of features in \phi (i.e., the width of the feature map).

  • Important interpretation: The variables to learn are twofold in a generalized model: the feature map \phi (which defines the representation) and the linear weights w (which combine the features). The derivation and practical performance depend on both choices.

  • Notation recap:

    • The final predictor for a new input x is: y^(x)=wTϕ(x).\hat{y}(x) = w^T \phi(x).

    • The optimization variables are w (and, implicitly, the chosen \phi). The data determine \Phi and y.

Choosing and understanding the phi function

  • Core question: Which phi should we pick for a given problem? This is a design choice that trades off bias and variance.

  • In a simple linear regression task, phi may be the linear map; in other tasks, phi could be a polynomial expansion, trigonometric features, basis functions like radial basis functions, or features derived from domain knowledge.

  • In text tasks like sentiment analysis, phi maps a sentence to a high-dimensional word-occupancy vector; each dimension corresponds to a word or token feature.

  • Some notes from the discussion:

    • If you choose a single fixed basis (e.g., linear), you can still fit a lot by adjusting w, but you may underfit if the true relationship is non-linear.

    • The mapping can be highly nontrivial (e.g., including 1 for bias, x, x^2, etc.) to capture curvature or interactions.

  • Caveat: A highly rich feature map increases model capacity, which can reduce bias but often increases variance and requires more data to generalize.

  • Next step (in the course): discuss principled ways to choose phi and the implications for generalization and model complexity.

Infinite-dimensional feature spaces and the kernel intuition

  • In theory, one can design feature maps that are infinite-dimensional, enabling very expressive representations.

  • The practical challenge is that computing with such high- or infinite-dimensional \phi would be prohibitive unless one leverages a kernel trick.

  • Kernel viewpoint (implicit): Define a kernel function K that computes inner products in feature space without explicit computation of \phi:
    K(x,x′)=⟨ϕ(x),ϕ(x′)⟩.K(x, x') = \langle \phi(x), \phi(x') \rangle.

  • This allows working with rich feature representations while keeping computations tractable in the original input space, provided a suitable kernel is chosen.

Neural networks as a perspective on feature maps

  • Neural networks can be viewed as learning a flexible, data-driven feature map (the nonlinear transformations across layers) followed by a linear readout layer.

  • In the neural network view described: the network implements a composite feature map (the pi function) and a final linear function of these features.

  • The overall predictor is a linear combination of the outputs of nonlinear activations:

    • Phi becomes the composition of nonlinear layers, and the ultimate output is a linear combination of the last hidden activations.

  • Practical implication: designing a neural network is effectively designing a particular, learned phi, with a linear readout layer on top.

Practical question: How do we pick phi for sentiment/text tasks?

  • In the transcript, the mapping from a sentence to a vector is described as a bag-of-words-like approach: a finite-dimensional vector where each dimension corresponds to a particular word or token, and the value is a count or frequency.

  • The score for sentiment can be obtained by summing weights associated with present words: if wi is the weight for word i and the sentence contains word i, contribute wi to the score.

  • This yields a simple operational pipeline: map sentence to vector via a word-count representation (phi), then compute the linear score with weights w.

  • This illustrates the general pattern: for any input type, define phi to produce a vector in which a linear model can operate, then train w by minimizing the squared error (or another loss) on labeled data.

Optimization perspective recap: solving in the phi-space

  • With the constructed Phi matrix, training reduces to solving:
    min⁡w∣y−Φw∣2.\min_{w} | \mathbf{y} - \Phi w |^2.

  • Inference for a new input x is given by:
    y^(x)=wTϕ(x).\hat{y}(x) = w^T \phi(x).

  • The dimensionality of the optimization grows with the richness of phi; more features lead to larger w and potentially more challenging optimization.

Bias-variance trade-off and model selection via data size

  • Core observation from the lecture: model complexity interacts with training data size to determine generalization performance.

  • Experimental intuition (described in the transcript):

    • With higher-degree polynomial features, the training error can be made very small, but the test error can rise due to overfitting, especially as model capacity grows beyond what the data support.

    • When you fix a model (fixed capacity) and increase the training set size, training error typically decreases, but variance across training sets also changes in predictable ways.

    • The gap between training error and testing error grows with model complexity when data are limited, illustrating overfitting.

  • Definitions (classical bias-variance decomposition):

    • For a given input x, the expected prediction error can be decomposed as:
      E<em>D[(f</em>D(x)−f∗(x))2]=Bias2(x)+Var(fD(x))+σ2(x),\mathbb{E}<em>{D}[ (f</em>{D}(x) - f^*(x))^2 ] = \text{Bias}^2(x) + \text{Var}(f_{D}(x)) + \sigma^2(x),
      where:

    • f^*(x) is the true underlying function (the best possible predictor given the true distribution).

    • f_{D}(x) is the predictor learned from a training set D.

    • Bias(x) = E<em>D[f</em>D(x)]−f∗(x)\mathbb{E}<em>{D}[f</em>{D}(x)] - f^*(x);

    • Var(x) = E<em>D[(f</em>D(x)−E<em>D[f</em>D(x)])2]\mathbb{E}<em>{D}[(f</em>{D}(x) - \mathbb{E}<em>{D}[f</em>{D}(x)])^2];

    • σ2(x)\sigma^2(x) is irreducible noise (variance of the target given x).

  • Conceptual takeaway:

    • Constant predictor (extremely low capacity) has high bias but low variance.

    • Highly flexible models (high capacity) have low bias but high variance.

    • The goal is to balance bias and variance to minimize the total expected error.

  • Illustrative intuition from the discussion:

    • A constant horizontal line has poor alignment with a non-linear true function (high bias) but stable predictions (low variance).

    • A highly flexible model (e.g., high-degree polynomial) can fit training data very well (low bias on training) but generalize poorly (high variance).

    • With sufficient data, higher capacity can reduce both bias and variance; with limited data, high capacity often hurts generalization.

Practical implications: data, capacity, and training data size

  • The number of possible models grows with the complexity of phi, and more training data is needed to ensure that the smaller-than-full training set generalizes.

  • The transcript notes that to properly compare many models (with many degrees of freedom), you need a larger data set to constrain the model and reduce variance.

  • In the toy exercise with 31 variables and 31 binary features, scaling up to a much larger feature space (e.g., with many binary features) requires a proportionally larger training set to stabilize predictions and avoid overfitting.

  • The discussion emphasizes that the bias-variance trade-off is central to model selection: choose phi and model complexity to keep the sum of bias^2 and variance small for the target distribution, given the amount of available data.

Concrete notational recap for study reference

  • Linear regression core model:
    y^=wTx,min⁡<em>w∑</em>i=1n(y<em>i−wTx</em>i)2.\hat{y} = w^T x, \quad \min<em>w \sum</em>{i=1}^n (y<em>i - w^T x</em>i)^2.

  • Generalized model with feature map:
    y^=wTϕ(x),min⁡<em>w∣y−Φw∣2,Φ=[ϕ(x</em>1)T;… ;ϕ(xn)T].\hat{y} = w^T \phi(x), \quad \min<em>w | \mathbf{y} - \Phi w |^2, \quad \Phi = [ \phi(x</em>1)^T; \dots; \phi(x_n)^T ].

  • Polynomial and bias terms: incorporating 1 and higher powers in \phi to capture nonlinearity:

    • Example: if \phi(x) = [1, x, x^2]^T, the model is a quadratic in x.

  • Infinite-dimensional intuition and kernel idea (brief): inner products in feature space can be computed via a kernel K without explicit \phi.

  • Sentiment analysis mapping (bag-of-words example):

    • Vocabulary V with size |V|; feature vector \phi(x) ∈ \mathbb{R}^{|V|} with components equal to word counts or frequencies.

    • Prediction is a weighted sum of word indicators: y^=∑<em>i=1∣V∣w</em>iϕi(x).\hat{y} = \sum<em>{i=1}^{|V|} w</em>i \phi_i(x).

  • Bias-variance decomposition (input-specific form):
    E<em>D[(f</em>D(x)−f∗(x))2]=Bias2(x)+Var(fD(x))+σ2(x).\mathbb{E}<em>{D}[ (f</em>D(x) - f^*(x))^2 ] = \text{Bias}^2(x) + \text{Var}(f_D(x)) + \sigma^2(x).

  • The general lesson: choose phi and model complexity to balance bias and variance given data availability and the target task.

Notes on the audience and context

  • The transcript includes informal student-faculty dialogue about courses (e.g., 580 class, professors, and PhD work), but the essential content for studying is the regression framework, feature maps, and bias-variance discussion.

  • Real-world relevance: understanding how to map inputs to a suitable feature space and how model complexity interacts with data is central to building generalizable machine learning systems.

  • Practical exam-oriented tips:

    • Be able to explain the role of phi in turning nonlinear problems into linear ones in a higher-dimensional space.

    • Be able to write the basic optimization in phi-space as a least-squares problem.

    • Understand the bias-variance trade-off and how training size affects model choice and generalization.

    • Recognize the sentiment-analysis toy example as a concrete instance of a linear model operating on a high-dimensional, sparse feature space (bag-of-words).

End of notes on the lecture content. The next steps typically include a deeper dive into how to select phi in practice, regularization, and how to implement these ideas efficiently for large-scale data.

Logistics and course context

  • Homework: Homework 1 consists of two specific questions as outlined in the syllabus. The reference to “six five” is likely a course code or time mentioned in the transcription, indicating an administrative detail for students.

  • Programming/practice: A question arose regarding the necessity of a specific library for programming tasks versus the sufficiency of a simpler programming tool. The response indicated no firm requirement, suggesting flexibility in tool choice.

  • Office hours and logistics: Office hours are explicitly available for machine learning topic-specific questions, encouraging students to seek direct help. Any logistical questions or comments regarding IT difficulties were acknowledged, and no further outstanding concerns were recorded.

Recap: From linear regression to a general framework

  • Linear regression goal: The primary objective is to derive a linear model capable of predicting a target variable from a set of input features. Given data comprising features XX (an n×dn \times d matrix, where nn is the number of samples and dd is the number of features) and a target vector y\mathbf{y} (a vector of nn observations), the model's prediction is represented as:

    y^=wTx\hat{y} = w^T x

    where ww is the vector of learned weights and xx is a single feature vector.

  • Optimization viewpoint: The process of learning is translated into an optimization problem, typically aimed at minimizing a loss function. When assuming that the noise in the data follows a Gaussian (normal) distribution, the principle of Maximum Likelihood Estimation (MLE) naturally leads to a least-squares objective function for linear models, as this choice maximizes the probability of observing the given data under the model's assumptions.

  • Typical objective (least squares): The standard objective function for linear regression, derived from the MLE under Gaussian noise, seeks to minimize the sum of squared differences between the actual and predicted target values. This is expressed as:

    min⁡<em>w∑</em>i=1n(y<em>i−wTx</em>i)2=min⁡<em>w∣y−Xw∣</em>22\min<em>w \sum</em>{i=1}^n (y<em>i - w^T x</em>i)^2 = \min<em>w |\mathbf{y} - X w|</em>2^2

    Here, XX is the design matrix, where each row x<em>iTx<em>i^T corresponds to the feature vector for the ii-th sample, and y=[y</em>1,…,yn]T\mathbf{y} = [y</em>1, \dots, y_n]^T is the column vector of observed target values. The minimization aims to find the weight vector ww that best fits the data.

  • Connection with probability: For errors that are independently and identically distributed (i.i.d.) Gaussian, minimizing the sum of squared errors is mathematically equivalent to finding the maximum likelihood estimates for the model parameters. This optimization problem is convex and quadratic in ww, allowing it to be solved efficiently using either direct linear algebra methods (such as the normal equations) or iterative optimization algorithms (like gradient descent).

  • Conceptual simplification: This framework allows many learning problems, including regression and classification, to be posed as minimization problems over a vector space. This provides a powerful and unifying perspective, reducing diverse learning tasks to a common optimization structure, which makes them amenable to similar solution techniques.

Extending beyond linear regression: bias, variance, and feature maps

  • Main idea: To model non-linear relationships using fundamentally linear models, a key technique is to employ a feature map (or basis function). This map transforms the original input features into a higher-dimensional feature space, where the relationship can then be approximated by a linear model. This effectively turns a non-linear problem in the original space into a linear problem in the transformed space.

  • Feature map (basis function): A function  ϕ\,\phi that maps an input xx from its original input space X\mathcal{X} to a new, potentially higher-dimensional vector space Rm\mathbb{R}^m:

    ϕ:X→Rm,with ϕ(x)=[ϕ<em>1(x),ϕ</em>2(x),…,ϕm(x)]T.\phi: \mathcal{X} \to \mathbb{R}^m, \quad \text{with } \phi(x) = [\phi<em>1(x), \phi</em>2(x), \dots, \phi_m(x)]^T.

    Each component ϕj(x)\phi_j(x) is a basis function that transforms the original input data.

  • Generalized linear model with feature map: Once the input xx is transformed by ϕ(x)\phi(x), the linear model is applied in this new feature space:

    y^=wTϕ(x).\hat{y} = w^T \phi(x).

    This model is linear with respect to the weights ww but can capture complex non-linearities with respect to the original input xx due to the transformation by ϕ\phi.

  • Special cases:

    • Identity map: If ϕ(x)=x\phi(x) = x, the feature map is simply the identity function, and we recover the ordinary linear regression model, operating directly in the original feature space.

    • Enhanced feature maps: Common enhancements include adding a bias term (a constant feature, typically 1) or polynomial terms (e.g., x,x2,x3x, x^2, x^3 for a single input dimension) by expanding the feature space. For multiple input dimensions, interaction terms (e.g., x<em>1x</em>2x<em>1 x</em>2) can also be included.

  • Dimensionality of feature space:

    • If the feature map increases the dimension of the data (e.g., from dd to mm where m>dm > d), the linear model then operates in this larger feature space. For instance, adding a constant feature increases the dimension by one, while adding polynomial terms dramatically expands it. This process can significantly increase the model's capacity to fit complex data.

    • In higher-dimensional settings (e.g., dd dimensions for the input, expanded to mm features), the inner product between the transformed feature vectors in the new space directly yields the components of the objective function. For example, the loss function will involve linear terms in this expanded feature space.

  • Infinite-dimensional feature spaces (conceptual):

    • Conceptually, it is possible to define feature maps that project inputs into an infinite-dimensional space. In such scenarios, the parameters could be viewed as functions, and the inner product might be an integral (e.g., an integral over a continuum for some basis functions) rather than a finite sum:

      ⟨ϕ<em>x,ϕ</em>x′⟩=∫w(t)ϕ<em>x(t)ϕ</em>x′(t)dt  (illustrative, general idea).\langle \phi<em>x, \phi</em>{x'} \rangle = \int w(t) \phi<em>x(t) \phi</em>{x'}(t) dt \; \text{(illustrative, general idea)}.

    • This theoretical possibility allows for extremely rich and flexible representations, capable of approximating virtually any continuous function. While direct computation in infinite dimensions is impossible, kernels (discussed later) provide a powerful way to work implicitly in such spaces.

  • Connection to neural networks: A neural network can be conceptualized as a sophisticated feature mapping system. The initial layers of a neural network (before the final output layer) perform a series of non-linear transformations on the input, effectively creating a learned, complex feature map, often denoted as π(x)\pi(x). The final layer of the network typically acts as a linear model applied to these transformed features. In this view, learning within a neural network involves simultaneously optimizing both the complex feature map (through the network's architecture and parameters) and the final linear weights ww.

  • Practical takeaway: In machine learning, we often explicitly specify a feature map ϕ\phi (the basis functions) for a given model. With ϕ\phi fixed, the learning task simplifies to optimizing the corresponding weights ww. This separation simplifies the problem into a tractable optimization challenge.

What basis functions do we actually use?

  • The simplest basis: The most fundamental basis function is the linear (identity) basis, where ϕ(x)=x\phi(x) = x. If no bias term is included, the model represents a straight line passing through the origin. This is suitable only for purely linear relationships.

  • To enrich the model, add more features:

    • Include a constant term (bias) and the original feature xx to create a first-degree feature expansion. This results in a model with both an intercept and a slope, allowing the linear model to shift vertically and capture a wider range of linear relationships.

    • Include higher-degree terms (e.g., x2,x3x^2, x^3). By adding these polynomial terms to ϕ(x)\phi(x), the linear model in the expanded space corresponds to a polynomial function in the original input xx. This enables the model to capture curvature and more complex non-linear patterns.

  • General strategy: A common approach in model design is to begin with a relatively small and manageable set of basis functions. Gradually, the richness or complexity of ϕ\phi is increased until an acceptable balance between bias and variance is achieved, ensuring the model is expressive enough without overfitting.

  • Important nuance: The selection of basis functions is a critical design decision. It directly influences the model's capacity (its ability to fit complex patterns), the dynamics of the learning process, and ultimately, its ability to generalize well to unseen data. An inappropriate choice for ϕ\phi can lead to either underfitting or overfitting.

Sentiment analysis as a concrete example of feature maps

  • Input type: In sentiment analysis, the input is typically a sentence or a block of text, which is an unstructured string of words, not a simple real-valued scalar or vector. The input space X\mathcal{X} for such tasks consists of natural language strings.

  • A natural mapping: To apply numerical models like linear regression to text, a natural and common approach is to map a sentence into a finite-dimensional vector space. Methods for this include counting word occurrences (often called