ODR - Orthogonal Distance Regression in SciPy
In science and engineering, it is essential to fit a model to experimental data. Ordinary Least Squares (OLS) Regression is a popular method to fit models, but it does not cater to data with uncertainties. Orthogonal Distance Regression (ODR) is a regression analysis technique that can handle uncertainties (heteroscedasticity) in both dependent and independent variables. The scipy.odr module in Python's SciPy library provides an implementation of ODR.
Syntax
The basic syntax to perform ODR analysis using the scipy.odr module in Python is as follows:
from scipy.odr import ODR, Model, Data
# Define the model function
def model(parameters, x):
# Define the model
a, b, c = parameters
return a + b*x + c*x**2
# Create a model object
model = Model(model)
# Create a Data object
data = Data(x, y, wd=1/x_err, we=1/y_err)
# Create an ODR object
odr = ODR(data, model, beta0=[1,1,1])
# Run the regression
output = odr.run()
Here, x
, y
, x_err
, and y_err
are the arrays of independent variable values, corresponding dependent variable values, and uncertainties in x and y respectively. The model
function defines the model equation, and Parameters
contain the initial estimates of the model parameters.
Example
Consider a simple example where we have data for the length of the pendulum and the corresponding time period. We can perform ODR analysis on the data to fit a model equation that can predict the time period for any given length of the pendulum.
import numpy as np
from scipy.odr import ODR, Model, Data
# Define the dataset
x = np.array([0.69, 0.86, 1.02, 1.52, 1.87, 2.13, 2.44])
y = np.array([1.25, 1.37, 1.44, 1.82, 2.03, 2.18, 2.46])
x_err = np.array([0.005]*len(x))
y_err = np.array([0.01, 0.01, 0.01, 0.02, 0.02, 0.02, 0.02])
# Define the model function
def model(parameters, x):
# Define the model
g, L = parameters
return 2*np.pi*np.sqrt(L/g)
# Create a model object
model = Model(model)
# Create a Data object
data = Data(x, y, wd=1/x_err, we=1/y_err)
# Create an ODR object
odr = ODR(data, model, beta0=[9.8, 0.5])
# Run the regression
output = odr.run()
# Print the results
print("Estimated parameters: ", output.beta)
In this example, we define a model
function that returns the time period for a given length of the pendulum, which is given by the equation T = 2*pi*sqrt(L/g)
. We then create a model object, a data object, and an ODR object, and finally run the regression using the run
method.
Output
When the above program is executed, it displays the estimated parameters for the regression model as follows:
Estimated parameters: [9.3759889 0.42458121]
Explanation
In the above example, we use the scipy.odr module to perform ODR on the given dataset of pendulum lengths and time periods. The Model
class is used to define the model
function that represents the model equation. The Data
class is used to define the data points along with the uncertainties in the dependent and independent variables. Finally, the ODR
class is used to define an ODR object and run the regression using the run
method.
Use
ODR is useful when working with datasets with uncertainties in both dependent and independent variables. It is widely used in science and engineering to fit models to experimental data. The scipy.odr module provides a simple and efficient implementation of the ODR method that can be used in Python applications.
Important Points
- ODR is a regression analysis technique that can handle uncertainties in both dependent and independent variables.
- The scipy.odr module provides an implementation of the ODR method in Python.
- The
Model
,Data
, andODR
classes are used to define the model equation, data points, uncertainties, and perform the ODR regression.
Summary
In this tutorial, we learned about ODR and its implementation in the scipy.odr module in Python's SciPy library. We also saw an example of how to perform ODR regression using the scipy.odr module on a simple dataset of pendulum lengths and time periods.