# Path: pymicro/xray/fitting.py
# def lin_reg(xi, yi):
#     """Apply linear regression to a series of points.
#     
#     This function return the best linear fit in the least square sense.
#     :param ndarray xi: a 1D array of the x coordinate.
#     :param ndarray yi: a 1D array of the y coordinate.
#     :return tuple: the linear intercept, slope and correlation coefficient.
#     """
#     n = len(xi)
#     assert (n == len(yi))
#     sx = np.sum(xi)
#     sy = np.sum(yi)
#     sxx = np.sum(xi ** 2)
#     sxy = np.sum(xi * yi)
#     syy = np.sum(yi ** 2)
#     beta = (n * sxy - sx * sy) / (n * sxx - sx ** 2)
#     alpha = 1. / n * sy - 1. / n * sx * beta
#     r = (n * sxy - sx * sy) / np.sqrt((n * sxx - sx ** 2) * (n * syy - sy ** 2))
#     return alpha, beta, r


import unittest
import numpy as np
from pymicro.xray.fitting import lin_reg

class FittingTests(unittest.TestCase):

    def setUp(self):
        """testing the fitting module:"""

    def test_lin_reg(self):
        """Verify the linear regression, example from wikipedia."""
        xi = np.array([1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65, 1.68, 1.70, 1.73, 1.75, 1.78, 1.80, 1.83])
        yi = np.array([52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29, 63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46])

# Path: pymicro/xray/fitting.py
# def lin_reg(xi, yi):
#     """Apply linear regression to a series of points.
#     
#     This function return the best linear fit in the least square sense.
#     :param ndarray xi: a 1D array of the x coordinate.
#     :param ndarray yi: a 1D array of the y coordinate.
#     :return tuple: the linear intercept, slope and correlation coefficient.
#     """
#     n = len(xi)
#     assert (n == len(yi))
#     sx = np.sum(xi)
#     sy = np.sum(yi)
#     sxx = np.sum(xi ** 2)
#     sxy = np.sum(xi * yi)
#     syy = np.sum(yi ** 2)
#     beta = (n * sxy - sx * sy) / (n * sxx - sx ** 2)
#     alpha = 1. / n * sy - 1. / n * sx * beta
#     r = (n * sxy - sx * sy) / np.sqrt((n * sxx - sx ** 2) * (n * syy - sy ** 2))
#     return alpha, beta, r


# Path: pymicro/xray/tests/test_fitting.py
import unittest
import numpy as np
from pymicro.xray.fitting import lin_reg


class FittingTests(unittest.TestCase):

    def setUp(self):
        """testing the fitting module:"""

    def test_lin_reg(self):
        """Verify the linear regression, example from wikipedia."""
        xi = np.array([1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65, 1.68, 1.70, 1.73, 1.75, 1.78, 1.80, 1.83])
        yi = np.array([52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29, 63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46])

        alpha, beta, r = lin_reg(xi, yi)