From fcdbde679285d0e505c49ff349e7f8debaff1fdb Mon Sep 17 00:00:00 2001 From: Arthur Guiot Date: Wed, 19 Feb 2020 19:51:39 -0500 Subject: [PATCH] Revert "[May be undone] Trying to fix the regression problem" This reverts commit 3cf7533bae40597c50a05230541bcff8b6e02f27. --- Sources/Euler/Matrices/MOperations.swift | 37 ----------------- Sources/Euler/Matrices/Matrix.swift | 5 --- Sources/Euler/Statistics/Regression.swift | 50 ----------------------- Tests/EulerTests/BigIntTests.swift | 1 - 4 files changed, 93 deletions(-) diff --git a/Sources/Euler/Matrices/MOperations.swift b/Sources/Euler/Matrices/MOperations.swift index 2e31b7e..60668f9 100644 --- a/Sources/Euler/Matrices/MOperations.swift +++ b/Sources/Euler/Matrices/MOperations.swift @@ -1077,41 +1077,4 @@ extension Matrix { throw MatrixSystemError.SolveError } } - - /// Concatenate two Double arrays - private func concat(_ v1: [Double], _ v2: [Double]) -> [Double] { - var values = Array(repeating: 0, count: v1.count + v2.count) - - values[0.. Matrix { - - let a = self.transpose().grid - let b = m.transpose().grid - let values = concat(a, b) - var result = Matrix(rows: self.columns + m.columns, columns: m.rows, grid: values) - - return result.transpose() - } - - - /// Appends a Matrix vertically to the current Matrix. - /// - /// - parameter m: The Matrix that should be appended. - /// - returns: A new Matrix based on the current Matrix and `m`. - func appendVertical(m: Matrix) -> Matrix { - let values = concat(self.grid, m.grid) - - let result = Matrix(rows: self.columns + m.columns, columns: m.rows, grid: values) - - return result - } } diff --git a/Sources/Euler/Matrices/Matrix.swift b/Sources/Euler/Matrices/Matrix.swift index ce39a91..436aad3 100644 --- a/Sources/Euler/Matrices/Matrix.swift +++ b/Sources/Euler/Matrices/Matrix.swift @@ -160,11 +160,6 @@ extension Matrix { } return m } - - /// Sets all values to `value`. - mutating func setValues(value: Double) { - self.grid = Array(repeating: value, count: grid.count) - } } extension Matrix { diff --git a/Sources/Euler/Statistics/Regression.swift b/Sources/Euler/Statistics/Regression.swift index 28bb59b..238353e 100644 --- a/Sources/Euler/Statistics/Regression.swift +++ b/Sources/Euler/Statistics/Regression.swift @@ -6,7 +6,6 @@ // import Foundation -import Accelerate public extension Statistics { static func linearRegression(points: [Point]) throws -> Polynomial { @@ -66,52 +65,3 @@ public extension Statistics { // return try Polynomial(grid) // } } - -class Regression { - let degree: Int - - private (set) var betas: [Double] = [] - - - init(x: Matrix, y: Matrix, degree: Int) throws { - self.degree = degree - self.betas = try self.findBetas(x: x, y: y, degree: degree) - } - - - private func calculatePowersOfX(x: Matrix) -> Matrix { - var newX = x - - if degree > 1 { - for i in 2...degree { - let degreeOfX = Matrix(rows: x.rows, columns: 1, grid: x.grid.map({ pow($0, Double(i)) })) - - newX = newX.appendHorizontal(m: degreeOfX) - } - } - - return newX - } - - - private func findBetas(x: Matrix, y: Matrix, degree: Int) throws -> [Double] { - let newX = calculatePowersOfX(x: x) - let ones = Matrix(rows: y.rows, columns: 1, repeatedValue: 1) - let onedNewX = ones.appendHorizontal(m: newX) - - let a = (onedNewX.transpose() <*> onedNewX) - let b = (onedNewX.transpose() <*> y) - - return try a.solveEquationsSystem(vector: b.grid).grid - } - - - func predict(x: Matrix) -> Matrix { - let beta = Matrix(rows: betas.count, columns: 1, grid: betas) - let newX = calculatePowersOfX(x: x) - let ones = Matrix(rows: newX.rows, columns: 1, repeatedValue: 1) - let onedNewX = ones.appendHorizontal(m: newX) - - return onedNewX * beta - } -} diff --git a/Tests/EulerTests/BigIntTests.swift b/Tests/EulerTests/BigIntTests.swift index 74ce984..3f033df 100644 --- a/Tests/EulerTests/BigIntTests.swift +++ b/Tests/EulerTests/BigIntTests.swift @@ -127,7 +127,6 @@ class BigIntTests: XCTestCase { // let r = try? Statistics.polynomialRegression(points: [Point(x: 0, y: 2),Point(x: 2, y: 4),Point(x: 3, y: 5)]) - try? Regression(x: Matrix(arrayLiteral: 0, 2, 3), y: Matrix(arrayLiteral: 2, 4, 5), degree: 2).betas } static var allTests = [