Exemple #1
0
    def _fit_odr(self):
        #
        # Fitting function
        #
        def fcn(params, x):
            for i in xrange(len(params)):
                if abs(params[i]) == nan or abs(params[i]) == inf:
                    params[i] = self.model.x0[i]()['value']
                    return [1e100]*len(x)
                if params[i] < self.model.x0[i]()['min']:
                    params[i] = self.model.x0[i]()['min']
                    return [1e100]*len(x)
                if params[i] > self.model.x0[i]()['max']:
                    params[i] = self.model.x0[i]()['max']
                    return [1e100]*len(x)
            return self.model.fcn(params, x, self.model.const)
        #
        # create Model
        #
        model = odr.Model(fcn)
        #
        # Get a list with the fitting start parameters and create the
        # ODR fitting object

        self.solver = odr.ODR(self.data, model, beta0=self.model.params,
                              ifixb=self.model.froozen, maxit=self.maxfev)
        self.solver.set_job(fit_type=self.fitType-1)

        #
        # Run the fitting ...
        #
        output = self.solver.run()
        #
        # If result is not satisfying (output.info > 4) run fitting again
        #
        if(output.info >= 4):
            self.solver = odr.ODR(self.data, model, beta0=output.beta,
                                  ifixb=self.model.froozen, maxit=self.maxfev)
            self.solver.set_job(fit_type=self.fitType-1, restart=0)
            output = self.solver.run()
            if(output.info >= 4):
                if self.verbose:
                    ('ODRPACK still unable to find solution: \n%s\n\t\t'
                     %(output.stopreason) )


        for i in xrange(self.model.get_paramCount()):
            if not self.model.froozen[i]:
                self.model.params[i] = output.beta[i]

        self.storeResult(output)

        return self.result
Exemple #2
0
    def fit(self):
        '''
        Description
        -----------
        Standard fit with or without errors 
        
        Parameters
        -----------
        
        Returns
        -----------
        θ:      array
                parameters
        σ:      array
                standard deviations
        Σ:      array
                covariance matrix
        '''

        # Fitting
        usemod = odr.Model(
            lambda θ, x: modeling.__dict__[self.model](θ, x, *self.add).fit(),
            lambda θ, x: modeling.__dict__[self.model]
            (θ, x, *self.add).jacobian())
        #usemod = eval('odr.Model(self.'+self.model+'_fit, extra_args=self.add)')
        dats = odr.RealData(self.x, self.y, sx=self.xerr, sy=self.yerr)
        r = odr.ODR(dats, usemod, self.init).run()

        # Outputting
        self.θ = r.beta
        self.σ = r.sd_beta
        self.Σ = r.cov_beta
        self.r = r
        return self.θ, self.σ, self.Σ
Exemple #3
0
def gauss_fit_ba(datax, datay, errory, beta0=[0, 1, 1, 0, 0]):
    mod = sdr.Model(gaussian_ba)
    dat = sdr.RealData(datax, datay, sy=errory)
    odr = sdr.ODR(dat, mod, beta0=beta0)
    odr.set_job(fit_type=2)
    res = odr.run()
    return [res.beta, res.sd_beta, res.res_var]
Exemple #4
0
def autoCorrelation(xData):
    maxTp = 400

    xMean = np.average(xData)
    C = np.zeros(maxTp)
    tPr = np.zeros(maxTp)
    for i in xrange(maxTp):
        xLen = xData.shape[0]
        runTot = 0
        for j in xrange(xLen - i):
            runTot += xData[j] * xData[j + i]
        C[i] = (float(runTot) / float(xLen - i))
        tPr[i] = (i)
    C = C - xMean**2.

    lnC = np.log(C)
    myodr = odr.ODR(odr.Data(tPr, lnC),
                    odr.Model(str8Line),
                    beta0=[-0.00558, -1.0])
    result = myodr.run()
    fitLine = result.beta[0] * tPr + result.beta[1]

    pl.plot(tPr, lnC)
    pl.plot(tPr, fitLine)
    pl.show()

    correlationTime = -1.0 / result.beta[0]

    print correlationTime
    return correlationTime
def autoCorrelation(xData,maxRange):                                    
    """
    A function to calculate the auto correlation time of a data set.
    :xData: the data for which the time is found.
    :maxRange: the range over which an exponential decay is fitted(=100).
    """
    maxTp = maxRange                                                    # the maximum distance between values in a list, eg List[0] and List[100]
    xMean = np.average(xData)                                           # finds the mean of all values
    C = np.zeros(maxTp)                                                 # numpy arrays to hold Correlation
    tPr = np.zeros(maxTp)                                               # different Correlation times, t', No of elements between values
    for i in xrange(maxTp):
        xLen = xData.shape[0]
        runTot = 0
        for j in xrange(xLen-i):
            runTot+=xData[j]*xData[j+i]                                 # checks how correlated each bit is
        C[i]=(float(runTot)/float(xLen-i))
        tPr[i]=(i)
    C = C - xMean**2.
    
    lnC = np.log(C)
    myodr = odr.ODR(odr.Data(tPr,lnC), odr.Model(str8Line), beta0=[-1.0, -1.0])
    result = myodr.run()
    fitLine = result.beta[0]*tPr + result.beta[1]
    
    #pl.plot(tPr,lnC)
    #pl.plot(tPr,fitLine)
    #pl.show()
    
    correlationTime = -1.0/result.beta[0]
    if np.isnan(correlationTime):return 700
    return correlationTime
Exemple #6
0
def ODR_Linear_Fit(X, dX, Y, dY):

    if len(X) != len(Y):
        raise Exception("The vectors are not the same length! ")
    if len(X) < 3: raise Exception("Use at least 3 points! ")
    dX = np.array([dX])
    dY = np.array([dY])

    N = len(X)

    if len(dX) == 1: dX = dX * np.ones(N)
    if len(dY) == 1: dY = dY * np.ones(N)

    linear = odr.Model(ODR_Linear_Fit_f)
    mydata = odr.RealData(X, y=Y, sx=dX, sy=dY)
    myodr = odr.ODR(mydata, linear, beta0=[1, 2])
    myoutput = myodr.run()
    myoutput.pprint()

    m = myoutput.beta[0]
    b = myoutput.beta[1]
    sm = myoutput.sd_beta[0]
    sb = myoutput.sd_beta[1]
    rchi2 = myoutput.res_var
    dof = len(myoutput.beta)
    chi2 = rchi2 * dof
    P = 1 - scichi.cdf(chi2, dof)
    return m, sm, b, sb, rchi2, P
Exemple #7
0
def fitLine(data):
    start = 0
    end = 0
    rest = 0
    listOfSpeeds = []
    errors = []
    frameNo = []
    metEnd = False
    for i in xrange(len(data) - 1):
        rest += 1
        frameNo.append(i)
        if data[i + 1] < data[i] and metEnd == False:
            start = i + 9
            metEnd = True
        if data[i + 1] > data[i] and rest > 10:
            metEnd = False
            rest = 0
            end = i
            x = frameNo[start:end]
            y = data[start:end]
            myodr = odr.ODR(odr.Data(frameNo[start:end], data[start:end]),
                            odr.Model(str8Line),
                            beta0=[-0.5, 70])
            result = myodr.run()
            fitLine = result.beta[0] * frameNo[start:end] + result.beta[1]
            listOfSpeeds.append(result.beta[0])
            errors.append(result.sd_beta[0])
    return np.mean(listOfSpeeds), np.mean(errors)
Exemple #8
0
    def __init__(self, list_of_points, pointcloud):
        self.points = list_of_points 
        model = odr.Model(odr_linear_definition)

        #extract coordinates from list of points
        x = [pointcloud.points[p][0] for p in list_of_points]
        y = [pointcloud.points[p][1] for p in list_of_points]

        #calculate standard errors along dimensions or something
        wd = 1/(np.std(x)**2) #FIXME: I have no idea what this is 
        we = 1/(np.std(y)**2) #or if I did it right

        #fit ODR
        data = odr.Data(x, y, wd=wd, we=we)
        self.odr = odr.ODR(data, model, beta0=[1., 1.])
        self.odr.run()

        params = self.odr.output.beta 

        #apply bounds for start and end point
        bounds = []
        for index in [-1, 0]:
            m, b = params 
            p_x, p_y = pointcloud.points[self.points[index]]
            bounds.append(self._find_closest_input_val(m, b, p_x, p_y))
        bounds.sort()

        #save the params in nice formats
        params = {"m": params[0], "b": params[1], "low": bounds[0], "high": bounds[1]}
        self.params = params
        self.start_point = [params["low"], params["low"] * params["m"] + params["b"]]
        self.end_point = [params["high"], params["high"] * params["m"] + params["b"]]
def perform_odr(x, y, sx, sy):
    linear = odr.Model(f)
    mydata = odr.Data(x, y, wd=1./np.power(sx,2), we=1./np.power(sy,2))
    myodr = odr.ODR(mydata, linear, beta0=linreg[0:2])
    output = myodr.run()
    output.pprint()
    return output
Exemple #10
0
    def odr(self, i, plot=False, eps=2):
        """Orthogonal distance regression for temporally concurrent points of overlapping time series. Uses :mod:`scipy.odr`.

        :param i: the 'join' index (``0`` is the index of the join between series ``0`` and ``1`` etc.)
        :param plot: whether or not to plot the given join
        :param eps: the ``eps`` param to be handed down to the :meth:`.dbscan` method for outlier detection
        :returns: dictionary with computed statistics, including the additive offset (key ``offs``)
        :rtype: :obj:`dict`

        """
        c = self.var.iloc[:, i:i + 2].dropna(0, 'any')

        # outliers are detected from the differenc between the two series
        diff = c.diff(1, 1).values[:, 1]
        k, labels = self.dbscan(diff, True, True, eps=eps)

        x, y = c.iloc[k].values.T
        o = odr.ODR(odr.Data(x, y), odr.models.unilinear, beta0=[1, 0]).run()
        diff = diff[k]
        offs = np.nanmean(diff)
        m = np.argmin(abs(diff - offs)) + np.where(k)[0].min()
        self.starts[i + 1] += m
        self.ends[i] = self.starts[i + 1] - 1

        if plot:
            fig, axs = plt.subplots(1, 2, figsize=(12, 5))
            t = c.index.values.astype('datetime64[s]').astype(float).reshape(
                (-1, 1))
            x, y = c.values.T
            for h in labels:
                pl = axs[0].plot(t[h], x[h], 'x')[0]
                axs[0].plot(t[h], y[h], '*', color=pl.get_color())
                axs[1].plot(x[h], y[h], 'x')
                axs[1].plot(o.xplus, o.y, 'r-')
            axs[1].plot(x, y, 'mo', mfc='none')
            axs[1].yaxis.tick_right()
            plt.show()
        else:
            idx = c.index.symmetric_difference(c.index[k])
            j = np.where(np.diff(idx) != self.delta)[0]
            if len(j) > 1:
                print('Complex outlier structure deteced at knot {}'.format(i))
            elif len(
                    j
            ) == 1:  # means there are outliers on either end of the overlap
                self.out.loc[idx[:j[0] + 1], 'outliers'] = i + 1
                self.out.loc[idx[j[0] + 1:], 'outliers'] = i
            elif len(
                    idx
            ) > 0:  # means there are outliers only in one of the two series
                self.out.loc[idx, 'outliers'] = i + int(idx[0] == c.index[0])
            return {
                'offs': offs,
                'kind': 'mean',
                'stdev': diff.std() / np.sqrt(len(diff)),
                'odr_slope': o.beta[0],
                'odr_offs': o.beta[1],
                'RSS1': np.mean(o.delta**2),
                'RSS2': np.mean(o.eps**2),
            }
Exemple #11
0
def fit_circle(points):
    """
    fits a circle to the given points. The method has been adapted from
        http://wiki.scipy.org/Cookbook/Least_Squares_Circle
    The function returns an instance of Circle
    """
    def calc_dist(xc, yc):
        """ calculate the distance of each point from the center (xc, yc) """
        return np.linalg.norm(points - np.array([[xc, yc]]), axis=0)
    
    def circle_implicit(beta, x):
        """ implicit definition of the circle """
        return (x[0] - beta[0])**2 + (x[1] - beta[1])**2 - beta[2]**2

    # coordinates of the bary center
    x_m, y_m = np.mean(points, axis=0)

    # initial guess for parameters
    R_m = calc_dist(x_m, y_m).mean()
    beta0 = [x_m, y_m, R_m]    
    
    # for implicit function :
    #       data.x contains both coordinates of the points (data.x = [x, y])
    #       data.y is the dimensionality of the response
    lsc_data  = odr.Data(points.T, y=1)
    lsc_model = odr.Model(circle_implicit, implicit=True)
    lsc_odr   = odr.ODR(lsc_data, lsc_model, beta0)
    lsc_out   = lsc_odr.run()
    
    # collect result
    xc, yc, R = lsc_out.beta
    return shapes.Circle(xc, yc, R)
def fiting_test():
    a = 2.
    b = 3.
    N = 10
    x = np.linspace(-a, a, N)
    y = b * np.sqrt(1. - (x / a)**2)

    x += (2. * np.random.random(N) - 1.) * 0.1
    y += (2. * np.random.random(N) - 1.) * 0.1

    xx = np.array([x, y])

    mdr = odr.Model(f, implicit=True)
    mydata = odr.Data(xx, y=1)

    myodr = odr.ODR(mydata, mdr, beta0=[1., 2.])
    myoutput = myodr.run()
    myoutput.pprint()

    ax = plt.subplot(111, aspect='equal')
    plt.scatter(x, y)  # raw data with randomness

    ell = Ellipse(xy=(0., 0.),
                  width=2. * myoutput.beta[0],
                  height=2. * myoutput.beta[1],
                  angle=0.0)
    ell.set_facecolor('none')
    ax.add_artist(ell)  # fitted curve

    plt.show()
Exemple #13
0
def odRegress(x, y, weights=[], linreg=None):
    # Produce Orthogonal Data Reduction (fit) for the passed x,y pairings.
    # Returns: [fit parameters] , [parm. errors], [[covariance matrix]],
    #           [estimated error in x vars], [estimated error in y vars]
    # Code modified from: Robin Wilson, see the site for copyright notice:
    # http://blog.rtwilson.com/orthogonal-distance-regression-in-python/
    #
    # x,y both arrays of same length
    assert len(x) == len(y)

    if linreg is None:
        linreg = stats.linregress(x, y)

    # Linear function defined above
    model = odr.Model(Linear)
    if len(weights) != len(x):
        data = odr.Data(x, y=y)
    else:
        data = odr.Data(x, y=y, we=weights)

    # Bad variable naming, I know...so caps to avoid(?) confusion. :P
    ODR = odr.ODR(data, model, beta0=linreg[0:2])

    out = ODR.run()
    return out.beta, out.sd_beta, out.cov_beta, out.delta, out.eps
Exemple #14
0
    def _odr(self, x, y, x_err, y_err):
        """Orthogonal distance regression (ODR).

        Parameters
        ----------
        x : array
            x values
        y : array
            y values
        x_err : array
            x standard deviations
        y_err : array
            y standard deviations

        Returns
        -------
        tuple
            Regression results
        """
        lin_reg = self._linear_fit(x, y)
        linear_model = odr.Model(self._linear_func)
        data = odr.RealData(x, y, sx=x_err, sy=y_err)
        odr_fit = odr.ODR(data, linear_model, beta0=lin_reg[0:2])
        out = odr_fit.run()

        slope = out.beta[0]
        intercept = out.beta[1]
        r2 = self._odr_r_squared(out.y, y)
        slope_std_err = out.sd_beta[0]
        intercept_std_err = out.sd_beta[0]

        return slope, intercept, r2, slope_std_err, intercept_std_err
Exemple #15
0
def plot_lin_reg(X, Y, x_name, y_name, x_err=[], y_err=[]):
    '''
    plot linear regression results
    :param x: data for x axis
    :param y: data for y axis
    :param intercept: returned intercept value from linear regression fit
    :param coeff: returned coefficient value from linear regression fit
    :return: None
    '''
    X = X.to_numpy()
    Y = Y.to_numpy()
    #popt, pcov = curve_fit(line, X, Y, xerr=x_err, yerr=y_err)
    linear = odr.Model(f)
    mydata = odr.RealData(X, Y, sx=x_err, sy=y_err)
    myodr = odr.ODR(mydata, linear, beta0=[1., 0.2])
    myoutput = myodr.run()
    linear_fit_x = np.linspace(np.min(X), 1.1 * np.max(X), 100)
    linear_fit_y = list(
        map(lambda x: x * myoutput.beta[0] + myoutput.beta[1], linear_fit_x))
    #plotting
    fig, ax = plt.subplots()
    ax.plot(linear_fit_x, linear_fit_y)
    ax.errorbar(X,
                Y,
                xerr=x_err,
                yerr=y_err,
                color='coral',
                fmt='o',
                capsize=3,
                capthick=1,
                ecolor='coral')
    plt.xlabel(plot_names[x_name])
    plt.ylabel(plot_names[y_name])
    fig.show()
Exemple #16
0
def line_fit(datax,datay,errory,beta0=[90,1]):
    mod = sdr.Model(line)
    dat = sdr.RealData(datax,datay,errory)
    odr = sdr.ODR(dat,mod,beta0=beta0)
    odr.set_job(fit_type=2)
    res = odr.run()
    return res
Exemple #17
0
def perform_odr(add, dev, wadd, wdev, beta0=[0.]):
    """
    A wrapper around scipy.ODR.

    Params:
    -------
    add, dev:   np.array
                x and y coordinates of the regression
    wadd, wdev: np.array
                standard deviations
    beta0:      float
                initialization for ODR

    Output:
    -------
    an ODR object
    """
    def f(B, x):
        """A linear function for the ODR."""
        return B * (x)

    linear = odr.Model(f)
    mydata = odr.RealData(add, dev, sx=wadd, sy=wdev)
    myodr = odr.ODR(mydata, linear, beta0=beta0)
    myoutput = myodr.run()

    if myoutput.stopreason[0] == 'Numerical error detected':
        print('Warning: Numerical error detected, ODR failed.')

    return myoutput
Exemple #18
0
def gauss_fit(datax,datay,errory,beta0=[0,1,1,0,0]):
    mod = sdr.Model(gaussian)
    dat = sdr.RealData(datax,datay,sy=errory)
    odr = sdr.ODR(dat,mod,beta0=beta0)
    odr.set_job(fit_type=2)
    res = odr.run()
    return res
Exemple #19
0
def ODR_Gauss_Fit(X,dX,Y,dY,guess):
	#print(X,dX,Y,dY,guess)

	if len(X) != len(Y): raise Exception("The vectors are not the same length! ")
	if len(X) < 3: raise Exception("Use at least 3 points! ")
	dX = np.array([dX])
	dY = np.array([dY])
	
	N = len(X)
		
	if len(dX) == 1: dX = dX*np.ones(N)
	if len(dY) == 1: dY = dY*np.ones(N)

	Gauss = odr.Model(ODR_Gauss_Fit_f)
	mydata = odr.RealData(X,y=Y,sx=dX,sy=dY)
	myodr = odr.ODR(mydata,Gauss,beta0 = guess,maxit=1000)
	myoutput = myodr.run()
	#myoutput.pprint()
	
	a = myoutput.beta[0]
	b = myoutput.beta[1]
	c = myoutput.beta[2]
	sa = myoutput.sd_beta[0] 
	sb = myoutput.sd_beta[1] 
	sc = myoutput.sd_beta[2] 
	rchi2 = myoutput.res_var
	dof = N - len(myoutput.beta)
	chi2 = rchi2*dof
	P = 1 - scichi.cdf(chi2,dof)
	return a, sa, b, sb, c, sc, rchi2, P
Exemple #20
0
    def optimize_odr(self, data):
        """
        Calculate the orthogonal distance regression for a dataset fitted to a circle

        Parameters
        ----------
        data: 2D array
            x-y coordinates of all datapoints in all images

        Returns
        -------
        calibration: float array
            array of midpoint coordinates and respective radii
        """
        self.grid = data[0][1]
        self.grid_stack = np.array(list(self.grid) * len(data))
        x = np.array(data)[:, 0, :].flatten()
        lsc_data = odr.Data(x, y=1)
        lsc_model = odr.Model(fcn=self.f_odr_multivariate,
                              implicit=True,
                              estimate=self.calc_estimate_odr_multivariate)
        lsc_odr = odr.ODR(lsc_data, lsc_model)
        lsc_out = lsc_odr.run()
        lsc_out.pprint()
        opt = lsc_out.beta

        calibration = []
        for i in range(len(data)):
            xc = opt[i * 2]
            yc = opt[i * 2 + 1]
            r = opt[-1]
            package = [xc, yc, r]
            calibration.append(package)
        return calibration
Exemple #21
0
    def run_ODR(self, mag, phase, regressors=None):
        linearfit = odr.Model(self.multiplelinear)

        stdm, noise = self.get_noise_est(mag)
        stdp, _ = self.get_noise_est(phase)

        if regressors is not None:
            design = np.concatenate((phase[:, np.newaxis], regressors,
                                     np.ones_like(phase[:, np.newaxis])), axis=1).T
            ests = np.concatenate(([stdm/stdp],
                                   np.std(regressors, axis=0)/stdp,
                                   [np.mean(mag) / np.mean(phase)]))
            mydata = odr.RealData(design, mag,
                                  sx=np.concatenate(([stdp],
                                                     np.std(
                                                         regressors, axis=0),
                                                     [1])),
                                  sy=stdm)
        else:
            design = np.row_stack((phase, np.ones(phase.shape)))
            ests = [np.std(mag) / np.std(phase),
                    np.mean(mag) / np.mean(phase)]
            mydata = odr.RealData(design, mag,
                                  sx=np.hstack([stdp, np.finfo(stdp).eps]),
                                  sy=stdm)

        # and fit model
        # mag = A*phase + B*regressors + C
        # (y=mx+b)
        # call : (x,y,sx,sy)
        odr_obj = odr.ODR(mydata, linearfit, beta0=ests, maxit=400)
        res = odr_obj.run()
        return res, design
Exemple #22
0
def odr_fit(x_data,
            x_data_err,
            y_data,
            y_data_err,
            fitfunc=double_power_law,
            initial_guesses=(100, -20, 0, -0.5)):
    """Fit data to a function using ODR fitting.


    Inputs:
        - x_data: Data along x-axis
        - x_data_err: Error of data along x-axis
        - y_data: Data along y-axis
        - y_data_err: Error in data along y-axis
        - fitfunc: Function to fit data to [default value = double_power_law]

    Returns:
        - ODR output object
    """
    from scipy import odr

    # Instantiate classes for ODR run
    fit_func = odr.Model(fitfunc)
    mydata = odr.RealData(x_data, y_data, sx=x_data_err, sy=y_data_err)

    # Instantiate ODR
    myodr = odr.ODR(mydata, fit_func, beta0=initial_guesses)

    # Get output
    output = myodr.run()
    output.pprint()

    return output
Exemple #23
0
def perform_odr(x,y,xerr,yerr):
    line=odr.Model(odr_line)
    mydata=odr.RealData(x,y,sx=xerr,sy=yerr)
    myodr=odr.ODR(mydata,line,beta0=[1.,0.])
    myoutput=myodr.run()
    myoutput.pprint()
    return myoutput
Exemple #24
0
 def run(self):
     Model = odr.Model(self._linear)
     Data = odr.RealData(self._x, self._y, sx=self._dx, sy=self._dy)
     initial = self._initial_guess()
     solver = odr.ODR(Data, Model, initial)
     self.result = solver.run()
     return self.result.beta, self.result.sd_beta
Exemple #25
0
    def linreg(self, filtered_kym):

        linear_model = odr.Model(self.linear_function)
        x_points, y_points = np.nonzero(filtered_kym)
        mydata = odr.RealData(x_points, y_points)
        myodr = odr.ODR(mydata, linear_model, beta0=[0., 1.])
        myodr.run()
        m, b = myodr.output.beta
        print(m, b)

        if b < 0:
            y0 = 0
            x0 = int(-b / m)
            y1 = 1
            x1 = int((1 - b) / m)
        elif b == 0:
            x0 = 0
            y0 = 0
            x1 = 1
            y1 = m
        else:
            y0 = int(b) - 1
            x0 = int((y0 - b) / m)
            y1 = int(b) - 2
            x1 = int((y1 - b) / m)

        return [[x0, y0], [x1, y1]]
def fit(x,y):
    #Fit the data using scipy.odr
    fit_func = lambda B,x: B[0]*x + B[1]
    Model = odr.Model(fit_func)
    Data = odr.RealData(x, y)
    Odr = odr.ODR(Data, Model, beta0=[1,1])
    return Odr.run()
Exemple #27
0
    def fit_ellipse(self,
                    beta_i,
                    num_fitting_pts=5000,
                    ring_width=40,
                    num_high_pix=20):
        '''
        Fit an ellipse to a ring image
        ============================== 
        beta_i          - float tuple , ( x_center, y_center, x_radius, y_radius )
        num_fitting_pts - int, number of pixels to include in fit 
        ring_width      - int, width ring-like region on image that contains ring of interest (pixels units)
        num_high_pix    - int, remove this many pixels before running fit (removes artificial high pixels that might bias the fit)
        '''

        r = sqrt((self.x - beta_i[0])**2 + (self.y - beta_i[1])**2)
        self.img[r > beta_i[2] + ring_width / 2] = 0
        self.img[r < beta_i[2] - ring_width / 2] = 0

        img1 = self.img.ravel()
        self._remove_highest(img1, num_high_pix)

        num_pts = where(img1 > 0)[0].shape[0]
        if num_fitting_pts >= num_pts:
            num_fitting_pts = num_pts / 2

        inds = argsort(img1)[::-1][:num_fitting_pts]

        pts = row_stack([self.x1[inds], self.y1[inds]])
        lsc_data = odr.Data(pts, y=1)
        lsc_odr = odr.ODR(lsc_data, self.ellipse_model, beta_i)
        lsc_out = lsc_odr.run()
        return lsc_out.beta
Exemple #28
0
def MaritimeSummary_ODR(df):

    maritime = df[(df['Maritime'] == 1) & (abs(df['Lat']) <= 15)]
    subset = df[(df['Maritime'] == 1) & (abs(df['Lat']) > 35)]

    linmod = odr.Model(linreg)
    #instantiate with results of linear regression
    beta0 = [0.0012, 0.0059]
    y = subset['Slope'].values.astype(float)
    x = 10 * subset['Press'].values.astype(float)
    sx = subset['Slope_std'].values.astype(float)
    sy = subset['Press_std'].values.astype(float)
    realdata = odr.RealData(x, y, sx=sx, sy=sy)
    model = odr.ODR(realdata, linmod, beta0=beta0)
    results = model.run()
    print(
        'Beta0: {}, Beta0 std: {}, Beta1: {}, Beta1 std: {}, inv condnum: {}, resvar: {}'
        .format(results.beta[0], results.sd_beta[0], results.beta[1],
                results.sd_beta[1], results.inv_condnum, results.res_var))

    x = sm.add_constant(x)
    results = sm.OLS(y, x, missing='drop').fit()
    print(results.summary())

    print('Results for d18O slopes vs Omega 500 slopes:')
    y = maritime['Slope'].values.astype(float)
    x = 10 * maritime['Omega'].values.astype(float)
    x = sm.add_constant(x)
    results = sm.OLS(y, x, missing='drop').fit()
    print(results.summary())
def perform_odr(x, y, xerr, yerr):
    quadr = odr.Model(odr_line)
    mydata = odr.Data(x, y, wd=1. / xerr, we=1. / yerr)
    #mydata = odr.Data(x, y)
    myodr = odr.ODR(mydata, quadr, beta0=[0., 0.])
    output = myodr.run()
    return output
Exemple #30
0
def hedge(df, days_moving_avg):  # USED DEBUGGING
    y = np.asarray(df[stock1].tolist()[-days_moving_avg:])  # stock 1 data
    x = np.asarray(df[stock2].tolist()[-days_moving_avg:])  # stock 2 data

    # Fit the data using scipy.odr
    def f(B, x):
        return B[0] * x + B[1]

    linear = odr.Model(f)
    mydata = odr.RealData(x, y, sx=np.std(x), sy=np.std(y))
    fit_np = np.polyfit(x, y, 1)
    print fit_np
    myodr = odr.ODR(mydata, linear,
                    beta0=[fit_np[0], fit_np[1]])  # THINK ABOUT CHANGING BETA0
    myoutput = myodr.run()
    # fit the data using numpy.polyfit
    # graph to compare the fit
    print 'polyfit beta', fit_np[0]
    print 'least errors beta', myoutput.beta[0]
    plt.plot(x, y, label='Actual Data',
             linestyle='dotted')  # actual data points
    plt.plot(x, np.polyval(fit_np, x), "r--", lw=2,
             label='Polyfit')  # polyfit regression line
    plt.plot(x, f(myoutput.beta, x), "g--", lw=2,
             label='Least Errors')  # least errors regression line
    plt.legend(loc='lower right')
    plt.title('Linear Fit Through Historical Data')
    plt.show()
    # myoutput.pprint()
    # df["HedgeRatio"] = calculateHedgeRatio(df[])
    return myoutput.beta[0]  # returns the hedge ratio