Fit regression model to data

        :param: time (column of data)

        :return: predicted points
        :return: residuals
        :return: mean residual
        :return: error
6915
def _get_fit(self, time: int) -> typing.Tuple[np.ndarray, np.ndarray, float, float]:
        
        rawdata = self.averagedata[:, time]
        domain = np.arange(len(rawdata))
        datalength = len(domain)
        coefficients = np.zeros((datalength, self.function_number + 2))
        coefficients[:, 0] = 1
        coefficients[:, 1] = domain
        for i in range(self.function_number):
            coefficients[:, 2 + i] = self._gaussian_function(datalength, domain, 1, i)
        betas = linalg.inv(coefficients.transpose().dot(coefficients)).dot(coefficients.transpose().dot(rawdata))
        predicted_values = coefficients.dot(betas)
        residuals = rawdata - predicted_values
        error = np.sqrt(residuals.transpose().dot(residuals) / (datalength - (self.function_number + 2)))
        return predicted_values, residuals, residuals.mean(), error
406967
def predict(self, h=5):      
             

        if self.latent_variables.estimated is False:
            raise Exception("No latent variables estimated!")
        else:
            # Retrieve data, dates and (transformed) latent variables        
            date_index = self.shift_dates(h)
            forecasted_values = np.ones(h)*self.states[-1]
            t_params = self.transform_z()

            if self.model_name2 == 'Skewt':
                forecasted_values = forecasted_values + ((t_params[-3] - (1.0/t_params[-3]))*t_params[-2]*gas.SkewtScore.tv_variate_exp(t_params[-1]))

            result = pd.DataFrame(self.link(forecasted_values))
            result.rename(columns={0:self.data_name}, inplace=True)
            result.index = date_index[-h:]

            return result
843724
def predict(self, X):
        
        X = np.atleast_2d(X)
        m = np.empty(shape=(0,1))
        s = np.empty(shape=(0,1))

        for k in range(X.shape[0]):
            preds = []
            for pred in self.model.estimators_:
                preds.append(pred.predict(X[k,:])[0])
            m = np.vstack((m ,np.array(preds).mean()))
            s = np.vstack((s ,np.array(preds).std()))
        return m, s
1246024
def report_fit(self):
        
        if not self.fitted:
            print('Model not yet fit.')
            return

        print('Null Log-liklihood: {0:.3f}'.format(
            self.log_likelihoods['null']))
        print('Log-liklihood at convergence: {0:.3f}'.format(
            self.log_likelihoods['convergence']))
        print('Log-liklihood Ratio: {0:.3f}\n'.format(
            self.log_likelihoods['ratio']))

        tbl = PrettyTable(
            ['Component', ])
        tbl = PrettyTable()

        tbl.add_column('Component', self.fit_parameters.index.values)
        for col in ('Coefficient', 'Std. Error', 'T-Score'):
            tbl.add_column(col, self.fit_parameters[col].values)

        tbl.align['Component'] = 'l'
        tbl.float_format = '.3'

        print(tbl)
417822
def regression(fname="regression.png"):
    
    _, axes = plt.subplots(ncols=2, figsize=(18, 6))
    alphas = np.logspace(-10, 1, 300)
    data = load_concrete(split=True)

    # Plot prediction error in the middle
    oz = PredictionError(LassoCV(alphas=alphas), ax=axes[0])
    oz.fit(data.X.train, data.y.train)
    oz.score(data.X.test, data.y.test)
    oz.finalize()

    # Plot residuals on the right
    oz = ResidualsPlot(RidgeCV(alphas=alphas), ax=axes[1])
    oz.fit(data.X.train, data.y.train)
    oz.score(data.X.test, data.y.test)
    oz.finalize()

    # Save figure
    path = os.path.join(FIGURES, fname)
    plt.tight_layout()
    plt.savefig(path)
37125
def _hotellings_time(self):
        
        # get residuals
        df = self._df
        R = self._A - np.dot(self._X, self._Bhat)
        Sigma_Z = np.dot(R.T,R)*(1./df)
        # compute p-values
        T_2_list = []
        p_value_list = []
        for i in np.arange(0,self._Bhat.shape[0]):
            Bstar       = self._Bhat[i,np.arange(0,np.shape(self._A)[1])]
            lstar       = float(np.shape(Bstar)[0])
            cx          = self._Cx[i,i]
            Einv        = np.linalg.pinv(Sigma_Z)
            Zs          = Bstar/np.sqrt(cx)
            T_2         = ((df - lstar + 1.)/(df*lstar)*np.dot(np.dot(Zs,Einv),Zs.T))
            p_value     = 1. - stats.f.cdf(T_2, lstar, df - lstar + 1.)

            p_value_list.append(p_value)
            T_2_list.append(T_2)
        # save pvalue results in a table
        self._results = [['Comparison','Hotellings T^2', "p-value"]]
        for i in np.arange(0,len(self._col_names)):
            self._results.append([self._col_names[i], T_2_list[i], p_value_list[i]])
822502
def predict(self, t):
        
        t = np.asarray(t)
        return self._predict(np.ravel(t)).reshape(t.shape)
319131
def _fit_bmr_model(self, X, y):
        """"""
        self.f_bmr = BayesMinimumRiskClassifier()
        X_bmr = self.predict_proba(X)
        self.f_bmr.fit(y, X_bmr)
        return self
155020
def fit(self, X):
        
        params_dict = _fit_gmm_params(X=X, n_mixtures=self.n_clusters, \
                        n_init=self.n_trials, init_method=self.init_method, \
                        n_iter=self.max_iter, tol=self.tol)
        self.priors_ = params_dict['priors']
        self.means_  = params_dict['means']
        self.covars_ = params_dict['covars']

        self.converged = True
        self.labels_ = self.predict(X)
676869
def QA_data_futuremin_resample(min_data, type_='5min'):
    

    min_data.tradeime = pd.to_datetime(min_data.tradetime)

    CONVERSION = {
        'code': 'first',
        'open': 'first',
        'high': 'max',
        'low': 'min',
        'close': 'last',
        'trade': 'sum',
        'tradetime': 'last',
        'date': 'last'
    }
    resx = min_data.resample(
        type_,
        closed='right',
        loffset=type_
    ).apply(CONVERSION)
    return resx.dropna().reset_index().set_index(['datetime', 'code'])
27707
def regress_fit(fqdn, result, *argl, **argd):
    
    if len(argl) > 2:
        yP = argl[2]
    out = _generic_fit(fqdn, result, regress_predict, yP, *argl, **argd)
    return out
161042
def plot_fit(self, **kwargs):
        

        import matplotlib.pyplot as plt
        import seaborn as sns

        figsize = kwargs.get('figsize',(10,7))
        plt.figure(figsize=figsize)
        date_index = self.index[self.ar:self.data.shape[0]]
        mu, Y = self._model(self.latent_variables.get_z_values())
        plt.plot(date_index,Y,label='Data')
        plt.plot(date_index,mu,label='Filter',c='black')
        plt.title(self.data_name)
        plt.legend(loc=2)   
        plt.show()
317137
def plot_fit(self, intervals=True, **kwargs):
        
        import matplotlib.pyplot as plt
        import seaborn as sns

        figsize = kwargs.get('figsize',(10,7))
        series_type = kwargs.get('series_type','Smoothed')

        if self.latent_variables.estimated is False:
            raise Exception("No latent variables estimated!")
        else:
            date_index = copy.deepcopy(self.index)
            date_index = date_index[self.integ:self.data_original.shape[0]+1]

            if series_type == 'Smoothed':
                mu, V = self.smoothed_state(self.data,self.latent_variables.get_z_values())
            elif series_type == 'Filtered':
                mu, V, _, _, _ = self._model(self.data,self.latent_variables.get_z_values())
            else:
                mu, V = self.smoothed_state(self.data,self.latent_variables.get_z_values())

            mu = mu[0][:-1]
            V = V.ravel()

            plt.figure(figsize=figsize) 
            
            plt.subplot(3, 1, 1)
            plt.title(self.data_name + " Raw and " + series_type)   

            if intervals == True:
                alpha =[0.15*i/float(100) for i in range(50,12,-2)]
                plt.fill_between(date_index[2:], mu[2:] + 1.98*np.sqrt(V[:-1][2:]), mu[2:] - 1.98*np.sqrt(V[:-1][2:]), alpha=0.15,label='95% C.I.') 

            plt.plot(date_index,self.data,label='Data')
            plt.plot(date_index,mu,label=series_type,c='black')
            plt.legend(loc=2)
            
            plt.subplot(3, 1, 2)
            plt.title(self.data_name + " Local Level")  

            if intervals == True:
                alpha =[0.15*i/float(100) for i in range(50,12,-2)]
                plt.fill_between(date_index[2:], mu[2:] + 1.98*np.sqrt(V[:-1][2:]), mu[2:] - 1.98*np.sqrt(V[:-1][2:]), alpha=0.15,label='95% C.I.') 

            plt.plot(date_index,mu,label='Local Level')
            plt.legend(loc=2)
            plt.subplot(3, 1, 3)
            plt.title("Measurement Noise")  
            plt.plot(date_index,self.data-mu)
            plt.show()
635502
def run(time: datetime, altkm: float,
        glat: Union[float, np.ndarray], glon: Union[float, np.ndarray], *,
        f107a: float = None, f107: float = None, Ap: int = None) -> xarray.Dataset:
    
    glat = np.atleast_2d(glat)
    glon = np.atleast_2d(glon)  # has to be here
# %% altitude 1-D
    if glat.size == 1 and glon.size == 1 and isinstance(time, (str, date, datetime, np.datetime64)):
        atmos = rungtd1d(time, altkm, glat.squeeze()[()], glon.squeeze()[()],
                         f107a=f107a, f107=f107, Ap=Ap)
# %% lat/lon grid at 1 altitude
    else:
        atmos = loopalt_gtd(time, glat, glon, altkm,
                            f107a=f107a, f107=f107, Ap=Ap)

    return atmos
401343
def Fit(self, zxq):
        
        z, trans, Q = zip(*zxq)

        assert len(trans) == len(z)
        ndf = len(z) - 3

        z = np.array(z)
        trans = np.array(trans)

        def dbexpl(t, p):
            return(p[0] - p[1] * t + p[2] * t ** 2)

        def residuals(p, data, t):
            err = data - dbexpl(t, p)
            return err

        doc = {}

        try:
            assert ndf > 0

            p0 = [1, 0, 0]  # initial guesses
            pbest = leastsq(residuals, p0, args=(trans, z), full_output=1)
            bestparams = pbest[0]
            good_of_fit = sum(pbest[2]['fvec'] ** 2)
            good_of_fit = float(good_of_fit / ndf)
            doc['params'] = list(bestparams)
            doc['gof'] = good_of_fit
        except:
            doc['gof'] = 'FAIL'
            doc['params'] = [0, 0, 0]

        return doc
1149296
def ols_covariance(self):
                 

        Y = np.array([reg[self.lags:reg.shape[0]] for reg in self.data])        
        return (1.0/(Y[0].shape[0]))*np.dot(self.residuals(Y),np.transpose(self.residuals(Y)))
114782
def fit(self, data):
        

        _raise_error_if_not_sframe(data, "data")

        fitted_state = {}
        feature_columns = _internal_utils.get_column_names(data, self._exclude, self._features)

        if not feature_columns:
            raise RuntimeError("No valid feature columns specified in transformation.")

        fitted_state['features'] = feature_columns
        fitted_state['fitted'] = True

        self.__proxy__.update(fitted_state)

        return self