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
+17-16Lines changed: 17 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ We can describe the straight line in terms of two variables:
30
30
31
31
This is the equation for our line:
32
32
33
-
carPrice = slope * carAge + intercept
33
+
`carPrice = slope * carAge + intercept`
34
34
35
35
36
36
How can we find the best values for the intercept and the slope? Let's look at two different ways to do this.
@@ -50,18 +50,19 @@ This is how we can represent our straight line:
50
50
```swift
51
51
var intercept =0.0
52
52
var slope =0.0
53
-
funcpredictedCarPrice(carAge: Double) ->Double {
53
+
funcpredictedCarPrice(_carAge: Double) ->Double {
54
54
return intercept + slope * carAge
55
55
}
56
+
56
57
```
57
58
Now for the code which will perform the iterations:
58
59
59
60
```swift
60
61
let numberOfCarAdvertsWeSaw = carPrice.count
61
-
letiterations=2000
62
+
letnumberOfIterations=100
62
63
let alpha =0.0001
63
64
64
-
for n in1...iterations {
65
+
for n in1...numberOfIterations {
65
66
for i in0..<numberOfCarAdvertsWeSaw {
66
67
let difference = carPrice[i] -predictedCarPrice(carAge[i])
67
68
intercept += alpha * difference
@@ -74,7 +75,7 @@ for n in 1...iterations {
74
75
75
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*.
76
77
77
-
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 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
79
79
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.
80
81
@@ -104,17 +105,17 @@ There is another way we can calculate the line of best fit, without having to do
104
105
First we need some helper functions. This one calculates the average (the mean) of an array of Doubles:
We are using the ```reduce``` Swift function to sum up all the elements of the array, and then divide that by the number of elements. This gives us the mean value.
112
113
113
114
We also need to be able to multiply each element in an array by the corresponding element in another array, to create a new array. Here is a function which will do this:
let sum1 =average(multiply(ys, xs)) -average(xs) *average(ys)
129
+
let sum2 =average(multiply(xs, xs)) -pow(average(xs), 2)
129
130
let slope = sum1 / sum2
130
-
let intercept =average(yVariable) - slope *average(xVariable)
131
-
return { intercept + slope *$0 }
131
+
let intercept =average(ys) - slope *average(xs)
132
+
return { x inintercept + slope *x }
132
133
}
133
134
```
134
-
This function takes as arguments two arrays of Doubles, and returns a function which is the line of best fit. The formulas to calculate the slope and the intercept can be derived from our definition of the function J. Let's see how the output from this line fits our data:
135
+
This function takes as arguments two arrays of Doubles, and returns a function which is the line of best fit. The formulas to calculate the slope and the intercept can be derived from our definition of the function `J`. Let's see how the output from this line fits our data:
135
136
136
137

137
138
@@ -145,4 +146,4 @@ Well, the line we've found doesn't fit the data perfectly. For one thing, the gr
145
146
146
147
It turns out that in some of these more complicated models, the iterative approach is the only viable or efficient approach. This can also occur when the arrays of data are very large and may be sparsely populated with data values.
147
148
148
-
*Written for Swift Algorithm Club by James Harrop*
149
+
*Written for Swift Algorithm Club by James Harrop*
0 commit comments