Skip to main content

Regression of data with error propagation

Project description

Betterfit

Installation

pip install betterfit

Getting Started

Refer to getting_started.py:

# getting_started.py
import matplotlib.pyplot as plt
import sympy as smp
from uncertainties import ufloat

from betterfit import Dataset, LinearFit

# create immutable datasets
resistance_data = Dataset.fromiter(symbol='R',
                                   values=[2000, 1800, 1600, 1400, 1200, 1000, 900],
                                   errors=10)
length_data = Dataset.fromiter(symbol=smp.Symbol('l'),
                               values=[160, 172, 192, 223, 254, 298, 327],
                               errors=[1.5, 2.4, 2, 2.3, 2.1, 3, 5])
length_data = length_data.multiply(1e-3)

# define quantities as sympy symbols
R, l, d = smp.symbols('R l d')

# define an expression depending on these symbols
phi = smp.atan(l / d)

# perform linear fit
linearfit = LinearFit()
linearfit.add_constant(symbol=d,
                       value=ufloat(0.535, 0.01))
linearfit.add_datasets(resistance_data, length_data)
slope, yintercept = linearfit.fit(x_expr=1 / phi,
                                  y_expr=R)

# linearfit.fit return sympy symbols representing the slope and yintercept
# the values can be accessed through linearfit[slope] and linearfit[yintercept]
print(f'slope: {linearfit[slope]:.3g}')
print(f'yintercept: {linearfit[yintercept]:.3g}')

# similarily, values of expressions can be accessed through linearfit[expr]
print(f'phi: {linearfit[phi]}')

# plot setup
fig, ax = plt.subplots()
ax.set_title('Example Linear Fit with Weighted Least Squares')
ax.set_xlabel(r'$\frac{1}{\phi}$ [$s$]')
ax.set_ylabel(r'$R$ [$\Omega$]')

# plot fit
linearfit.plot_on(ax,
                  x=1 / phi,
                  y=R,
                  fmt='o',
                  label='data')
linearfit.plot_fit_on(ax)

ax.legend()

plt.show()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

betterfit-0.0.2a3.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

betterfit-0.0.2a3-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file betterfit-0.0.2a3.tar.gz.

File metadata

  • Download URL: betterfit-0.0.2a3.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for betterfit-0.0.2a3.tar.gz
Algorithm Hash digest
SHA256 46adc75ba94aeac0fe5dd34ec9b1d713e81dd49e1bffaee93efbddd62a99c888
MD5 a90a613fc98d5a9043b057d15debdc2c
BLAKE2b-256 a82ff382daca2d37e86c78672cc66310c1dfaa29752a9dd36a3914d461ac0b71

See more details on using hashes here.

Provenance

The following attestation bundles were made for betterfit-0.0.2a3.tar.gz:

Publisher: python-publish.yaml on leftgoes/betterfit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file betterfit-0.0.2a3-py3-none-any.whl.

File metadata

  • Download URL: betterfit-0.0.2a3-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for betterfit-0.0.2a3-py3-none-any.whl
Algorithm Hash digest
SHA256 bd42d7ec10f039811f8c0552004097836917b516af4e0087b2571a89c3bfd6ae
MD5 5ee48a2f36da5b0a6d5cc7a5e20d1d4c
BLAKE2b-256 8b5e7eba40013ae729f464efc3fdc7096a7b8c8d400bffefe7e772a415b23366

See more details on using hashes here.

Provenance

The following attestation bundles were made for betterfit-0.0.2a3-py3-none-any.whl:

Publisher: python-publish.yaml on leftgoes/betterfit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page