From 8928cbe10868a88635eab08edcf7e3fb4a145966 Mon Sep 17 00:00:00 2001 From: Pascal Burlet <59558722+paescebu@users.noreply.github.com> Date: Thu, 15 Dec 2022 15:28:57 +0100 Subject: [PATCH 1/6] Create LICENSE.md --- LICENSE.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE.md diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..b7532ea --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Pascal Burlet + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 3aa07c5ba6b6526a4938b5a70c88efa3ac2ae0a6 Mon Sep 17 00:00:00 2001 From: Domenico Date: Thu, 23 Mar 2023 21:53:00 +0100 Subject: [PATCH 2/6] Add function for "Residual sum of squares" --- .../PolynomialRegression.swift | 14 ++++++++++++++ .../PolynomialRegressionTests.swift | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/Sources/PolynomialRegressionSwift/PolynomialRegression.swift b/Sources/PolynomialRegressionSwift/PolynomialRegression.swift index 7e48d76..516af98 100644 --- a/Sources/PolynomialRegressionSwift/PolynomialRegression.swift +++ b/Sources/PolynomialRegressionSwift/PolynomialRegression.swift @@ -128,6 +128,20 @@ public class PolynomialRegression { return b } + + static func calculateResidualSumOfSquares(ofPoints points: [CGPoint], withCoefficients coefficients: [Double]) -> Double{ + + let yPolynomialResults = vDSP.evaluatePolynomial(usingCoefficients: coefficients.reversed(), withVariables: points.map { Double($0.x) } ) + let yValuesOfPoints = points.map { $0.y } + + var sumOfSquares: Double = 0 + + for (px, y) in zip(yPolynomialResults, yValuesOfPoints) { + sumOfSquares += Double(pow(px - y, 2)) + } + + return sumOfSquares + } public enum LAPACKError: Swift.Error { case internalError diff --git a/Tests/PolynomialRegressionSwiftTests/PolynomialRegressionTests.swift b/Tests/PolynomialRegressionSwiftTests/PolynomialRegressionTests.swift index 4ee5311..fb48195 100644 --- a/Tests/PolynomialRegressionSwiftTests/PolynomialRegressionTests.swift +++ b/Tests/PolynomialRegressionSwiftTests/PolynomialRegressionTests.swift @@ -97,4 +97,10 @@ class PolynomialRegressionTests: XCTestCase { regression = PolynomialRegression.regression(withPoints: [CGPoint(x: 0, y: 0)], degree: 1) XCTAssertEqual(regression, nil) } + + func testPolynomialRegressionSumOfSquares(){ + let regression = PolynomialRegression.regression(withPoints: points, degree: 3) + let sumOfSquare = PolynomialRegression.calculateResidualSumOfSquares(ofPoints: points, withCoefficients: regression!) + XCTAssertEqual(sumOfSquare, 551.0158144934717) + } } From 00f3bb7032fbcab943104cabe8db8f6c4b2bf058 Mon Sep 17 00:00:00 2001 From: Domenico Date: Fri, 24 Mar 2023 09:14:19 +0100 Subject: [PATCH 3/6] Add check for params and improve tests --- .../PolynomialRegression.swift | 9 ++++++++- .../PolynomialRegressionTests.swift | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Sources/PolynomialRegressionSwift/PolynomialRegression.swift b/Sources/PolynomialRegressionSwift/PolynomialRegression.swift index 516af98..bbc725d 100644 --- a/Sources/PolynomialRegressionSwift/PolynomialRegression.swift +++ b/Sources/PolynomialRegressionSwift/PolynomialRegression.swift @@ -129,7 +129,14 @@ public class PolynomialRegression { return b } - static func calculateResidualSumOfSquares(ofPoints points: [CGPoint], withCoefficients coefficients: [Double]) -> Double{ + static func calculateResidualSumOfSquares(ofPoints points: [CGPoint], withCoefficients coefficients: [Double]) -> Double? { + guard points.count > 1 else { + return nil + } + + guard coefficients.count > 1 else { + return nil + } let yPolynomialResults = vDSP.evaluatePolynomial(usingCoefficients: coefficients.reversed(), withVariables: points.map { Double($0.x) } ) let yValuesOfPoints = points.map { $0.y } diff --git a/Tests/PolynomialRegressionSwiftTests/PolynomialRegressionTests.swift b/Tests/PolynomialRegressionSwiftTests/PolynomialRegressionTests.swift index fb48195..8a380b4 100644 --- a/Tests/PolynomialRegressionSwiftTests/PolynomialRegressionTests.swift +++ b/Tests/PolynomialRegressionSwiftTests/PolynomialRegressionTests.swift @@ -101,6 +101,22 @@ class PolynomialRegressionTests: XCTestCase { func testPolynomialRegressionSumOfSquares(){ let regression = PolynomialRegression.regression(withPoints: points, degree: 3) let sumOfSquare = PolynomialRegression.calculateResidualSumOfSquares(ofPoints: points, withCoefficients: regression!) - XCTAssertEqual(sumOfSquare, 551.0158144934717) + XCTAssertEqual(sumOfSquare!, 551.0158144934717) + } + + func testPolynomialRegressionSumOfSquaresFails(){ + let regression = PolynomialRegression.regression(withPoints: points, degree: 3) + + var sumOfSquare = PolynomialRegression.calculateResidualSumOfSquares(ofPoints: [], withCoefficients: regression!) + XCTAssertEqual(sumOfSquare, nil) + + sumOfSquare = PolynomialRegression.calculateResidualSumOfSquares(ofPoints: points, withCoefficients: []) + XCTAssertEqual(sumOfSquare, nil) + + sumOfSquare = PolynomialRegression.calculateResidualSumOfSquares(ofPoints: [CGPoint(x: 0, y: 0)], withCoefficients: regression!) + XCTAssertEqual(sumOfSquare, nil) + + sumOfSquare = PolynomialRegression.calculateResidualSumOfSquares(ofPoints: [], withCoefficients: []) + XCTAssertEqual(sumOfSquare, nil) } } From 054efbbcd6138f34da7cd95e131c2b2c0e9e5b3c Mon Sep 17 00:00:00 2001 From: Domenico Date: Fri, 24 Mar 2023 12:17:37 +0100 Subject: [PATCH 4/6] Make func public --- Sources/PolynomialRegressionSwift/PolynomialRegression.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/PolynomialRegressionSwift/PolynomialRegression.swift b/Sources/PolynomialRegressionSwift/PolynomialRegression.swift index bbc725d..327fdb5 100644 --- a/Sources/PolynomialRegressionSwift/PolynomialRegression.swift +++ b/Sources/PolynomialRegressionSwift/PolynomialRegression.swift @@ -129,7 +129,7 @@ public class PolynomialRegression { return b } - static func calculateResidualSumOfSquares(ofPoints points: [CGPoint], withCoefficients coefficients: [Double]) -> Double? { + public static func calculateResidualSumOfSquares(ofPoints points: [CGPoint], withCoefficients coefficients: [Double]) -> Double? { guard points.count > 1 else { return nil } From f808167d5168a5b28791033aa223196396ecce96 Mon Sep 17 00:00:00 2001 From: Domenico Date: Fri, 24 Mar 2023 15:24:24 +0100 Subject: [PATCH 5/6] Refactoring --- .../PolynomialRegression.swift | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Sources/PolynomialRegressionSwift/PolynomialRegression.swift b/Sources/PolynomialRegressionSwift/PolynomialRegression.swift index 327fdb5..4f21ae9 100644 --- a/Sources/PolynomialRegressionSwift/PolynomialRegression.swift +++ b/Sources/PolynomialRegressionSwift/PolynomialRegression.swift @@ -137,14 +137,16 @@ public class PolynomialRegression { guard coefficients.count > 1 else { return nil } - - let yPolynomialResults = vDSP.evaluatePolynomial(usingCoefficients: coefficients.reversed(), withVariables: points.map { Double($0.x) } ) + + let xValuesOfPoints = points.map { Double($0.x) } let yValuesOfPoints = points.map { $0.y } - + + let yPolynomialResults = vDSP.evaluatePolynomial(usingCoefficients: coefficients.reversed(), withVariables: xValuesOfPoints ) + var sumOfSquares: Double = 0 - for (px, y) in zip(yPolynomialResults, yValuesOfPoints) { - sumOfSquares += Double(pow(px - y, 2)) + for (yPoly, yPoint) in zip(yPolynomialResults, yValuesOfPoints) { + sumOfSquares += Double(pow(yPoly - yPoint, 2)) } return sumOfSquares From 5d8c4faa49c5979bcb35895364938b1823c36fe9 Mon Sep 17 00:00:00 2001 From: Pascal Burlet <59558722+paescebu@users.noreply.github.com> Date: Thu, 20 Apr 2023 22:26:35 +0200 Subject: [PATCH 6/6] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index daddc48..211e0c1 100644 --- a/README.md +++ b/README.md @@ -61,3 +61,9 @@ You're welcome to use it in commercial, closed-source, open source, free or any ## Warranty The code comes with no warranty of any kind. I hope it'll be useful to you (it certainly is to me), but I make no guarantees regarding its functionality or otherwise. + +## Donations + +You really don't have to pay anything to use this package. But if you feel generous today and would like to donate because this package helped you so much, here's a PayPal donation link: https://www.paypal.com/donate/?hosted_button_id=JYL8DBGA2X4YQ + +or just buy me a hot chocolate: https://www.buymeacoffee.com/paescebu \ No newline at end of file