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. 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 diff --git a/Sources/PolynomialRegressionSwift/PolynomialRegression.swift b/Sources/PolynomialRegressionSwift/PolynomialRegression.swift index 7e48d76..4f21ae9 100644 --- a/Sources/PolynomialRegressionSwift/PolynomialRegression.swift +++ b/Sources/PolynomialRegressionSwift/PolynomialRegression.swift @@ -128,6 +128,29 @@ public class PolynomialRegression { return b } + + public 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 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 (yPoly, yPoint) in zip(yPolynomialResults, yValuesOfPoints) { + sumOfSquares += Double(pow(yPoly - yPoint, 2)) + } + + return sumOfSquares + } public enum LAPACKError: Swift.Error { case internalError diff --git a/Tests/PolynomialRegressionSwiftTests/PolynomialRegressionTests.swift b/Tests/PolynomialRegressionSwiftTests/PolynomialRegressionTests.swift index 4ee5311..8a380b4 100644 --- a/Tests/PolynomialRegressionSwiftTests/PolynomialRegressionTests.swift +++ b/Tests/PolynomialRegressionSwiftTests/PolynomialRegressionTests.swift @@ -97,4 +97,26 @@ 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) + } + + 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) + } }