Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
910 views


��Y�@suddlmZddlmZddlmZdddd�ZddlmZddl	Tdddd	d
�Z
dS)�)�print_function)�sqrt)�odrNc	Cs�dd�}t|�d}|dkr}|dkrItt|��}nd|d}t|�}||}t|�}	t||�}
t||�}t||�}|||	|
|||	|	}
|
||	||||	|	}t||||	|	�}t||||	|	�}|dkrPt|
|||d�}n t|
||||d�}||}n�|d|d|d|d}|d|d|}tj|�}tjd|d	|d
|d|�}tj||||g�}|j�}|j	\}
}|j
\}}|j}|dkrb|t|�}|t|�}|dkr{td�n
td
�td|�td|�|
|||||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
    cSs|\}}|||S)N�)�p�x�a�brr�&/home/user/LinearRegression/fitting.py�lin_func#szlinear_fit.<locals>.lin_func�Ng�?�rr�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�sumrr�Model�RealData�ODR�run�beta�sd_beta�res_var�print)�xdata�ydata�sigmay�sigmaxr�dof�w�sw�wxZswxZswyZswxyZswx2rr	�sa�sb�chi2�rchi2�a0�b0�model�data�od�outrrr
�
linear_fitsL
"" 
"$		



r/)�	curve_fit)�*c	s�t|�t|�}|dkr�t�||||�\}}|dkrlt�||�|d�}	n!t�||�||d�}	|	|}
tt|��}x�tdt|��D] }t|||f�||<q�Wn��fdd�}
tj|
�}tjd|d|d|d	|�}tj	|||�}|j
�}|j}|j}|j
}
|
d
kr�|t|
�}|t|
�}|dkr�td�n
td�td
|�td|
�|||
|fS)z�
    Pass all arguments to curve_fit, which uses non-linear least squares
    to fit a function, f, to data.  Calculate the uncertaities in the
    fit parameters from the covariance matrix.
    Nrrcs
�||�S)Nr)rr)�frr
�f_fixed�szgeneral_fit.<locals>.f_fixedrrrrg�?zFresults of general_fit: no uncertainties provided, so use with cautionzresults of general_fit:z   degrees of freedom = z   reduced chi squared = )rr0r�zeros�arangerrrrrrrrrr)r2rr�p0rr r!ZpoptZpcovr'r(Zpunc�ir3r+r,r-r.r%r&r)r2r
�general_fitjs6 !
!$				



r8)�
__future__r�mathr�scipyrr/Zscipy.optimizer0�pylabr8rrrr
�<module>sV