当前位置: 首页>>代码示例>>Python>>正文


Python api.WLS属性代码示例

本文整理汇总了Python中statsmodels.api.WLS属性的典型用法代码示例。如果您正苦于以下问题:Python api.WLS属性的具体用法?Python api.WLS怎么用?Python api.WLS使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在statsmodels.api的用法示例。


在下文中一共展示了api.WLS属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testWLS

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def testWLS(self):
        # WLS centered SS changed (fixed) in 0.5.0
        sm_version = sm.version.version
        if sm_version < LooseVersion('0.5.0'):
            raise nose.SkipTest("WLS centered SS not fixed in statsmodels"
                                " version {0}".format(sm_version))

        X = DataFrame(np.random.randn(30, 4), columns=['A', 'B', 'C', 'D'])
        Y = Series(np.random.randn(30))
        weights = X.std(1)

        self._check_wls(X, Y, weights)

        weights.ix[[5, 15]] = np.nan
        Y[[2, 21]] = np.nan
        self._check_wls(X, Y, weights) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:18,代码来源:test_ols.py

示例2: __init__

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def __init__(self, endog, exog, weights=1., missing='none', hasconst=None,
                 **kwargs):
        weights = np.array(weights)
        if weights.shape == ():
            if (missing == 'drop' and 'missing_idx' in kwargs and
                    kwargs['missing_idx'] is not None):
                # patsy may have truncated endog
                weights = np.repeat(weights, len(kwargs['missing_idx']))
            else:
                weights = np.repeat(weights, len(endog))
        # handle case that endog might be of len == 1
        if len(weights) == 1:
            weights = np.array([weights.squeeze()])
        else:
            weights = weights.squeeze()
        super(WLS, self).__init__(endog, exog, missing=missing,
                                  weights=weights, hasconst=hasconst, **kwargs)
        nobs = self.exog.shape[0]
        weights = self.weights
        # Experimental normalization of weights
        weights = weights / np.sum(weights) * nobs
        if weights.size != nobs and weights.shape[0] != nobs:
            raise ValueError('Weights must be scalar or same length as design') 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:25,代码来源:linear_model.py

示例3: whiten

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def whiten(self, X):
        """
        Whitener for WLS model, multiplies each column by sqrt(self.weights)

        Parameters
        ----------
        X : array-like
            Data to be whitened

        Returns
        -------
        whitened : array-like
            sqrt(weights)*X
        """

        X = np.asarray(X)
        if X.ndim == 1:
            return X * np.sqrt(self.weights)
        elif X.ndim == 2:
            return np.sqrt(self.weights)[:, None]*X 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:22,代码来源:linear_model.py

示例4: _check_wls

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def _check_wls(self, x, y, weights):
        result = ols(y=y, x=x, weights=1 / weights)

        combined = x.copy()
        combined['__y__'] = y
        combined['__weights__'] = weights
        combined = combined.dropna()

        endog = combined.pop('__y__').values
        aweights = combined.pop('__weights__').values
        exog = sm.add_constant(combined.values, prepend=False)

        sm_result = sm.WLS(endog, exog, weights=1 / aweights).fit()

        assert_almost_equal(sm_result.params, result._beta_raw)
        assert_almost_equal(sm_result.resid, result._resid_raw)

        self.checkMovingOLS('rolling', x, y, weights=weights)
        self.checkMovingOLS('expanding', x, y, weights=weights) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:21,代码来源:test_ols.py

示例5: reg_m

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def reg_m(y, x, estimator, weights=None):
    ones = np.ones(len(x[0]))
    X = sm.add_constant(np.column_stack((x[0], ones)))
    for ele in x[1:]:
        X = sm.add_constant(np.column_stack((ele, X)))

    if estimator=='ols':
        return sm.OLS(y, X).fit()

    elif estimator=='wls':
        return sm.WLS(y, X, weights).fit()

    elif estimator=='gls':
        return sm.GLS(y, X).fit()

    return None

############################
#Run general linear regression
####func array contains the array of functions consdered in the regression
####params coefficients are reversed; the first param coefficient corresponds to the last function in func array
####notice the independent vectors are given in dictionary format, egs:{'bob':[1,2,3,4,5],'mary':[1,2,3,4,5]} 
开发者ID:MacroConnections,项目名称:DIVE-backend,代码行数:24,代码来源:fit.py

示例6: whiten

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def whiten(self, X):
        """
        Whitener for WLS model, multiplies each column by sqrt(self.weights)

        Parameters
        ----------
        X : array-like
            Data to be whitened

        Returns
        -------
        sqrt(weights)*X
        """
        #print(self.weights.var()))
        X = np.asarray(X)
        if X.ndim == 1:
            return X * np.sqrt(self.weights)
        elif X.ndim == 2:
            return np.sqrt(self.weights)[:, None]*X 
开发者ID:nccgroup,项目名称:Splunking-Crime,代码行数:21,代码来源:linear_model.py

示例7: setup

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def setup(self):
        #fit for each test, because results will be changed by test
        x = self.exog
        np.random.seed(987689)
        y = x.sum(1) + np.random.randn(x.shape[0])
        self.results = sm.WLS(y, self.exog, weights=np.ones(len(y))).fit() 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:8,代码来源:test_generic_methods.py

示例8: loglike

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def loglike(self, params):
        """
        Returns the value of the Gaussian log-likelihood function at params.

        Given the whitened design matrix, the log-likelihood is evaluated
        at the parameter vector `params` for the dependent variable `endog`.

        Parameters
        ----------
        params : array-like
            The parameter estimates

        Returns
        -------
        loglike : float
            The value of the log-likelihood function for a GLS Model.


        Notes
        -----
        The log-likelihood function for the normal distribution is

        .. math:: -\\frac{n}{2}\\log\\left(\\left(Y-\\hat{Y}\\right)^{\\prime}\\left(Y-\\hat{Y}\\right)\\right)-\\frac{n}{2}\\left(1+\\log\\left(\\frac{2\\pi}{n}\\right)\\right)-\\frac{1}{2}\\log\\left(\\left|\\Sigma\\right|\\right)

        Y and Y-hat are whitened.

        """
        # TODO: combine this with OLS/WLS loglike and add _det_sigma argument
        nobs2 = self.nobs / 2.0
        SSR = np.sum((self.wendog - np.dot(self.wexog, params))**2, axis=0)
        llf = -np.log(SSR) * nobs2      # concentrated likelihood
        llf -= (1+np.log(np.pi/nobs2))*nobs2  # with likelihood constant
        if np.any(self.sigma):
            # FIXME: robust-enough check? unneeded if _det_sigma gets defined
            if self.sigma.ndim == 2:
                det = np.linalg.slogdet(self.sigma)
                llf -= .5*det[1]
            else:
                llf -= 0.5*np.sum(np.log(self.sigma))
            # with error covariance matrix
        return llf 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:43,代码来源:linear_model.py

示例9: __init__

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def __init__(self, y, x, intercept=True, weights=None, nw_lags=None,
                 nw_overlap=False):
        try:
            import statsmodels.api as sm
        except ImportError:
            import scikits.statsmodels.api as sm

        self._x_orig = x
        self._y_orig = y
        self._weights_orig = weights
        self._intercept = intercept
        self._nw_lags = nw_lags
        self._nw_overlap = nw_overlap

        (self._y, self._x, self._weights, self._x_filtered,
         self._index, self._time_has_obs) = self._prepare_data()

        if self._weights is not None:
            self._x_trans = self._x.mul(np.sqrt(self._weights), axis=0)
            self._y_trans = self._y * np.sqrt(self._weights)
            self.sm_ols = sm.WLS(self._y.get_values(),
                                 self._x.get_values(),
                                 weights=self._weights.values).fit()
        else:
            self._x_trans = self._x
            self._y_trans = self._y
            self.sm_ols = sm.OLS(self._y.get_values(),
                                 self._x.get_values()).fit() 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:30,代码来源:ols.py

示例10: lm

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def lm(data, xseq, **params):
    """
    Fit OLS / WLS if data has weight
    """
    if params['formula']:
        return lm_formula(data, xseq, **params)

    X = sm.add_constant(data['x'])
    Xseq = sm.add_constant(xseq)
    weights = data.get('weights', None)

    if weights is None:
        init_kwargs, fit_kwargs = separate_method_kwargs(
            params['method_args'], sm.OLS, sm.OLS.fit)
        model = sm.OLS(data['y'], X, **init_kwargs)
    else:
        if np.any(weights < 0):
            raise ValueError(
                "All weights must be greater than zero."
            )
        init_kwargs, fit_kwargs = separate_method_kwargs(
            params['method_args'], sm.WLS, sm.WLS.fit)
        model = sm.WLS(data['y'], X, weights=data['weight'], **init_kwargs)

    results = model.fit(**fit_kwargs)
    data = pd.DataFrame({'x': xseq})
    data['y'] = results.predict(Xseq)

    if params['se']:
        alpha = 1 - params['level']
        prstd, iv_l, iv_u = wls_prediction_std(
            results, Xseq, alpha=alpha)
        data['se'] = prstd
        data['ymin'] = iv_l
        data['ymax'] = iv_u

    return data 
开发者ID:has2k1,项目名称:plotnine,代码行数:39,代码来源:smoothers.py

示例11: regression_apply

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def regression_apply(row, timepoints, weighted):
    """
    :py:meth:`pandas.DataFrame.apply` apply function for calculating 
    enrichment using linear regression. If *weighted* is ``True`` perform
    weighted least squares; else perform ordinary least squares.

    Weights for weighted least squares are included in *row*.

    Returns a :py:class:`pandas.Series` containing regression coefficients,
    residuals, and statistics.
    """
    # retrieve log ratios from the row
    y = row[["L_{}".format(t) for t in timepoints]]

    # re-scale the x's to fall within [0, 1]
    xvalues = [x / float(max(timepoints)) for x in timepoints]

    # perform the fit
    X = sm.add_constant(xvalues)  # fit intercept
    if weighted:
        W = row[["W_{}".format(t) for t in timepoints]]
        fit = sm.WLS(y, X, weights=W).fit()
    else:
        fit = sm.OLS(y, X).fit()

    # re-format as a data frame row
    values = np.concatenate(
        [fit.params, [fit.bse["x1"], fit.tvalues["x1"], fit.pvalues["x1"]], fit.resid]
    )
    index = ["intercept", "slope", "SE_slope", "t", "pvalue_raw"] + [
        "e_{}".format(t) for t in timepoints
    ]
    return pd.Series(data=values, index=index) 
开发者ID:FowlerLab,项目名称:Enrich2,代码行数:35,代码来源:selection.py

示例12: validate

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def validate(self):
        """
        Raises an informative ``ValueError`` if the time points in the analysis are not suitable.

        Calls validate method on all child SeqLibs.
        """
        # check the time points
        if 0 not in self.timepoints:
            raise ValueError("Missing timepoint 0 [{}]".format(self.name))
        if self.timepoints[0] != 0:
            raise ValueError("Invalid negative timepoint [{}]".format(self.name))
        if len(self.timepoints) < 2:
            raise ValueError("Multiple timepoints required [{}]".format(self.name))
        elif len(self.timepoints) < 3 and self.scoring_method in ("WLS", "OLS"):
            raise ValueError(
                "Insufficient number of timepoints for regression scoring [{}]".format(
                    self.name
                )
            )

        # check the wild type sequences
        if self.has_wt_sequence():
            for child in self.children[1:]:
                if self.children[0].wt != child.wt:
                    self.logger.warning("Inconsistent wild type sequences")
                    break

        # check that we're not doing wild type normalization on something with no wild type
        # if not self.has_wt_sequence() and self.logr_method == "wt":
        #    raise ValueError("No wild type sequence for wild type normalization [{}]".format(self.name))

        # validate children
        for child in self.children:
            child.validate() 
开发者ID:FowlerLab,项目名称:Enrich2,代码行数:36,代码来源:selection.py

示例13: predict

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def predict(self, X):
        neighbors = self.nn.kneighbors(X)
        distances = neighbors[0][0]
        neighbor_indices = neighbors[1][0]
        local_X = self.X.take(neighbor_indices, axis=0)
        local_Y = self.Y.take(neighbor_indices, axis=0)
        wls = sm.WLS(local_Y, local_X, weights=self.weight_func(distances)).fit()
        return wls.predict(X) 
开发者ID:viveksck,项目名称:langchangetrack,代码行数:10,代码来源:LocalLinearRegression.py

示例14: loglike

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def loglike(self, params):
        """
        Returns the value of the Gaussian log-likelihood function at params.

        Given the whitened design matrix, the log-likelihood is evaluated
        at the parameter vector `params` for the dependent variable `endog`.

        Parameters
        ----------
        params : array-like
            The parameter estimates

        Returns
        -------
        loglike : float
            The value of the log-likelihood function for a GLS Model.


        Notes
        -----
        The log-likelihood function for the normal distribution is

        .. math:: -\\frac{n}{2}\\log\\left(\\left(Y-\\hat{Y}\\right)^{\\prime}\\left(Y-\\hat{Y}\\right)\\right)-\\frac{n}{2}\\left(1+\\log\\left(\\frac{2\\pi}{n}\\right)\\right)-\\frac{1}{2}\\log\\left(\\left|\\Sigma\\right|\\right)

        Y and Y-hat are whitened.

        """
        #TODO: combine this with OLS/WLS loglike and add _det_sigma argument
        nobs2 = self.nobs / 2.0
        SSR = np.sum((self.wendog - np.dot(self.wexog, params))**2, axis=0)
        llf = -np.log(SSR) * nobs2      # concentrated likelihood
        llf -= (1+np.log(np.pi/nobs2))*nobs2  # with likelihood constant
        if np.any(self.sigma):
        #FIXME: robust-enough check?  unneeded if _det_sigma gets defined
            if self.sigma.ndim==2:
                det = np.linalg.slogdet(self.sigma)
                llf -= .5*det[1]
            else:
                llf -= 0.5*np.sum(np.log(self.sigma))
            # with error covariance matrix
        return llf 
开发者ID:nccgroup,项目名称:Splunking-Crime,代码行数:43,代码来源:linear_model.py

示例15: estimate_treatment_effect

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import WLS [as 别名]
def estimate_treatment_effect(covariates, treatment, outcome):
    """Estimate treatment effects using propensity weighted least-squares.

    Parameters
    ----------
        covariates: `np.ndarray`
            Array of shape [num_samples, num_features] of features
        treatment:  `np.ndarray`
            Binary array of shape [num_samples]  indicating treatment status for each
            sample.
        outcome:  `np.ndarray`
            Array of shape [num_samples] containing the observed outcome for each sample.

    Returns
    -------
        result: `whynot.framework.InferenceResult`
            InferenceResult object for this procedure

    """
    start_time = perf_counter()

    # Compute propensity scores with logistic regression model.
    features = sm.add_constant(covariates, prepend=True, has_constant="add")
    logit = sm.Logit(treatment, features)
    model = logit.fit(disp=0)
    propensities = model.predict(features)

    # IP-weights
    treated = treatment == 1.0
    untreated = treatment == 0.0
    weights = treated / propensities + untreated / (1.0 - propensities)

    treatment = treatment.reshape(-1, 1)
    features = np.concatenate([treatment, covariates], axis=1)
    features = sm.add_constant(features, prepend=True, has_constant="add")

    model = sm.WLS(outcome, features, weights=weights)
    results = model.fit()
    stop_time = perf_counter()

    # Treatment is the second variable (after the constant offset)
    ate = results.params[1]
    stderr = results.bse[1]
    conf_int = tuple(results.conf_int()[1])

    return InferenceResult(
        ate=ate,
        stderr=stderr,
        ci=conf_int,
        individual_effects=None,
        elapsed_time=stop_time - start_time,
    ) 
开发者ID:zykls,项目名称:whynot,代码行数:54,代码来源:propensity_weighted_ols.py


注:本文中的statsmodels.api.WLS属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。