Wednesday, 12 October 2011

Drawing the Hypothesis Function and Predicting the Outcome

Question: Once you've minimised the cost function J(θ0, θ1), how do you predict the house prices?


When you've done all the calculations and minimised J(θ0, θ1) over all values of θ0 and θ1,  those values of  θ0 and θ1to give you the best possible line hθ(x) = θ0 + θ1x through the data. This line can be used to predict the outcome y for some new input x. For the house price example, x is the size of the house and y is the amount it is sold for. The hypothesis hθ(x) predicts what the price will be given a particular size of house.

I'll use the same values as I used in the previous post.

Visually


Visually, we can draw a graph of  y = hθ(x) and then read the answer off the graph. I put the values from the previous post into an applet (linked to by Antony Flores) to obtain  hθ(x).  Note that it gives y = 0.92x + 0.93 which is a different order from the way we're writing  hθ(x) = 0.93 + 0.92x. (It doesn't make any difference, but could lead to confusion over which is θ0 and which is θ1.)

To Draw the Line


There are various different approaches to drawing the line, y = 0.93 + 0.92x. The value given by θ0, here 0.93, is called the y-intercept. It's the point where the  line crosses the y-axis. Plot that point. Next work out another point on the line, say when x= 10, ( y = 0.93 + 0.92*10 = 10.13) and plot that too.  Draw a line through the two points. Ideally, you should draw a third point for error  checking purposes. (If the three points aren't in a straight line, you've made a mistake plotting or calculating one of the points.)


Reading the Answer from the Graph



Once you've got your line on the graph,  you can read off the predicted y-value given an x-value. Suppose we want to know what y is when x=6. We draw a line (or visualise it) going vertically up from the x-axis at x=6 up to the line y= 0.93 + 0.92x. When it hits the line, head off horizontally towards the y-axis. Where you meet the y-axis, you can read off the predicted y-value. From my graph, it looks like it's somewhere around 6.5 as the scale is too small 9 (and the line way too thick) to be accurate.

Inaccuracy


The problem with this method is that it is difficult to read accurately. The scale of my graph isn't good enough to get an accurate prediction. A better option is to calculate the value using the function  hθ(x) = 0.93 + 0.92x.

Calculating the Value of y


Instead of trying to read off the value from the graph, we can calculate the predicted value of y from the hypothesis hθ(x) = 0.93 + 0.92x. To do this, we input the value of x we are interested in, in this case, x=6.

hθ(6) = 0.93 + 0.92*6 = 0.93+5.52 = 6.45. The predicted value of y when x=6 is y=6.45.


Examples


House prices (in $1000) are predicted by the hypothesis hθ(x) = 50 + 0.1x where x is the area in square feet.

What is the predicted price of a house which is
(a) 1000 square feet,
(b) 1250 square feet,
(c) 1500 square feet?

Answers: purple on purple background
(a)  hθ(1000) = 50 + 0.1*1000 = $150000
(b) hθ(1250) = 50 + 0.1*1250 = $175000
(c) hθ(1500) = 50 + 0.1*1500 = $200000

No comments:

Post a Comment