Skip to content

Commit 7905337

Browse files
committed
First draft of Linear Regression
First draft
1 parent 912c21d commit 7905337

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Linear Regression
2+
3+
import Foundation
4+
5+
let carAge: [Double] = [10, 8, 3, 3, 2, 1]
6+
let carPrice: [Double] = [500, 400, 7000, 8500, 11000, 10500]
7+
var intercept = 10000.0
8+
var slope = -1000.0
9+
let alpha = 0.0001
10+
let numberOfCarAdvertsWeSaw = carPrice.count
11+
12+
func h(carAge: Double) -> Double {
13+
return intercept + slope * carAge
14+
}
15+
16+
for n in 1...10000 {
17+
for i in 1...numberOfCarAdvertsWeSaw {
18+
intercept += alpha * (carPrice[i-1] - h(carAge[i-1])) * 1
19+
slope += alpha * (carPrice[i-1] - h(carAge[i-1])) * carAge[i-1]
20+
}
21+
}
22+
23+
print("A car age 4 years is predicted to be worth £\(Int(h(4)))")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Linear Regression/LinearRegression.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Linear Regression/README.markdown

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Linear Regression
2+
## First draft version of this document
3+
4+
Linear regression is a technique for creating a model of the relationship between two (or more) variable quantities.
5+
6+
For example, let's say we are planning to sell a car. We are not sure how much money to ask for. So we look at recent advertisments for the asking prices of other cars. There are a lot of variables we could look at - for example: make, model, engine size. To simplify our task, we collect data on just the age of the car and the price:
7+
8+
Age (in years)| Price (in £)
9+
--------------|-------------
10+
10 | 500
11+
8 | 400
12+
3 | 7,000
13+
3 | 8,500
14+
2 | 11,000
15+
1 | 10,500
16+
17+
Our car is 4 years old. How can we set a price for our car based on the data in this table?
18+
19+
*Written for Swift Algorithm Club by James Harrop*

0 commit comments

Comments
 (0)