You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Linear Regression/README.markdown
+3-3
Original file line number
Diff line number
Diff line change
@@ -75,7 +75,7 @@ for n in 1...numberOfIterations {
75
75
76
76
The program loops through each data point (each car age and car price). For each data point it adjusts the intercept and the slope to bring them closer to the correct values. The equations used in the code to adjust the intercept and the slope are based on moving in the direction of the maximal reduction of these variables. This is a *gradient descent*.
77
77
78
-
We want to minimse the square of the distance between the line and the points. We define a function `J` which represents this distance - for simplicity we consider only one point here. This function `J` is proprotional to `((slope.carAge + intercept) - carPrice)) ^ 2`.
78
+
We want to minimise the square of the distance between the line and the points. We define a function `J` which represents this distance - for simplicity we consider only one point here. This function `J` is proportional to `((slope.carAge + intercept) - carPrice)) ^ 2`.
79
79
80
80
In order to move in the direction of maximal reduction, we take the partial derivative of this function with respect to the slope, and similarly for the intercept. We multiply these derivatives by our factor alpha and then use them to adjust the values of slope and intercept on each iteration.
81
81
@@ -98,7 +98,7 @@ Here is the same data shown as a graph. Each of the blue lines on the graph repr
98
98
99
99
After 18,000 iterations it looks as if the line is getting closer to what we would expect (just by looking) to be the correct line of best fit. Also, each additional 2,000 iterations has less and less effect on the final result - the values of the intercept and the slope are converging on the correct values.
100
100
101
-
##A closed form solution
101
+
##A closed form solution
102
102
103
103
There is another way we can calculate the line of best fit, without having to do multiple iterations. We can solve the equations describing the least squares minimisation and just work out the intercept and slope directly.
104
104
@@ -139,7 +139,7 @@ This function takes as arguments two arrays of Doubles, and returns a function w
139
139
Using this line, we would predict a price for our 4 year old car of £6952.
140
140
141
141
142
-
##Summary
142
+
##Summary
143
143
We've seen two different ways to implement a simple linear regression in Swift. An obvious question is: why bother with the iterative approach at all?
144
144
145
145
Well, the line we've found doesn't fit the data perfectly. For one thing, the graph includes some negative values at high car ages! Possibly we would have to pay someone to tow away a very old car... but really these negative values just show that we have not modelled the real life situation very accurately. The relationship between the car age and the car price is not linear but instead is some other function. We also know that a car's price is not just related to its age but also other factors such as the make, model and engine size of the car. We would need to use additional variables to describe these other factors.
0 commit comments