33 views (last 30 days)

Show older comments

New to MatLab (and programming in general) and started messing around with my first attempt at linear regression. My code is running but when I run this, the plot has the data points but does not show the linear regression line. Any idea as to why?

format long g

t = [1; 246; 417; 607; 736]; %feature variable, univariate (x axis)

p = [234.31; 431.76; 652.14; 817.26; 2358.96]; %results variable (y axis)

m = length(p); %number of training examples

theta = zeros(2,1); %initiate parameters

iterations = 1500; %iterations needed for GD

alpha = 0.01;

plot(t,p, "bd", "MarkerSize",8);

title ('Bitcoin price');

xlabel('Days Transpired');

ylabel('Bitcoin Price');

%compute cost function

%add column to t

t = [ones(m,1),t];

J = computeprice(t, p, theta);

%running gradient descent

[theta, J_history] = GradientDescentBTC(t, p, theta, alpha, iterations);

hold on;

plot(t(:, 2),t * theta, '--', "LineWidth", 1);

legend('Training data', 'Linear regression');

hold off;

Star Strider
on 28 Jan 2021

I believe there is something wrong with ‘computeprice’. It should produce a vector the same length as ‘t’ that is then compared with ’p’ to estimate ‘theta’. Note that it also must have ‘m’ as an argument.

Make necessary changes in ‘computeprice’ in this code (coded as an anonymous function here) to get the desired result:

computeprice = @(t, theta, m) 1 ./ (2 * m) * t.*theta;

t = [1; 246; 417; 607; 736]; %feature variable, univariate (x axis)

p = [234.31; 431.76; 652.14; 817.26; 2358.96]; %results variable (y axis)

m = length(p); %number of training examples

est_theta = fminsearch(@(theta) norm(computeprice(t, theta, m) - p), rand);

figure

plot(t, p, 'p')

hold on

plot(t, computeprice(t, est_theta, m), '-r', 'LineWidth',2)

hold off

grid

Since it is supposed to be a linear regression, and if I correctly understand what ‘computeprice’ is supposed to do, a much simpler way to calculate this is to use the mldivide,\ function or operator:

est_theta = [1/(2*m)*t] \ p;

figure

plot(t, p, 'p')

hold on

plot(t, est_theta*1/(2*m)*t, '-r', 'LineWidth',2)

hold off

grid

producing the same result as the fminsearch call.

Note that this will demonstrate the linear regression.

.

Star Strider
on 28 Jan 2021

As always, my pleasure!

‘re: your t(:,2) comment, didn't I create a 2 column vector with t = [ones(m,1),t]?’

I must have missed copying that when I tried running your code.

Running your code again (and copying everything), ‘J_history’ starts at 3.1176e+012 and quickly overflows. The value for ‘theta’ is a (2x2) NaN vector, so myultiplying it by ‘t’ produces a NaN vector:

CheckResult = t * theta

produces:

CheckResult =

NaN

NaN

NaN

NaN

NaN

and NaN (and other non-finite) values do not plot.

I did my best to see in detail what the problems are with your code, however I could not figure out the logic.

VBBV
on 25 Jan 2021

%if true

plot(t(:, 2),t.*theta, '--', "LineWidth", 1);

What is size of matrix theta? Also use element matrix multiplication

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!