Exemple #1
0
    def component_fitting(self,
                          mode='gaussian',
                          nmax=10,
                          full=False,
                          minamp=None,
                          alpha=0.05,
                          allownegative=False,
                          verbose=False,
                          save=False):
        '''
        Fitting to phases is much more numerically stable for von mises function
        '''

        fitter = lambda x, y, n: u.fit_components(
            x, y, mode, n, allownegative=allownegative)

        if minamp is not None:
            MAX = np.max(self.data) * minamp
        N = self.getNbins()

        # Fit first component
        n = 1
        nparamA = 3
        fitfunc, errfunc, pfit, perr, s_sq = fitter(self.phases, self.data, n)
        residsA = self.data - fitfunc(pfit, self.phases)
        RSS_funcA = np.sum(residsA**2)

        def doreturn(save=False):
            if save:
                self.data = fitfunc(pfit, self.phases)
            if full:
                return fitfunc(pfit, self.phases), pfit, n
            return fitfunc(pfit, self.phases)

        for n in range(2, nmax + 1):
            if verbose:
                print("Fitting component: %i" % n)
            nparamB = 3 * n
            fitfunc, errfunc, pfit, perr, s_sq = fitter(
                self.phases, self.data, n)
            residsB = self.data - fitfunc(pfit, self.phases)
            RSS_funcB = np.sum(residsB**2)

            if minamp is not None and np.all(residsB < MAX):
                return doreturn(save=save)

            # F-test
            F = ((RSS_funcA - RSS_funcB) /
                 (nparamB - nparamA)) / (RSS_funcB / (N - nparamB - 1))

            p_value = stats.f.sf(F, nparamB - nparamA, N - nparamB - 1)  #cdf?
            #print F,RSS_funcA,RSS_funcB,nparamA,nparamB,p_value
            if p_value > alpha:  # if p_value < alpha, then B is significant, so keep going
                # Replace old values
                return doreturn(save=save)
            nparamA = nparamB
            residsA = residsB
            RSS_funcA = RSS_funcB
        return doreturn(save=save)
        '''
Exemple #2
0
    def component_fitting(self,mode='gaussian',nmax=10,full=False,minamp=None,alpha=0.05,allownegative=False,verbose=False,save=False):
        '''
        Fitting to phases is much more numerically stable for von mises function
        '''

        fitter = lambda x,y,n: u.fit_components(x,y,mode,n,allownegative=allownegative)

        if minamp is not None:
            MAX = np.max(self.data)*minamp
        N = self.getNbins()
        
        # Fit first component
        n = 1
        nparamA = 3
        fitfunc,errfunc,pfit,perr,s_sq = fitter(self.phases,self.data,n)
        residsA = self.data - fitfunc(pfit,self.phases)
        RSS_funcA = np.sum(residsA**2)
        

        def doreturn(save=False):
            if save:
                self.data = fitfunc(pfit,self.phases)
            if full:
                return fitfunc(pfit,self.phases),pfit,n
            return fitfunc(pfit,self.phases)

        
        for n in range(2,nmax+1):
            if verbose:
                print("Fitting component: %i"%n)
            nparamB = 3*n
            fitfunc,errfunc,pfit,perr,s_sq = fitter(self.phases,self.data,n)
            residsB = self.data - fitfunc(pfit,self.phases)
            RSS_funcB = np.sum(residsB**2)


            if minamp is not None and np.all(residsB<MAX):
                return doreturn(save=save)

            # F-test
            F = ((RSS_funcA-RSS_funcB)/(nparamB-nparamA))/(RSS_funcB/(N-nparamB-1))

            p_value = stats.f.sf(F,nparamB-nparamA,N-nparamB-1) #cdf?
            #print F,RSS_funcA,RSS_funcB,nparamA,nparamB,p_value
            if p_value > alpha: # if p_value < alpha, then B is significant, so keep going
            # Replace old values
                return doreturn(save=save)
            nparamA = nparamB
            residsA = residsB
            RSS_funcA = RSS_funcB
        return doreturn(save=save)


        '''
Exemple #3
0
    def component_fitting(self, mode='gaussian', nmax=10):
        n = 1
        chisqs = 10000.0
        fitter = lambda x, y, n: u.fit_components(x, y, mode, n)

        while True:
            fitfunc, errfunc, pfit, perr, s_sq = fitter(
                self.bins, self.data, n)
            #print s_sq
            if s_sq < chisq:
                chisq = s_sq
            else:
                break
            n += 1
            if n == nmax:
                break

        n -= 1
        if n <= 0:
            n = 1
        fitfunc, errfunc, pfit, perr, s_sq = fitter(self.bins, self.data, n)
        return fitfunc(pfit, self.bins)