Revert "[May be undone] Trying to fix the regression problem"
This reverts commit 3cf7533bae.
This commit is contained in:
1 parent
3639ccfc92
commit
fcdbde6792
4 files changed
-93
No files matched your search
@@ -1077,41 +1077,4 @@ extension Matrix {
|
||||
throw MatrixSystemError.SolveError
|
||||
}
|
||||
}
|
||||
|
||||
/// Concatenate two Double arrays
|
||||
private func concat(_ v1: [Double], _ v2: [Double]) -> [Double] {
|
||||
var values = Array<Double>(repeating: 0, count: v1.count + v2.count)
|
||||
|
||||
values[0..<v1.count] = ArraySlice(v1)
|
||||
values[v1.count..<v1.count + v2.count] = ArraySlice(v2)
|
||||
|
||||
return values
|
||||
}
|
||||
|
||||
/// Appends a Matrix horizontally to the current Matrix.
|
||||
///
|
||||
/// - parameter m: The Matrix that should be appended.
|
||||
/// - returns: A new Matrix based on the current Matrix and `m`.
|
||||
func appendHorizontal(m: Matrix) -> 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
|
||||
}
|
||||
}
|
||||
@@ -160,11 +160,6 @@ extension Matrix {
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
/// Sets all values to `value`.
|
||||
mutating func setValues(value: Double) {
|
||||
self.grid = Array<Double>(repeating: value, count: grid.count)
|
||||
}
|
||||
}
|
||||
|
||||
extension Matrix {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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 = [
|
||||
|
||||
Reference in new issue
Block a user