Python pandas.ols() Examples

The following are 8 code examples of pandas.ols(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pandas , or try the search function .
Example #1
Source File: test_dynamic.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_window_ols_full(ols_data):
    y, x = ols_data['y'], ols_data['x']
    res1 = _window_ols(y, x, window_type='full_sample')
    res2 = _window_ols(y, x)
    res3 = pd.ols(y=y, x=x, window_type='full_sample')
    assert_ols_equal(res1, res2)
    assert_ols_equal(res1, res3) 
Example #2
Source File: test_dynamic.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_window_ols_rolling(ols_data):
    y, x = ols_data['y'], ols_data['x']
    res1 = _window_ols(y, x, window_type='rolling', window=100)
    res2 = pd.ols(y=y, x=x, window_type='rolling', window=100)
    assert_ols_equal(res1, res2) 
Example #3
Source File: test_dynamic.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_window_ols_expanding(ols_data):
    y, x = ols_data['y'], ols_data['x']
    res1 = _window_ols(y, x, window_type='expanding')
    res2 = pd.ols(y=y, x=x, window_type='expanding')
    assert_ols_equal(res1, res2) 
Example #4
Source File: test_math.py    From Computable with MIT License 5 votes vote down vote up
def test_solve_rect(self):
        if not _have_statsmodels:
            raise nose.SkipTest("no statsmodels")

        b = Series(np.random.randn(N), self.frame.index)
        result = pmath.solve(self.frame, b)
        expected = ols(y=b, x=self.frame, intercept=False).beta
        self.assert_(np.allclose(result, expected)) 
Example #5
Source File: technical_indicator.py    From NowTrade with MIT License 5 votes vote down vote up
def __init__(self, y_data, x_data, lookback):
        TechnicalIndicator.__init__(self)
        self.y_data = y_data
        self.x_data = x_data
        self.lookback = lookback
        self.value = 'PAIR_%s_%s_%s' %(y_data, x_data, lookback)
        self.ols = self.value
        self.hedge_ratio = 'HEDGE_RATIO_%s_%s_%s' %(y_data, x_data, lookback)
        self.spread = 'SPREAD_%s_%s_%s' %(y_data, x_data, lookback)
        self.zscore = 'ZSCORE_%s_%s_%s' %(y_data, x_data, lookback)
        self.logger.info('Initialized - %s' %self) 
Example #6
Source File: technical_indicator.py    From NowTrade with MIT License 5 votes vote down vote up
def results(self, data_frame):
        y_value = data_frame[self.y_data]
        x_value = data_frame[self.x_data]
        if self.lookback >= len(x_value):
            return ([self.value, self.hedge_ratio, self.spread, self.zscore], \
                    [pd.Series(np.nan), pd.Series(np.nan), pd.Series(np.nan), pd.Series(np.nan)])
        ols_result = pd.ols(y=y_value, x=x_value, window=self.lookback)
        hedge_ratio = ols_result.beta['x']
        spread = y_value - hedge_ratio * x_value
        data_frame[self.value] = ols_result.resid
        data_frame[self.hedge_ratio] = hedge_ratio
        data_frame[self.spread] = spread
        data_frame[self.zscore] = (spread - \
                                   pd.rolling_mean(spread, self.lookback)) / \
                                   pd.rolling_std(spread, self.lookback) 
Example #7
Source File: dynamic.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def equations(self):
        eqs = {}
        for col, ts in iteritems(self.y):
            # TODO: Remove in favor of statsmodels implemetation
            model = pd.ols(y=ts, x=self.x, window=self._window,
                           window_type=self._window_type,
                           min_periods=self._min_periods)

            eqs[col] = model

        return eqs 
Example #8
Source File: ret.py    From tia with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_alpha_beta(self, bm_rets):
        if isinstance(bm_rets, pd.Series):
            bm = CumulativeRets(bm_rets)
        elif isinstance(bm_rets, CumulativeRets):
            bm = bm_rets
        else:
            raise ValueError('bm_rets must be series or CumulativeRetPerformace not %s' % (type(bm_rets)))

        bm_freq = guess_freq(bm_rets)
        if self.pds_per_year != bm.pds_per_year:
            tgt = {'B': 'dly', 'W': 'weekly', 'M': 'monthly', 'Q': 'quarterly', 'A': 'annual'}.get(bm_freq, None)
            if tgt is None:
                raise ValueError('No mapping for handling benchmark with frequency: %s' % bm_freq)
            tmp = getattr(self, tgt)
            y = tmp.rets
            y_ann = tmp.ltd_ann
        else:
            y = self.rets
            y_ann = self.ltd_ann

        x = bm.rets.truncate(y.index[0], y.index[-1])
        x_ann = bm.ltd_ann

        model = pd.ols(x=x, y=y)
        beta = model.beta[0]
        alpha = y_ann - beta * x_ann
        return pd.Series({'alpha': alpha, 'beta': beta}, name=bm_freq)