Friday, June 29, 2012

How to implement normal equation (least square solution) in Matlab

normal equation in matlab
Consider the following problem. Problem Video link.

House Size
Price
1700
53000
2100
65000
1900
59000
1300
41000
1600
50000
2200
68000
From these data we want to predict the future house price from their size. Assuming the relationship between house price and size is linear, the question is
Price = (multi)*size + constant
We have to find two parameters here. First the multi and then constant.  Let us solve this problem using normal equation (it is also called least square solution). In this example house size is our only attribute. Therefore, we have to construct the following matrix.
 X=[1 1700;1 2100;1 1900;1 1300;1 1600;1 2200]
X =
           1        1700
           1        2100
           1        1900
           1        1300
           1        1600
           1        2200
Note that, here we augment the first column with 1.
Our price vector will be the following
p=[53000;65000;59000;41000;50000;68000]
p =
       53000
       65000
       59000
       41000
       50000
       68000
Now, using normal equation we can find those two parameters

THETA= inverse(X’X)X’p
Theta = (inv(X'*X)*X')*p
ans =
   1.0e+03 *
    2.0000
    0.0300
Therefore, multi=30 and constant=30

No comments:

Post a Comment

Please ask if anything is not clear enough..........