本文整理汇总了Python中statsmodels.tsa.tsatools.add_trend函数的典型用法代码示例。如果您正苦于以下问题:Python add_trend函数的具体用法?Python add_trend怎么用?Python add_trend使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了add_trend函数的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_dataframe
def test_dataframe(self):
df = pd.DataFrame(self.arr_2d)
appended = tools.add_trend(df)
expected = df.copy()
expected["const"] = self.c
assert_frame_equal(expected, appended)
prepended = tools.add_trend(df, prepend=True)
expected = df.copy()
expected.insert(0, "const", self.c)
assert_frame_equal(expected, prepended)
df = pd.DataFrame(self.arr_2d)
appended = tools.add_trend(df, trend="t")
expected = df.copy()
expected["trend"] = self.t
assert_frame_equal(expected, appended)
df = pd.DataFrame(self.arr_2d)
appended = tools.add_trend(df, trend="ctt")
expected = df.copy()
expected["const"] = self.c
expected["trend"] = self.t
expected["trend_squared"] = self.t ** 2
assert_frame_equal(expected, appended)
开发者ID:phobson,项目名称:statsmodels,代码行数:25,代码来源:test_tsa_tools.py
示例2: test_mixed_recarray
def test_mixed_recarray(self):
dt = np.dtype([('c0', np.float64), ('c1', np.int8), ('c2', 'S4')])
ra = np.array([(1.0, 1, 'aaaa'), (1.1, 2, 'bbbb')], dtype=dt).view(np.recarray)
added = tools.add_trend(ra, trend='ct')
dt = np.dtype([('c0', np.float64), ('c1', np.int8), ('c2', 'S4'), ('const', np.float64), ('trend', np.float64)])
expected = np.array([(1.0, 1, 'aaaa', 1.0, 1.0), (1.1, 2, 'bbbb', 1.0, 2.0)], dtype=dt).view(np.recarray)
assert_equal(added, expected)
开发者ID:Bonfils-ebu,项目名称:statsmodels,代码行数:7,代码来源:test_tsa_tools.py
示例3: test_series
def test_series(self):
s = pd.Series(self.arr_1d)
appended = tools.add_trend(s)
expected = pd.DataFrame(s)
expected['const'] = self.c
assert_frame_equal(expected, appended)
prepended = tools.add_trend(s, prepend=True)
expected = pd.DataFrame(s)
expected.insert(0, 'const', self.c)
assert_frame_equal(expected, prepended)
s = pd.Series(self.arr_1d)
appended = tools.add_trend(s, trend='ct')
expected = pd.DataFrame(s)
expected['const'] = self.c
expected['trend'] = self.t
assert_frame_equal(expected, appended)
开发者ID:Bonfils-ebu,项目名称:statsmodels,代码行数:18,代码来源:test_tsa_tools.py
示例4: test_array
def test_array(self):
base = np.vstack((self.arr_1d, self.c, self.t, self.t ** 2)).T
assert_equal(tools.add_trend(self.arr_1d), base[:, :2])
assert_equal(tools.add_trend(self.arr_1d, trend='t'), base[:, [0, 2]])
assert_equal(tools.add_trend(self.arr_1d, trend='ct'), base[:, :3])
assert_equal(tools.add_trend(self.arr_1d, trend='ctt'), base)
base = np.hstack((self.c[:, None], self.t[:, None], self.t[:, None] ** 2, self.arr_2d))
assert_equal(tools.add_trend(self.arr_2d, prepend=True), base[:, [0, 3, 4]])
assert_equal(tools.add_trend(self.arr_2d, trend='t', prepend=True), base[:, [1, 3, 4]])
assert_equal(tools.add_trend(self.arr_2d, trend='ct', prepend=True), base[:, [0, 1, 3, 4]])
assert_equal(tools.add_trend(self.arr_2d, trend='ctt', prepend=True), base)
开发者ID:Bonfils-ebu,项目名称:statsmodels,代码行数:12,代码来源:test_tsa_tools.py
示例5: test_duplicate_const
def test_duplicate_const(self):
assert_raises(ValueError, tools.add_trend, x=self.c, trend='c', has_constant='raise')
assert_raises(ValueError, tools.add_trend, x=self.c, trend='ct', has_constant='raise')
df = pd.DataFrame(self.c)
assert_raises(ValueError, tools.add_trend, x=df, trend='c', has_constant='raise')
assert_raises(ValueError, tools.add_trend, x=df, trend='ct', has_constant='raise')
skipped = tools.add_trend(self.c, trend='c')
assert_equal(skipped, self.c[:,None])
skipped_const = tools.add_trend(self.c, trend='ct', has_constant='skip')
expected = np.vstack((self.c, self.t)).T
assert_equal(skipped_const, expected)
added = tools.add_trend(self.c, trend='c', has_constant='add')
expected = np.vstack((self.c, self.c)).T
assert_equal(added, expected)
added = tools.add_trend(self.c, trend='ct', has_constant='add')
expected = np.vstack((self.c, self.c, self.t)).T
assert_equal(added, expected)
开发者ID:Bonfils-ebu,项目名称:statsmodels,代码行数:21,代码来源:test_tsa_tools.py
示例6: test_recarray
def test_recarray(self):
recarray = pd.DataFrame(self.arr_2d).to_records(index=False, convert_datetime64=False)
appended = tools.add_trend(recarray)
expected = pd.DataFrame(self.arr_2d)
expected['const'] = self.c
expected = expected.to_records(index=False, convert_datetime64=False)
assert_equal(expected, appended)
prepended = tools.add_trend(recarray, prepend=True)
expected = pd.DataFrame(self.arr_2d)
expected.insert(0, 'const', self.c)
expected = expected.to_records(index=False, convert_datetime64=False)
assert_equal(expected, prepended)
appended = tools.add_trend(recarray, trend='ctt')
expected = pd.DataFrame(self.arr_2d)
expected['const'] = self.c
expected['trend'] = self.t
expected['trend_squared'] = self.t ** 2
expected = expected.to_records(index=False, convert_datetime64=False)
assert_equal(expected, appended)
开发者ID:Bonfils-ebu,项目名称:statsmodels,代码行数:21,代码来源:test_tsa_tools.py
示例7: test_duplicate_const
def test_duplicate_const(self):
assert_raises(ValueError, tools.add_trend, x=self.c, trend="c", has_constant="raise")
assert_raises(ValueError, tools.add_trend, x=self.c, trend="ct", has_constant="raise")
df = pd.DataFrame(self.c)
assert_raises(ValueError, tools.add_trend, x=df, trend="c", has_constant="raise")
assert_raises(ValueError, tools.add_trend, x=df, trend="ct", has_constant="raise")
skipped = tools.add_trend(self.c, trend="c")
assert_equal(skipped, self.c[:, None])
skipped_const = tools.add_trend(self.c, trend="ct", has_constant="skip")
expected = np.vstack((self.c, self.t)).T
assert_equal(skipped_const, expected)
added = tools.add_trend(self.c, trend="c", has_constant="add")
expected = np.vstack((self.c, self.c)).T
assert_equal(added, expected)
added = tools.add_trend(self.c, trend="ct", has_constant="add")
expected = np.vstack((self.c, self.c, self.t)).T
assert_equal(added, expected)
开发者ID:phobson,项目名称:statsmodels,代码行数:21,代码来源:test_tsa_tools.py
示例8: _stackX
def _stackX(self, k_ar, trend):
"""
Private method to build the RHS matrix for estimation.
Columns are trend terms then lags.
"""
endog = self.endog
X = lagmat(endog, maxlag=k_ar, trim='both')
k_trend = util.get_trendorder(trend)
if k_trend:
X = add_trend(X, prepend=True, trend=trend)
self.k_trend = k_trend
return X
开发者ID:0ceangypsy,项目名称:statsmodels,代码行数:13,代码来源:ar_model.py
示例9: add_constant
def add_constant(data, prepend=True, has_constant='skip'):
"""
Adds a column of ones to an array
Parameters
----------
data : array-like
`data` is the column-ordered design matrix
prepend : bool
If true, the constant is in the first column. Else the constant is
appended (last column).
has_constant : str {'raise', 'add', 'skip'}
Behavior if ``data'' already has a constant. The default will return
data without adding another constant. If 'raise', will raise an
error if a constant is present. Using 'add' will duplicate the
constant, if one is present.
Returns
-------
data : array, recarray or DataFrame
The original values with a constant (column of ones) as the first or
last column. Returned value depends on input type.
Notes
-----
When the input is recarray or a pandas Series or DataFrame, the added
column's name is 'const'.
"""
if _is_using_pandas(data, None) or _is_recarray(data):
from statsmodels.tsa.tsatools import add_trend
return add_trend(data, trend='c', prepend=prepend, has_constant=has_constant)
# Special case for NumPy
x = np.asanyarray(data)
if x.ndim == 1:
x = x[:,None]
elif x.ndim > 2:
raise ValueError('Only implementd 2-dimensional arrays')
is_nonzero_const = np.ptp(x, axis=0) == 0
is_nonzero_const &= np.all(x != 0.0, axis=0)
if is_nonzero_const.any():
if has_constant == 'skip':
return x
elif has_constant == 'raise':
raise ValueError("data already contains a constant")
x = [np.ones(x.shape[0]), x]
x = x if prepend else x[::-1]
return np.column_stack(x)
开发者ID:Leo666,项目名称:statsmodels,代码行数:50,代码来源:tools.py
示例10: _make_arma_exog
def _make_arma_exog(endog, exog, trend):
k_trend = 1 # overwritten if no constant
if exog is None and trend == 'c': # constant only
exog = np.ones((len(endog),1))
elif exog is not None and trend == 'c': # constant plus exogenous
exog = add_trend(exog, trend='c', prepend=True)
elif exog is not None and trend == 'nc':
# make sure it's not holding constant from last run
if exog.var() == 0:
exog = None
k_trend = 0
if trend == 'nc':
k_trend = 0
return k_trend, exog
开发者ID:arokem,项目名称:statsmodels,代码行数:14,代码来源:arima_model.py
示例11: get_var_endog
def get_var_endog(y, lags, trend='c'):
"""
Make predictor matrix for VAR(p) process
Z := (Z_0, ..., Z_T).T (T x Kp)
Z_t = [1 y_t y_{t-1} ... y_{t - p + 1}] (Kp x 1)
Ref: Lutkepohl p.70 (transposed)
"""
nobs = len(y)
# Ravel C order, need to put in descending order
Z = np.array([y[t-lags : t][::-1].ravel() for t in xrange(lags, nobs)])
# Add constant, trend, etc.
if trend != 'nc':
Z = tsa.add_trend(Z, prepend=True, trend=trend)
return Z
开发者ID:Autodidact24,项目名称:statsmodels,代码行数:18,代码来源:util.py
示例12: get_var_endog
def get_var_endog(y, lags, trend="c", has_constant="skip"):
"""
Make predictor matrix for VAR(p) process
Z := (Z_0, ..., Z_T).T (T x Kp)
Z_t = [1 y_t y_{t-1} ... y_{t - p + 1}] (Kp x 1)
Ref: Lutkepohl p.70 (transposed)
has_constant can be 'raise', 'add', or 'skip'. See add_constant.
"""
nobs = len(y)
# Ravel C order, need to put in descending order
Z = np.array([y[t - lags : t][::-1].ravel() for t in range(lags, nobs)])
# Add constant, trend, etc.
if trend != "nc":
Z = tsa.add_trend(Z, prepend=True, trend=trend, has_constant=has_constant)
return Z
开发者ID:caitouwh,项目名称:statsmodels,代码行数:20,代码来源:util.py
示例13: simulate_kpss
def simulate_kpss(nobs, B, trend='c', rng=None):
"""
Simulated the KPSS test statistic for nobs observations,
performing B replications.
"""
if rng is None:
rng = RandomState()
rng.seed(0)
standard_normal = rng.standard_normal
e = standard_normal((nobs, B))
z = np.ones((nobs, 1))
if trend == 'ct':
z = add_trend(z, trend='t')
zinv = np.linalg.pinv(z)
trend_coef = zinv.dot(e)
resid = e - z.dot(trend_coef)
s = np.cumsum(resid, axis=0)
lam = np.mean(resid ** 2.0, axis=0)
kpss = 1 / (nobs ** 2.0) * np.sum(s ** 2.0, axis=0) / lam
return kpss
开发者ID:bashtage,项目名称:arch,代码行数:22,代码来源:kpss_critical_values_simulation.py
示例14: test_dataframe_duplicate
def test_dataframe_duplicate(self):
df = pd.DataFrame(self.arr_2d, columns=['const', 'trend'])
tools.add_trend(df, trend='ct')
tools.add_trend(df, trend='ct', prepend=True)
开发者ID:Bonfils-ebu,项目名称:statsmodels,代码行数:4,代码来源:test_tsa_tools.py
示例15: coint
def coint(y0, y1, trend='c', method='aeg', maxlag=None, autolag='aic',
return_results=None):
"""Test for no-cointegration of a univariate equation
The null hypothesis is no cointegration. Variables in y0 and y1 are
assumed to be integrated of order 1, I(1).
This uses the augmented Engle-Granger two-step cointegration test.
Constant or trend is included in 1st stage regression, i.e. in
cointegrating equation.
Parameters
----------
y1 : array_like, 1d
first element in cointegrating vector
y2 : array_like
remaining elements in cointegrating vector
trend : str {'c', 'ct'}
trend term included in regression for cointegrating equation
* 'c' : constant
* 'ct' : constant and linear trend
* also available quadratic trend 'ctt', and no constant 'nc'
method : string
currently only 'aeg' for augmented Engle-Granger test is available.
default might change.
maxlag : None or int
keyword for `adfuller`, largest or given number of lags
autolag : string
keyword for `adfuller`, lag selection criterion.
return_results : bool
for future compatibility, currently only tuple available.
If True, then a results instance is returned. Otherwise, a tuple
with the test outcome is returned.
Set `return_results=False` to avoid future changes in return.
Returns
-------
coint_t : float
t-statistic of unit-root test on residuals
pvalue : float
MacKinnon's approximate, asymptotic p-value based on MacKinnon (1994)
crit_value : dict
Critical values for the test statistic at the 1 %, 5 %, and 10 %
levels based on regression curve. This depends on the number of
observations.
Notes
-----
The Null hypothesis is that there is no cointegration, the alternative
hypothesis is that there is cointegrating relationship. If the pvalue is
small, below a critical size, then we can reject the hypothesis that there
is no cointegrating relationship.
P-values and critical values are obtained through regression surface
approximation from MacKinnon 1994 and 2010.
TODO: We could handle gaps in data by dropping rows with nans in the
auxiliary regressions. Not implemented yet, currently assumes no nans
and no gaps in time series.
References
----------
MacKinnon, J.G. 1994 "Approximate Asymptotic Distribution Functions for
Unit-Root and Cointegration Tests." Journal of Business & Economics
Statistics, 12.2, 167-76.
MacKinnon, J.G. 2010. "Critical Values for Cointegration Tests."
Queen's University, Dept of Economics Working Papers 1227.
http://ideas.repec.org/p/qed/wpaper/1227.html
"""
trend = trend.lower()
if trend not in ['c', 'nc', 'ct', 'ctt']:
raise ValueError("trend option %s not understood" % trend)
y0 = np.asarray(y0)
y1 = np.asarray(y1)
if y1.ndim < 2:
y1 = y1[:, None]
nobs, k_vars = y1.shape
k_vars += 1 # add 1 for y0
if trend == 'nc':
xx = y1
else:
xx = add_trend(y1, trend=trend, prepend=False)
res_co = OLS(y0, xx).fit()
res_adf = adfuller(res_co.resid, maxlag=maxlag, autolag=None,
regression='nc')
# no constant or trend, see egranger in Stata and MacKinnon
if trend == 'nc':
crit = [np.nan] * 3 # 2010 critical values not available
else:
crit = mackinnoncrit(N=k_vars, regression=trend, nobs=nobs - 1)
# nobs - 1, the -1 is to match egranger in Stata, I don't know why.
# TODO: check nobs or df = nobs - k
pval_asy = mackinnonp(res_adf[0], regression=trend, N=k_vars)
#.........这里部分代码省略.........
开发者ID:Inoryy,项目名称:statsmodels,代码行数:101,代码来源:stattools.py
示例16: adfuller
#.........这里部分代码省略.........
.. [3] MacKinnon, J.G. 1994. "Approximate asymptotic distribution functions for
unit-root and cointegration tests. `Journal of Business and Economic
Statistics` 12, 167-76.
.. [4] MacKinnon, J.G. 2010. "Critical Values for Cointegration Tests." Queen's
University, Dept of Economics, Working Papers. Available at
http://ideas.repec.org/p/qed/wpaper/1227.html
"""
if regresults:
store = True
trenddict = {None: 'nc', 0: 'c', 1: 'ct', 2: 'ctt'}
if regression is None or isinstance(regression, (int, long)):
regression = trenddict[regression]
regression = regression.lower()
if regression not in ['c', 'nc', 'ct', 'ctt']:
raise ValueError("regression option %s not understood") % regression
x = np.asarray(x)
nobs = x.shape[0]
if maxlag is None:
#from Greene referencing Schwert 1989
maxlag = int(np.ceil(12. * np.power(nobs / 100., 1 / 4.)))
xdiff = np.diff(x)
xdall = lagmat(xdiff[:, None], maxlag, trim='both', original='in')
nobs = xdall.shape[0] # pylint: disable=E1103
xdall[:, 0] = x[-nobs - 1:-1] # replace 0 xdiff with level of x
xdshort = xdiff[-nobs:]
if store:
resstore = ResultsStore()
if autolag:
if regression != 'nc':
fullRHS = add_trend(xdall, regression, prepend=True)
else:
fullRHS = xdall
startlag = fullRHS.shape[1] - xdall.shape[1] + 1 # 1 for level # pylint: disable=E1103
#search for lag length with smallest information criteria
#Note: use the same number of observations to have comparable IC
#aic and bic: smaller is better
if not regresults:
icbest, bestlag = _autolag(OLS, xdshort, fullRHS, startlag,
maxlag, autolag)
else:
icbest, bestlag, alres = _autolag(OLS, xdshort, fullRHS, startlag,
maxlag, autolag,
regresults=regresults)
resstore.autolag_results = alres
bestlag -= startlag # convert to lag not column index
#rerun ols with best autolag
xdall = lagmat(xdiff[:, None], bestlag, trim='both', original='in')
nobs = xdall.shape[0] # pylint: disable=E1103
xdall[:, 0] = x[-nobs - 1:-1] # replace 0 xdiff with level of x
xdshort = xdiff[-nobs:]
usedlag = bestlag
else:
usedlag = maxlag
icbest = None
if regression != 'nc':
resols = OLS(xdshort, add_trend(xdall[:, :usedlag + 1],
regression)).fit()
else:
resols = OLS(xdshort, xdall[:, :usedlag + 1]).fit()
adfstat = resols.tvalues[0]
# adfstat = (resols.params[0]-1.0)/resols.bse[0]
# the "asymptotically correct" z statistic is obtained as
# nobs/(1-np.sum(resols.params[1:-(trendorder+1)])) (resols.params[0] - 1)
# I think this is the statistic that is used for series that are integrated
# for orders higher than I(1), ie., not ADF but cointegration tests.
# Get approx p-value and critical values
pvalue = mackinnonp(adfstat, regression=regression, N=1)
critvalues = mackinnoncrit(N=1, regression=regression, nobs=nobs)
critvalues = {"1%" : critvalues[0], "5%" : critvalues[1],
"10%" : critvalues[2]}
if store:
resstore.resols = resols
resstore.maxlag = maxlag
resstore.usedlag = usedlag
resstore.adfstat = adfstat
resstore.critvalues = critvalues
resstore.nobs = nobs
resstore.H0 = ("The coefficient on the lagged level equals 1 - "
"unit root")
resstore.HA = "The coefficient on the lagged level < 1 - stationary"
resstore.icbest = icbest
resstore._str = 'Augmented Dickey-Fuller Test Results'
return adfstat, pvalue, critvalues, resstore
else:
if not autolag:
return adfstat, pvalue, usedlag, nobs, critvalues
else:
return adfstat, pvalue, usedlag, nobs, critvalues, icbest
开发者ID:Inoryy,项目名称:statsmodels,代码行数:101,代码来源:stattools.py
示例17: trade_allocation
def trade_allocation(context, data, resOLS):
olsPara = resOLS.params # OLS paramters for [x, dx, 1]
resid = resOLS.resid
ols_var = np.dot(resid, np.transpose(resid))/(resOLS.df_resid)
# Replicate the OLS in adf test to predict the movement of stationary portfolio
# RecentData = TrainPort[-context.lookback/50:] # only the recent data matters
RecentData = context.TrainPort
xdiff = np.diff(RecentData)
nobs = xdiff.shape[0]
xdall = np.column_stack((RecentData[-nobs:], xdiff[:,None]))
x = add_trend(xdall[:,:context.lag+1], 'c')
y_pred = np.dot(x, olsPara)
y_actual = xdiff[-nobs:]
profit_potential = y_pred[-1]/context.cost
# y_mean = np.mean(TrainPort)
# y_std = np.std(TrainPort)
# Calculate the score of deviation from current value
# dev_score = y_pred[-1]#/np.sqrt(ols_var)
# dev_score = -(y_pred[-1] - y_mean)/y_std
# dev_score = -(TrainPort[-1] - np.mean(TrainPort))/np.std(TrainPort)
curr_price = context.base_prices[-1,:]
dollar_allocation = np.multiply(curr_price, context.weights)*profit_potential
pct_allocation = context.leverage_limit*dollar_allocation/np.sum(np.abs(dollar_allocation))
pct_allocation = np.asarray(pct_allocation)
pct_allocation = pct_allocation[0,:]
# print '-'*20
# print 'ADF test statistics: %f' %adfstat
# print 'ADF test critvalues: %s' %str(critvalues)
# print 'ADF pvalue: %f' %pvalue
# print ' '
# print dir(resOLS)
# print 'Predic value: %s' %str(y_pred[-5:])
# print 'Fitted value: %s' %str(resOLS.fittedvalues[-5:])
# print 'Actual value: %s' %str(y_actual[-5:])
# print ' '
# print 'Latest value of TrainPort: %f' %TrainPort[-1]
# print 'Mean value of TrainPort: %f' %np.mean(TrainPort)
# print 'Std Value of TrainPort: %f' %np.std(TrainPort)
# print ' '
# print 'avg of actual change: %f' %np.mean(abs(y_actual))
# print 'std of actual change: %f' %np.std(y_actual)
# print ' '
# print 'avg of pred change: %f' %np.mean(abs(y_pred))
# print 'std of pred change: %f' %np.std(y_pred)
# print ' '
# print 'The predicted bar: %f' %y_pred[-1]
# print 'The current bar: %f' %y_actual[-1]
# print ' '
# print 'The ols std: %f' %np.sqrt(ols_var)
# print 'mse_total %f' %resOLS.mse_total
# print 'OLS rsquared: %f' %resOLS.rsquared
# print 'profit_potential: %f' %profit_potential
# print 'dev_score: %f' %dev_score
# print 'Dollar Allocation: %s' %str(dollar_allocation)
# if abs(y_pred[-1]) > 2*context.cost/abs(TrainPort[-1]):
# return pct_allocation
# elif abs(y_pred[-1]) < context.cost/abs(TrainPort[-1]):
# return pct_allocation*0
# else:
# return None
# Trading window is determined by the abs(dev_score)
if abs(profit_potential) < context.dev_lower_threshold:
# Liquidate all positions if fall below lower threshold
return pct_allocation*0.
elif abs(profit_potential) < context.dev_upper_threshold:
# Do nothing
return None
else:
# Rebalance if the dev is above the upper threshold.
return pct_allocation
开发者ID:archlight,项目名称:tigris,代码行数:78,代码来源:MeanReversion_on_ETFs.py
注:本文中的statsmodels.tsa.tsatools.add_trend函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论