Giter VIP home page Giter VIP logo

wrapodr's Introduction

wrapODR

This is a simple wrapper around the scipy.odr library (orthogonal distance regression).

Installation

This package is pip installable using: pip install -i https://test.pypi.org/simple/ wrapODR-pjh6654==0.0.5

Usage

Below is test code using wrapODR to fit a sigmoid with uncertainies in x and y.

import numpy as np
import matplotlib.pyplot as plt
from wrapODR import labfit as lf

# Random seed (for consistency between tests)
np.random.seed(1)

# Define the function you want to fit
def g(B, x):
    return B[0] / (1 + np.exp(-(x - B[1])))

#
# Test Parameters
#
n = 20  # number of data points
a = 5.0
b = 0.0
sigma_x = 0.2  # uncertainty in x
sigma_y = 0.2  # uncertainty in y
xmin = -10.0
xmax = 10.0
xarray = np.linspace(xmin, xmax, n)

# generate data with x and y uncertainties
x = np.copy(xarray) + np.random.normal(0.0, sigma_x, n)
y = g([a, b], xarray) + np.random.normal(0.0, sigma_y, n)

# initial guess for fitting
beta0 = [6, 1]

# fit using both x and y uncertainty
params, error, bestfit, chi_vals = lf.fit(g, x, y, xerr=sigma_x, yerr=sigma_y, beta0=beta0)

# Print results of the fit
print("B[0] = {:.3f} +/- {:.3f}".format(params[0], error[0]))
print("B[1] = {:.3f} +/- {:.3f}".format(params[1], error[1]))
print("Chi-Squared: {}".format(chi_vals[0]))
print("Reduced Chi-Squared: {}".format(chi_vals[1]))

# Plot the original data and the best fit curve
plt.errorbar(x, y, xerr=sigma_x, yerr=sigma_y, fmt='.', capsize=3)
plt.plot(*bestfit)
plt.show()

wrapodr's People

Contributors

vi-hart 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.