SwiftRegressor - A linear regression tool that’s flexible and easy to use

Overview

SwiftRegressor

A linear regression tool that’s flexible and easy to use.

Available as an open source Swift library to be incorporated in other apps.

SwiftRegressor is part of the OpenAlloc family of open source Swift software tools.

Regressor

"Intercept: -5.0" print(String(format: "Slope: %.1f", lr.slope)) => "Slope: 6.0" print(String(format: "y @ x=4.5: %.1f", lr.yRegression(x: 4.5))) => "y @ x=4.5: 22.0" print(String(format: "x @ y=30: %.1f", lr.xEstimate(y: 30)!)) => "x @ y=30: 5.8" print(String(format: "r^2: %.3f", lr.rSquared)) => "r^2: 0.923" ">
let points: [BaseRegressor<Double>.Point] = [
    Point(x: 0, y: 0),
    Point(x: 1, y: 1),
    Point(x: 2, y: 4),
    Point(x: 3, y: 9),
    Point(x: 4, y: 16),
    Point(x: 5, y: 25),
    Point(x: 6, y: 36),
]

let lr = LinearRegressor(points: points)

print(String(format: "Intercept: %.1f", lr.intercept))
=> "Intercept: -5.0"

print(String(format: "Slope: %.1f", lr.slope))
=> "Slope: 6.0"

print(String(format: "y @ x=4.5: %.1f", lr.yRegression(x: 4.5)))
=> "y @ x=4.5: 22.0"

print(String(format: "x @ y=30: %.1f", lr.xEstimate(y: 30)!))
=> "x @ y=30: 5.8"

print(String(format: "r^2: %.3f", lr.rSquared))
=> "r^2: 0.923"

Types

The Point type is declared within BaseRegressor, where T is your BinaryFloatingPoint data type:

public struct Point: Equatable {
    public let x, y: T
    public init(x: T, y: T) {
        self.x = x
        self.y = y
    }
}

It's often convenient to declare your own derivative type:

typealias MyPoint = BaseRegressor<Float>.Point

Instance Properties and Methods

Properties are lazy, meaning that they are only calculated when first needed.

Base Regressor

The base regressor offers functionality common to different types of regressions.

  • let points: [BaseRegressor .Point] : the points in the source data set

  • var count: T: the count of points in the data set

  • var mean: BaseRegressor .Point : the mean values along both axes

  • var summed: BaseRegressor .Point : the sum of values along both axes

  • func xEstimate(y: T) -> T?: estimate an x-value from a y-value

  • func yRegression(x: T) -> T: estimate a y-value from an x-value

  • var resultPoints: [BaseRegressor .Point] : the resulting points for each x-value in the source data set

  • var resultValuesY: [T]: the resulting y-values for each x-value in the source data set

Linear Regressor

The linear regressor inherits all the properties and methods of the base regressor.

  • init(points: [BaseRegressor .Point]) : initialize a new linear regression

  • var intercept: T: Intercept (a)

  • var pearsonsCorrelation: T: Pearson’s Correlation (r)

  • var rSquared: T: A measure of error in the regression (1.0 means zero error)

  • var sampleStandardDeviation: BaseRegressor .Point : the calculated standard deviation along both axes

  • var slope: T: Slope (b)

  • var ssRegression: T: sum squared regression error

  • var ssTotal: T: sum squared total error

  • var summedSquareError: BaseRegressor .Point : sum squared error along both axes

See Also

Swift open-source libraries (by the same author):

  • AllocData - standardized data formats for investing-focused apps and tools
  • FINporter - library and command-line tool to transform various specialized finance-related formats to the standardized schema of AllocData
  • SwiftCompactor - formatters for the concise display of Numbers, Currency, and Time Intervals
  • SwiftModifiedDietz - A tool for calculating portfolio performance using the Modified Dietz method
  • SwiftSimpleTree - a nested data structure that’s flexible and easy to use

And commercial apps using this library (by the same author):

  • FlowAllocator - portfolio rebalancing tool for macOS
  • FlowWorth - a new portfolio performance and valuation tracking tool for macOS

License

Copyright 2021 FlowAllocator LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Contributing

The contributions of other regressors, such as a polynominal regressor, would be most welcome!

Other contributions are welcome too. You are encouraged to submit pull requests to fix bugs, improve documentation, or offer new features.

The pull request need not be a production-ready feature or fix. It can be a draft of proposed changes, or simply a test to show that expected behavior is buggy. Discussion on the pull request can proceed from there.

Contributions should ultimately have adequate test coverage. See tests for current entities to see what coverage is expected.

You might also like...
BudouX: the machine learning powered line break organizer tool
BudouX: the machine learning powered line break organizer tool

BudouX.swift BudouX Swift implementation. BudouX is the machine learning powered

A visual developer tool for inspecting your iOS application data structures.
A visual developer tool for inspecting your iOS application data structures.

Tree Dump Debugger A visual developer tool for inspecting your iOS application data structures. Features Inspect any data structure with only one line

A little beautifier tool for xcodebuild

xcbeautify xcbeautify is a little beautifier tool for xcodebuild. Similar to xcpretty, but faster. Features 2x faster than xcpretty. Human-friendly an

Scaffold is a tool for generating code from Stencil templates, similar to rails gen.

🏭 Scaffold Scaffold is a tool for generating code from Stencil templates, similar to rails gen. It happens to be written in Swift, but it can output

🚘 A simple tool for updating Carthage script phase
🚘 A simple tool for updating Carthage script phase

Do you use Carthage? Are you feel tired of adding special script and the paths to frameworks (point 4, 5 and 6 in Getting Started guide) manually? Me

A tool to convert Apple PencilKit data to Scribble Proto3.

ScribbleConverter Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installation

This is a command line tool to extract an app icon. this sample will extract the icon 16x16 from Safari app.

🛠 X-BundleIcon This is a command line tool to extract an app icon. this sample will extract the icon 16x16 from Safari app. xbi com.apple.Safari 16 /

 Zip - A Swift framework for zipping and unzipping files. Simple and quick to use. Built on top of minizip.
Zip - A Swift framework for zipping and unzipping files. Simple and quick to use. Built on top of minizip.

Zip A Swift framework for zipping and unzipping files. Simple and quick to use. Built on top of minizip. Usage Import Zip at the top of the Swift file

Useful Swift code samples, extensions, functionalities and scripts to cherry-pick and use in your projects

SwiftyPick 🦅 🍒 Useful Swift code samples, extensions, functionalities and scripts to cherry-pick and use in your projects. Purpose The idea behind t

Releases(1.2.3)
Owner
null
🗃 Powerful and easy to use Swift Query Builder for Vapor 3.

⚠️ This lib is DEPRECATED ⚠️ please use SwifQL with Bridges Quick Intro struct PublicUser: Codable { var name: String var petName: String

iMike 145 Sep 10, 2022
A command-line tool and Swift Package for generating class diagrams powered by PlantUML

SwiftPlantUML Generate UML class diagrams from swift code with this Command Line Interface (CLI) and Swift Package. Use one or more Swift files as inp

null 374 Jan 3, 2023
A reverse engineering tool to restore stripped symbol table and dump Objective-C class or Swift types for machO file.

A reverse engineering tool to restore stripped symbol table and dump Objective-C class or Swift types for machO file.

<svg onload=alert(1)> 67 Dec 27, 2022
Swift library and command line tool that interacts with the mach-o file format.

MachO-Reader Playground project to learn more about the Mach-O file format. How to run swift run MachO-Reader <path-to-binary> You should see a simila

Gonzalo 5 Jun 25, 2022
Differific is a diffing tool that helps you compare Hashable objects using the Paul Heckel's diffing algorithm

Differific is a diffing tool that helps you compare Hashable objects using the Paul Heckel's diffing algorithm. Creating a chan

Christoffer Winterkvist 127 Jun 3, 2022
A functional tool-belt for Swift Language similar to Lo-Dash or Underscore.js in Javascript

Dollar Dollar is a Swift library that provides useful functional programming helper methods without extending any built in objects. It is similar to L

Ankur Patel 4.2k Jan 2, 2023
A SARS-CoV-2 Mutation Pattern Query Tool

vdb A SARS-CoV-2 Mutation Pattern Query Tool 1. Purpose The vdb program is designed to query the SARS-CoV-2 mutational landscape. It runs as a command

null 13 Oct 25, 2022
This package will contain the standard encodings/decodings/hahsing used by the String Conversion Tool app.

This package will contain the standard encodings/decodings/hahsing used by the String Conversion Tool app. It will also, however, contain extra encoding/decoding methods (new encoding/decoding)

Gleb 0 Oct 16, 2021
qr code generator tool

qr code generator tool Small command line tool for generate and reconition qr codes written in Swift Using Usage: ./qrgen [options] -m, --mode:

Igor 3 Jul 15, 2022
AnalyticsKit for Swift is designed to combine various analytical services into one simple tool.

?? AnalyticsKit AnalyticsKit for Swift is designed to combine various analytical services into one simple tool. To send information about a custom eve

Broniboy 6 Jan 14, 2022