Regression analysis in MATLAB - Time & Space Complexity
When we run regression analysis, we want to know how long it takes as our data grows.
We ask: How does the time to find the best line change when we add more data points?
Analyze the time complexity of the following code snippet.
% X is input data matrix (n rows, p columns)
% y is output vector (n rows)
X = [ones(n,1) data];
coefficients = (X' * X) \ (X' * y);
This code calculates regression coefficients using the normal equation method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Matrix multiplication and matrix inversion.
- How many times: These operations process all n data points and p features at once.
As the number of data points (n) grows, the time to multiply matrices grows roughly with n times p squared.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Small matrix multiplications and inversion |
| 100 | About 10 times more work than n=10 |
| 1000 | About 100 times more work than n=10 |
Pattern observation: The work grows roughly linearly with the number of data points but depends more on the number of features squared.
Time Complexity: O(n p^2 + p^3)
This means the time grows mostly with the number of data points times the square of features, plus the cube of features for matrix inversion.
[X] Wrong: "The time only depends on the number of data points n."
[OK] Correct: The number of features p also affects time a lot because matrix operations depend on p squared and cubed.
Understanding how regression scales helps you explain your code choices clearly and shows you know what happens as data grows.
"What if we used an iterative method like gradient descent instead of the normal equation? How would the time complexity change?"
Practice
What is the main purpose of regression analysis in MATLAB?
Solution
Step 1: Understand regression analysis goal
Regression analysis is used to find how one variable depends on others.Step 2: Match purpose with options
Only To find relationships between variables describes finding relationships between variables, which is the goal of regression.Final Answer:
To find relationships between variables -> Option AQuick Check:
Regression = find relationships [OK]
- Confusing regression with sorting or plotting
- Thinking regression calculates matrix inverses
Which MATLAB command correctly fits a linear regression model to data X and response Y?
?
Solution
Step 1: Recall MATLAB function for linear regression
The functionfitlmfits linear regression models in MATLAB.Step 2: Check options for correct syntax
Only mdl = fitlm(X, Y); usesfitlm(X, Y)correctly; others are invalid or incorrect functions.Final Answer:
mdl = fitlm(X, Y); -> Option AQuick Check:
fitlm = linear model fit [OK]
- Using non-existent functions like linearfit or fitmodel
- Confusing regress syntax with fitlm
What is the output of this MATLAB code?
X = (1:5)'; Y = [2; 4; 6; 8; 10]; mdl = fitlm(X, Y); disp(mdl.Coefficients.Estimate(2));
Solution
Step 1: Understand data and model
X is 1 to 5, Y is 2,4,6,8,10 which is Y = 2*X exactly.Step 2: Interpret coefficient output
The slope (coefficient for X) is 2, somdl.Coefficients.Estimate(2)returns 2.Final Answer:
2 -> Option BQuick Check:
Slope = 2 [OK]
- Confusing intercept with slope coefficient
- Misreading coefficient index
Identify the error in this MATLAB code for regression:
X = [1 2 3 4 5]; Y = [2; 4; 6; 8; 10]; mdl = fitlm(X, Y);
Solution
Step 1: Check input shapes for fitlm
fitlm expects predictors and response as column vectors or tables.Step 2: Identify vector orientation
X is a row vector and Y is a column vector; they should be column vectors for fitlm to work properly.Final Answer:
X and Y should be column vectors, not row vectors -> Option DQuick Check:
fitlm needs column vectors [OK]
- Passing row vectors instead of column vectors
- Thinking fitlm needs 3 inputs
- Believing fitlm can't handle numeric vectors
You have data vectors X and Y with some outliers. Which approach in MATLAB regression helps reduce outlier impact?
Solution
Step 1: Understand outlier effect on regression
Outliers can distort ordinary least squares regression results.Step 2: Identify MATLAB option for robustness
UsingRobustOptsset to 'on' infitlmapplies robust regression reducing outlier influence.Final Answer:
Usefitlm(X, Y, ''RobustOpts'', ''on'')-> Option CQuick Check:
RobustOpts = reduce outlier impact [OK]
- Ignoring outliers and using default fitlm
- Removing data points without analysis
- Using polynomial fit which may not reduce outliers
Ready to prove it?
Pass the test and this pattern moves unknown
