當前位置: 首頁>>代碼示例>>Python>>正文


Python pandas.rolling_std方法代碼示例

本文整理匯總了Python中pandas.rolling_std方法的典型用法代碼示例。如果您正苦於以下問題:Python pandas.rolling_std方法的具體用法?Python pandas.rolling_std怎麽用?Python pandas.rolling_std使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pandas的用法示例。


在下文中一共展示了pandas.rolling_std方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: getVol

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import rolling_std [as 別名]
def getVol(ret):
    '''
    calculate volatility value of log return ratio
    :param DataFrame ret: return value
    :param int interval: interval over which volatility is calculated
    :return: DataFrame standard_error: volatility value
    '''
    print '''*************************************************************************************
    a kind WARNING from the programmer(not the evil interpreter) function getVol:
    we have different values for interval in test code and real code,because the sample file
    may not have sufficient rows for real interval,leading to empty matrix.So be careful of
    the value you choose
    **************************************************************************************
          '''
    # real value
    # interval = 26
    # test value
    interval = 4
    standard_error = pd.rolling_std(ret, interval)
    standard_error.dropna(inplace=True)
    standard_error.index = range(standard_error.shape[0])
    return standard_error 
開發者ID:icezerowjj,項目名稱:MultipleFactorRiskModel,代碼行數:24,代碼來源:own_tech.py

示例2: sample_531_1

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import rolling_std [as 別名]
def sample_531_1():
    """
    5.3.1_1 繪製股票的收益,及收益波動情況 demo list
    :return:
    """
    # 示例序列
    demo_list = np.array([2, 4, 16, 20])
    # 以三天為周期計算波動
    demo_window = 3
    # pd.rolling_std * np.sqrt
    print('pd.rolling_std(demo_list, window=demo_window, center=False) * np.sqrt(demo_window):\n',
          pd_rolling_std(demo_list, window=demo_window, center=False) * np.sqrt(demo_window))

    print('pd.Series([2, 4, 16]).std() * np.sqrt(demo_window):', pd.Series([2, 4, 16]).std() * np.sqrt(demo_window))
    print('pd.Series([4, 16, 20]).std() * np.sqrt(demo_window):', pd.Series([4, 16, 20]).std() * np.sqrt(demo_window))
    print('np.sqrt(pd.Series([2, 4, 16]).var() * demo_window):', np.sqrt(pd.Series([2, 4, 16]).var() * demo_window)) 
開發者ID:bbfamily,項目名稱:abu,代碼行數:18,代碼來源:c5.py

示例3: results

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import rolling_std [as 別名]
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) 
開發者ID:edouardpoitras,項目名稱:NowTrade,代碼行數:17,代碼來源:technical_indicator.py

示例4: volatility

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import rolling_std [as 別名]
def volatility(self, n, freq=None, which='close', ann=True, model='ln', min_periods=1, rolling='simple'):
        """Return the annualized volatility series. N is the number of lookback periods.

        :param n: int, number of lookback periods
        :param freq: resample frequency or None
        :param which: price series to use
        :param ann: If True then annualize
        :param model: {'ln', 'pct', 'bbg'}
                        ln - use logarithmic price changes
                        pct - use pct price changes
                        bbg - use logarithmic price changes but Bloomberg uses actual business days
        :param rolling:{'simple', 'exp'}, if exp, use ewmstd. if simple, use rolling_std
        :return:
        """
        if model not in ('bbg', 'ln', 'pct'):
            raise ValueError('model must be one of (bbg, ln, pct), not %s' % model)
        if rolling not in ('simple', 'exp'):
            raise ValueError('rolling must be one of (simple, exp), not %s' % rolling)

        px = self.frame[which]
        px = px if not freq else px.resample(freq, how='last')
        if model == 'bbg' and periods_in_year(px) == 252:
            # Bloomberg uses business days, so need to convert and reindex
            orig = px.index
            px = px.resample('B').ffill()
            chg = np.log(px / px.shift(1))
            chg[chg.index - orig] = np.nan
            if rolling == 'simple':
                vol = pd.rolling_std(chg, n, min_periods=min_periods).reindex(orig)
            else:
                vol = pd.ewmstd(chg, span=n, min_periods=n)
            return vol if not ann else vol * np.sqrt(260)
        else:
            chg = px.pct_change() if model == 'pct' else np.log(px / px.shift(1))
            if rolling == 'simple':
                vol = pd.rolling_std(chg, n, min_periods=min_periods)
            else:
                vol = pd.ewmstd(chg, span=n, min_periods=n)
            return vol if not ann else vol * np.sqrt(periods_in_year(vol)) 
開發者ID:bpsmith,項目名稱:tia,代碼行數:41,代碼來源:ins.py

示例5: rolling_std

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import rolling_std [as 別名]
def rolling_std(self, n):
        return pd.rolling_std(self.rets, n) 
開發者ID:bpsmith,項目名稱:tia,代碼行數:4,代碼來源:ret.py

示例6: rolling_std_ann

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import rolling_std [as 別名]
def rolling_std_ann(self, n):
        return self.rolling_std(n) * np.sqrt(self.pds_per_year) 
開發者ID:bpsmith,項目名稱:tia,代碼行數:4,代碼來源:ret.py

示例7: rolling_std

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import rolling_std [as 別名]
def rolling_std(x, window, min_periods=None, center=False, ddof=1):
    if PD_VERSION >= '0.18.0':
        return x.rolling(
            window, min_periods=min_periods, center=center
        ).std(ddof=ddof)
    else:
        return pd.rolling_std(
            x, window, min_periods=min_periods, center=center, ddof=ddof
        ) 
開發者ID:JoinQuant,項目名稱:jqfactor_analyzer,代碼行數:11,代碼來源:compat.py

示例8: run

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import rolling_std [as 別名]
def run(self, data, symbols, lookback, **kwargs):
        prices = data['prices'].copy()

        rolling_std = pd.rolling_std(prices, lookback)
        rolling_mean = pd.rolling_mean(prices, lookback)

        bollinger_values = (prices - rolling_mean) / (rolling_std)

        for s_key in symbols:
            prices[s_key] = prices[s_key].fillna(method='ffill')
            prices[s_key] = prices[s_key].fillna(method='bfill')
            prices[s_key] = prices[s_key].fillna(1.0)

        return bollinger_values 
開發者ID:Emsu,項目名稱:prophet,代碼行數:16,代碼來源:bollinger.py

示例9: explain_anomalies_rolling_std

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import rolling_std [as 別名]
def explain_anomalies_rolling_std(y, window_size, sigma=1.0):
    """Helps in exploring the anamolies using rolling standard deviation

    Args:
        y (pandas.Series): independent variable
        window_size (int): rolling window size
        sigma (int): value for standard deviation

    Returns:
        a dict (dict of 'standard_deviation': int, 'anomalies_dict': (index: value))
        containing information about the points indentified as anomalies

    """
    avg = moving_average(y, window_size)
    avg_list = avg.tolist()
    residual = y - avg
    # Calculate the variation in the distribution of the residual
    testing_std = pd.rolling_std(residual, window_size)
    testing_std_as_df = pd.DataFrame(testing_std)
    rolling_std = testing_std_as_df.replace(np.nan,
                                            testing_std_as_df.ix[window_size - 1]).round(3).iloc[:, 0].tolist()
    std = np.std(residual)
    return {'stationary standard_deviation': round(std, 3),
            'anomalies_dict': collections.OrderedDict([(index, y_i)
                                                       for index, y_i, avg_i, rs_i in zip(count(),
                                                                                          y, avg_list,
                                                                                          rolling_std)
                                                       if (y_i > avg_i + (sigma * rs_i)) | (
                                                               y_i < avg_i - (sigma * rs_i))])}


# This function is repsonsible for displaying how the function performs on the given dataset. 
開發者ID:jsonbruce,項目名稱:MTSAnomalyDetection,代碼行數:34,代碼來源:moving_average.py

示例10: generate_features

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import rolling_std [as 別名]
def generate_features(df):
    """ Generate features for a stock/index based on historical price and performance
    Args:
        df (dataframe with columns "Open", "Close", "High", "Low", "Volume", "Adjusted Close")
    Returns:
        dataframe, data set with new features
    """
    df_new = pd.DataFrame()
    # 6 original features
    df_new['open'] = df['Open']
    df_new['open_1'] = df['Open'].shift(1)
    df_new['close_1'] = df['Close'].shift(1)
    df_new['high_1'] = df['High'].shift(1)
    df_new['low_1'] = df['Low'].shift(1)
    df_new['volume_1'] = df['Volume'].shift(1)
    # 31 original features
    # average price
    df_new['avg_price_5'] = pd.rolling_mean(df['Close'], window=5).shift(1)
    df_new['avg_price_30'] = pd.rolling_mean(df['Close'], window=21).shift(1)
    df_new['avg_price_365'] = pd.rolling_mean(df['Close'], window=252).shift(1)
    df_new['ratio_avg_price_5_30'] = df_new['avg_price_5'] / df_new['avg_price_30']
    df_new['ratio_avg_price_5_365'] = df_new['avg_price_5'] / df_new['avg_price_365']
    df_new['ratio_avg_price_30_365'] = df_new['avg_price_30'] / df_new['avg_price_365']
    # average volume
    df_new['avg_volume_5'] = pd.rolling_mean(df['Volume'], window=5).shift(1)
    df_new['avg_volume_30'] = pd.rolling_mean(df['Volume'], window=21).shift(1)
    df_new['avg_volume_365'] = pd.rolling_mean(df['Volume'], window=252).shift(1)
    df_new['ratio_avg_volume_5_30'] = df_new['avg_volume_5'] / df_new['avg_volume_30']
    df_new['ratio_avg_volume_5_365'] = df_new['avg_volume_5'] / df_new['avg_volume_365']
    df_new['ratio_avg_volume_30_365'] = df_new['avg_volume_30'] / df_new['avg_volume_365']
    # standard deviation of prices
    df_new['std_price_5'] = pd.rolling_std(df['Close'], window=5).shift(1)
    df_new['std_price_30'] = pd.rolling_std(df['Close'], window=21).shift(1)
    df_new['std_price_365'] = pd.rolling_std(df['Close'], window=252).shift(1)
    df_new['ratio_std_price_5_30'] = df_new['std_price_5'] / df_new['std_price_30']
    df_new['ratio_std_price_5_365'] = df_new['std_price_5'] / df_new['std_price_365']
    df_new['ratio_std_price_30_365'] = df_new['std_price_30'] / df_new['std_price_365']
    # standard deviation of volumes
    df_new['std_volume_5'] = pd.rolling_std(df['Volume'], window=5).shift(1)
    df_new['std_volume_30'] = pd.rolling_std(df['Volume'], window=21).shift(1)
    df_new['std_volume_365'] = pd.rolling_std(df['Volume'], window=252).shift(1)
    df_new['ratio_std_volume_5_30'] = df_new['std_volume_5'] / df_new['std_volume_30']
    df_new['ratio_std_volume_5_365'] = df_new['std_volume_5'] / df_new['std_volume_365']
    df_new['ratio_std_volume_30_365'] = df_new['std_volume_30'] / df_new['std_volume_365']
    # # return
    df_new['return_1'] = ((df['Close'] - df['Close'].shift(1)) / df['Close'].shift(1)).shift(1)
    df_new['return_5'] = ((df['Close'] - df['Close'].shift(5)) / df['Close'].shift(5)).shift(1)
    df_new['return_30'] = ((df['Close'] - df['Close'].shift(21)) / df['Close'].shift(21)).shift(1)
    df_new['return_365'] = ((df['Close'] - df['Close'].shift(252)) / df['Close'].shift(252)).shift(1)
    df_new['moving_avg_5'] = pd.rolling_mean(df_new['return_1'], window=5)
    df_new['moving_avg_30'] = pd.rolling_mean(df_new['return_1'], window=21)
    df_new['moving_avg_365'] = pd.rolling_mean(df_new['return_1'], window=252)
    # the target
    df_new['close'] = df['Close']
    df_new = df_new.dropna(axis=0)
    return df_new 
開發者ID:PacktPublishing,項目名稱:Python-Machine-Learning-By-Example,代碼行數:58,代碼來源:1stock_price_prediction.py


注:本文中的pandas.rolling_std方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。