GithubHelp home page GithubHelp logo

johannes-sahlmann / linearfit Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 30 KB

General least-squares fit of a linear model using numpy matrix inversion.

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

linearfit's Introduction

linearfit

python class that implements a general least-squares fit of a linear model using numpy matrix inversion.

Uncertainties in the dependent variables (but not in the independent variables) can be taken into account. All inputs have to be numpy matrices.

Math is based on Press'
Numerical Receipes p661 : Section 15.2 Fitting Data to a Straight Line
Numerical Receipes p671 : Section 15.4 General Linear Least Squares

Code is based on an early yorick implementation by Damien Segransan (University of Geneva)

Python implementation and tools by Johannes Sahlmann 2009-2017 (University of Geneva, European Space Agency, STScI/AURA)

Please see the test in test_linearfit.py for an example usage.

Basic example usage (Fitting a straight line to data with uncertainties in y)

# independent variable
x = np.array([0.0, 0.9, 1.8, 2.6, 3.3, 4.4, 5.2, 6.1, 6.5,7.4])

# dependent variable	
y = np.array([5.9, 5.4, 4.4, 4.6, 3.5, 3.7, 2.8, 2.8, 2.4, 1.5])

# weights of dependent variable	
wy = np.array([1,1.8,4,8,20,20,70,70,100,500])

# prepare matrices
M = np.mat(y);
#       diagonal covariance matrix of dependent variables
S = np.mat(np.diag(wy));
# matrix of independent variables, here only ones
C = np.mat(np.vstack( (np.ones(len(x)) , x)))

# initialise object
res = linearfit.LinearFit(M,S,C);

# do the fit
res.fit()
res.display_results()

Documentation

All classes and methods/functions include basic documentation.

Installation notes

This package was developed in a python 2.7 environment, but was also successfully tested using python 3.5.

The following python packages are required:

  • numpy

How to run the example script

You may use pip for installation:
pip install linearfit

Or get the source files, e.g.:
git clone https://github.com/johannes-sahlmann/linearfit

Install pygacs:
cd linearfit
python setup.py install --user

To run the test, do:
python test_linearfit.py

License

Copyright (c) 2017 Johannes Sahlmann, STScI/AURA

linearfit is open source and free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

linearfit's People

Contributors

johannes-sahlmann avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.