@@ -19,26 +19,26 @@ let alpha = 0.0001
1919
2020for n in 1 ... numberOfIterations {
2121 for i in 0 ..< numberOfCarAdvertsWeSaw {
22- let difference = carPrice [ i] - predictedCarPrice( carAge [ i] )
22+ let difference = carPrice [ i] - predictedCarPrice( carAge: carAge [ i] )
2323 intercept += alpha * difference
2424 slope += alpha * difference * carAge[ i]
2525 }
2626}
2727
28- print ( " A car age of 4 years is predicted to be worth £ \( Int ( predictedCarPrice ( 4 ) ) ) " )
28+ print ( " A car age of 4 years is predicted to be worth £ \( Int ( predictedCarPrice ( carAge : 4 ) ) ) " )
2929
3030// A closed form solution
3131
32- func average( input: [ Double ] ) -> Double {
33- return input. reduce ( 0 , combine : + ) / Double( input. count)
32+ func average( _ input: [ Double ] ) -> Double {
33+ return input. reduce ( 0 , + ) / Double( input. count)
3434}
3535
36- func multiply( input1: [ Double ] , _ input2: [ Double ] ) -> [ Double ] {
37- return input1. enumerate ( ) . map ( { ( index, element) in return element*input2 [ index] } )
36+ func multiply( _ input1: [ Double ] , _ input2: [ Double ] ) -> [ Double ] {
37+ return input1. enumerated ( ) . map ( { ( index, element) in return element*input2 [ index] } )
3838}
3939
40- func linearRegression( xVariable: [ Double ] , _ yVariable: [ Double ] ) -> ( Double -> Double ) {
41- let sum1 = average ( multiply ( xVariable , yVariable ) ) - average( xVariable) * average( yVariable)
40+ func linearRegression( _ xVariable: [ Double ] , _ yVariable: [ Double ] ) -> ( ( Double ) -> Double ) {
41+ let sum1 = average ( multiply ( yVariable , xVariable ) ) - average( xVariable) * average( yVariable)
4242 let sum2 = average ( multiply ( xVariable, xVariable) ) - pow( average ( xVariable) , 2 )
4343 let slope = sum1 / sum2
4444 let intercept = average ( yVariable) - slope * average( xVariable)
0 commit comments