Skip to main content

This library contains the code usefull in physics lab

Project description

Physicslab-coda

A Python library for data analysis in physics laboratories, including:

  • linear interpolation with uncertainties
  • statistical variables
  • common statistical tests (χ², t-Student, F-test, etc.)

The goal is to provide ready-to-use tools for students and researchers working with experimental data.


Statistics

Interpolation

Class for linear interpolation with experimental uncertainties.

  • linear_function(x, a, b)
    Simple linear function.

  • erry_var_errx_null(x, y, erry, verbose=False)
    Linear interpolation with variable error on Y and negligible error on X.

  • linear_interpolation(x, y, errx, erry, threshold=30, tries=10, verbose=False)
    Linear interpolation with variable errors on both X and Y.
    Automatically chooses the best approximation depending on error magnitudes.


StatisticalVariables

Class for calculating basic statistical variables.

  • r(x, y)
    Computes the sample correlation coefficient.
    Output: r (sample correlation coefficient)

  • mean(x)
    Computes the mean of a data set.
    Output: mean (data set mean)

  • var(x, sample=True)
    Computes the sample variance of a data set.
    Output: var (data set variance)

  • sdev(x, sample=True)
    Computes the sample standard deviation of a data set.
    Output: sdev (data set standard deviation)

  • post_var(f, parameters, x, y)
    Computes the posterior variance of a data set.
    Output: pv (data set posterior variance)

  • cov(x, y, sample=True)
    Computes the sample covariance of a data set.
    Output: cov (data set covariance)


StatisticalTests

Class for applying common statistical tests.

  • chi_sqrd(f, parameters, x, y, erry)
    Compute chi-squared.
    Output: chi2 (chi-squared value), df (degrees of freedom)

  • reduced_chi_sqrd(f, parameters, x, y, erry)
    Compute reduced chi-squared.
    Output: reduced_chi2 (reduced chi-squared value), df (degrees of freedom)

  • post_error(f, parameters, x, y)
    Compute posterior error.
    Output: pe (posterior error), df (degrees of freedom)

  • r_t_student(x, y)
    Compute Student’s t-value for correlation.
    Output: t (t-value), df (degrees of freedom)

  • t_student(x, mu)
    Compute Student’s t-value for a sample mean.
    Output: t (t-value), df (degrees of freedom)

  • alternative_f_test(f_0, f_a, v_0, v_a, x, y)
    Perform F-test to compare two models.
    Output: f_test (F-test value), df1, df2 (degrees of freedom)

  • t_student_comp(x, y)
    Student’s t-test for two samples with different variances.
    Output: t (t-value), df (degrees of freedom)


Installation

git clone https://github.com/yourusername/physics-lab-data-analysis.git
cd physics-lab-data-analysis
pip install -r requirements.txt

Usage Example

import numpy as np
from physicslab-coda import Interpolation, StatisticalVariables, StatisticalTests

# Example data
x = np.array([1, 2, 3, 4])
y = np.array([2.1, 4.2, 5.9, 8.2])
erry = np.array([0.1, 0.2, 0.1, 0.3])
errx = np.array([0.05, 0.05, 0.05, 0.05])

# Linear interpolation
interp = Interpolation()
a, b, s_a, s_b = interp.linear_interpolation(x, y, errx, erry, verbose=True)
print("a =", a, " b =", b, " σa =", s_a, " σb =", s_b)

# Correlation coefficient
stat_vars = StatisticalVariables()
r = stat_vars.r(x, y)
print("r =", r)

# Reduced chi-squared
tests = StatisticalTests()
y_fit = a + b*x
rchi2, df = tests.reduced_chi_sqrd(lambda x: a + b*x, parameters=2, x=x, y=y, erry=erry)
print("Reduced χ² =", rchi2, " with degrees of freedom =", df)

Author

Andrea Codarin

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

physicslab_coda-0.2.1.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

physicslab_coda-0.2.1-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file physicslab_coda-0.2.1.tar.gz.

File metadata

  • Download URL: physicslab_coda-0.2.1.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for physicslab_coda-0.2.1.tar.gz
Algorithm Hash digest
SHA256 443f8cc05d075fbde0fe10aafd46c8a2ac527da83d41daf133c25141641d7af4
MD5 cae2ecc615c6bc687bce076e396d3373
BLAKE2b-256 6a3279b9644a54f54832480870bb6f23bd3642a17ec15988585241b426eda3dc

See more details on using hashes here.

File details

Details for the file physicslab_coda-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for physicslab_coda-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 660d4164c8636f0d3356f9b420b585192413bc0e0d7e3dc613805ce2eb60eb15
MD5 44521a6d8e0c11a22759ae6ddd556d16
BLAKE2b-256 08f0d73130c7fda445dbd2fc1af9147079db773e9319eea162e099b92309820d

See more details on using hashes here.

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