3
��Y � @ sP d dl mZ d dlmZ d dlmZ ddd�Zd dlmZ d dl T dd d
�Z
dS )
� )�print_function)�sqrt)�odrNc C s dd� }t |�d }|dk�r6|dkr4tt |��}nd|d }t|�}|| }t|�} t|| �}
t|| �}t|| �}|| | |
|| | | }
|
| | | || | | }t||| | | �}t||| | | �}|dk�rt|
| | | d �}nt|
| | | | d �}|| }n�|d
|d | d | d }|d | d | }tj|�}tj| |||d�}tj||||g�}|j� }|j \}
}|j
\}}|j}|dk �r�|t|� }|t|� }|dk�r�td � ntd
� td|� td|� |
|||||fS )a8
Performs a linear fit to data.
Parameters
----------
xdata : An array of length N.
ydata : An array of length N.
sigmay : None or an array of length N.
sigmax : None or an array of length N.
If one is provided, it is the standard deviation of ydata. Analytical linear regression used.
If both are provided, they are the standard deviations of ydata and xdata, respectively. ODR is used.
Returns
-------
a, b : Optimal parameter of linear fit (y = a*x + b)
sa, sb : Uncertainties of the parameters
c S s | \}}|| | S )N� )�p�x�a�br r �&/home/user/LinearRegression/fitting.py�lin_func# s zlinear_fit.<locals>.lin_func� Ng �?� r )r �y�sx�syzPno uncertainties provided, use uncertainties of slope and intercept with cautionzresults of linear_fit:z reduced chi squared = z degrees of freedom = �����r )
�len�ones�sumr r �Model�RealData�ODR�run�beta�sd_beta�res_var�print)�xdata�ydata�sigmay�sigmaxr �dof�w�sw�wxZswxZswyZswxyZswx2r r �sa�sb�chi2�rchi2�a0�b0�model�data�od�outr r r
�
linear_fit sL
r/ )� curve_fit)�*c s^ t |�t |� }|dkr�t� ||||�\}}|dkrPt� |f|�� | d �} nt� |f|�� | | d �} | | }
tt |��}x�tdt |��D ]}t|||f �||<