Git Product home page Git Product logo

ios-polynomial-regression's Introduction

iOS-Polynomial-Regression

Objective-C function for calculation the polynomial regression of a given dataset.

There are no mathematical data analysis functions in Objective-C and I couldn't find a suitable math framework for this task. For my thesis I require a function to calculate the polynomial regression of a give data set. Since this function didn't exist I had to write it myself. Problem was that other programming languages usually have this implemented by default as some kind of "polyfit" function so I dind't really had an example to base myself on.

So I had to go into the math and figure out how to turn it into an algorithm. A hellish job I wouldn't recommend to anyone. I programmed an Objective-C function for the calculation the polynomial regression of a given dataset which is extremely easy to use.

You have to give an array of NSNumber for the x values and y values and the desired degree of polynomial you would like to aquire. The function will return an NSMutableArray containing the polynomial constants.

Overview

Features

  • Can calculate any degree of polynomial
  • Easy to implement
  • Very compact, one line usage
  • Also contains a custom matrix class for this project which handles doubles

Example

NSMutableArray *x = [[NSMutableArray alloc] init];
[x addObject: [NSNumber numberWithDouble: 0]];
[x addObject: [NSNumber numberWithDouble: 9]];
[x addObject: [NSNumber numberWithDouble: 13]];
[x addObject: [NSNumber numberWithDouble: 15]];
[x addObject: [NSNumber numberWithDouble: 19]];
[x addObject: [NSNumber numberWithDouble: 20]];
[x addObject: [NSNumber numberWithDouble: 26]];
[x addObject: [NSNumber numberWithDouble: 26]];
[x addObject: [NSNumber numberWithDouble: 29]];
[x addObject: [NSNumber numberWithDouble: 30]];

NSMutableArray *y = [[NSMutableArray alloc] init];

[y addObject: [NSNumber numberWithDouble: 1]];
[y addObject: [NSNumber numberWithDouble: -7]];
[y addObject: [NSNumber numberWithDouble: 6]];
[y addObject: [NSNumber numberWithDouble: 12]];
[y addObject: [NSNumber numberWithDouble: -4]];
[y addObject: [NSNumber numberWithDouble: -12]];
[y addObject: [NSNumber numberWithDouble: -2]];
[y addObject: [NSNumber numberWithDouble: 13]];
[y addObject: [NSNumber numberWithDouble: 23]];
[y addObject: [NSNumber numberWithDouble: 30]];

int degree = 6;
NSMutableArray *regression = [PolynomialRegression regressionWithXValues: x AndYValues: y PolynomialDegree: degree];

NSLog(@"The result is the sum of");

for(int i = 0; i < [regression count]; i++) {
    double value = [[regression objectAtIndex: i] doubleValue];
    NSLog(@"%f * x^%d", value, i);
}

Generates the following output in console

2015-03-22 19:52:48.751 PolynomialRegression[2701:111554] The result is the sum of
2015-03-22 19:52:48.752 PolynomialRegression[2701:111554] 1.011300 * x^0
2015-03-22 19:52:48.752 PolynomialRegression[2701:111554] -23.964676 * x^1
2015-03-22 19:52:48.752 PolynomialRegression[2701:111554] 4.546635 * x^2
2015-03-22 19:52:48.752 PolynomialRegression[2701:111554] -0.236831 * x^3
2015-03-22 19:52:48.752 PolynomialRegression[2701:111554] -0.000581 * x^4
2015-03-22 19:52:48.753 PolynomialRegression[2701:111554] 0.000309 * x^5
2015-03-22 19:52:48.753 PolynomialRegression[2701:111554] -0.000005 * x^6

Implementation

Implementation is easy. Just add the classes to your project and import the PolynomialRegression.h file.

Donate

You can support contributors of this project individually. Every contributor is welcomed to add his/her line below with any content. Ordering shall be alphabetically by GitHub username.

Please consider a small donation if you use iOS Polynomial Regression in your projects. It would make me really happy.

License

The license for the code is MIT.

You're welcome to use it in commercial, closed-source, open source, free or any other kind of software, as long as you credit me appropriately and share any improvements to the code.

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.

ios-polynomial-regression's People

Contributors

kingisulgard avatar samkrishna avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ios-polynomial-regression's Issues

Index out of bounds error

Hi, I ran into this error when I used a degree of 50 on a fairly large dataset (a couple hundred x,y pairs).

It happened because in the function "decompose", the int maxPosition never got set after initialized to -1, which was then used as an array index in line 139.
Perhaps this is some overflow error?
Now, I'm not mathematically inclined, so that's all I can say for now. I'll mess around with it a bit more in the meantime.
Thanks so much for publishing this work!

Use in non-GPL project

Hi there Sam!

I'm making an app, and I've played around with some features using your library. Those features won't be published in a while but your library is already in the public source code of the "version-3" branch.

Now the problem is that my project is currently licensed under MIT, and I'm thinking about moving it to a license that I made up called the "MMF License" as I plan to monetize the app soon while keeping it open source, and I couldn't find any existing licenses for that purpose.

As far as I understand, this is not allowed under GPL. According to GPL, my whole project needs to be under GPL since your code is included and licensed under GPL.

So I'm asking you as the copyright holder for express permission to use your code in my project.
I'd really appreciated it, but of course I'll understand your decision whatever it may be.

Thanks!


Here is the "MMF License" I came up with:

https://github.com/noah-nuebling/mac-mouse-fix/blob/version-3/LICENSE

Clarify Licence?

I'm wondering if you could clear up which licence this code is under.

The README says 'basically BSD; with attribution'
The Code says GPL
Github says GPL2.0

Thank you!

High macOS version requirements

Hi! This library seems very simple to use and exactly what I need but the high macOS requirements (macOS 12 or higher IIRC) prevented me from using it for my app.

If it’s possible it would be awesome to lower the requirements.

Thanks for considering.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.