本文整理汇总了Python中statsmodels.tsa.tsatools.add_trend函数的典型用法代码示例。如果您正苦于以下问题:Python add_trend函数的具体用法?Python add_trend怎么用?Python add_trend使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了add_trend函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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
示例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)
示例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
示例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
示例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
示例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
示例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)
示例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)
#.........这里部分代码省略.........